Contenu connexe Similaire à appengine java night #1 (20) Plus de Shinichi Ogawa (9) appengine java night #12. ...shin1ogawa
Google, Java, AppEngine, Eclipse,
Wicket, Maven, Hudson, ...
• OSS
• [SF.jp]Jiemamy...
• [SF.jp]gae-j-samples...GAE/J GAE/J/Maven
• [SF.jp]gaejtools...GAE/J
• [SF.jp]asclipse...Amateras AIR GEAR AS3
• java-ja, wicket-ja,
(http://www.topgate.co.jp)
GoogleAppEngine GoogleApps
8. High Low ( )
JDO API
shin1ogawa Low-Level API
API
9. High Low ( )
•
•
• ApiProxy#Delegate makeSyncCall()
Low-Level API
• Low-Level API
Low-Level API
10. Low-Level API
Low-Level API API …
Javadoc
API
…
shin1ogawa
https://sites.google.com/a/topgate.co.jp/systemsolution/Home/
googleappengine/datastore-lowlevelapi
12. Low-Level API
1. XXXService#hogeMethod()
2.XXXService ApiProxy#getDelegate()
Delegate
3.XXXService
ProtocolBuffer (byte[]
) Delgate#makeSyncCall(
String serviceName, String methodName,
byte[] request)
14. Low-Level API
7. ProtocolBuffer
byte[] response
Delegate
8.Delegate byte[] response
ProtocolBuffer
XXXService
9.XXXService ProtocolBuffer
Java
17. Datastore Service
• Key, KeyFactory
• Entity
• DatastoreService
• Transaction
• Query
• FilterOperator/SortDirection
• PreparedQuery
• FetchOptions
18. Datastore Service
Key
Key ApplicationID, Kind,
Entity Key(ancestorKey )
RootEntity Key Entity Key
Key EntityGroup
Key Owned Unonwed
EntityGroup
Key EntityGroup
21. Datastore Service
Key
Parent(1) prop1 prop2
Parent(1)/Child(1) prop1 prop2
Parent(1)/Child(2) prop1 prop2
Parent(1)/Child(3) prop1 prop2
Key EntityGroup
22. Datastore Service
Key
Parent(1) prop1 prop2
Parent(1)/Parent(2) prop3 prop4
Parent(1)/Parent(3) prop1 prop3
Parent(1)/Parent(4) prop2 prop4
Kind EntityGroup
23. Datastore Service
Key, KeyFactory
• Key#getKind() Kind
• Key#getParent() Entity Key
•
• JDO ”encoded-pkey”
• KeyFactory#keyToString(Key) Key
• KeyFactory#stringToKey(String)
24. Datastore Service
Key, KeyFactory
• KeyFactory#createKey(String kind, Stirng name)
• Key#getName() String name
• 1.2.5 name
• Key
• 1.2.5 Key
• Key#getId() Long id
• id Kind
26. Datastore Service
Entity
• Map =
• setProperty(String , Obejct )
• setUnindexedProperty( , )
• setUnindexedProperty(“p”, String|
Long|...)
index
JDO Entity
"
29. Datastore Service
Entity
•
• new Entity(kind )
• new Entity(kind , Key)
• new Entity(kind , key name )
• new Entity(kind , key name , Key)
• new Entity(Key primaryKey) ... 1.2.5
•
• KEY_RESERVED_PROPERTY ... “__key__”
• PrimaryKey
30. Datastore Service
DatastoreService
• Factory
• DatastoreServiceFactory#getDatastoreService()
• Key Entity
• Entity get([Transaction tx,] Key key)
• Map<Key, Entity> get(
[Transaction tx,] Iterable<Key> key)
• Key
EntityNotFoundException
• Query Entity
• " (Query )
31. Datastore Service
DatastoreService
• Entity
• Key put([Transaction tx,] Entity entity)
• List<Key> put(
[Transaction tx], Iterable<Entity> entity)
• Kind …
Entity … k
• Entity
• delete([Transaction tx,] Key... keys)
• delete([Transaction tx,] Iterable<Key> keys)
• Key
32. Datastore Service
DatastoreService
• Key ...1.2.5
• KeyRange allocateIds(
[Key parentKey,] String kind, long num)
• KeyRange
• Key getStart()
• Key getEnd()
• Iterator<Key> iterator()
• Transaction
• Transaction beginTransaction()
• Transcation getCurrentTransaction()
•
33. Datastore Service
(Entity )
KeyRange parentKeys =
service.allocateIds("Parent", 1);
Key parentKey =
parentKeys.getStart();
KeyRange childKeys =
service.allocateIds(parentKey, "Child", 2);
Iterator<Key> childKeysIterator =
childKeys.iterator();
Entity parent = new Entity(parentKey);
Entity child1 = new Entity(childKeys.next());
Entity child2 = new Entity(childKeys.next());
34. Datastore Service
( )
Transaction tx = service.beginTransaction();
try {
List<Entity> entities =
Arrays.asList(parent, child1, child2
service.put(tx, entities);
tx.commit();
} finally {
if (tx.isActive()) { tx.rollback(); }
}
35. Datastore Service
Query
• new Query(String kind)
• JDO
• new Query(String kind, Key ancestorKey)
• Kind
• new Query(Key ancestorKey)
• Kind
•
36. Datastore Service
Query
•
• Query addFilter(String propertyName,
FilterOperator operator, Object value)
•
• Query addSort(String propertyName,
SortDirection direction)
•
• Query setKeysOnly()
• Key
Entity.KEY_RESERVED_PROPERTY
38. Datastore Service
PreparedQuery
•
• List<Entity> asList(FetchOptions)
•
• Iterator<Entity> asIterator([FetchOptions])
•
• Iterable<Entity> asIterable([FetchOptions])
• iterator() Datastore
asIterator()
• FetchOptions
• offset, limit
39. Datastore Service
Query
Query query = new Query(“Entity”);
query.addFilter(“name”,
FilterOperator.GREATER_THAN_OR_EQUAL, “hoge”)
.addSort("name", SortDirection.ASCENDING)
.addSort("__key__", SortDirection.ASCENDING);
DatastoreService service =
DatastoreServiceFactory.getDatastoreService();
List<Entity> entities =
service.prepare(query).asList(
FetchOptions.Builder.withOffset(0).limit(100));
40. Datastore Service
Query : EntityGroup
Iterator<Entity> entityGroup =
service.prepare(new Query(rootKey)
.addSort(“__key__”)).asIterator(
FetchOptions.Builder.withOffset(0).limit(100));
Parent parent = null;
while (entityGroup.hasNext) {
Entity entity = entityGroup.next();
if (entity.getKind().equals(“Parent”))
parent = EntityUtil.toBean(entity,Parent.class);
else if (entity.getKind().equals(“Child”))
parent.getChidren().add(
EntityUtil.toBean(entity, Child.class);
...
41. Datastore Service
JDO
JDO
class MyEntity {
List<Child> children1;
List<Child> children2;
}
myEntity.getChildren1().addAll(childA, childB);
myEntity.getChildren2().addAll(childC, childD);
manager.makePersistent(myEntity);
List
42. Datastore Service
JDO
MyEntity myEntity =
manager.getObjectById(MyEntity.class, key);
List<Child> children1 =
myEntity.getChildren1();
List<Child> children2 =
myEntity.getChildren2();
children1 childA childB
childC childD children2 null
43. Datastore Service
JDO
MyEntiy(1) Child
(A-D)
MyEntiy(1)/Child(A)
MyEntiy(1)/Child(B)
MyEntiy(1)/Child(C)
MyEntiy(1)/Child(D)
45. AppEngine
AppEngine
ApiProxy#getDelegate()
Delegate
Web Delegate
Web
ApiProxy
#getCurrentEnvironment()
Environment
(ApplicationID, versionID )
46. AppEngine
JUnit SDK Web
ApiProy
• Environment
ApiProxy#setEnvironmentForCurrentThread()
• Delegate ApiProxyLocalImpl
ApiProxy#setDelegate()
50. Delegate
class MyDelegate implements Delegate {
Delegate original = ApiProxy.getDelegate();
public byte[] makeSyncCall(
Environment environment,
String service, String method,
byte[] request) throws ApiProxyException {
System.out.println(service+”:”+method);
return original.makeSyncCall(
environment, service, method, requst);
}
}
54. 2
makeSyncCall()
• datastore_v3#GET
• : DatastorePb.GetRequest
• new GetRequest().mergeFrom(byte[])
• GetRequest#keyIterator() Key
Iterator
• : DatastorePb.GetResponse
• new GetResponse().mergeFrom(byte[])
• GetResponse#entityIterator()
Entity Iterator
55. 2
makeSyncCall()
• Delegate
…
• datastore_v3#GET
• memcache#GET datastore
mamcache#GET
• datastore_v3#PUT/DELETE
• memcache#DELETE datastore
59. 2
...!!!
• Datastore
RemoteApiDelegate RemoteApiServlet
• Eclipse Plug-in
• CUI Memcache stats