Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
appengine
java night #1
               Low-Level API
  # Low-Level API

 shin1ogawa@
...shin1ogawa
Google, Java, AppEngine, Eclipse,
Wicket, Maven, Hudson,        ...
•               OSS

    •   [SF.jp]Jiem...
1.Low-Level API
 1.1.
 1.2.
 1.3.DatastoreService
2.
 2.1.AppEngine
3.
Low-Level API
High-Level API(         )
               )




       API(    )>>>>>       (   )
                        API




   Datast...
Low-Level API

AppEngine
                  2
                             JDO/JPA
                                       L...
Low-Level API


AppEngine


                      Low-Level API




Framework


            ※   LowLevelAPI           …
High           Low        (         )

   JDO           API


  shin1ogawa           Low-Level API
                       ...
High                Low            (       )
•
•




•            ApiProxy#Delegate    makeSyncCall()
                  Lo...
Low-Level API

Low-Level API               API                      …


                                       Javadoc


 ...
Low-Level API
Low-Level API
1.                XXXService#hogeMethod()


2.XXXService         ApiProxy#getDelegate()
 Delegate
3.XXXServi...
Low-Level API
4.Delegate        serviceName,
 methodName


5.                 (ProtocolBuffer
              )   byte[] req...
Low-Level API
7.                           ProtocolBuffer
                        byte[] response
     Delegate
8.Delegate...
Low-Level API

1.DatastoreService#put(Entity entity)
 1.        entity
  DatastorePb.PutRequest requestPb
 2.byte[] ApiPro...
Low-Level API
DatastoreService
Datastore Service

• Key, KeyFactory
• Entity
• DatastoreService
 • Transaction
• Query
 • FilterOperator/SortDirection
• ...
Datastore Service
Key
               Key       ApplicationID, Kind,
Entity      Key(ancestorKey        )
            RootE...
Datastore Service
Key

 Parent(1)       prop1      prop2      children




                                       }
      ...
Datastore Service
Key

 Parent(1)       prop1      prop2      children




                                       }
      ...
Datastore Service
Key

 Parent(1)             prop1   prop2

 Parent(1)/Child(1)    prop1   prop2

 Parent(1)/Child(2)    ...
Datastore Service
Key

 Parent(1)              prop1   prop2

 Parent(1)/Parent(2)    prop3   prop4

 Parent(1)/Parent(3) ...
Datastore Service
Key, KeyFactory
• Key#getKind()    Kind
• Key#getParent()      Entity   Key
•
    • JDO    ”encoded-pkey...
Datastore Service
Key, KeyFactory
• KeyFactory#createKey(String kind, Stirng name)


 • Key#getName()              String ...
Datastore Service
Entity
•
• JDO/JPA         Pojo
• Entity#getKey()
• getKey()
    • Key Entity#getParent()
    • String E...
Datastore Service
Entity
•            Map              =
    • setProperty(String          , Obejct   )
    • setUnindexed...
Datastore Service
Entity
• getProperty(String         )
    • Object                 "      cast
    •


•
    • Integer  ...
Datastore Service
Entity
• hasProperty(String              )
    •
    •                null   (viewer   ”<null>”)
      •...
