Java Code Examples for com.google.appengine.api.datastore.DatastoreService#get()
The following examples show how to use
com.google.appengine.api.datastore.DatastoreService#get() .
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: EntitiesTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Test public void kindExample_writesEntity() throws Exception { // [START kind_example] Entity employee = new Entity("Employee", "asalieri"); employee.setProperty("firstName", "Antonio"); employee.setProperty("lastName", "Salieri"); employee.setProperty("hireDate", new Date()); employee.setProperty("attendedHrTraining", true); DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); datastore.put(employee); // [END kind_example] Entity got = datastore.get(employee.getKey()); assertWithMessage("got.firstName") .that((String) got.getProperty("firstName")) .isEqualTo("Antonio"); assertWithMessage("got.lastName") .that((String) got.getProperty("lastName")) .isEqualTo("Salieri"); assertWithMessage("got.hireDate").that((Date) got.getProperty("hireDate")).isNotNull(); assertWithMessage("got.attendedHrTraining") .that((boolean) got.getProperty("attendedHrTraining")) .isTrue(); }
Example 2
Source File: LocalDatastoreSmoketestServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); Entity e = new Entity("foo"); e.setProperty("foo", 23); Key key = ds.put(e); try { e = ds.get(key); } catch (EntityNotFoundException e1) { throw new ServletException(e1); } e.setProperty("bar", 44); ds.put(e); Query q = new Query("foo"); q.addFilter("foo", Query.FilterOperator.GREATER_THAN_OR_EQUAL, 22); Iterator<Entity> iter = ds.prepare(q).asIterator(); iter.next(); }
Example 3
Source File: RemoteApiSharedTests.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
/** * Propagates {@link EntityNotFoundException} as {@link RuntimeException} */ private Entity quietGet(DatastoreService ds, Key key) { try { return ds.get(key); } catch (EntityNotFoundException e) { throw new RuntimeException(e); } }
Example 4
Source File: DemoEntityManagerNoSql.java From solutions-photo-sharing-demo-java with Apache License 2.0 | 5 votes |
/** * Looks up an entity by key. * * @param ds the datastore service objct. * @param key the entity key. * @return the entity; null if the key could not be found. */ protected Entity getDatastoreEntity(DatastoreService ds, Key key) { try { return ds.get(key); } catch (EntityNotFoundException e) { logger.fine("No entity found:" + key.toString()); } return null; }
Example 5
Source File: PrintServlet.java From appengine-tck with Apache License 2.0 | 5 votes |
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { log.info("Ping - " + req); if (requestHandler != null) { requestHandler.handleRequest(req); } lastRequest = req; final DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); try { Entity entity = new Entity("Qwert"); entity.setProperty("xyz", 123); Key key = ds.put(entity); entity = ds.get(key); log.info(entity.toString()); FileService fs = FileServiceFactory.getFileService(); AppEngineFile file = fs.createNewBlobFile("qwertfile"); FileWriteChannel fwc = fs.openWriteChannel(file, false); try { log.info("b_l = " + fwc.write(ByteBuffer.wrap("qwert".getBytes()))); } finally { fwc.close(); } } catch (Exception e) { throw new IOException(e); } }
Example 6
Source File: AppEngineCredentialStore.java From google-oauth-java-client with Apache License 2.0 | 5 votes |
@Override public boolean load(String userId, Credential credential) { DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Key key = KeyFactory.createKey(KIND, userId); try { Entity entity = datastore.get(key); credential.setAccessToken((String) entity.getProperty("accessToken")); credential.setRefreshToken((String) entity.getProperty("refreshToken")); credential.setExpirationTimeMilliseconds((Long) entity.getProperty("expirationTimeMillis")); return true; } catch (EntityNotFoundException exception) { return false; } }
Example 7
Source File: ServersStartServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
private int datastoreCount() { Key key = KeyFactory.createKey("Counter", getKeyName()); DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); Entity entity; try { entity = ds.get(key); Long val = ((Long) entity.getProperty("value")); return val.intValue(); } catch (Exception e) { // not found return 0; } }
Example 8
Source File: RemoteApiSharedTests.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
private void assertEntityNotFoundException(DatastoreService ds, Key missingKey) { try { ds.get(missingKey); throw new RuntimeException("Did not receive expected exception"); } catch (EntityNotFoundException e) { // expected } }
Example 9
Source File: RemoteApiSharedTests.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
private void assertGetEquals(DatastoreService ds, Key keyToGet, Entity expectedEntity) { // Test the single key api. Entity entityFromGet = quietGet(ds, keyToGet); assertEquals(expectedEntity, entityFromGet); assertRemoteAppId(entityFromGet.getKey()); // Test the multi-get api. Map<Key, Entity> getResults = ds.get(Collections.singletonList(keyToGet)); assertEquals(1, getResults.size()); Entity entityFromBatchGet = getResults.get(keyToGet); assertEquals(expectedEntity, entityFromBatchGet); assertRemoteAppId(entityFromBatchGet.getKey()); }
Example 10
Source File: StartupServlet.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Key isPopulatedKey = KeyFactory.createKey(IS_POPULATED_ENTITY, IS_POPULATED_KEY_NAME); boolean isAlreadyPopulated; try { datastore.get(isPopulatedKey); isAlreadyPopulated = true; } catch (EntityNotFoundException expected) { isAlreadyPopulated = false; } if (isAlreadyPopulated) { resp.getWriter().println("ok"); return; } ImmutableList.Builder<Entity> people = ImmutableList.builder(); for (String name : US_PRESIDENTS) { Entity person = new Entity(PERSON_ENTITY); person.setProperty(NAME_PROPERTY, name); people.add(person); } datastore.put(people.build()); datastore.put(new Entity(isPopulatedKey)); resp.getWriter().println("ok"); }
Example 11
Source File: TaskServlet.java From sc2gears with Apache License 2.0 | 4 votes |
private void customTask( final HttpServletRequest request, final PersistenceManager pm ) throws IOException { LOGGER.fine( "Key: " + request.getParameter( "key" ) + ", file type: " + request.getParameter( "fileType" ) ); final FileType fileType = FileType.fromString( request.getParameter( "fileType" ) ); if ( fileType == null ) { LOGGER.severe( "Invalid File type!" ); return; } final Key key = KeyFactory.stringToKey( request.getParameter( "key" ) ); final DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); final Entity e; try { e = ds.get( key ); } catch ( final EntityNotFoundException enfe ) { LOGGER.log( Level.WARNING, "Entity not found!", enfe ); return; } if ( !e.getKind().equals( "Rep" ) && !e.getKind().equals( "Smpd" ) && !e.getKind().equals( "OtherFile" ) ) { LOGGER.severe( "Invalid Entity kind:" + e.getKind() ); return; } if ( (Long) e.getProperty( "v" ) == 4 ) { LOGGER.warning( "Entity is already v4!" ); return; } // Update common properties: // TODO final int size = ( (Long) e.getProperty( "size" ) ).intValue(); if ( size < FileServlet.DATASTORE_CONTENT_STORE_LIMIT ) { final FileService fileService = FileServiceFactory.getFileService(); final AppEngineFile appeFile = new AppEngineFile( FileSystem.BLOBSTORE, ( (BlobKey) e.getProperty( "blobKey" ) ).getKeyString() ); final FileReadChannel channel = fileService.openReadChannel( appeFile, false ); final byte[] content = new byte[ size ]; final ByteBuffer wrapper = ByteBuffer.wrap( content ); while ( channel.read( wrapper ) > 0 ) ; channel.close(); e.setProperty( "content", new Blob( content ) ); e.setProperty( "blobKey", null ); fileService.delete( appeFile ); } e.setUnindexedProperty( "blobKey", e.getProperty( "blobKey" ) ); e.setUnindexedProperty( "content", e.getProperty( "content" ) ); switch ( fileType ) { case SC2REPLAY : e.setUnindexedProperty( "matchup", e.getProperty( "matchup" ) ); break; case MOUSE_PRINT : break; case OTHER : break; default: throw new RuntimeException( "Invalid file type!" ); } // UPGRADE COMPLETE! e.setProperty( "v", 4 ); ds.put( e ); }
Example 12
Source File: RemoteApiSharedTests.java From appengine-java-vm-runtime with Apache License 2.0 | 4 votes |
@Override public void run( DatastoreService ds, Supplier<Key> keySupplier, Supplier<Entity> entitySupplier) { Entity entity1 = new Entity(keySupplier.get()); entity1.setProperty("prop1", 75L); // Verify results of Put Key keyReturnedFromPut = ds.put(entity1); assertEquals(entity1.getKey(), keyReturnedFromPut); // Make sure we can retrieve it again. assertGetEquals(ds, keyReturnedFromPut, entity1); // Test EntityNotFoundException Key unsavedKey = keySupplier.get(); assertEntityNotFoundException(ds, unsavedKey); // Test batch get Entity entity2 = new Entity(keySupplier.get()); entity2.setProperty("prop1", 88L); ds.put(entity2); Map<Key, Entity> batchGetResult = ds.get(Arrays.asList(entity1.getKey(), unsavedKey, entity2.getKey())); // Omits the unsaved key from results. assertEquals(2, batchGetResult.size()); assertEquals(entity1, batchGetResult.get(entity1.getKey())); assertEquals(entity2, batchGetResult.get(entity2.getKey())); // Test Put and Get with id generated by Datastore backend. Entity entity3 = entitySupplier.get(); entity3.setProperty("prop1", 35L); assertNoIdOrName(entity3.getKey()); Key assignedKey = ds.put(entity3); assertTrue(assignedKey.getId() > 0); assertEquals(assignedKey, entity3.getKey()); assertGetEquals(ds, assignedKey, entity3); }
Example 13
Source File: TransactionsTest.java From java-docs-samples with Apache License 2.0 | 4 votes |
@Test public void entityGroups() throws Exception { try { // [START entity_groups] DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Entity person = new Entity("Person", "tom"); datastore.put(person); // Transactions on root entities Transaction txn = datastore.beginTransaction(); Entity tom = datastore.get(person.getKey()); tom.setProperty("age", 40); datastore.put(txn, tom); txn.commit(); // Transactions on child entities txn = datastore.beginTransaction(); tom = datastore.get(person.getKey()); Entity photo = new Entity("Photo", tom.getKey()); // Create a Photo that is a child of the Person entity named "tom" photo.setProperty("photoUrl", "http://domain.com/path/to/photo.jpg"); datastore.put(txn, photo); txn.commit(); // Transactions on entities in different entity groups txn = datastore.beginTransaction(); tom = datastore.get(person.getKey()); Entity photoNotAChild = new Entity("Photo"); photoNotAChild.setProperty("photoUrl", "http://domain.com/path/to/photo.jpg"); datastore.put(txn, photoNotAChild); // Throws IllegalArgumentException because the Person entity // and the Photo entity belong to different entity groups. txn.commit(); // [END entity_groups] fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { // We expect to get an exception that complains that we don't have a XG-transaction. } }
Example 14
Source File: DatastoreHelperTestBase.java From appengine-tck with Apache License 2.0 | 4 votes |
protected Entity getSingleEntity(DatastoreService ds, Key key) { Map<Key, Entity> map = ds.get(Collections.singleton(key)); return (map.isEmpty() ? null : map.values().iterator().next()); }
Example 15
Source File: InvocationData.java From appengine-tck with Apache License 2.0 | 4 votes |
public void postGet(DatastoreService ds) throws Exception { if (lrd != null) { lastReceivedDocument = ds.get(lrd); } }
Example 16
Source File: Ping.java From appengine-tck with Apache License 2.0 | 4 votes |
public void postGet(DatastoreService ds) throws Exception { if (lrd != null) { lastReceivedDocument = ds.get(lrd); } }
Example 17
Source File: RequestLogsTest.java From appengine-tck with Apache License 2.0 | 4 votes |
private Entity getTestDataEntity() throws EntityNotFoundException { DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); return datastore.get(KeyFactory.createKey(ENTITY_KIND, ENTITY_NAME)); }