Search in sources :

Example 61 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project nigori by ucam-cl-dtg.

the class AppEngineDatabase method getIndices.

@Override
public Collection<byte[]> getIndices(User user) {
    PersistenceManager pm = pmfInstance.getPersistenceManager();
    try {
        Query getIndices = new Query(Lookup.class.getSimpleName());
        getIndices.setAncestor(castUser(user).getKey());
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        List<Entity> results = datastore.prepare(getIndices).asList(FetchOptions.Builder.withDefaults());
        List<byte[]> answer = new ArrayList<byte[]>();
        for (Entity result : results) {
            Object index = result.getProperty("index");
            if (index instanceof ShortBlob) {
                answer.add(((ShortBlob) index).getBytes());
            } else if (index instanceof Blob) {
                answer.add(((Blob) index).getBytes());
            } else
                throw new ClassCastException("Could not transform to byte[] via a (Short)Blob: " + index.getClass().getSimpleName());
        }
        return answer;
    } catch (JDOObjectNotFoundException e) {
        return null;
    } finally {
        pm.close();
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) ShortBlob(com.google.appengine.api.datastore.ShortBlob) Blob(com.google.appengine.api.datastore.Blob) Query(com.google.appengine.api.datastore.Query) PersistenceManager(javax.jdo.PersistenceManager) DatastoreService(com.google.appengine.api.datastore.DatastoreService) ArrayList(java.util.ArrayList) ShortBlob(com.google.appengine.api.datastore.ShortBlob) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 62 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project nigori by ucam-cl-dtg.

the class AppEngineDatabase method deleteRecord.

@Override
public boolean deleteRecord(User user, byte[] index) {
    PersistenceManager pm = pmfInstance.getPersistenceManager();
    try {
        Key lookupKey = getLookupKey(user, index);
        Lookup lookup = pm.getObjectById(Lookup.class, lookupKey);
        // TODO(drt24) cleanup
        try {
            Query getRevisionValues = new Query(AppEngineRecord.class.getSimpleName());
            getRevisionValues.setAncestor(lookupKey);
            DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
            List<Entity> results = datastore.prepare(getRevisionValues).asList(FetchOptions.Builder.withDefaults());
            for (Entity entity : results) {
                pm.deletePersistent(pm.getObjectById(AppEngineRecord.class, entity.getKey()));
            }
        } finally {
            // even if there is no value the index still needs to be deleted - but we haven't
            // actually done a delete
            pm.deletePersistent(lookup);
        }
        return true;
    } catch (JDOObjectNotFoundException e) {
        return false;
    } finally {
        pm.close();
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) PersistenceManager(javax.jdo.PersistenceManager) DatastoreService(com.google.appengine.api.datastore.DatastoreService) Key(com.google.appengine.api.datastore.Key)

Example 63 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project nigori by ucam-cl-dtg.

the class AppEngineDatabase method getRecord.

@Override
public Collection<RevValue> getRecord(User user, byte[] index) throws IOException {
    PersistenceManager pm = pmfInstance.getPersistenceManager();
    try {
        // TODO(drt24): cleanup this method
        Key lookupKey = getLookupKey(user, index);
        // If this doesn't exist there is no key so null gets returned by JDOObjectNotFoundException
        Lookup lookup = pm.getObjectById(Lookup.class, lookupKey);
        List<RevValue> answer = new ArrayList<RevValue>();
        Query getRevisionValues = new Query(AppEngineRecord.class.getSimpleName());
        getRevisionValues.setAncestor(lookupKey);
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        List<Entity> results = datastore.prepare(getRevisionValues).asList(FetchOptions.Builder.withDefaults());
        for (Entity result : results) {
            ByteArrayInputStream bais = new ByteArrayInputStream(((Blob) result.getProperty("revision")).getBytes());
            ObjectInputStream ndis = new ObjectInputStream(bais);
            answer.add(new RevValue(((Revision) ndis.readObject()).getBytes(), ((Blob) result.getProperty("value")).getBytes()));
        }
        if (lookup != null) {
            return answer;
        } else {
            return null;
        }
    } catch (JDOObjectNotFoundException e) {
        return null;
    } catch (ClassNotFoundException e) {
        throw new IOException(e);
    } finally {
        pm.close();
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) ShortBlob(com.google.appengine.api.datastore.ShortBlob) Blob(com.google.appengine.api.datastore.Blob) Query(com.google.appengine.api.datastore.Query) PersistenceManager(javax.jdo.PersistenceManager) DatastoreService(com.google.appengine.api.datastore.DatastoreService) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) RevValue(com.google.nigori.common.RevValue) ByteArrayInputStream(java.io.ByteArrayInputStream) Key(com.google.appengine.api.datastore.Key) ObjectInputStream(java.io.ObjectInputStream)

Example 64 with DatastoreService

use of com.google.appengine.api.datastore.DatastoreService in project nigori by ucam-cl-dtg.

the class AppEngineDatabase method clearOldNonces.

@Override
public void clearOldNonces() {
    PersistenceManager pm = pmfInstance.getPersistenceManager();
    try {
        Query getAllNonces = new Query(AENonce.class.getSimpleName());
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        List<Entity> results = datastore.prepare(getAllNonces).asList(FetchOptions.Builder.withDefaults());
        for (Entity entity : results) {
            int sinceEpoch = (int) (long) ((Long) entity.getProperty("sinceEpoch"));
            if (!Nonce.isRecent(sinceEpoch)) {
                pm.deletePersistent(pm.getObjectById(AENonce.class, entity.getKey()));
            }
        }
    } finally {
        pm.close();
    }
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PersistenceManager(javax.jdo.PersistenceManager) DatastoreService(com.google.appengine.api.datastore.DatastoreService)

Aggregations

DatastoreService (com.google.appengine.api.datastore.DatastoreService)64 Entity (com.google.appengine.api.datastore.Entity)43 Test (org.junit.Test)29 Key (com.google.appengine.api.datastore.Key)25 Query (com.google.appengine.api.datastore.Query)25 Transaction (com.google.appengine.api.datastore.Transaction)15 EntityNotFoundException (com.google.appengine.api.datastore.EntityNotFoundException)6 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)6 LocalDatastoreService (com.google.appengine.api.datastore.dev.LocalDatastoreService)6 ArrayList (java.util.ArrayList)6 Date (java.util.Date)5 PersistenceManager (javax.jdo.PersistenceManager)5 Filter (com.google.appengine.api.datastore.Query.Filter)4 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)4 FilterPredicate (com.google.appengine.api.datastore.Query.FilterPredicate)3 FilterChain (javax.servlet.FilterChain)3 BlobKey (com.google.appengine.api.blobstore.BlobKey)2 Blob (com.google.appengine.api.datastore.Blob)2 DatastoreServiceConfig (com.google.appengine.api.datastore.DatastoreServiceConfig)2 FetchOptions (com.google.appengine.api.datastore.FetchOptions)2