Datastore Service
Entity
•
    • new Entity(kind   )
    • new Entity(kind   ,   Key)
    • new Entity(kind   , key      n...
Datastore Service
DatastoreService
•                       Factory
 • DatastoreServiceFactory#getDatastoreService()
• Key ...
Datastore Service
DatastoreService
• Entity
 • Key put([Transaction tx,] Entity entity)
 • List<Key> put(
     [Transactio...
Datastore Service
DatastoreService
•            Key                   ...1.2.5
   • KeyRange allocateIds(
       [Key pare...
Datastore Service
                 (Entity           )
KeyRange parentKeys =
  service.allocateIds("Parent", 1);
Key paren...
Datastore Service
                (      )

Transaction tx = service.beginTransaction();
try {
  List<Entity> entities =
 ...
Datastore Service
Query
• new Query(String kind)
  • JDO
• new Query(String kind, Key ancestorKey)
  •       Kind


• new ...
Datastore Service
Query
•
    • Query addFilter(String propertyName,
        FilterOperator operator, Object value)
•
    ...
Datastore Service
PreparedQuery
• DatastoreService#prepare()       Query
                  PreaparedQuery
•
    • int coun...
Datastore Service
PreparedQuery
•
    • List<Entity> asList(FetchOptions)
      •
    • Iterator<Entity> asIterator([Fetch...
Datastore Service
Query
Query query = new Query(“Entity”);
query.addFilter(“name”,
  FilterOperator.GREATER_THAN_OR_EQUAL,...
Datastore Service
Query              : EntityGroup
Iterator<Entity> entityGroup =
  service.prepare(new Query(rootKey)
   ...
Datastore Service
          JDO
JDO

class MyEntity {
  List<Child> children1;
  List<Child> children2;
}
myEntity.getChil...
Datastore Service
            JDO
MyEntity myEntity =
  manager.getObjectById(MyEntity.class, key);
List<Child> children1 ...
Datastore Service
       JDO

MyEntiy(1)                    Child
                      (A-D)
MyEntiy(1)/Child(A)

MyEntiy...
AppEngine
                          AppEngine

        ApiProxy#getDelegate()
            Delegate
      Web          Dele...
AppEngine
JUnit    SDK    Web
                       ApiProy


• Environment
 ApiProxy#setEnvironmentForCurrentThread()


...
Environment
•
    •          SingleThread


        •


    • ApplicationID, versionID,
ApiProxyLocalImpl(Delegate                    )
• Delegate


    Delegate
•


    •                     WEB-INF/queue.xml
...
Delegate
• ApiProxyLocalImpl


      Delegate
 •         …ApiProxyLocalImpl


 • ApiProxy#getDelegate()       ApiProxy
   ...
Delegate
class MyDelegate implements Delegate {
  Delegate original = ApiProxy.getDelegate();
  public byte[] makeSyncCall...
1
1
makeSyncCall()

byte[] Delegate#makeSyncCall(
    Environment environment,
    String service,
    String method,
    by...
2
makeSyncCall()

byte[] Delegate#makeSyncCall(
    Environment environment,
    String service,
    String method,
    by...
2
makeSyncCall()
•            datastore_v3#GET
    •        : DatastorePb.GetRequest
        • new GetRequest().mergeFrom(...
2
makeSyncCall()
•                               Delegate
         …
    • datastore_v3#GET
     • memcache#GET           ...
2
2
com.google.apphosting.utils.remot
eapi.RemoteApiServlet
• Google            admin
• HttpServlet            GET    POST
 ...
2
com.google.apphosting.utils.remot
eapi.RemoteApiPb

• RemoteApiServlet      API   POST
      ProtocolBuffer
 • Request
 ...
2
                    ...!!!

•               Datastore
    RemoteApiDelegate       RemoteApiServlet


• Eclipse Plug-in

...
shin1ogawa@
appengine java night #1
Upcoming SlideShare
Loading in …5
×

appengine java night #1

4,394 views

Published on

appengine java night #1

  1. 1. appengine java night #1 Low-Level API # Low-Level API shin1ogawa@
  2. 2. ...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
  3. 3. 1.Low-Level API 1.1. 1.2. 1.3.DatastoreService 2. 2.1.AppEngine 3.
  4. 4. Low-Level API
  5. 5. High-Level API( ) ) API( )>>>>> ( ) API Datastore
  6. 6. Low-Level API AppEngine 2 JDO/JPA Low- Level API Low-Level API( API)
  7. 7. Low-Level API AppEngine Low-Level API Framework ※ LowLevelAPI …
  8. 8. High Low ( ) JDO API shin1ogawa Low-Level API API
  9. 9. High Low ( ) • • • ApiProxy#Delegate makeSyncCall() Low-Level API • Low-Level API Low-Level API
  10. 10. Low-Level API Low-Level API API … Javadoc API … shin1ogawa https://sites.google.com/a/topgate.co.jp/systemsolution/Home/ googleappengine/datastore-lowlevelapi
  11. 11. Low-Level API
  12. 12. Low-Level API 1. XXXService#hogeMethod() 2.XXXService ApiProxy#getDelegate() Delegate 3.XXXService ProtocolBuffer (byte[] ) Delgate#makeSyncCall( String serviceName, String methodName, byte[] request)
  13. 13. Low-Level API 4.Delegate serviceName, methodName 5. (ProtocolBuffer ) byte[] request 6. (?)
  14. 14. Low-Level API 7. ProtocolBuffer byte[] response Delegate 8.Delegate byte[] response ProtocolBuffer XXXService 9.XXXService ProtocolBuffer Java
  15. 15. Low-Level API 1.DatastoreService#put(Entity entity) 1. entity DatastorePb.PutRequest requestPb 2.byte[] ApiProxy#getDelegate( “datastore_v3”, “PUT”, requestPb.toByteArray()) 3. byte DatastorePb.PutResponse responsePb 4.responsePb.getKey(0)
  16. 16. Low-Level API DatastoreService
  17. 17. Datastore Service • Key, KeyFactory • Entity • DatastoreService • Transaction • Query • FilterOperator/SortDirection • PreparedQuery • FetchOptions
  18. 18. Datastore Service Key Key ApplicationID, Kind, Entity Key(ancestorKey ) RootEntity Key Entity Key Key EntityGroup Key Owned Unonwed EntityGroup Key EntityGroup
  19. 19. Datastore Service Key Parent(1) prop1 prop2 children } Child(1) prop1 prop2 Child(2) prop1 prop2 Child(3) prop1 prop2
  20. 20. Datastore Service Key Parent(1) prop1 prop2 children } Child(1) prop1 prop2 Child(2) prop1 prop2 Child(3) prop1 prop2
  21. 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. 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. 23. Datastore Service Key, KeyFactory • Key#getKind() Kind • Key#getParent() Entity Key • • JDO ”encoded-pkey” • KeyFactory#keyToString(Key) Key • KeyFactory#stringToKey(String)
  24. 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
  25. 25. Datastore Service Entity • • JDO/JPA Pojo • Entity#getKey() • getKey() • Key Entity#getParent() • String Entity#getKind()
  26. 26. Datastore Service Entity • Map = • setProperty(String , Obejct ) • setUnindexedProperty( , ) • setUnindexedProperty(“p”, String| Long|...) index JDO Entity "
  27. 27. Datastore Service Entity • getProperty(String ) • Object " cast • • • Integer " Long / • Set " ArrayList /
  28. 28. Datastore Service Entity • hasProperty(String ) • • null (viewer ”<null>”) • getProperty() " null • hasProperty() " true • (viewer ”<missing>”) • getProperty() " null • hasProperty() " false •
  29. 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. 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. 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. 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. 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. 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. 35. Datastore Service Query • new Query(String kind) • JDO • new Query(String kind, Key ancestorKey) • Kind • new Query(Key ancestorKey) • Kind •
  36. 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
  37. 37. Datastore Service PreparedQuery • DatastoreService#prepare() Query PreaparedQuery • • int countEntities() • • Entity asSingleResult() • null • Entity TooManyResultsException
  38. 38. Datastore Service PreparedQuery • • List<Entity> asList(FetchOptions) • • Iterator<Entity> asIterator([FetchOptions]) • • Iterable<Entity> asIterable([FetchOptions]) • iterator() Datastore asIterator() • FetchOptions • offset, limit
  39. 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. 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. 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. 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. 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)
  44. 44. AppEngine AppEngine ApiProxy#getDelegate() Delegate Web Delegate Web ApiProxy #getCurrentEnvironment() Environment (ApplicationID, versionID )
  45. 45. AppEngine JUnit SDK Web ApiProy • Environment ApiProxy#setEnvironmentForCurrentThread() • Delegate ApiProxyLocalImpl ApiProxy#setDelegate()
  46. 46. Environment • • SingleThread • • ApplicationID, versionID,
  47. 47. ApiProxyLocalImpl(Delegate ) • Delegate Delegate • • WEB-INF/queue.xml datastore-indexes.xml
  48. 48. Delegate • ApiProxyLocalImpl Delegate • …ApiProxyLocalImpl • ApiProxy#getDelegate() ApiProxy Delegate ApiProxy AOP
  49. 49. 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); } }
  50. 50. 1
  51. 51. 1 makeSyncCall() byte[] Delegate#makeSyncCall( Environment environment, String service, String method, byte[] request) throws ApiProxyException
  52. 52. 2 makeSyncCall() byte[] Delegate#makeSyncCall( Environment environment, String service, String method, byte[] request) throws ApiProxyException
  53. 53. 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
  54. 54. 2 makeSyncCall() • Delegate … • datastore_v3#GET • memcache#GET datastore mamcache#GET • datastore_v3#PUT/DELETE • memcache#DELETE datastore
  55. 55. 2
  56. 56. 2 com.google.apphosting.utils.remot eapi.RemoteApiServlet • Google admin • HttpServlet GET POST • GET yaml • API POST • POST Http “X-appcfg-api- version”
  57. 57. 2 com.google.apphosting.utils.remot eapi.RemoteApiPb • RemoteApiServlet API POST ProtocolBuffer • Request • TransactionRequest • Response • ApplicationError
  58. 58. 2 ...!!! • Datastore RemoteApiDelegate RemoteApiServlet • Eclipse Plug-in • CUI Memcache stats
  59. 59. shin1ogawa@

×