Java Code Examples for com.google.appengine.api.datastore.DatastoreServiceFactory#getDatastoreService()

The following examples show how to use com.google.appengine.api.datastore.DatastoreServiceFactory#getDatastoreService() . 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: ListPeopleServletTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@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 2
Source File: EntitiesTest.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: AppEngineCredentialStore.java    From google-oauth-java-client with Apache License 2.0 5 votes vote down vote up
@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 4
Source File: StartupServlet.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@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 5
Source File: AppEngineCredentialStore.java    From google-oauth-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Migrates to the new format using {@link DataStore} of {@link StoredCredential}.
 *
 * @param credentialDataStore credential data store
 * @since 1.16
 */
public final void migrateTo(DataStore<StoredCredential> credentialDataStore) throws IOException {
  DatastoreService service = DatastoreServiceFactory.getDatastoreService();
  PreparedQuery queryResult = service.prepare(new Query(KIND));
  for (Entity entity : queryResult.asIterable()) {
    StoredCredential storedCredential = new StoredCredential().setAccessToken(
        (String) entity.getProperty("accessToken"))
        .setRefreshToken((String) entity.getProperty("refreshToken"))
        .setExpirationTimeMilliseconds((Long) entity.getProperty("expirationTimeMillis"));
    credentialDataStore.set(entity.getKey().getName(), storedCredential);
  }
}
 
Example 6
Source File: DatastoreUtil.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
public void purgeTestRunRecords() {
    DatastoreService datastoreService = DatastoreServiceFactory. getDatastoreService();
    FilterPredicate testRunFilter = new FilterPredicate(TEST_RUN_ID, FilterOperator.EQUAL, testRunId);
    Query query = new Query(entityName).setFilter(testRunFilter).setKeysOnly();
    for (Entity readRec : datastoreService.prepare(query).asIterable()) {
        datastoreService.delete(readRec.getKey());
    }
}
 
Example 7
Source File: DatastoreUtil.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
public Entity getMarker(String marker) {
    DatastoreService service = DatastoreServiceFactory.getDatastoreService();
    FilterPredicate testRunFilter = new FilterPredicate(TEST_RUN_ID, FilterOperator.EQUAL, testRunId);
    FilterPredicate markerFilter = new FilterPredicate(MARKER, FilterOperator.EQUAL, marker);
    CompositeFilter filter = CompositeFilterOperator.and(testRunFilter, markerFilter);
    Query query = new Query(entityName).setFilter(filter);
    return service.prepare(query).asSingleEntity();
}
 
Example 8
Source File: EntitiesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void datastoreServiceFactory_returnsDatastoreService() throws Exception {
  // [START working_with_entities]
  DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
  // [END working_with_entities]
  assertWithMessage("datastore").that(datastore).isNotNull();
}
 
Example 9
Source File: NotificationCleanupServlet.java    From io2014-codelabs with Apache License 2.0 5 votes vote down vote up
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 10
Source File: DatastoreSessionFilter.java    From getting-started-java with Apache License 2.0 5 votes vote down vote up
@Override
public void init(FilterConfig config) throws ServletException {
  // initialize local copy of datastore session variables

  datastore = DatastoreServiceFactory.getDatastoreService();
  // Delete all sessions unmodified for over two days
  DateTime dt = DateTime.now(DateTimeZone.UTC);
  Query query = new Query(SESSION_KIND).setFilter(new FilterPredicate(
          "lastModified", FilterOperator.LESS_THAN_OR_EQUAL, dt.minusDays(2).toString(DTF)));
  Iterator<Entity> results = datastore.prepare(query).asIterator();
  while (results.hasNext()) {
    Entity stateEntity = results.next();
    datastore.delete(stateEntity.getKey());
  }
}
 
Example 11
Source File: RestoreCommitLogsActionTest.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Before
public void init() {
  action.gcsService = gcsService;
  action.dryRun = false;
  action.datastoreService = DatastoreServiceFactory.getDatastoreService();
  action.fromTime = now.minusMillis(1);
  action.retrier = new Retrier(new FakeSleeper(new FakeClock()), 1);
  action.diffLister = new GcsDiffFileLister();
  action.diffLister.gcsService = gcsService;
  action.diffLister.gcsBucket = GCS_BUCKET;
  action.diffLister.executor = newDirectExecutorService();
}
 
Example 12
Source File: DatastoreDao.java    From getting-started-java with Apache License 2.0 4 votes vote down vote up
public DatastoreDao() {
  datastore = DatastoreServiceFactory.getDatastoreService(); // Authorized Datastore service
}
 
Example 13
Source File: RequestLogsTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
private Entity getTestDataEntity() throws EntityNotFoundException {
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    return datastore.get(KeyFactory.createKey(ENTITY_KIND, ENTITY_NAME));
}
 
Example 14
Source File: DatastoreDao.java    From getting-started-java with Apache License 2.0 4 votes vote down vote up
public DatastoreDao() {
  datastore = DatastoreServiceFactory.getDatastoreService(); // Authorized Datastore service
}
 
Example 15
Source File: StocksRepositoryImplTest.java    From phisix with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
	helper.setUp();
	datastore = DatastoreServiceFactory.getDatastoreService();
	stocksRepository = new StocksRepositoryImpl(pseClient, gaClient);
}
 
Example 16
Source File: QueriesTest.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
  helper.setUp();
  datastore = DatastoreServiceFactory.getDatastoreService();
}
 
Example 17
Source File: DatastoreDao.java    From getting-started-java with Apache License 2.0 4 votes vote down vote up
public DatastoreDao() {
  datastore = DatastoreServiceFactory.getDatastoreService(); // Authorized Datastore service
}
 
Example 18
Source File: IndexesTest.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
  helper.setUp();
  datastore = DatastoreServiceFactory.getDatastoreService();
}
 
Example 19
Source File: BackendConfigManager.java    From io2014-codelabs with Apache License 2.0 4 votes vote down vote up
/**
 * Default constructor.
 */
public BackendConfigManager() {
  this(DatastoreServiceFactory.getDatastoreService(),
      MemcacheServiceFactory.getMemcacheService());
}
 
Example 20
Source File: CloudEndpointsConfigManager.java    From solutions-mobile-backend-starter-java with Apache License 2.0 4 votes vote down vote up
public CloudEndpointsConfigManager() {
  this(DatastoreServiceFactory.getDatastoreService(),
      MemcacheServiceFactory.getMemcacheService());
}