Showing posts with label Appengine. Show all posts
Showing posts with label Appengine. Show all posts

Aug 29, 2013

GAE - Datastore filtering using IN operator

Since I can't find in datastore queries the documentation on how to use the IN operator, that why I decide to share my expirimentaion.

Here how I did.
Query q = new Query("YourKindHere");
// Create a list of possible data tobe compare in your IN operator
List<Long> levels =  new ArrayList<Long>(); // Initiate a List object
// Add the possible levels
// INFO: If you notice, I put "l" to represent the value as Long
arrayList.add(1l); // add level 1
arrayList.add(2l); // add level 2
arrayList.add(3l); // add level 3

// Create and AND operation with IN operators
q.setFilter(CompositeFilterOperator.and(
  FilterOperator.EQUAL.of("active", 1l), // compare as active=1
  FilterOperator.IN.of("level", levels) // compare ad level IN (1, 2, 3)
));


For other info about queries please check google appengine datastore query docs. Java DataStore Queries.

Apr 25, 2013

GAE - PreparedQuery.TooManyResultsException

Just take note before using asSingleEntity you must to make sure that you query will on return one entity(record) or else you will came up a problem TooManyResultsException

Suggestion to limit this unexpected behavior

public static Entity getSingleData(Query q){
  FetchOptions fOption = FetchOptions.Builder.withLimit(1);
  try{
   return datastore.prepare(q).asList(fOption).get(0);
  }catch (IndexOutOfBoundsException e) {
   logger.warning("No data sorry: "+e.getMessage());
   return null;
   
  }
  //return datastore.prepare(q).asSingleEntity();
 }

Jan 28, 2013

GAE - How to to handle DeadlineExceededException

Before we do. Lets recap how this Exception encounter or what is the cause?

This Exception encounter when a request runtime exceed the 60 sec runtime. Some you are working with loop.


Lets take some example. Somehow you are looping some data and update some properties of every record.


Possible solutions/workaround

Somehow you must have extract servlet the equivalent process to every record. Lets take you have servlet /updateData and /updateDataBackend. You updateDataBackend will process the other data that will possible reach by Deadline. By the use getRemainingMillis() method you can able determine if the extra will be reach by Deadline.



/updateDataBackend

import com.google.apphosting.api.ApiProxy;

...

for(Entity ent : myDataEntities){
  if (ApiProxy.getCurrentEnvironment().getRemainingMillis() > 5000){
     // I think 5 seconds can still do the job
  // Update data
  }else{
    // Opps, It almost reach DeadlineExceededException better create a backend procress
    // for extra data
    //  Please the updateDataBackend to run in backend
  }
}

Hope that helps. Also try to read The_Request_Timer

Happy reading

Dec 12, 2012

GAE- Generate Child Entity key

Here the simple way to make.

ParentEntity.getKey().getChild(ChildKind, keyname);

GAE- The method addFilter(String, Query.FilterOperator, Object) from the type Query is deprecated

In this situation, we can fixed this by changing out code.


Import the Libraries
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.FilterOperator;

Sample:
Query query = new Query(Kind);
query.addFilter("active", FilterOperator.EQUAL, 1l);
query.addFilter("user", FilterOperator.EQUAL, _user);

Solution1:
Filter activeFilter =
  new FilterPredicate("active",FilterOperator.EQUAL,1l);
Filter userFilter =
  new FilterPredicate("user",FilterOperator.EQUAL,_user);

// Combine the Two filter
Filter queryFilter =
  CompositeFilterOperator.and(activeFilter, userFilter);

// Use class Query to assemble a query
Query q = new Query("Person").setFilter(queryFilter);

Solution 2
query = new Query(Kind);
query.setFilter(CompositeFilterOperator.and(
  FilterOperator.EQUAL.of("active", 1l),
  FilterOperator.EQUAL.of("user", _user)
  )
);

If you want look more info you cant find Datastore Queries and GAE Query

Happy Reading