Android Open Source - NexusData Atomic 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;
/*from  w  ww .  ja v a2  s.  co  m*/
import java.io.File;
import java.net.URL;
import java.util.*;

import com.github.dkharrat.nexusdata.metamodel.Property;
import com.github.dkharrat.nexusdata.metamodel.Relationship;
import com.github.dkharrat.nexusdata.utils.ObjectUtil;

/**
 * An AtomicStore is a persistence store in which data is loaded and saved all at once. It is useful when the data set
 * is small enough to fit in memory and good performance is needed.
 */
public abstract class AtomicStore extends PersistentStore {

    private final Map<ObjectID, StoreCacheNode> idsToCacheNodes = new HashMap<ObjectID, StoreCacheNode>();

    /**
     * Constructs a new Atomic store
     *
     * @param location  the location in which to save the data persistence file
     */
    public AtomicStore(URL location) {
        super(location);
    }

    public AtomicStore(File location) {
        super(location);
    }

    public abstract void load();
    public abstract void save();
    public abstract Object createReferenceObjectForManagedObject(ManagedObject object);

    @Override
    protected void loadMetadata() {
        setUuid(UUID.randomUUID());
    }

    Set<StoreCacheNode> getCacheNodes() {
        return new HashSet<StoreCacheNode>(idsToCacheNodes.values());
    }

    protected void addCacheNode(StoreCacheNode cacheNode) {
        idsToCacheNodes.put(cacheNode.getID(), cacheNode);
    }

    protected void removeCacheNode(StoreCacheNode cacheNode) {
        idsToCacheNodes.remove(cacheNode.getID());
    }

    protected void updateCacheNode(StoreCacheNode cacheNode, ManagedObject object) {
        for(Property property : object.getEntity().getProperties()) {
            Object value = object.getValue(property.getName());
            if (property.isRelationship()) {
                Relationship relationship = (Relationship) property;
                if (relationship.isToOne()) {
                    ManagedObject relatedObject = (ManagedObject) value;
                    if (relatedObject != null) {
                        cacheNode.setProperty(relationship.getName(), relatedObject.getID());
                    }
                } else {
                    FaultingSet<?> relatedObjects = (FaultingSet<?>) value;
                    cacheNode.setProperty(relationship.getName(), relatedObjects.getObjectIDs());
                }
            } else {
                cacheNode.setProperty(property.getName(), value);
            }
        }
    }

    StoreCacheNode createCacheNode(ManagedObject object) {
        ObjectID id = object.getID();
        StoreCacheNode cacheNode = new StoreCacheNode(id);
        updateCacheNode(cacheNode, object);

        return cacheNode;
    }

    private StoreCacheNode getCacheNode(ObjectID objectID) {
        return idsToCacheNodes.get(objectID);
    }

    @Override
    List<ObjectID> getPermanentIDsForObjects(List<ManagedObject> objects) {
        List<ObjectID> objectIDs = new ArrayList<ObjectID>();
        for (ManagedObject object : objects) {
            ObjectID id;

            Object refObject = createReferenceObjectForManagedObject(object);
            id = createObjectID(object.getEntity(), refObject);

            objectIDs.add(id);
        }

        return objectIDs;
    }

    @Override
    StoreCacheNode getObjectValues(ObjectID objectID, ObjectContext context) {
        return getCacheNode(objectID);
    }

    @Override
    ObjectID getToOneRelationshipValue(
            ObjectID objectID,
            Relationship relationship,
            ObjectContext context) {

        StoreCacheNode cacheNode = getCacheNode(objectID);

        return (ObjectID) cacheNode.getProperty(relationship.getName());
    }

    @Override
    Set<ObjectID> getToManyRelationshipValue(
            ObjectID objectID,
            Relationship relationship,
            ObjectContext context) {

        StoreCacheNode cacheNode = getCacheNode(objectID);
        @SuppressWarnings("unchecked")
        Set<ObjectID> relatedObjectIDs = (Set<ObjectID>) cacheNode.getProperty(relationship.getName());

        return relatedObjectIDs;
    }

    private <T extends ManagedObject> void sort(final List<T> list, final List<SortDescriptor> sortDescriptors) {
        Collections.sort(list, new Comparator<T>() {
            @SuppressWarnings("unchecked")
            @Override
            public int compare(T lhs, T rhs) {
                int result = 0;
                for (SortDescriptor sortDesc : sortDescriptors) {
                    Object lhsValue = lhs.getValue(sortDesc.getAttributeName());
                    Object rhsValue = rhs.getValue(sortDesc.getAttributeName());

                    if (lhsValue == null && rhsValue == null) {
                        return 0;
                    } else if (lhsValue == null || rhsValue == null) {
                        return -1;
                    } else {
                        @SuppressWarnings("rawtypes")
                        Comparable lhsComparable = ObjectUtil.toComparable(lhsValue);
                        @SuppressWarnings("rawtypes")
                        Comparable rhsComparable = ObjectUtil.toComparable(rhsValue);
                        result = lhsComparable.compareTo(rhsComparable);

                        if (result == 0) {
                            continue;
                        }

                        result = sortDesc.isAscending() ? result : -result;
                    }
                }
                return result;
            }
        });
    }

    @Override
    <T extends ManagedObject> List<T> executeFetchRequest(final FetchRequest<T> request, final ObjectContext context) {
        List<T> results = new ArrayList<T>();

        for (StoreCacheNode cacheNode : getCacheNodes()) {
            ObjectID objID = cacheNode.getID();
            @SuppressWarnings("unchecked")
            T obj = (T)context.getExistingObject(objID);

            if (objID.getType().isAssignableFrom(request.getEntity().getType())) {
                if (request.getPredicate() == null || request.getPredicate().evaluate(obj)) {
                    results.add(obj);
                }
            }
        }

        if (request.hasSortDescriptors()) {
            sort(results, request.getSortDescriptors());
        }

        if (request.getLimit() < Integer.MAX_VALUE && request.getLimit() < results.size()) {
            results = results.subList(0, request.getLimit());
        }

        return results;
    }

    @Override
    void executeSaveRequest(SaveChangesRequest request, ObjectContext context) {
        for (ManagedObject object : request.getChanges().getInsertedObjects()) {
            StoreCacheNode cacheNode = createCacheNode(object);
            addCacheNode(cacheNode);
        }

        for (ManagedObject object : request.getChanges().getDeletedObjects()) {
            if (!object.isInserted()) {
                StoreCacheNode cacheNode = getObjectValues(object.getID(), context);
                removeCacheNode(cacheNode);
                object.setManagedObjectContext(null);
            }
        }

        for (ManagedObject object : request.getChanges().getUpdatedObjects()) {
            StoreCacheNode cacheNode = getObjectValues(object.getID(), context);
            updateCacheNode(cacheNode, object);
        }

        save();
    }
}




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