Class DBCachedObjectStoreFactory
DBAnnotatedObject
See DBAnnotatedObject for more documentation, including an example object definition. To
create a store, e.g., for Person:
interface MyDomainObject {
Person createPerson(String name, String address);
Person getPerson(long id);
Collection<? extends Person> getPeopleNamed(String name);
}
public class DBMyDomainObject extends DBCachedDomainObjectAdapter implements MyDomainObject {
private final DBCachedObjectStoreFactory factory;
private final DBCachedObjectStore<DBPerson> people;
private final DBCachedObjectIndex<String, DBPerson> peopleByName;
public DBMyDomainObject() { // Constructor parameters elided
// super() invocation elided
factory = new DBCachedObjectStoreFactory(this);
try {
people = factory.getOrCreateCachedStore(DBPerson.TABLE_NAME, DBPerson.class,
DBPerson::new, false);
peopleByName = people.getIndex(String.class, DBPerson.NAME_COLUMN);
}
catch (VersionException e) {
// ...
}
catch (IOException e) {
// ...
}
}
@Override
public Person createPerson(String name, String address) {
// Locking details elided
DBPerson person = people.create();
person.set(name, address);
return person;
}
@Override
public Person getPerson(int id) {
// Locking details elided
return people.getAt(id);
}
@Override
public Collection<Person> getPeopleNamed(String name) {
// Locking details elided
return peopleByName.get(name);
}
}
The factory manages tables on behalf of the domain object, so it is typically the first thing
constructed. In practice, complex domain objects should be composed of several managers, each of
which constructs its own stores, but for simplicity in this example, we construct the people
store in the domain object. This will check the schema and could throw a
VersionException. Typically, immediately after constructing the store, all desired
indexes of the store are retrieved. The domain object then provides API methods for creating and
retrieving people. Providing direct API client access to the store from a domain object is highly
discouraged.
- Implementation Notes:
- This class bears the responsibility of processing the
DBAnnotatedField,DBAnnotatedColumn, andDBAnnotatedObjectInfoannotations. The relevant entry point is {buildInfo(Class). It creates aDBCachedObjectStoreFactory.TableInfofor the given class, which builds the schema for creating theTablethat backs an object store for that class.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classDBCachedObjectStoreFactory.AbstractDBFieldCodec<VT,OT extends DBAnnotatedObject, FT extends Field> An abstract implementation ofDBCachedObjectStoreFactory.DBFieldCodecstatic classThe built-in codec forbooleanstatic classThe built-in codec forbyte[]static classThe built-in codec forbytestatic interfaceDBCachedObjectStoreFactory.DBFieldCodec<VT,OT extends DBAnnotatedObject, FT extends Field> A codec for encoding alternative data typesstatic classDBCachedObjectStoreFactory.EnumDBByteFieldCodec<OT extends DBAnnotatedObject,E extends Enum<E>> The built-in codec forEnumstatic classThe built-in codec forintstatic classThe built-in codec forlong[]static classThe built-in codec forlongstatic interfaceCodec for a primitive typestatic final recordstatic final recordstatic classThe built-in codec forshortstatic classThe built-in codec forStringstatic classA custom codec for field of "variant" type -
Constructor Summary
ConstructorsConstructorDescriptionConstruct an object store factory -
Method Summary
Modifier and TypeMethodDescription<T extends DBAnnotatedObject>
DBCachedObjectStore<T> getOrCreateCachedStore(String tableName, Class<T> cls, DBAnnotatedObjectFactory<T> factory, boolean upgradable) Get or create a cached store of objects of the given classgetOrCreateTable(String name, Class<? extends DBAnnotatedObject> cls, boolean upgradable) Get or create the table needed to store objects of the given class
-
Constructor Details
-
DBCachedObjectStoreFactory
Construct an object store factory- Parameters:
adapter- the object whose tables to manage
-
-
Method Details
-
getOrCreateTable
public Table getOrCreateTable(String name, Class<? extends DBAnnotatedObject> cls, boolean upgradable) throws IOException, VersionException Get or create the table needed to store objects of the given classSee
getOrCreateCachedStore(String, Class, DBAnnotatedObjectFactory, boolean)- Parameters:
name- the table namecls- the type of objects to storeupgradable- true ifVersionExceptions should be marked upgradable when an existing table's version is earlier than expected- Returns:
- the table
- Throws:
IOException- if there's an issue accessing the databaseVersionException- if an existing table's version does not match that expected
-
getOrCreateCachedStore
public <T extends DBAnnotatedObject> DBCachedObjectStore<T> getOrCreateCachedStore(String tableName, Class<T> cls, DBAnnotatedObjectFactory<T> factory, boolean upgradable) throws VersionException, IOException Get or create a cached store of objects of the given class- Type Parameters:
T- the type of objects in the store- Parameters:
tableName- the table namecls- the class describingDBCachedObjectStoreFactoryfactory- the object's constructor, usually a method reference or lambdaupgradable- true ifVersionExceptions should be marked upgradable when an existing table's version is earlier than expected- Returns:
- the table
- Throws:
IOException- if there's an issue accessing the databaseVersionException- if an existing table's version does not match that expected
-