Android Open Source - NexusData Persistent Store






From Project

Back to project page NexusData.

License

The source code is released under:

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCT...

If you think the Android project NexusData listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.github.dkharrat.nexusdata.core;
//ww w .  j  av a 2  s  .c om
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

import com.github.dkharrat.nexusdata.metamodel.Entity;
import com.github.dkharrat.nexusdata.metamodel.Relationship;

/**
 * A PersistentStore represents the data store where the object graph is persisted. A persistent store is uniquely
 * identified by a UUID, which is also persisted in the store along with other metadata. There are two kinds of
 * PersistentStore:
 * <ul>
 *     <li>{@link AtomicStore}: A store in which the entire object graph is loaded into memory and saved together all at
 *     once. This is useful when the data set is small enough where optimal performance is required </li>
 *     <li>{@link IncrementalStore}: A store in which the object graph is loaded and saved incrementally, on demand.
 *     This is useful when working against a large dataset that is expensive to load entirely into memory, though this
 *     comes at the cost of some performance.</li>
 * </ul>
 */
public abstract class PersistentStore {

    private static final String UUID_KEY    = "_UUID";

    private PersistentStoreCoordinator storeCoordinator;
    private final URL location;
    private final Map<String,Object> metadata = new HashMap<String,Object>();

    /**
     * Constructs a new PersistentStore
     *
     * @param location  the location in which to save the data persistence file
     */
    PersistentStore(URL location) {
        this.location = location;
    }

    /**
     * Constructs a new PersistentStore
     *
     * @param location  the location in which to save the data persistence file
     */
    PersistentStore(File location) {
        URL url = null;
        if (location != null) {
            try {
                url = location.toURI().toURL();
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            }
        }
        this.location = url;
    }

    /**
     * Called when the PersistentStore is first loaded in order to load the store's metadata (e.g. store UUID, type, etc.).
     * A store's metadata must at minimum contain the store's UUID, which can be set by calling {@link #setUuid(UUID)}.
     */
    protected abstract void loadMetadata();

    /**
     * Returns the persistent store coordinator that owns this persistent store.
     *
     * @return the persistent store coordinator that owns this persistent store
     */
    public PersistentStoreCoordinator getCoordinator() {
        return storeCoordinator;
    }

    /**
     * Returns the location where this persistent store is saved.
     *
     * @return the file where this persistent store is saved
     */
    public URL getLocation() {
        return location;
    }

    /**
     * Returns the store's unique identifier. This is persisted with the store, so it's useful to identify specific
     * store instances. Note that nothing prevents a user from copying a persistent store (and thus it's associated
     * UUID). Therefore, you may technically encounter two stores with the same UUID if one manually made a copy of
     * the store.
     *
     * Store UUIDs are used to identify {@link ManagedObject}
     *
     * @return the store's UUID
     */
    public UUID getUuid() {
        return (UUID) metadata.get(UUID_KEY);
    }

    /**
     * Sets the store's unique identifier. This is used by the Store itself when loading its metadata. Once it's set,
     * it must not change throughout the lifetime of the Store.
     *
     * @param uuid  the UUID to use.
     */
    protected void setUuid(UUID uuid) {
        metadata.put(UUID_KEY, uuid);
    }

    /**
     * Sets the associated persistent store coordinator.
     *
     * @param coordinator the persistent store coordinator to use
     */
    void setPersistentStoreCoordinator(PersistentStoreCoordinator coordinator) {
        storeCoordinator = coordinator;
    }

    /**
     * Creates a new {@link ObjectID} associated with a reference object.
     *
     * @param entity            the entity associated with the ObjectID
     * @param referenceObject   the reference object that uniquely identifies the object. This object must implement
     *                          {@link Object#hashCode()} and {@link Object#equals(Object)} appropriately, such that
     *                          for two different instances that represent the same reference object,
     *                          {@code Object#hashCode()} must return the same value and
     *                          {@code Object#equals(Object)} must return true.
     * @return the newly created id
     */
    ObjectID createObjectID(Entity<?> entity, Object referenceObject) {
        ObjectID id = new ObjectID(this, entity, referenceObject);
        return id;
    }

