import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterOperator.EQUAL;
import com.google.appengine.api.datastore.Query.FilterOperator.GREATER_THAN;
import com.google.appengine.api.datastore.Query.FilterOperator.GREATER_THAN_OR_EQUAL;
import com.google.appengine.api.datastore.Query.FilterOperator.IN;
import com.google.appengine.api.datastore.Query.FilterOperator.LESS_THAN;
import com.google.appengine.api.datastore.Query.FilterOperator.LESS_THAN_OR_EQUAL;
import com.google.appengine.api.datastore.Query.FilterOperator.NOT_EQUAL;
import com.google.appengine.api.datastore.Query.SortDirection.ASCENDING;
import com.google.appengine.api.datastore.Query.SortDirection.DESCENDING;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
// only way I got this working is to use java_class and put on variable or constant...
define('FetchOptionsBuilder', java_class('com.google.appengine.api.datastore.FetchOptions$Builder'));
// query
$q = new Query('test');
// filter
$q->addFilter('field1', EQUAL, 'value1');
// sort
$q->addSort('field1', ASCENDING);
// offset and count
$dataService = DatastoreServiceFactory::getDatastoreService();
$prepared = $dataService->prepare($q);
echo $prepared->countEntities(FetchOptionsBuilder->withOffset(0));