public final class GAERepository extends Object implements Repository
add, update and
remove MUST in a transaction. Invocation of method get (by id) in a
transaction will try to get object from transaction snapshot; if the invocation made is not in a transaction, retrieve object from
datastore directly. See GAE 事务隔离 for more details.Query,
GAETransaction| Modifier and Type | Field and Description |
|---|---|
private com.google.appengine.api.datastore.DatastoreService |
datastoreService
GAE datastore service.
|
private static com.google.appengine.api.datastore.Key |
DEFAULT_PARENT_KEY
Default parent key.
|
private static Set<Class<?>> |
GAE_SUPPORTED_TYPES
GAE datastore supported types.
|
private static Logger |
LOGGER
Logger.
|
private String |
name
Repository name.
|
private static int |
QUERY_CHUNK_SIZE
Query chunk size.
|
static ThreadLocal<GAETransaction> |
TX
The current transaction.
|
private boolean |
writable
Writable?
|
| Constructor and Description |
|---|
GAERepository(String name)
Constructs a GAE repository with the specified name.
|
| Modifier and Type | Method and Description |
|---|---|
String |
add(JSONObject jsonObject)
Adds the specified json object with the default parent key.
|
private String |
add(JSONObject jsonObject,
String parentKeyKind,
String parentKeyName)
Adds.
|
GAETransaction |
beginTransaction() |
long |
count() |
long |
count(Query query) |
static JSONObject |
entity2JSONObject(com.google.appengine.api.datastore.Entity entity)
Converts the specified
entity to a json object. |
static String |
genTimeMillisId()
Gets current date time string.
|
private JSONObject |
get(int currentPageNum,
int pageSize,
int pageCount,
Set<Projection> projections,
Map<String,SortDirection> sorts,
Filter filter)
Gets the result object by the specified current page number, page size, page count, sorts, filter.
|
Map<String,JSONObject> |
get(Iterable<String> ids) |
private JSONObject |
get(com.google.appengine.api.datastore.Key parentKey,
String id)
Gets a json object with the specified parent key and id.
|
JSONObject |
get(Query query) |
private JSONObject |
get(com.google.appengine.api.datastore.Query query,
int currentPageNum,
int pageSize,
int pageCount)
Gets result json object by the specified query, current page number, page size, page count.
|
JSONObject |
get(String id)
Gets a json object by the specified id with the default parent key.
|
String |
getName() |
List<JSONObject> |
getRandomly(int fetchSize) |
private com.google.appengine.api.datastore.Cursor |
getStartCursor(int currentPageNum,
int pageSize,
com.google.appengine.api.datastore.PreparedQuery preparedQuery)
Gets the end cursor of the specified current page number, page size and
the prepared query.
|
boolean |
has(String id) |
boolean |
hasTransactionBegun() |
boolean |
isWritable() |
private com.google.appengine.api.datastore.Query.CompositeFilter |
processCompositeFilter(CompositeFilter compositeFilter)
Converts the specified composite filter to a GAE composite filter.
|
private com.google.appengine.api.datastore.Query.FilterPredicate |
processPropertyFiler(PropertyFilter propertyFilter)
Converts the specified property filter to a GAE filter predicate.
|
void |
remove(String id)
Removes a json object by the specified id with the default parent key.
|
private void |
remove(String id,
String parentKeyKind,
String parentKeyName)
Removes.
|
static void |
setProperties(com.google.appengine.api.datastore.Entity entity,
JSONObject jsonObject)
Sets the properties of the specified entity by the specified json object.
|
void |
setWritable(boolean writable) |
void |
update(String id,
JSONObject jsonObject)
Updates a certain json object by the specified id and the specified new json object.
|
private void |
update(String id,
JSONObject jsonObject,
String parentKeyKind,
String parentKeyName)
Updates.
|
private static final Logger LOGGER
private final com.google.appengine.api.datastore.DatastoreService datastoreService
private static final Set<Class<?>> GAE_SUPPORTED_TYPES
private static final com.google.appengine.api.datastore.Key DEFAULT_PARENT_KEY
"parentKind", name is
"parentKeyName".private boolean writable
private static final int QUERY_CHUNK_SIZE
public static final ThreadLocal<GAETransaction> TX
private String name
public GAERepository(String name)
name - the specified namepublic String add(JSONObject jsonObject) throws RepositoryException
add in interface RepositoryjsonObject - the specified json objectRepositoryException - repository exceptionprivate String add(JSONObject jsonObject, String parentKeyKind, String parentKeyName) throws RepositoryException
jsonObject - the specified json objectparentKeyKind - the specified parent key kindparentKeyName - the specified parent key nameRepositoryException - repository exceptionpublic void update(String id, JSONObject jsonObject) throws RepositoryException
The parent key of the entity to update is the default parent key.
Invokes this method for an non-existent entity will create a new entity in database, as the same effect of method add(org.json.JSONObject).
Update algorithm steps:
Note: the specified id is NOT the key of a database record, but the value of "oId" stored in database value entry of a record.
update in interface Repositoryid - the specified idjsonObject - the specified new json objectRepositoryException - repository exceptionKeys.OBJECT_IDprivate void update(String id, JSONObject jsonObject, String parentKeyKind, String parentKeyName) throws RepositoryException
id - the specified idjsonObject - the specified json objectparentKeyKind - the specified parent key kindparentKeyName - the specified parent key nameRepositoryException - repository exceptionpublic void remove(String id) throws RepositoryException
remove in interface Repositoryid - the specified idRepositoryException - repository exceptionprivate void remove(String id, String parentKeyKind, String parentKeyName) throws RepositoryException
id - the specified idparentKeyKind - the specified parent key kindparentKeyName - the specified parent key nameRepositoryException - repository exceptionpublic JSONObject get(String id) throws RepositoryException
get in interface Repositoryid - the specified idnull if not foundRepositoryException - repository exceptionpublic Map<String,JSONObject> get(Iterable<String> ids) throws RepositoryException
get in interface RepositoryRepositoryExceptionprivate JSONObject get(com.google.appengine.api.datastore.Key parentKey, String id) throws RepositoryException
parentKey - the specified parent keyid - the specified idnull if not foundRepositoryException - repository exceptionpublic boolean has(String id) throws RepositoryException
has in interface RepositoryRepositoryExceptionpublic JSONObject get(Query query) throws RepositoryException
get in interface RepositoryRepositoryExceptionprivate JSONObject get(int currentPageNum, int pageSize, int pageCount, Set<Projection> projections, Map<String,SortDirection> sorts, Filter filter) throws RepositoryException
currentPageNum - the specified current page numberpageSize - the specified page sizepageCount - if the pageCount specified with -1, the returned (pageCnt, recordCnt) value will be calculated, otherwise,
the returned pageCnt will be this pageCount, and recordCnt will be 0, means these values will not be calculatedprojections - the specified projectionssorts - the specified sortsfilter - the specified filterRepositoryException - repository exceptionprivate com.google.appengine.api.datastore.Query.CompositeFilter processCompositeFilter(CompositeFilter compositeFilter) throws RepositoryException
compositeFilter - the specified composite filterRepositoryException - repository exceptionprivate com.google.appengine.api.datastore.Query.FilterPredicate processPropertyFiler(PropertyFilter propertyFilter) throws RepositoryException
propertyFilter - the specified property filterRepositoryException - repository exceptionpublic List<JSONObject> getRandomly(int fetchSize) throws RepositoryException
getRandomly in interface RepositoryRepositoryExceptionpublic long count()
count in interface Repositorypublic long count(Query query) throws RepositoryException
count in interface RepositoryRepositoryExceptionpublic static JSONObject entity2JSONObject(com.google.appengine.api.datastore.Entity entity)
entity to a json object.entity - the specified entitypublic static void setProperties(com.google.appengine.api.datastore.Entity entity,
JSONObject jsonObject)
throws JSONException
entity - the specified entityjsonObject - the specified json objectJSONException - json exceptionprivate JSONObject get(com.google.appengine.api.datastore.Query query, int currentPageNum, int pageSize, int pageCount) throws RepositoryException
If the specified page count equals to -1, this method will calculate the page count.
query - the specified querycurrentPageNum - the specified current page numberpageSize - the specified page sizepageCount - if the pageCount specified with -1, the returned (pageCnt, recordCnt) value will be calculated, otherwise,
the returned pageCnt will be this pageCount, and recordCnt will be 0, means these values will not be calculated
{
"pagination": {
"paginationPageCount": 10, // May be specified by the specified query.pageCount
"paginationRecordCount": "100" // If query.pageCount has been specified with not -1 or null, this value will
be 0 also
},
"rslts": [{
"oId": "...."
}, ....]
}
RepositoryException - repository exceptionpublic static String genTimeMillisId()
public GAETransaction beginTransaction()
beginTransaction in interface Repositorypublic boolean hasTransactionBegun()
hasTransactionBegun in interface Repositorypublic boolean isWritable()
isWritable in interface Repositorypublic void setWritable(boolean writable)
setWritable in interface Repositorypublic String getName()
getName in interface Repositoryprivate com.google.appengine.api.datastore.Cursor getStartCursor(int currentPageNum,
int pageSize,
com.google.appengine.api.datastore.PreparedQuery preparedQuery)
currentPageNum - the specified current page number, MUST larger
then 1pageSize - the specified page sizepreparedQuery - the specified prepared queryCopyright © 2010–2016 B3log. All rights reserved.