    /**
     * Returns the associated reference object for the specified ObjectID.
     *
     * @param objectID  the ObjectID to get its reference object
     * @return          the reference object associated with the specified ObjectID
     */
    protected Object getReferenceObjectForObjectID(ObjectID objectID) {
        return objectID.getReferenceObject();
    }

    /**
     * Returns the corresponding ObjectIDs for the specified ManagedObjects. These ObjectIDs must be permanent and must
     * not be changed after this call. Objects that already have a permanent ObjectID must return the same one.
     *
     * @param objects   the list of objects that shall get permanent IDs.
     *
     * @return the corresponding ObjectIDs of the specified ManagedObjects
     */
    abstract List<ObjectID> getPermanentIDsForObjects(List<ManagedObject> objects);

    /**
     * Returns the associated cache node of the specified ObjectID.
     *
     * @param objectID  the ObjectID
     * @param context   the context to which the data will be returned
     *
     * @return the associated cache node of the specified ObjectID, or {@code null} if no such object exists
     */
    abstract StoreCacheNode getObjectValues(ObjectID objectID, ObjectContext context);

    /**
     * Returns the ObjectID of the related object for the to-one relationship.
     *
     * @param objectID      the objectID of the source object for which the relationship is requested
     * @param relationship  the relationship property to retrieve
     * @param context       the context to which the data will be returned
     *
     * @return the ObjectID of the related object, or {@code null} if there is no related object
     */
    abstract ObjectID getToOneRelationshipValue(ObjectID objectID, Relationship relationship, ObjectContext context);

    /**
     * Returns the set of ObjectIDs of the related objects for the to-many relationship.
     *
     * @param objectID      the objectID of the source object for which the relationship is requested
     * @param relationship  the relationship property to retrieve
     * @param context       the context to which the data will be returned
     *
     * @return the set of ObjectIDs of the related objects. The returned collection may be empty if no related objects
     *         exist.
     */
    abstract Set<ObjectID> getToManyRelationshipValue(ObjectID objectID, Relationship relationship, ObjectContext context);

    /**
     * Queries the persistent store's records based on the specified criteria.
     *
     * @param request   the request that describes the criteria to query
     * @param context   the context to which the data will be returned
     *
     * @return the list of objects that match the query criteria
     */
    abstract <T extends ManagedObject> List<T> executeFetchRequest(FetchRequest<T> request, ObjectContext context);

    /**
     * Saves the specified changes to the persistent store.
     *
     * @param request   the request that describes the changes to save
     * @param context   the context that is requesting the save
     */
    abstract void executeSaveRequest(SaveChangesRequest request, ObjectContext context);

    @Override
    public String toString() {
        return getClass().getSimpleName()+" [location=" + location + ", UUID=" + getUuid() + "]";
    }
}




Java Source Code List

