com.google.appengine.api.datastore.Key Java Examples
The following examples show how to use
com.google.appengine.api.datastore.Key.
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: QueryBasicsTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@SuppressWarnings("deprecation") @Test public void testDeprecatedFiltersAreSupported() throws Exception { Key parentKey = createQueryBasicsTestParent("testDeprecatedFiltersAreSupported"); Entity johnDoe = createEntity("Person", parentKey) .withProperty("name", "John") .withProperty("lastName", "Doe") .store(); Entity johnBooks = createEntity("Person", parentKey) .withProperty("name", "John") .withProperty("lastName", "Books") .store(); Entity janeDoe = createEntity("Person", parentKey) .withProperty("name", "Jane") .withProperty("lastName", "Doe") .store(); Query query = new Query("Person") .setAncestor(parentKey) .addFilter("name", EQUAL, "John") .addFilter("lastName", EQUAL, "Doe"); assertSingleResult(johnDoe, query); }
Example #2
Source File: QueryOptimizationsTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testProjectionQueryOnlyReturnsEntitiesContainingProjectedProperty() throws Exception { String methodName = "testProjectionQueryOnlyReturnsEntitiesContainingProjectedProperty"; Entity parent = createTestEntityWithUniqueMethodNameKey("Kind", methodName); Key key = parent.getKey(); Entity e1 = createEntity("Kind", key) .withProperty("foo", "foo") .store(); Entity e2 = createEntity("Kind", key) .withProperty("bar", "bar") .store(); Query query = new Query("Kind") .setAncestor(key) .addProjection(new PropertyProjection("foo", String.class)); List<Entity> results = service.prepare(query).asList(withDefaults()); assertEquals(Collections.singletonList(e1), results); }
Example #3
Source File: RawKeyStdSerializer.java From gwt-jackson with Apache License 2.0 | 6 votes |
@Override public void serialize( Key value, JsonGenerator jgen, SerializerProvider provider ) throws IOException { jgen.writeStartObject(); boolean includeNull = Include.NON_NULL != provider.getConfig().getDefaultPropertyInclusion().getValueInclusion(); if ( value.getParent() != null || includeNull ) { jgen.writeObjectField( RawKeyConstant.PARENT, value.getParent() ); } if ( value.getKind() != null || includeNull ) { jgen.writeStringField( RawKeyConstant.KIND, value.getKind() ); } jgen.writeNumberField( RawKeyConstant.ID, value.getId() ); if ( value.getName() != null || includeNull ) { jgen.writeStringField( RawKeyConstant.NAME, value.getName() ); } jgen.writeEndObject(); }
Example #4
Source File: SecurityChecker.java From io2014-codelabs with Apache License 2.0 | 6 votes |
/** * Creates a {@link com.google.appengine.api.datastore.Key} from the specified kindName and CloudEntity id. If * the kindName has _private suffix, the key will be created under a namespace * for the specified {@link com.google.appengine.api.users.User}. * * @param kindName * Name of kind * @param id * CloudEntity id * @param user * {@link com.google.appengine.api.users.User} of the requestor * @return {@link com.google.appengine.api.datastore.Key} */ public Key createKeyWithNamespace(String kindName, String id, User user) { // save the original namespace String origNamespace = NamespaceManager.get(); // set namespace based on the kind suffix if (kindName.startsWith(SecurityChecker.KIND_PREFIX_PRIVATE)) { String userId = getUserId(user); NamespaceManager.set(userId); } else { NamespaceManager.set(SecurityChecker.NAMESPACE_DEFAULT); } // create a key Key k = KeyFactory.createKey(kindName, id); // restore the original namespace NamespaceManager.set(origNamespace); return k; }
Example #5
Source File: SignGuestbookServlet.java From appengine-modules-sample-java with Apache License 2.0 | 6 votes |
@Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { final UserService userService = UserServiceFactory.getUserService(); final User user = userService.getCurrentUser(); final String guestbookName = req.getParameter("guestbookName"); final Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName); final String content = req.getParameter("content"); final Date date = new Date(); final Entity greeting = new Entity("Greeting", guestbookKey); greeting.setProperty("user", user); greeting.setProperty("date", date); greeting.setProperty("content", content); final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); datastore.put(greeting); resp.sendRedirect("/guestbook.jsp?guestbookName=" + guestbookName); }
Example #6
Source File: SchemaTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testPropertyMetadata() { NamespaceManager.set(namespaceDat[2]); // sort by kind/property, kindDat[1] < kindDat[0] < kindDat[2] Query q = new Query("__property__").addSort(Entity.KEY_RESERVED_PROPERTY).setKeysOnly(); // filter out properties for kind "testing" Key key1 = Entities.createPropertyKey(kindDat[0], "urlData"); Key key2 = Entities.createPropertyKey(kindDat[2], "urlData"); q.setFilter(CompositeFilterOperator.and( new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN, key1), new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.LESS_THAN_OR_EQUAL, key2))); List<Entity> el = service.prepare(q).asList(fo); // un-indexed property, textData, will not be returned in __property__ queries. assertEquals(13, el.size()); for (int i = 0; i < el.size(); i++) { assertEquals(namespaceDat[2], el.get(i).getKey().getNamespace()); assertEquals(kindDat[2], el.get(i).getKey().getParent().getName()); if (i == 0) { assertEquals("adressData", el.get(0).getKey().getName()); } else if (i == el.size() - 1) { assertEquals("urlData", el.get(el.size() - 1).getKey().getName()); } } }
Example #7
Source File: QueryBasicsTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void singleEntityThrowsTooManyResultsExceptionWhenMoreThanOneResult() throws Exception { String methodName = "singleEntityThrowsTooManyResultsExceptionWhenMoreThanOneResult"; Key parentKey = createQueryBasicsTestParent(methodName); createEntity("Person", parentKey).store(); createEntity("Person", parentKey).store(); PreparedQuery preparedQuery = service.prepare(new Query("Person")); try { preparedQuery.asSingleEntity(); fail("Expected PreparedQuery.TooManyResultsException"); } catch (PreparedQuery.TooManyResultsException e) { // pass } }
Example #8
Source File: TransactionsTest.java From appengine-tck with Apache License 2.0 | 6 votes |
private void assertRollbackSucceedsWhenResultFetchedWith(ResultFetcher resultFetcher) throws EntityNotFoundException { String methodName = "assertRollbackSucceedsWhenResultFetchedWith"; Entity entity = createTestEntityWithUniqueMethodNameKey(TRANSACTION_TEST_ENTITY, methodName); Key parentKey = entity.getKey(); entity.setProperty("name", "original"); Key key = service.put(entity); try { Transaction tx = service.beginTransaction(); PreparedQuery preparedQuery = service.prepare(new Query(TRANSACTION_TEST_ENTITY).setAncestor(parentKey)); Entity entity2 = resultFetcher.fetchResult(preparedQuery); entity2.setProperty("name", "modified"); service.put(tx, entity2); tx.rollback(); Entity entity3 = service.get(key); assertEquals("original", entity3.getProperty("name")); } finally { service.delete(entity.getKey()); } }
Example #9
Source File: QueryBasicsTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testFilteringWithNotEqualReturnsOnlyEntitiesContainingTheProperty() throws Exception { String methodName = "testFilteringWithNotEqualReturnsOnlyEntitiesContainingTheProperty"; Key parentKey = createQueryBasicsTestParent(methodName); Entity e1 = createEntity("Entry", parentKey) .withProperty("foo", "aaa") .store(); createEntity("Entry", parentKey) .withProperty("bar", "aaa") .store(); Query query = new Query("Entry") .setAncestor(parentKey) .setFilter(new Query.FilterPredicate("foo", NOT_EQUAL, "bbb")); assertEquals(Collections.singletonList(e1), service.prepare(query).asList(withDefaults())); }
Example #10
Source File: ServerUtils.java From sc2gears with Apache License 2.0 | 6 votes |
/** * Returns the last logged event specified by the entity key and type. * @param entityKey entity key to search the event for * @param type type to search the event for * @return the last logged event specified by the entity key and type or <code>null</code> if no such event is logged */ public static Event getLastEvent( final Key entityKey, final Type type ) { PersistenceManager pm = null; try { pm = PMF.get().getPersistenceManager(); final List< Event > eventList = new JQBuilder<>( pm, Event.class ).filter( "entityKey==p1 && type==p2", "KEY p1, Integer p2" ).desc( "date" ).range( 0, 1 ).get( entityKey, type.ordinal() ); if ( eventList.isEmpty() ) return null; return eventList.get( 0 ); } finally { if ( pm != null ) pm.close(); } }
Example #11
Source File: AppEngineBackEnd.java From appengine-pipelines with Apache License 2.0 | 6 votes |
private void deleteAll(final String kind, final Key rootJobKey) { logger.info("Deleting all " + kind + " with rootJobKey=" + rootJobKey); final int chunkSize = 100; final FetchOptions fetchOptions = FetchOptions.Builder.withChunkSize(chunkSize); final PreparedQuery preparedQuery = dataStore.prepare(new Query(kind).setKeysOnly().setFilter( new FilterPredicate(ROOT_JOB_KEY_PROPERTY, EQUAL, rootJobKey))); tryFiveTimes(new Operation<Void>("delete") { @Override public Void call() { Iterator<Entity> iter = preparedQuery.asIterator(fetchOptions); while (iter.hasNext()) { ArrayList<Key> keys = new ArrayList<>(chunkSize); for (int i = 0; i < chunkSize && iter.hasNext(); i++) { keys.add(iter.next().getKey()); } logger.info("Deleting " + keys.size() + " " + kind + "s with rootJobKey=" + rootJobKey); dataStore.delete(null, keys); } return null; } }); }
Example #12
Source File: JsonGenerator.java From appengine-pipelines with Apache License 2.0 | 6 votes |
private static Map<String, Object> buildMapRepresentation(Slot slot) { Map<String, Object> map = new HashMap<>(5); String statusString = (slot.isFilled() ? FILLED_STATUS : WAITING_STATUS); map.put(SLOT_STATUS, statusString); try { map.put(SLOT_VALUE, slot.getValue()); } catch (RuntimeException ex) { map.put(SLOT_VALUE, ex); } Date fillTime = slot.getFillTime(); if (null != fillTime) { map.put(SLOT_FILL_TIME, fillTime.getTime()); } Key sourceJobKey = slot.getSourceJobKey(); if (null != sourceJobKey) { map.put(SLOT_SOURCE_JOB, sourceJobKey.getName()); } return map; }
Example #13
Source File: TaskServlet.java From sc2gears with Apache License 2.0 | 5 votes |
private void processFileWithContent( final PersistenceManager pm, final Key fileKey, final String fileTypeString ) throws IOException { LOGGER.fine( "File key: " + fileKey + ", file type: " + fileTypeString ); final FileType fileType = FileType.fromString( fileTypeString ); final FileMetaData fmd; try { fmd = (FileMetaData) pm.getObjectById( FILE_TYPE_CLASS_MAP.get( fileType ), fileKey ); } catch ( final JDOObjectNotFoundException jonfe ) { LOGGER.warning( "File not found! (Deleted?)" ); return; } LOGGER.fine( "sha1: " + fmd.getSha1() ); if ( fmd.getBlobKey() != null && fmd.getContent() == null ) { LOGGER.warning( "This file is already processed!" ); return; } if ( fmd.getContent() == null ) { LOGGER.warning( "File does not have content!" ); return; } // Store content in the Blobstore final FileService fileService = FileServiceFactory.getFileService(); final AppEngineFile appeFile = fileService.createNewBlobFile( FILE_TYPE_MIME_TYPE_MAP.get( fileType ), fmd.getSha1() ); final FileWriteChannel channel = fileService.openWriteChannel( appeFile, true ); final ByteBuffer bb = ByteBuffer.wrap( fmd.getContent().getBytes() ); while ( bb.hasRemaining() ) channel.write( bb ); channel.closeFinally(); fmd.setBlobKey( fileService.getBlobKey( appeFile ) ); fmd.setContent( null ); // I do not catch exceptions (so the task will be retried) }
Example #14
Source File: AppEngineBackEnd.java From appengine-pipelines with Apache License 2.0 | 5 votes |
/** * {@code inflate = true} means that {@link Barrier#getWaitingOnInflated()} * will not return {@code null}. */ private Barrier queryBarrier(Key barrierKey, boolean inflate) throws NoSuchObjectException { Entity entity = getEntity("queryBarrier", barrierKey); Barrier barrier = new Barrier(entity); if (inflate) { Collection<Barrier> barriers = new ArrayList<>(1); barriers.add(barrier); inflateBarriers(barriers); } logger.finest("Querying returned: " + barrier); return barrier; }
Example #15
Source File: AsyncServiceTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testDataAllocate() throws Exception { final long allocateNum = 5; // Range default namespace Future<KeyRange> futureRange = asyncService.allocateIds(ASYNC_ENTITY, allocateNum); KeyRange range = futureRange.get(); assertTaskIsDoneAndNotCancelled(futureRange); Entity noParent = createTestEntity(ASYNC_ENTITY); assertEntityNotInRange(noParent, range); // Range with specified parent Entity parent = new Entity(ASYNC_ENTITY); parent.setProperty("name", "parent" + new Date()); Key parentKey = service.put(parent); Future<KeyRange> futureRange2 = asyncService.allocateIds(parentKey, ASYNC_ENTITY, allocateNum); KeyRange range2 = futureRange2.get(); assertTaskIsDoneAndNotCancelled(futureRange2); Entity noParent2 = createTestEntity(ASYNC_ENTITY, parentKey); assertEntityNotInRange(noParent2, range2); // In Range entity should have same parent Entity child = new Entity(range2.getStart()); child.setProperty("name", "second" + new Date()); Key childKey = service.put(child); // child with allocated key should have correct parent. assertEquals(parentKey, childKey.getParent()); }
Example #16
Source File: RawKeyJsonSerializer.java From gwt-jackson with Apache License 2.0 | 5 votes |
@Override public void serialize( Key value, JsonGenerator jgen, SerializerProvider provider ) throws IOException { StringWriter writer = new StringWriter(); JsonGenerator jsonGenerator = jgen.getCodec().getFactory().createGenerator( writer ); rawKeyStdSerializer.serialize( value, jsonGenerator, provider ); jsonGenerator.close(); jgen.writeFieldName( writer.toString() ); }
Example #17
Source File: DatastoreMultitenancyTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testKeysCreatedUnderDifferentNamespacesAreNotEqual() throws Exception { NamespaceManager.set("one"); Key key1 = KeyFactory.createKey("Test", 1); NamespaceManager.set("two"); Key key2 = KeyFactory.createKey("Test", 1); assertFalse(key1.equals(key2)); }
Example #18
Source File: JobRecord.java From appengine-pipelines with Apache License 2.0 | 5 votes |
private static boolean checkForInflate(PipelineModelObject obj, Key expectedGuid, String name) { if (null == obj) { return false; } if (!expectedGuid.equals(obj.getKey())) { throw new IllegalArgumentException( "Wrong guid for " + name + ". Expected " + expectedGuid + " but was " + obj.getKey()); } return true; }
Example #19
Source File: RemoteApiSharedTests.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
@Override public void run( DatastoreService ds, Supplier<Key> keySupplier, Supplier<Entity> entitySupplier) { Transaction txn = ds.beginTransaction(TransactionOptions.Builder.withXG(true)); if (ds.put(new Entity("xgfoo")).getId() == 0) { throw new RuntimeException("first entity should have received an id"); } if (ds.put(new Entity("xgfoo")).getId() == 0) { throw new RuntimeException("second entity should have received an id"); } txn.commit(); }
Example #20
Source File: DatastoreSessionStore.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
/** * Return a {@link Key} for the given session "key" string * ({@link SessionManager#SESSION_PREFIX} + sessionId) in the empty namespace. */ static Key createKeyForSession(String key) { String originalNamespace = NamespaceManager.get(); try { NamespaceManager.set(""); return KeyFactory.createKey(SESSION_ENTITY_TYPE, key); } finally { NamespaceManager.set(originalNamespace); } }
Example #21
Source File: DeviceSubscription.java From io2014-codelabs with Apache License 2.0 | 5 votes |
/** * Gets a key for subscription kind entity based on device id. * * @param deviceId A unique device identifier */ protected Key getKey(String deviceId) { if (StringUtility.isNullOrEmpty(deviceId)) { throw new IllegalArgumentException("deviceId cannot be null or empty"); } else { return KeyFactory.createKey(SUBSCRIPTION_KIND, deviceId); } }
Example #22
Source File: EmbeddedEntityTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void test() throws Exception { EmbeddedEntity embedded = new EmbeddedEntity(); embedded.setProperty("string", "foo"); Entity entity = createTestEntity(); entity.setProperty("embedded", embedded); Key key = service.put(entity); Entity storedEntity = service.get(key); EmbeddedEntity storedEmbedded = (EmbeddedEntity) storedEntity.getProperty("embedded"); assertEquals(embedded, storedEmbedded); }
Example #23
Source File: AppEngineBackEnd.java From appengine-pipelines with Apache License 2.0 | 5 votes |
@Override public Slot querySlot(Key slotKey, boolean inflate) throws NoSuchObjectException { Entity entity = getEntity("querySlot", slotKey); Slot slot = new Slot(entity); if (inflate) { Map<Key, Entity> entities = getEntities("querySlot", slot.getWaitingOnMeKeys()); Map<Key, Barrier> barriers = new HashMap<>(entities.size()); for (Map.Entry<Key, Entity> entry : entities.entrySet()) { barriers.put(entry.getKey(), new Barrier(entry.getValue())); } slot.inflate(barriers); inflateBarriers(barriers.values()); } return slot; }
Example #24
Source File: InitSqlTestUtils.java From nomulus with Apache License 2.0 | 5 votes |
private static KV<Long, Object> expectedToOfyWithTimestamp(KV<Long, Serializable> kv) { return KV.of( kv.getKey(), kv.getValue() instanceof Key ? kv.getValue() : datastoreToOfyEntity((Entity) kv.getValue())); }
Example #25
Source File: EntityTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testKey() { clearData(kindName); Entity newRec = new Entity(kindName); newRec.setProperty("stamp", new Date()); Key key = service.put(newRec); Entity newRec2 = new Entity(key); Date newDate = new Date(); newRec2.setProperty("stamp", newDate); service.put(newRec2); assertEquals(newRec, newRec2); assertEquals(newDate, newRec2.getProperty("stamp")); }
Example #26
Source File: AsyncServiceTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test public void testDataDelete() throws Exception { Entity parent = createTestEntityWithUniqueMethodNameKey(ASYNC_ENTITY, "testDataDelete"); Key key = parent.getKey(); Entity newRec = new Entity(ASYNC_ENTITY); newRec.setProperty("count", 0); newRec.setProperty("timestamp", new Date()); Key ekey = service.put(newRec); Future<Void> future = asyncService.delete(ekey); future.get(); assertTaskIsDoneAndNotCancelled(future); assertEquals(0, service.prepare(simpleQuery(key)).countEntities(withDefaults())); }
Example #27
Source File: DatastoreDao.java From getting-started-java with Apache License 2.0 | 5 votes |
@Override public void updateBook(Book book) { Key key = KeyFactory.createKey(BOOK_KIND, book.getId()); // From a book, create a Key Entity entity = new Entity(key); // Convert Book to an Entity entity.setProperty(Book.AUTHOR, book.getAuthor()); entity.setProperty(Book.DESCRIPTION, book.getDescription()); entity.setProperty(Book.PUBLISHED_DATE, book.getPublishedDate()); entity.setProperty(Book.TITLE, book.getTitle()); datastore.put(entity); // Update the Entity }
Example #28
Source File: DeviceSubscription.java From solutions-mobile-backend-starter-java with Apache License 2.0 | 5 votes |
/** * Gets a key for subscription kind entity based on device id. * * @param deviceId A unique device identifier */ protected Key getKey(String deviceId) { if (StringUtility.isNullOrEmpty(deviceId)) { throw new IllegalArgumentException("deviceId cannot be null or empty"); } else { return KeyFactory.createKey(SUBSCRIPTION_KIND, deviceId); } }
Example #29
Source File: SignGuestbookServlet.java From appengine-java-vm-guestbook-extras with Apache License 2.0 | 5 votes |
@Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { UserService userService = UserServiceFactory.getUserService(); User user = userService.getCurrentUser(); String code = (String) req.getParameter("ccode"); String captcha = (String) req.getSession().getAttribute("captcha"); if (captcha != null && code != null) { if (!captcha.trim().equalsIgnoreCase(code)) { resp.getWriter().println("<html><head><title>error</title></head><body>error in captcha." + "<br/><a href='/guestbook.jsp'>Try again</a>.</body></html>"); return; } } else { resp.getWriter().println("error in captcha"); return; } String guestbookName = req.getParameter("guestbookName"); Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName); String content = req.getParameter("content"); Date date = new Date(); Entity greeting = new Entity("Greeting", guestbookKey); greeting.setProperty("user", user); greeting.setProperty("date", date); content = content.substring(0, Math.min(content.length(), 490)); greeting.setProperty("content", content); DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); datastore.put(greeting); resp.sendRedirect("/guestbook.jsp?guestbookName=" + guestbookName); }
Example #30
Source File: DatastoreHelperTestBase.java From appengine-tck with Apache License 2.0 | 5 votes |
protected void assertStoreDoesNotContain(Key key) throws EntityNotFoundException { try { Entity storedEntity = service.get(key); Assert.fail("expected the datastore not to contain anything under key " + key + ", but it contained the entity " + storedEntity); } catch (EntityNotFoundException e) { // pass } }