com.google.appengine.api.datastore.Entity Java Examples
The following examples show how to use
com.google.appengine.api.datastore.Entity.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TransactionTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testSingleDefault() throws EntityNotFoundException, InterruptedException { clearData(kindName); Transaction tx = service.beginTransaction(); Entity newRec = new Entity(kindName); newRec.setProperty("check", "4100331"); newRec.setProperty("step", "added"); Key key = service.put(tx, newRec); tx.commit(); Entity qRec = service.get(key); assertEquals("4100331", qRec.getProperty("check")); tx = service.beginTransaction(); qRec = service.get(key); qRec.setUnindexedProperty("step", "update"); service.put(tx, newRec); tx.rollback(); qRec = service.get(key); assertEquals("added", qRec.getProperty("step")); }
Example #2
Source File: RemoteApiSharedTests.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
@Override public void run( DatastoreService ds, Supplier<Key> keySupplier, Supplier<Entity> entitySupplier) { // Put a fresh entity. Entity originalEntity = new Entity(getFreshKindName()); originalEntity.setProperty("prop1", 75L); ds.put(originalEntity); Key key = originalEntity.getKey(); // Prepare a new version of it with a different property value. Entity mutatedEntity = new Entity(key); mutatedEntity.setProperty("prop1", 76L); // Test Get/Put within a transaction. Transaction txn = ds.beginTransaction(); assertGetEquals(ds, key, originalEntity); ds.put(mutatedEntity); // Write the mutated Entity. assertGetEquals(ds, key, originalEntity); // Within a txn, the put is not yet reflected. txn.commit(); // Now that the txn is committed, the mutated entity will show up in Get. assertGetEquals(ds, key, mutatedEntity); }
Example #3
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 6 votes |
/** * Delete a value stored in the project's datastore. * @param sessionId Request from which the session is extracted. */ protected void deleteSessionVariables(String sessionId, String... varNames) { if (sessionId.equals("")) { return; } Key key = KeyFactory.createKey(SESSION_KIND, sessionId); Transaction transaction = datastore.beginTransaction(); try { Entity stateEntity = datastore.get(transaction, key); for (String varName : varNames) { stateEntity.removeProperty(varName); } datastore.put(transaction, stateEntity); transaction.commit(); } catch (EntityNotFoundException e) { // Ignore - if there's no session, there's nothing to delete. } finally { if (transaction.isActive()) { transaction.rollback(); } } }
Example #4
Source File: DatastoreSessionFilter.java From getting-started-java with Apache License 2.0 | 6 votes |
/** * Delete a value stored in the project's datastore. * @param sessionId Request from which the session is extracted. */ protected void deleteSessionVariables(String sessionId, String... varNames) { if (sessionId.equals("")) { return; } Key key = KeyFactory.createKey(SESSION_KIND, sessionId); Transaction transaction = datastore.beginTransaction(); try { Entity stateEntity = datastore.get(transaction, key); for (String varName : varNames) { stateEntity.removeProperty(varName); } datastore.put(transaction, stateEntity); transaction.commit(); } catch (EntityNotFoundException e) { // Ignore - if there's no session, there's nothing to delete. } finally { if (transaction.isActive()) { transaction.rollback(); } } }
Example #5
Source File: RemoteApiSharedTests.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
@Override public void run( DatastoreService ds, Supplier<Key> keySupplier, Supplier<Entity> entitySupplier) { // Note that we can't use local keys here. Query will fail if you set an ancestor whose app // id does not match the "global" AppIdNamespace. // TODO(xx): Consider making it more lenient, but it's not a big deal. Users can // just use a Key that was created after installing the Remote API. Entity entity = new Entity(getFreshKindName()); entity.setProperty("prop1", 99L); ds.put(entity); // Make sure we can retrieve it via a query. Query query = new Query(entity.getKind()); query.setAncestor(entity.getKey()); query.setFilter( new Query.FilterPredicate( Entity.KEY_RESERVED_PROPERTY, Query.FilterOperator.GREATER_THAN_OR_EQUAL, entity.getKey())); Entity queryResult = ds.prepare(query).asSingleEntity(); // Queries return the Entities with the remote app id. assertRemoteAppId(queryResult.getKey()); assertEquals(99L, queryResult.getProperty("prop1")); }
Example #6
Source File: DatastoreDao.java From getting-started-java with Apache License 2.0 | 6 votes |
@Override public Result<Book> listBooks(String startCursorString) { FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time if (startCursorString != null && !startCursorString.equals("")) { fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off } Query query = new Query(BOOK_KIND) // We only care about Books .addSort(Book.TITLE, SortDirection.ASCENDING); // Use default Index "title" PreparedQuery preparedQuery = datastore.prepare(query); QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions); List<Book> resultBooks = entitiesToBooks(results); // Retrieve and convert Entities Cursor cursor = results.getCursor(); // Where to start next time if (cursor != null && resultBooks.size() == 10) { // Are we paging? Save Cursor String cursorString = cursor.toWebSafeString(); // Cursors are WebSafe return new Result<>(resultBooks, cursorString); } else { return new Result<>(resultBooks); } }
Example #7
Source File: TestMetadataServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
private void populate(DatastoreService ds, String namespace) { NamespaceManager.set(namespace); Entity e = new Entity("Fun"); e.setProperty("me", "yes"); e.setProperty("you", 23); e.setUnindexedProperty("haha", 0); ds.put(e); Entity s = new Entity("Strange"); ArrayList nowhereList = new ArrayList<Integer>(); nowhereList.add(1); nowhereList.add(2); nowhereList.add(3); s.setProperty("nowhere", nowhereList); ds.put(s); Entity s2 = new Entity("Stranger"); s2.setProperty("missing", new ArrayList<Integer>()); ds.put(s2); }
Example #8
Source File: DatastoreMultitenancyTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test(expected = IllegalArgumentException.class) public void testQueriesByAncestorInOtherNamespaceThrowsIllegalArgumentException() { deleteNsKinds("one", "foo"); deleteNsKinds("two", "foo"); sync(); NamespaceManager.set("one"); Entity fooOne = new Entity("foo"); service.put(fooOne); NamespaceManager.set("two"); Entity fooTwo = new Entity("foo"); service.put(fooTwo); sync(); // java.lang.IllegalArgumentException: Namespace of ancestor key and query must match. service.prepare(new Query("foo").setAncestor(fooOne.getKey())).asList(withDefaults()); }
Example #9
Source File: DatastoreDao.java From getting-started-java with Apache License 2.0 | 6 votes |
@Override public Result<Book> listBooksByUser(String userId, String startCursorString) { FetchOptions fetchOptions = FetchOptions.Builder.withLimit(10); // Only show 10 at a time if (startCursorString != null && !startCursorString.equals("")) { fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString)); // Where we left off } Query query = new Query(BOOK_KIND) // We only care about Books // Only for this user .setFilter(new Query.FilterPredicate( Book.CREATED_BY_ID, Query.FilterOperator.EQUAL, userId)) // a custom datastore index is required since you are filtering by one property // but ordering by another .addSort(Book.TITLE, SortDirection.ASCENDING); PreparedQuery preparedQuery = datastore.prepare(query); QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions); List<Book> resultBooks = entitiesToBooks(results); // Retrieve and convert Entities Cursor cursor = results.getCursor(); // Where to start next time if (cursor != null && resultBooks.size() == 10) { // Are we paging? Save Cursor String cursorString = cursor.toWebSafeString(); // Cursors are WebSafe return new Result<>(resultBooks, cursorString); } else { return new Result<>(resultBooks); } }
Example #10
Source File: QueryFilteringByGAEPropertyTypesTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testEntityKeyInequalityFilter() { Entity parentEntity = createTestEntityWithUniqueMethodNameKey(TEST_ENTITY_KIND, "testFilterByInequalityFilter"); Key parentKey = parentEntity.getKey(); Entity entity1 = new Entity("foo", parentKey); service.put(entity1); Entity entity2 = new Entity("foo", parentKey); service.put(entity2); Query query = new Query("foo") .setAncestor(parentKey) .setFilter(new Query.FilterPredicate(Entity.KEY_RESERVED_PROPERTY, GREATER_THAN, entity1.getKey())); List<Entity> list = service.prepare(query).asList(FetchOptions.Builder.withDefaults()); assertEquals(1, list.size()); assertEquals(entity2.getKey(), list.get(0).getKey()); }
Example #11
Source File: MetadataPropertiesTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
void printPropertyRange(DatastoreService ds, PrintWriter writer) { // Start with unrestricted keys-only property query Query q = new Query(Entities.PROPERTY_METADATA_KIND).setKeysOnly(); // Limit range q.setFilter( CompositeFilterOperator.and( new FilterPredicate( Entity.KEY_RESERVED_PROPERTY, Query.FilterOperator.GREATER_THAN_OR_EQUAL, Entities.createPropertyKey("Employee", "salary")), new FilterPredicate( Entity.KEY_RESERVED_PROPERTY, Query.FilterOperator.LESS_THAN_OR_EQUAL, Entities.createPropertyKey("Manager", "salary")))); q.addSort(Entity.KEY_RESERVED_PROPERTY, SortDirection.ASCENDING); // Print query results for (Entity e : ds.prepare(q).asIterable()) { writer.println(e.getKey().getParent().getName() + ": " + e.getKey().getName()); } }
Example #12
Source File: EntitiesTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Test public void repeatedProperties_storesList() throws Exception { // [START repeated_properties] Entity employee = new Entity("Employee"); ArrayList<String> favoriteFruit = new ArrayList<String>(); favoriteFruit.add("Pear"); favoriteFruit.add("Apple"); employee.setProperty("favoriteFruit", favoriteFruit); datastore.put(employee); // Sometime later employee = datastore.get(employee.getKey()); @SuppressWarnings("unchecked") // Cast can't verify generic type. ArrayList<String> retrievedFruits = (ArrayList<String>) employee.getProperty("favoriteFruit"); // [END repeated_properties] assertThat(retrievedFruits).containsExactlyElementsIn(favoriteFruit).inOrder(); }
Example #13
Source File: TestBase.java From appengine-tck with Apache License 2.0 | 6 votes |
protected static void deleteTempDataInTx(DatastoreService ds, Entity entity, Class<? extends TempData> type) { Transaction txn = ds.beginTransaction(TransactionOptions.Builder.withXG(true)); try { TempData data = type.newInstance(); data.fromProperties(entity.getProperties()); data.preDelete(ds); ds.delete(txn, entity.getKey()); data.postDelete(ds); txn.commit(); } catch (Exception e) { throw new IllegalStateException(e); } finally { if (txn.isActive()) { txn.rollback(); } } }
Example #14
Source File: AppEngineDataStoreFactory.java From google-http-java-client with Apache License 2.0 | 6 votes |
@Override public Collection<V> values() throws IOException { lock.lock(); try { // Unfortunately no getKeys() method on MemcacheService, so the only option is to clear all // and re-populate the memcache from scratch. This is clearly inefficient. if (memcache != null) { memcache.clearAll(); } List<V> result = Lists.newArrayList(); Map<String, V> map = memcache != null ? Maps.<String, V>newHashMap() : null; for (Entity entity : query(false)) { V value = deserialize(entity); result.add(value); if (map != null) { map.put(entity.getKey().getName(), value); } } if (memcache != null) { memcache.putAll(map, memcacheExpiration); } return Collections.unmodifiableList(result); } finally { lock.unlock(); } }
Example #15
Source File: QueryTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testCompositeFilter() { Query query = new Query(kindName, rootKey); Filter filter = Query.CompositeFilterOperator.and( Query.FilterOperator.LESS_THAN_OR_EQUAL.of("intData", 40), Query.FilterOperator.GREATER_THAN.of("intData", 0)); query.setFilter(filter); query.addSort("intData", Query.SortDirection.DESCENDING); List<Entity> es = service.prepare(query).asList(fo); assertEquals("check return count", 2, es.size()); assertEquals("check query filter", filter, query.getFilter()); assertEquals("check query key only", false, query.isKeysOnly()); Query.CompositeFilter cf = (Query.CompositeFilter) query.getFilter(); assertEquals(2, cf.getSubFilters().size()); assertEquals(Query.CompositeFilterOperator.AND, cf.getOperator()); }
Example #16
Source File: EntityToObjectConverter.java From yawp with MIT License | 5 votes |
private <T> void safeSetObjectProperty(Entity entity, T object, FieldModel fieldModel) { try { setObjectProperty(object, entity, fieldModel, fieldModel.getField()); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
Example #17
Source File: MatchTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testSearchOperatorNOTWithMatch() throws Exception { service.subscribe(TOPIC, "foo", 0, "title:tester NOT company:Google", createSchema("title", FieldType.STRING, "company", FieldType.STRING)); Entity entity = createCompanyEntity("RedHat"); assertServletWasInvokedWith(entity); }
Example #18
Source File: DatastoreHelperTestBase.java From appengine-tck with Apache License 2.0 | 5 votes |
protected Object[] getResult(Query query, String pName) { int count = service.prepare(query).countEntities(FetchOptions.Builder.withDefaults()); Object result[] = new Object[count]; int pt = 0; for (Entity readRec : service.prepare(query).asIterable()) { result[pt++] = readRec.getProperty(pName); } return result; }
Example #19
Source File: QueryOptimizationsTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testProjectionOfCollectionProperties() throws Exception { String methodName = "testProjectionOfCollectionProperties"; String entityKind = "test-proj"; Entity parent = createTestEntityWithUniqueMethodNameKey(entityKind, methodName); Key key = parent.getKey(); Entity e = createEntity(entityKind, key) .withProperty("prop", Arrays.asList("bbb", "ccc", "aaa")) .store(); Query query = new Query(entityKind) .setAncestor(key) .addProjection(new PropertyProjection("prop", String.class)) .addSort("prop"); PreparedQuery preparedQuery = service.prepare(query); List<Entity> results = preparedQuery.asList(withDefaults()); assertEquals(3, results.size()); Entity firstResult = results.get(0); Entity secondResult = results.get(1); Entity thirdResult = results.get(2); assertEquals(e.getKey(), firstResult.getKey()); assertEquals(e.getKey(), secondResult.getKey()); assertEquals(e.getKey(), thirdResult.getKey()); assertEquals("aaa", firstResult.getProperty("prop")); assertEquals("bbb", secondResult.getProperty("prop")); assertEquals("ccc", thirdResult.getProperty("prop")); }
Example #20
Source File: NotificationCleanupServlet.java From io2014-codelabs with Apache License 2.0 | 5 votes |
private void doCleanup() { log.log(Level.INFO, "Starting a job to clean up processed notification records"); DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Calendar cutoffTime = Calendar.getInstance(TimeZone.getTimeZone("UTC")); cutoffTime.add(Calendar.HOUR, -HOURS_TO_KEEP_RECORDS_OF_PROCESSED_NOTIFICATIONS); Query query = new Query(Worker.PROCESSED_NOTIFICATION_TASKS_ENTITY_KIND) .setFilter(new FilterPredicate("processedAt", FilterOperator.LESS_THAN, cutoffTime.getTime())) .setKeysOnly(); PreparedQuery preparedQuery = datastore.prepare(query); // Delete in batches List<Entity> entitiesToBeDeleted = null; do { entitiesToBeDeleted = preparedQuery.asList(FetchOptions.Builder.withLimit(5)); List<Key> keys = new ArrayList<Key>(); for (Entity entity : entitiesToBeDeleted) { keys.add(entity.getKey()); } datastore.delete(keys); } while (entitiesToBeDeleted.size() > 0); log.log(Level.INFO, "Finished a job to clean up processed notification records"); }
Example #21
Source File: AsyncTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testBeginTx() throws Exception { final AsyncDatastoreService service = DatastoreServiceFactory.getAsyncDatastoreService(); Transaction tx = waitOnFuture(service.beginTransaction(TransactionOptions.Builder.withXG(true))); Key key, key2; try { key = waitOnFuture(service.put(tx, new Entity("AsyncTx"))); key2 = waitOnFuture(service.put(tx, new Entity("AsyncTx"))); tx.commit(); } catch (Exception e) { tx.rollback(); throw e; } if (key != null && key2 != null) { tx = waitOnFuture(service.beginTransaction(TransactionOptions.Builder.withXG(true))); try { try { try { Assert.assertNotNull(waitOnFuture(service.get(tx, key))); Assert.assertNotNull(waitOnFuture(service.get(tx, Collections.singleton(key2)))); } finally { service.delete(tx, key2); } } finally { service.delete(tx, Collections.singleton(key)); } } finally { tx.rollback(); } } }
Example #22
Source File: LocalHighRepDatastoreTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Test public void testEventuallyConsistentGlobalQueryResult() { DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); Key ancestor = KeyFactory.createKey("foo", 3); ds.put(new Entity("yam", ancestor)); ds.put(new Entity("yam", ancestor)); // Global query doesn't see the data. assertEquals(0, ds.prepare(new Query("yam")).countEntities(withLimit(10))); // Ancestor query does see the data. assertEquals(2, ds.prepare(new Query("yam", ancestor)).countEntities(withLimit(10))); }
Example #23
Source File: PreparedQueryTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testAsQueryResultList() throws Exception { QueryResultList<Entity> list = preparedQuery.asQueryResultList(withDefaults()); assertNotNull(list); assertEquals(1, list.size()); assertEquals(john, list.get(0)); }
Example #24
Source File: RequiredIndexesTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testQueryWithEqalityAndInequalityFilterOnKeyPropertyDoesNotRequireConfiguredIndex() throws Exception { executeQuery( new Query("Unindexed") .setFilter( and(new Query.FilterPredicate(Entity.KEY_RESERVED_PROPERTY, GREATER_THAN, KeyFactory.createKey("Unindexed", 1)), new Query.FilterPredicate(Entity.KEY_RESERVED_PROPERTY, EQUAL, KeyFactory.createKey("Unindexed", 2))))); }
Example #25
Source File: DeleteEntityActionTest.java From nomulus with Apache License 2.0 | 5 votes |
@Test public void test_deleteNonExistentEntityRepliesWithError() { Entity entity = new Entity("not", "here"); String rawKey = KeyFactory.keyToString(entity.getKey()); BadRequestException thrown = assertThrows( BadRequestException.class, () -> new DeleteEntityAction(rawKey, response).run()); assertThat(thrown).hasMessageThat().contains("Could not find entity with key " + rawKey); }
Example #26
Source File: DeleteEntityActionTest.java From nomulus with Apache License 2.0 | 5 votes |
@Test public void test_deleteOneRawEntityAndOneRegisteredEntitySuccessfully() { Entity entity = new Entity("first", "raw"); getDatastoreService().put(entity); String rawKey = KeyFactory.keyToString(entity.getKey()); ReservedList ofyEntity = new ReservedList.Builder().setName("registered").build(); ofy().saveWithoutBackup().entity(ofyEntity).now(); String ofyKey = KeyFactory.keyToString(create(ofyEntity).getRaw()); new DeleteEntityAction(String.format("%s,%s", rawKey, ofyKey), response).run(); assertThat(response.getPayload()).isEqualTo("Deleted 1 raw entities and 1 registered entities"); }
Example #27
Source File: DatastoreHelperTestBase.java From appengine-tck with Apache License 2.0 | 5 votes |
protected List<Entity> doQuery(String kind, String pName, Class<?> type, boolean indexed) { FetchOptions fo = FetchOptions.Builder.withDefaults(); Query query = new Query(kind, rootKey); if (indexed) { query.addProjection(new PropertyProjection(pName, type)); query.addSort(pName); } return service.prepare(query).asList(fo); }
Example #28
Source File: TransactionsTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testBasicTxPut() throws Exception { Entity entity = createTestEntity(TRANSACTION_TEST_ENTITY, System.currentTimeMillis()); Transaction tx = service.beginTransaction(); try { service.put(tx, entity); assertStoreDoesNotContain(entity); tx.commit(); assertStoreContains(entity); } catch (Exception e) { tx.rollback(); throw e; } }
Example #29
Source File: IndexesTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Test public void propertyFilterExample_returnsMatchingEntities() throws Exception { // [START unindexed_properties_1] DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Key acmeKey = KeyFactory.createKey("Company", "Acme"); Entity tom = new Entity("Person", "Tom", acmeKey); tom.setProperty("name", "Tom"); tom.setProperty("age", 32); datastore.put(tom); Entity lucy = new Entity("Person", "Lucy", acmeKey); lucy.setProperty("name", "Lucy"); lucy.setUnindexedProperty("age", 29); datastore.put(lucy); Filter ageFilter = new FilterPredicate("age", FilterOperator.GREATER_THAN, 25); Query q = new Query("Person").setAncestor(acmeKey).setFilter(ageFilter); // Returns tom but not lucy, because her age is unindexed List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()); // [END unindexed_properties_1] assertWithMessage("query results").that(results).containsExactly(tom); }
Example #30
Source File: MatchTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testSearchOperatorANDWithoutMatch() throws Exception { service.subscribe(TOPIC, "foo", 0, "title:tester AND company:Google", createSchema("title", FieldType.STRING, "company", FieldType.STRING)); @SuppressWarnings("UnusedDeclaration") Entity entity = createCompanyEntity("tech"); assertServletWasNotInvoked(); }