use of com.google.cloud.datastore.DatastoreException in project google-cloud-java by GoogleCloudPlatform.
the class ITDatastoreTest method testGetArrayNoDeferredResults.
@Test
public void testGetArrayNoDeferredResults() {
DATASTORE.put(ENTITY3);
Iterator<Entity> result = DATASTORE.fetch(KEY1, Key.newBuilder(KEY1).setName("bla").build(), KEY2, KEY3).iterator();
assertEquals(ENTITY1, result.next());
assertNull(result.next());
assertEquals(ENTITY2, result.next());
Entity entity3 = result.next();
assertEquals(ENTITY3, entity3);
assertTrue(entity3.isNull("null"));
assertFalse(entity3.getBoolean("bool"));
assertEquals(LIST_VALUE2.get(), entity3.getList("list"));
FullEntity<IncompleteKey> partial1 = entity3.getEntity("partial1");
FullEntity<IncompleteKey> partial2 = entity3.getEntity("partial2");
assertEquals(PARTIAL_ENTITY2, partial1);
assertEquals(ENTITY2, partial2);
assertEquals(ValueType.BOOLEAN, entity3.getValue("bool").getType());
assertEquals(LAT_LNG_VALUE, entity3.getValue("latLng"));
assertEquals(EMPTY_LIST_VALUE, entity3.getValue("emptyList"));
assertEquals(8, entity3.getNames().size());
assertFalse(entity3.contains("bla"));
try {
entity3.getString("str");
fail("Expecting a failure");
} catch (DatastoreException expected) {
// expected - no such property
}
assertFalse(result.hasNext());
}
use of com.google.cloud.datastore.DatastoreException in project google-cloud-java by GoogleCloudPlatform.
the class ITDatastoreTest method testAddEntity.
@Test
public void testAddEntity() {
List<Entity> keys = DATASTORE.fetch(ENTITY1.getKey(), ENTITY3.getKey());
assertEquals(ENTITY1, keys.get(0));
assertNull(keys.get(1));
assertEquals(2, keys.size());
try {
DATASTORE.add(ENTITY1);
fail("Expecting a failure");
} catch (DatastoreException expected) {
// expected;
}
List<Entity> entities = DATASTORE.add(ENTITY3, PARTIAL_ENTITY1, PARTIAL_ENTITY2);
assertEquals(ENTITY3, DATASTORE.get(ENTITY3.getKey()));
assertEquals(ENTITY3, entities.get(0));
assertEquals(PARTIAL_ENTITY1.getNames(), entities.get(1).getNames());
assertEquals(PARTIAL_ENTITY1.getKey().getAncestors(), entities.get(1).getKey().getAncestors());
assertNotNull(DATASTORE.get(entities.get(1).getKey()));
assertEquals(PARTIAL_ENTITY2.getNames(), entities.get(2).getNames());
assertEquals(PARTIAL_ENTITY2.getKey().getAncestors(), entities.get(2).getKey().getAncestors());
assertNotNull(DATASTORE.get(entities.get(2).getKey()));
for (Entity entity : entities) {
DATASTORE.delete(entity.getKey());
}
}
use of com.google.cloud.datastore.DatastoreException in project jetty.project by eclipse.
the class GCloudSessionDataStore method doStore.
/**
* @see org.eclipse.jetty.server.session.AbstractSessionDataStore#doStore(java.lang.String, org.eclipse.jetty.server.session.SessionData, long)
*/
@Override
public void doStore(String id, SessionData data, long lastSaveTime) throws Exception {
if (LOG.isDebugEnabled())
LOG.debug("Writing session {} to DataStore", data.getId());
Entity entity = entityFromSession(data, makeKey(id, _context));
//attempt the update with exponential back-off
int backoff = getBackoffMs();
int attempts;
for (attempts = 0; attempts < getMaxRetries(); attempts++) {
try {
_datastore.put(entity);
return;
} catch (DatastoreException e) {
if (e.isRetryable()) {
if (LOG.isDebugEnabled())
LOG.debug("Datastore put retry {} waiting {}ms", attempts, backoff);
try {
Thread.currentThread().sleep(backoff);
} catch (InterruptedException x) {
}
backoff *= 2;
} else {
throw e;
}
}
}
//retries have been exceeded
throw new UnwriteableSessionDataException(id, _context, null);
}
use of com.google.cloud.datastore.DatastoreException in project jetty.project by eclipse.
the class GCloudSessionDataStore method sessionFromEntity.
/**
* Generate SessionData from an Entity retrieved from gcloud datastore.
* @param entity the entity
* @return the session data
* @throws Exception if unable to get the entity
*/
protected SessionData sessionFromEntity(Entity entity) throws Exception {
if (entity == null)
return null;
final AtomicReference<SessionData> reference = new AtomicReference<SessionData>();
final AtomicReference<Exception> exception = new AtomicReference<Exception>();
Runnable load = new Runnable() {
public void run() {
try {
//turn an Entity into a Session
String id = entity.getString(_model.getId());
String contextPath = entity.getString(_model.getContextPath());
String vhost = entity.getString(_model.getVhost());
long accessed = entity.getLong(_model.getAccessed());
long lastAccessed = entity.getLong(_model.getLastAccessed());
long createTime = entity.getLong(_model.getCreateTime());
long cookieSet = entity.getLong(_model.getCookieSetTime());
String lastNode = entity.getString(_model.getLastNode());
long lastSaved = 0;
//for compatibility with previously saved sessions, lastSaved may not be present
try {
lastSaved = entity.getLong(_model.getLastSaved());
} catch (DatastoreException e) {
LOG.ignore(e);
}
long expiry = entity.getLong(_model.getExpiry());
long maxInactive = entity.getLong(_model.getMaxInactive());
Blob blob = (Blob) entity.getBlob(_model.getAttributes());
SessionData session = newSessionData(id, createTime, accessed, lastAccessed, maxInactive);
session.setLastNode(lastNode);
session.setContextPath(contextPath);
session.setVhost(vhost);
session.setCookieSet(cookieSet);
session.setLastNode(lastNode);
session.setLastSaved(lastSaved);
session.setExpiry(expiry);
try (ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(blob.asInputStream())) {
Object o = ois.readObject();
session.putAllAttributes((Map<String, Object>) o);
} catch (Exception e) {
throw new UnreadableSessionDataException(id, _context, e);
}
reference.set(session);
} catch (Exception e) {
exception.set(e);
}
}
};
//ensure this runs in the context classloader
_context.run(load);
if (exception.get() != null)
throw exception.get();
return reference.get();
}
use of com.google.cloud.datastore.DatastoreException in project google-cloud-java by GoogleCloudPlatform.
the class DatastoreSnippets method addSingleEntity.
/**
* Example of adding a single entity.
*/
// [TARGET add(FullEntity)]
// [VARIABLE "my_key_name"]
public void addSingleEntity(String keyName) {
// [START addSingleEntity]
Key key = datastore.newKeyFactory().setKind("MyKind").newKey(keyName);
Entity.Builder entityBuilder = Entity.newBuilder(key);
entityBuilder.set("propertyName", "value");
Entity entity = entityBuilder.build();
try {
datastore.add(entity);
} catch (DatastoreException ex) {
if ("ALREADY_EXISTS".equals(ex.getReason())) {
// entity.getKey() already exists
}
}
// [END addSingleEntity]
}
Aggregations