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();
}
}
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();
}
}
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();
}
}
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();
}
}
Aggregations