com.google.appengine.api.datastore.DatastoreServiceFactory Java Examples
The following examples show how to use
com.google.appengine.api.datastore.DatastoreServiceFactory.
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: 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 #2
Source File: ListPeopleServletTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); helper.setUp(); datastore = DatastoreServiceFactory.getDatastoreService(); // Add test data. ImmutableList.Builder<Entity> people = ImmutableList.builder(); for (String name : TEST_NAMES) { people.add(createPerson(name)); } datastore.put(people.build()); // Set up a fake HTTP response. responseWriter = new StringWriter(); when(mockResponse.getWriter()).thenReturn(new PrintWriter(responseWriter)); servletUnderTest = new ListPeopleServlet(); }
Example #3
Source File: AsyncTestBase.java From appengine-tck with Apache License 2.0 | 6 votes |
protected <T> T inTx(Action<T> action) throws Exception { AsyncDatastoreService ads = DatastoreServiceFactory.getAsyncDatastoreService(); Transaction tx = ads.beginTransaction().get(); boolean ok = false; try { T result = action.run(ads); ok = true; return result; } finally { if (ok) tx.commitAsync(); else tx.rollbackAsync(); sync(); // wait for tx to finish } }
Example #4
Source File: SignGuestbookServletTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); helper.setUp(); ds = DatastoreServiceFactory.getDatastoreService(); // Set up some fake HTTP requests when(mockRequest.getRequestURI()).thenReturn(FAKE_URL); when(mockRequest.getParameter("guestbookName")).thenReturn("default2"); when(mockRequest.getParameter("content")).thenReturn(testPhrase); stringWriter = new StringWriter(); when(mockResponse.getWriter()).thenReturn(new PrintWriter(stringWriter)); servletUnderTest = new SignGuestbookServlet(); ObjectifyService.init(); ObjectifyService.register(Guestbook.class); ObjectifyService.register(Greeting.class); closeable = ObjectifyService.begin(); cleanDatastore(ds, "default"); }
Example #5
Source File: MyEndpoint.java From endpoints-codelab-android with GNU General Public License v3.0 | 6 votes |
@ApiMethod(name = "storeTask") public void storeTask(TaskBean taskBean) { DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService(); Transaction txn = datastoreService.beginTransaction(); try { Key taskBeanParentKey = KeyFactory.createKey("TaskBeanParent", "todo.txt"); Entity taskEntity = new Entity("TaskBean", taskBean.getId(), taskBeanParentKey); taskEntity.setProperty("data", taskBean.getData()); datastoreService.put(taskEntity); txn.commit(); } finally { if (txn.isActive()) { txn.rollback(); } } }
Example #6
Source File: TransactionsTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testMiscOps() throws Exception { AsyncDatastoreService service = DatastoreServiceFactory.getAsyncDatastoreService(); DatastoreAttributes attributes = waitOnFuture(service.getDatastoreAttributes()); Assert.assertNotNull(attributes); Assert.assertNotNull(attributes.getDatastoreType()); Map<Index, Index.IndexState> indexes = waitOnFuture(service.getIndexes()); Assert.assertNotNull(indexes); Transaction tx = waitOnFuture(service.beginTransaction()); try { String txId = tx.getId(); Assert.assertNotNull(txId); Assert.assertEquals(txId, tx.getId()); String appId = tx.getApp(); Assert.assertNotNull(appId); Assert.assertEquals(appId, tx.getApp()); } finally { tx.rollback(); } }
Example #7
Source File: TestBase.java From appengine-tck with Apache License 2.0 | 6 votes |
public static <T extends TempData> T getLastTempData(Class<T> type) { try { DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); String kind = getKind(type); PreparedQuery pq = ds.prepare(new Query(kind).addSort("timestamp", Query.SortDirection.DESCENDING)); Iterator<Entity> iter = pq.asIterator(); if (iter.hasNext()) { Entity entity = iter.next(); return readTempData(type, entity, ds); } else { return null; } } catch (Exception e) { throw new IllegalStateException(e); } }
Example #8
Source File: MyEndpoint.java From endpoints-codelab-android with GNU General Public License v3.0 | 6 votes |
@ApiMethod(name = "clearTasks") public void clearTasks() { DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService(); Transaction txn = datastoreService.beginTransaction(); try { Key taskBeanParentKey = KeyFactory.createKey("TaskBeanParent", "todo.txt"); Query query = new Query(taskBeanParentKey); List<Entity> results = datastoreService.prepare(query).asList(FetchOptions.Builder.withDefaults()); for (Entity result : results) { datastoreService.delete(result.getKey()); } txn.commit(); } finally { if (txn.isActive()) { txn.rollback(); } } }
Example #9
Source File: CachingService.java From sc2gears with Apache License 2.0 | 6 votes |
/** * Returns the Account key associated with the specified authorization key. * @param pm reference to the persistence manager * @param authorizationKey authorization key to return the account key for * @return the Account key associated with the specified authorization key; or <code>null</code> if the authorization key is invalid */ public static Key getAccountKeyByAuthKey( final PersistenceManager pm, final String authorizationKey ) { final String memcacheKey = CACHE_KEY_AUTH_KEY_ACCOUNT_KEY_PREFIX + authorizationKey; final String accountKeyString = (String) memcacheService.get( memcacheKey ); if ( accountKeyString != null ) return KeyFactory.stringToKey( accountKeyString ); final Query q = new Query( Account.class.getSimpleName() ); q.setFilter( new FilterPredicate( "authorizationKey", FilterOperator.EQUAL, authorizationKey ) ); q.setKeysOnly(); final List< Entity > entityList = DatastoreServiceFactory.getDatastoreService().prepare( q ).asList( FetchOptions.Builder.withDefaults() ); if ( entityList.isEmpty() ) return null; final Key accountKey = entityList.get( 0 ).getKey(); try { memcacheService.put( memcacheKey, KeyFactory.keyToString( accountKey ) ); } catch ( final MemcacheServiceException mse ) { LOGGER.log( Level.WARNING, "Failed to put key to memcache: " + memcacheKey, mse ); // Ignore memcache errors, do not prevent serving user request } return accountKey; }
Example #10
Source File: AsyncTest.java From appengine-tck with Apache License 2.0 | 6 votes |
@Test public void testCommitTx() throws Exception { AsyncDatastoreService service = DatastoreServiceFactory.getAsyncDatastoreService(); Transaction tx = waitOnFuture(service.beginTransaction(TransactionOptions.Builder.withDefaults())); Key key; try { Future<Key> fKey = service.put(tx, new Entity("AsyncTx")); key = waitOnFuture(fKey); waitOnFuture(tx.commitAsync()); } catch (Exception e) { waitOnFuture(tx.rollbackAsync()); throw e; } if (key != null) { Assert.assertNotNull(getSingleEntity(service, key)); } }
Example #11
Source File: TestBase.java From appengine-tck with Apache License 2.0 | 6 votes |
private static <T extends TempData> List<T> getAllTempData(Class<T> type, boolean unreadOnly) { try { DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); String kind = getKind(type); Query query = new Query(kind); if (unreadOnly) { query.setFilter(new Query.FilterPredicate(TEMP_DATA_READ_PROPERTY, Query.FilterOperator.EQUAL, false)); } else { query.addSort("timestamp", Query.SortDirection.ASCENDING); } PreparedQuery pq = ds.prepare(query); Iterator<Entity> iter = pq.asIterator(); List<T> result = new ArrayList<>(); while (iter.hasNext()) { Entity entity = iter.next(); T data = readTempData(type, entity, ds); result.add(data); } return result; } catch (Exception e) { throw new IllegalStateException(e); } }
Example #12
Source File: DatastoreSessionStore.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
@Override public Map<String, SessionData> getAllSessions() { final String originalNamespace = NamespaceManager.get(); NamespaceManager.set(""); try { DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); PreparedQuery pq = ds.prepare(new Query(SESSION_ENTITY_TYPE)); Map<String, SessionData> sessions = new HashMap<>(); for (Entity entity : pq.asIterable()) { sessions.put(entity.getKey().getName(), createSessionFromEntity(entity)); } return sessions; } finally { NamespaceManager.set(originalNamespace); } }
Example #13
Source File: TransactionsTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Test public void crossGroupTransactions() throws Exception { // [START cross-group_XG_transactions_using_the_Java_low-level_API] DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); TransactionOptions options = TransactionOptions.Builder.withXG(true); Transaction txn = datastore.beginTransaction(options); Entity a = new Entity("A"); a.setProperty("a", 22); datastore.put(txn, a); Entity b = new Entity("B"); b.setProperty("b", 11); datastore.put(txn, b); txn.commit(); // [END cross-group_XG_transactions_using_the_Java_low-level_API] }
Example #14
Source File: TransactionsTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Test public void creatingAnEntityInASpecificEntityGroup() throws Exception { String boardName = "my-message-board"; // [START creating_an_entity_in_a_specific_entity_group] DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); String messageTitle = "Some Title"; String messageText = "Some message."; Date postDate = new Date(); Key messageBoardKey = KeyFactory.createKey("MessageBoard", boardName); Entity message = new Entity("Message", messageBoardKey); message.setProperty("message_title", messageTitle); message.setProperty("message_text", messageText); message.setProperty("post_date", postDate); Transaction txn = datastore.beginTransaction(); datastore.put(txn, message); txn.commit(); // [END creating_an_entity_in_a_specific_entity_group] }
Example #15
Source File: ReadPolicyTest.java From java-docs-samples with Apache License 2.0 | 6 votes |
@Test public void readPolicy_strong_returnsAllResults() { double deadline = 5.0; ReadPolicy policy = new ReadPolicy(ReadPolicy.Consistency.STRONG); DatastoreServiceConfig datastoreConfig = DatastoreServiceConfig.Builder.withReadPolicy(policy).deadline(deadline); DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(datastoreConfig); Entity parent = new Entity("Person", "a"); Entity child = new Entity("Person", "b", parent.getKey()); datastore.put(ImmutableList.<Entity>of(parent, child)); Query q = new Query("Person").setAncestor(parent.getKey()); List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()); assertWithMessage("query results").that(results).hasSize(2); }
Example #16
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 #17
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 #18
Source File: TestUtils.java From appengine-tck with Apache License 2.0 | 5 votes |
public static void clean() { try { DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); TestLifecycleEvent event = TestLifecycles.createServiceLifecycleEvent(null, ds); TestLifecycles.after(event); } catch (Exception e) { throw new RuntimeException(e); } }
Example #19
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 #20
Source File: ReadPolicyTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Test public void readPolicy_eventual_returnsNoResults() { // [START data_consistency] double deadline = 5.0; // Construct a read policy for eventual consistency ReadPolicy policy = new ReadPolicy(ReadPolicy.Consistency.EVENTUAL); // Set the read policy DatastoreServiceConfig eventuallyConsistentConfig = DatastoreServiceConfig.Builder.withReadPolicy(policy); // Set the call deadline DatastoreServiceConfig deadlineConfig = DatastoreServiceConfig.Builder.withDeadline(deadline); // Set both the read policy and the call deadline DatastoreServiceConfig datastoreConfig = DatastoreServiceConfig.Builder.withReadPolicy(policy).deadline(deadline); // Get Datastore service with the given configuration DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(datastoreConfig); // [END data_consistency] Entity parent = new Entity("Person", "a"); Entity child = new Entity("Person", "b", parent.getKey()); datastore.put(ImmutableList.<Entity>of(parent, child)); // Even though we are using an ancestor query, the policy is set to // eventual, so we should get eventually-consistent results. Since the // local data store test config is set to 100% unapplied jobs, there // should be no results. Query q = new Query("Person").setAncestor(parent.getKey()); List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()); assertWithMessage("query results").that(results).isEmpty(); }
Example #21
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 #22
Source File: AppEngineDataStoreFactory.java From google-http-java-client with Apache License 2.0 | 5 votes |
AppEngineDataStore(AppEngineDataStoreFactory dataStoreFactory, String id) { super(dataStoreFactory, id); memcache = dataStoreFactory.disableMemcache ? null : MemcacheServiceFactory.getMemcacheService(id); memcacheExpiration = dataStoreFactory.memcacheExpiration; dataStoreService = DatastoreServiceFactory.getDatastoreService(); }
Example #23
Source File: SessionManagerTest.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
@Override public void setUp() throws Exception { super.setUp(); helper.setUp(); memcache = MemcacheServiceFactory.getMemcacheService(""); datastore = DatastoreServiceFactory.getDatastoreService(); NamespaceManager.set(startNamespace()); manager = new SessionManager(Arrays.asList(new DatastoreSessionStore(), new MemcacheSessionStore())); NamespaceManager.set(testNamespace()); }
Example #24
Source File: ConcurrentTxServlet.java From appengine-tck with Apache License 2.0 | 5 votes |
@Override public void init() throws ServletException { super.init(); DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); ds.put(ROOT_1); // create root 1 ds.put(ROOT_2); // create root 2 }
Example #25
Source File: NotificationCleanupServlet.java From solutions-mobile-backend-starter-java 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 #26
Source File: DatastoreConfigPrintingServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
@Override protected void doGet( HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { PrintWriter writer = httpServletResponse.getWriter(); // Just because it's deprecated doesn't mean we don't want to test it. DatastoreConfig config = DatastoreServiceFactory.getDefaultDatastoreConfig(); writer.println(config.getImplicitTransactionManagementPolicy().name()); DatastoreServiceConfig serviceConfig = DatastoreServiceConfig.Builder.withDefaults(); writer.println(serviceConfig.getImplicitTransactionManagementPolicy().name()); writer.println(serviceConfig.getReadPolicy().getConsistency()); writer.println(serviceConfig.getDeadline()); }
Example #27
Source File: NsTest.java From appengine-tck with Apache License 2.0 | 5 votes |
@Test @WithinNamespace({ "", "Ns1" }) public void testSmoke() throws Exception { final DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); Key key = ds.put(new Entity("NsTest")); try { Assert.assertNotNull(ds.get(key)); } finally { ds.delete(key); } }
Example #28
Source File: RemoteApiInsideAppEngineExample.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
void putInRemoteDatastore(Entity entity) throws IOException { RemoteApiInstaller installer = new RemoteApiInstaller(); installer.install(options); try { DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); System.out.println("Key of new entity is " + ds.put(entity)); } finally { installer.uninstall(); } }
Example #29
Source File: TestBase.java From appengine-tck with Apache License 2.0 | 5 votes |
/** * Should work in all envs? * A bit complex / overkill ... * * @return true if in-container, false otherewise */ protected static boolean isInContainer() { try { DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); Transaction tx = ds.beginTransaction(); try { return (ds.getCurrentTransaction() != null); } finally { tx.rollback(); } } catch (Throwable ignored) { return false; } }
Example #30
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; } }