public class MovieDB extends Object implements AutoCloseable
Movie
and Person
.
The database is embedded in-process (not a network service), and implemented using Berkeley DB Java Edition. The records in the database are JSON strings, which are mapped to Java objects using GSON.
The database has two primary key indexes (looking up a person or a movie by ID), and two secondary indexes (looking up a person by name, or looking up a movie by title).
You can open the database in a specified directory on disk using open(String)
.
When you have finished using the database, you should close it using close()
,
which clears up any locks and pending writes, and shuts down background threads. The
easiest way of doing this is with a
try-with-resources
statement, which takes advantage of the AutoCloseable
interface:
try (MovieDB database = MovieDB.open("document-db")) { ... // use the database }
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the database cleanly and shuts down its background threads.
|
Iterable<Person> |
getByNamePrefix(String namePrefix)
Scans over a set of people whose name begins with the specified string.
|
Iterable<Movie> |
getByTitlePrefix(String titlePrefix)
Scans over a set of movies whose title begins with the specified string.
|
Movie |
getMovieById(int id)
Looks up a movie by its unique identifier.
|
Person |
getPersonById(int id)
Looks up a person by their unique identifier.
|
static MovieDB |
open(String dataDir)
Opens the database, stored in the specified directory.
|
void |
putMovie(String json)
Inserts a movie into the database, given as a JSON string.
|
void |
putPerson(String json)
Inserts a person into the database, given as a JSON string.
|
public static MovieDB open(String dataDir)
dataDir
- The directory in which to store the data files.public void putMovie(String json)
json
- The JSON document representing the movie.public void putPerson(String json)
json
- The JSON document representing the person.public Movie getMovieById(int id)
id
- The identifier of the movie to search for.public Person getPersonById(int id)
id
- The identifier of the person to search for.public Iterable<Movie> getByTitlePrefix(String titlePrefix)
Movie.getTitle()
for notes on how the title is formatted. The
search is case-sensitive.
You need to iterate all the way to the end, because the iterator maintains a cursor in the database that is closed when the iterator has no more items remaining. Stopping iteration early would leak cursors.
titlePrefix
- The beginning of the title to search for.public Iterable<Person> getByNamePrefix(String namePrefix)
Person.getName()
for notes on how the name is formatted. The
search is case-sensitive.
You need to iterate all the way to the end, because the iterator maintains a cursor in the database that is closed when the iterator has no more items remaining. Stopping iteration early would leak cursors.
namePrefix
- The beginning of the name to search for.public void close()
close
in interface AutoCloseable
Copyright © 2016. All rights reserved.