Skip to content

Clotho Internals : Persistor

David Tran edited this page May 24, 2017 · 1 revision

Persistor

src/main/java/org/clothocad/core/persistence/Persistor.java

Summary

Persistor is the gateway to the database. Persistor contains methods related to CRUD operations and will perform final security & permission checks before allowing the database to be accessed.

How to Use

Thanks to Guice and dependency injection, you can pull an instance of the Persistor class from the global injector. Often times, that looks something like this:

private static Persistor persistor;

@Inject
public ClothoClass(Persistor persistor) {
    this.persistor = persistor;
}
...
...
//Use it sometime later
public void doStuff(ObjectId id)
{
	BioDesign bd = persistor.get(BioDesign.class,id);
	...
	...
}

Guice will automatically instantiate Persistor when the application is run.

Useful methods

There are many get methods to retrieve an object by ObjectId and to also cast the retrieved object to a class extending from ObjBase such as Part or BioDesign.
Ex: Persistor.get(BioDesign.class, idObject);

There are also the find and findRegex methods for querying the database by key:field pairs.

save handles both creating and updating the database depending on if the ObjectId already exists or not.

When to look/work here

Generally if you need to do anything related to the database that is broken/doesn't exist/needs to be extended, you'll do something related to Persistor, JongoConnection, and the interface ClothoConnection

If you ever need to expand the CRUD capabilities (such as adding/modifying regex search), you will probably need to work here (and also in JongoConnection).

If there are ever any moments when interacting with the database does not go the way you would expect, you may also need to work here. An example is when Persistor.get(BioDesign.class, bdId); would return a BioDesign object properly, but it would not check to see if the document from the database indeed represented a BioDesign object.