Storing data

juzna edited this page Jun 4, 2012 · 4 revisions

JDO

Standard interface for persistance, by Sun.
Support for both relational and nonrelational databases.
Object get identity when being made persistent. `pm.getObjectId();
Datastore identity JDO implementation managed
Application identity Our application managed

States

Transient

New object, not managed by JDO.
Every new object.
From persistent pm.makeTransient(obj);
From persistent pm.deletePersistent(obj)

Persistent

Persistent New after makePersistent(), not stored yet. To be stored after tx commit.
Persistent Clean saved and loaded.
Persistent Dirty - saved and loaded, modified in memory. To be stored after tx commit.
Persistent Deleted To be deleted after tx commit.
Persistent New Deleted (RARE), makePersistent() -> Persistence New -> deletePersistent() before commit.

Hollow

Saved in datastore. Not loaded in memory. Persistent becomes Hollow after tx commit.

Main classes

PersistenceManagerFactory

Configuration. Expensive init.

PersistenceManager

Non-expensive init.
pmf.getPersistenceManager()

Transaction

Atomic sequence of operations.
Completes or rollback, app is in consistent state.
pm.currentTransaction()

try { 
   …add/get/update/…
} finally { 
    if (tx.isActive()) { 
        tx.rollback(); 
}

Settings

Annotations

@PersistenceCapable - To declare a Java class as capable of being stored and retrieved from the datastore with JDO
**@Persistent **- fields of class to be stored
@Persistent(serialized="true") - stored as BLOB
@NotPersistent - fields of class NOT to be stored
@PrimaryKey, @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) - keys

Manipulation

Adding

tx.begin();
Person joe = new Person("Joe", "Doe");
pm.makePersistent(joe);
tx.commit();

Accessing

via identity
tx.begin();
Person somebody = (Person) pm.getObjectById(id, true);
tx.commit();
via Extent
tx.begin();
Extent extent = pm.getExtent(Person.class, false);
for (Iterator i = extent.iterator(); i.hasNext();) {
	Person somebody = (Person) i.next();
	… logic …
}
tx.commit();
via Query
tx.begin();
Query query = pm.newQUery(Person.class);
query.addFilter(…);
query.declareParameters(…); 
Collection result = (Collection) q.execute(param);
tx.commit();

Google App Engine

AppEngineDatastore main database.
Google Cloud SQL relational SQL, based on MySQL.
Google Cloud Storage for big files.

AppEngineDatastore

Object saved as Entity, identified by kind, identifier (string/numeric), [ancestor path].

NoSQL

NO join, aggregate, inequality filtering on multiple props, filtering data based on subquery.

Schemaless
Object

Implementation

Libraries
datanucleus-appengine-1.0.10.final.jar
datanucleus-core-1.1.5.jar
jdo2-api-2.3-eb.jar
Settings in jdoconfig.xml
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">

   <persistence-manager-factory name="transactions-optional">
       <property name="javax.jdo.PersistenceManagerFactoryClass"
           value="org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory"/>
       <property name="javax.jdo.option.ConnectionURL" value="appengine"/>
       <property name="javax.jdo.option.NontransactionalRead" value="true"/>
       <property name="javax.jdo.option.NontransactionalWrite" value="true"/>
       <property name="javax.jdo.option.RetainValues" value="true"/>
       <property name="datanucleus.appengine.autoCreateDatastoreTxns" value="false"/>
       <property name="datanucleus.appengine.datastoreEnableXGTransactions" value="true"/>
   </persistence-manager-factory>
</jdoconfig>
Class Card
@PersistenceCapable(detachable="true")
public class Card implements Serializable {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	@Persistent
	private BlobKey img;
	@Persistent
	private User owner;
	@Persistent
	private String name;
	@Persistent
	private boolean privacy;
	@Persistent
	private Date created;
	@Persistent
	private Set<Key> groupKeys;
	@Persistent(mappedBy = "card")
	@Element(dependent = "true")
	private List<Tag> tags;
Class Tag
@PersistenceCapable(detachable="true")
public class Tag implements Serializable {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	@Persistent
	private String name;
	@Persistent
	private String content;
	@Persistent
	private User owner;
	@Persistent
	private Date created;
	@Persistent
	private Card card;
Class Group
@PersistenceCapable(detachable="true")
public class Group implements Serializable {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	@Persistent
	private User owner;
	@Persistent
	private String name;
	@Persistent
	@Temporal(javax.persistence.TemporalType.DATE)
	private Date created;
	@Persistent
	private Set<Key> cardKeys;