Lev Kuznetsov
Lev Kuznetsov

Reputation: 3728

GAE DatastoreService Query not getting any results?

UPDATE: It appears to work if I remove transaction related code, any reason why that would be?

I'm having trouble with a simple hello world using GAE DatastoreService. I'm just trying to put stuff in and then list it, the following code should create two entities and puts them and then query for them and list them back. But I don't get the listing back, the query iterables are empty. No errors occur.

public static void main (String[] args) throws Throwable {
  // setup HRD datastore
  LocalUserServiceTestConfig lustc = new LocalUserServiceTestConfig ();
  LocalDatastoreServiceTestConfig ldstc = new LocalDatastoreServiceTestConfig ();
  ldstc.setDefaultHighRepJobPolicyUnappliedJobPercentage (100);
  LocalServiceTestHelper helper = new LocalServiceTestHelper (lustc, ldstc).setEnvIsLoggedIn (true)
                                                                           .setEnvAuthDomain ("localhost")
                                                                           .setEnvEmail ("test@localhost");
  helper.setUp ();

  // hello world
  try {
    DatastoreService service = DatastoreServiceFactory.getDatastoreService ();
    Entity e = new Entity ("foo");
    e.setProperty ("fooey", new Text ("hello world"));
    Transaction t = service.beginTransaction (withXG (true));
    service.put (t, e);
    e = new Entity ("bar");
    e.setProperty ("bary", "goodbye world");
    service.put (t, e);
    t.commitAsync ().get ();

    System.err.println ("foo:");
    for (Entity s : service.prepare (new Query ("foo")).asIterable ())
      System.err.println (s);
    System.err.println ("bar:");
    for (Entity s : service.prepare (new Query ("bar")).asIterable ())
      System.err.println (s);
  } finally {
  // teardown
    helper.tearDown ();
  }
}

My actual output:

Mar 11, 2015 4:46:24 PM com.google.appengine.api.datastore.dev.LocalDatastoreService init
INFO: Local Datastore initialized: 
    Type: High Replication
    Storage: In-memory
foo:
bar:

I would expect my entities to show up there, how come I don't see them?

EDIT:

I'm pretty sure these are the maven jars needed to make this work:

<dependency>
  <groupId>com.google.appengine</groupId>
  <artifactId>appengine-api-1.0-sdk</artifactId>
  <version>1.9.18</version>
</dependency>
<dependency>
  <groupId>com.google.appengine</groupId>
  <artifactId>appengine-testing</artifactId>
  <version>1.9.18</version>
  <!--scope>test</scope-->
</dependency>
<dependency>
  <groupId>com.google.appengine</groupId>
  <artifactId>appengine-api-stubs</artifactId>
  <version>1.9.18</version>
  <!--scope>test</scope-->
</dependency>

Upvotes: 0

Views: 111

Answers (1)

tj-recess
tj-recess

Reputation: 1809

All queries without parent are eventually consistent. Your JUnit settings tell app-engine to never allow eventual consistent reads. Specifically this line -

ldstc.setDefaultHighRepJobPolicyUnappliedJobPercentage (100);

Change it to:

ldstc.setApplyAllHighRepJobPolicy(); // 100% consistent (impractical but easy to test)

And everything will work as expected. In production, your system will work somewhere in between these 2 settings. Eventually consistent queries will show data after "some time".

Upvotes: 1

Related Questions