com.github.dkharrat.nexusdata.core.AtomicStore.java
com.github.dkharrat.nexusdata.core.ChangedObjectsSet.java
com.github.dkharrat.nexusdata.core.FaultingSet.java
com.github.dkharrat.nexusdata.core.FetchRequest.java
com.github.dkharrat.nexusdata.core.IncrementalStore.java
com.github.dkharrat.nexusdata.core.ManagedObject.java
com.github.dkharrat.nexusdata.core.NoSuchPropertyException.java
com.github.dkharrat.nexusdata.core.ObjectContextNotifier.java
com.github.dkharrat.nexusdata.core.ObjectContext.java
com.github.dkharrat.nexusdata.core.ObjectID.java
com.github.dkharrat.nexusdata.core.ObjectsChangedNotification.java
com.github.dkharrat.nexusdata.core.PersistentStoreCoordinator.java
com.github.dkharrat.nexusdata.core.PersistentStoreRequest.java
com.github.dkharrat.nexusdata.core.PersistentStore.java
com.github.dkharrat.nexusdata.core.SaveChangesRequest.java
com.github.dkharrat.nexusdata.core.SortDescriptor.java
com.github.dkharrat.nexusdata.core.StoreCacheNode.java
com.github.dkharrat.nexusdata.metamodel.Attribute.java
com.github.dkharrat.nexusdata.metamodel.Entity.java
com.github.dkharrat.nexusdata.metamodel.ObjectModelJsonParser.java
com.github.dkharrat.nexusdata.metamodel.ObjectModel.java
com.github.dkharrat.nexusdata.metamodel.Property.java
com.github.dkharrat.nexusdata.metamodel.Relationship.java
com.github.dkharrat.nexusdata.modelgen.ModelGenerator.java
com.github.dkharrat.nexusdata.modelgen.Startup.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Attribute.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Entity.java
com.github.dkharrat.nexusdata.modelgen.metamodel.EnumProperty.java
com.github.dkharrat.nexusdata.modelgen.metamodel.ModelWrapper.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Model.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Property.java
com.github.dkharrat.nexusdata.modelgen.metamodel.Relationship.java
com.github.dkharrat.nexusdata.predicate.ComparisonPredicate.java
com.github.dkharrat.nexusdata.predicate.CompoundPredicate.java
com.github.dkharrat.nexusdata.predicate.ConstantExpression.java
com.github.dkharrat.nexusdata.predicate.ExpressionBuilder.java
com.github.dkharrat.nexusdata.predicate.ExpressionVisitor.java
com.github.dkharrat.nexusdata.predicate.Expression.java
com.github.dkharrat.nexusdata.predicate.FieldPathExpression.java
com.github.dkharrat.nexusdata.predicate.NotPredicate.java
com.github.dkharrat.nexusdata.predicate.PredicateBuilder.java
com.github.dkharrat.nexusdata.predicate.Predicate.java
com.github.dkharrat.nexusdata.predicate.ThisExpression.java
com.github.dkharrat.nexusdata.predicate.parser.ComparisonParselet.java
com.github.dkharrat.nexusdata.predicate.parser.ConstantParselet.java
com.github.dkharrat.nexusdata.predicate.parser.ExpressionNode.java
com.github.dkharrat.nexusdata.predicate.parser.GroupParselet.java
com.github.dkharrat.nexusdata.predicate.parser.InfixParselet.java
com.github.dkharrat.nexusdata.predicate.parser.LexerGrammar.java
com.github.dkharrat.nexusdata.predicate.parser.Lexer.java
com.github.dkharrat.nexusdata.predicate.parser.LogicalParselet.java
com.github.dkharrat.nexusdata.predicate.parser.NameParselet.java
com.github.dkharrat.nexusdata.predicate.parser.ParseException.java
com.github.dkharrat.nexusdata.predicate.parser.Parser.java
com.github.dkharrat.nexusdata.predicate.parser.PredicateParser.java
com.github.dkharrat.nexusdata.predicate.parser.PrefixParselet.java
com.github.dkharrat.nexusdata.predicate.parser.Token.java
com.github.dkharrat.nexusdata.store.AndroidSqlPersistentStore.java
com.github.dkharrat.nexusdata.store.DatabaseHelper.java
com.github.dkharrat.nexusdata.store.InMemoryPersistentStore.java
com.github.dkharrat.nexusdata.store.PredicateToSQL.java
com.github.dkharrat.nexusdata.utils.DateUtil.java
com.github.dkharrat.nexusdata.utils.ObjectUtil.java
com.github.dkharrat.nexusdata.utils.SqlTableBuilder.java
com.github.dkharrat.nexusdata.utils.StreamUtil.java
com.github.dkharrat.nexusdata.utils.StringUtil.java
com.github.dkharrat.nexusdata.utils.android.CursorUtil.java
com.github.dkharrat.nexusdata.utils.android.SQLiteDatabaseHelper.java
org.example.todo.MainActivity.java
org.example.todo.NewTaskActivity.java
org.example.todo.Task.java
org.example.todo.TodoApp.java
org.example.todo.User.java
org.example.todo._Task.java
org.example.todo._User.java