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 #2a

2,832 views

Published on

Published in: Technology
  • Be the first to comment

Appengine Java Night #2a

  1. 1. appengine java night #2 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. Low-Level API #1
  4. 4. ) Key • Key EntityGroup • Entity EntityGroup … • Key • ApplicationID • Entity Key, Kind • name or long •
  5. 5. ) KeyFactory • createKey() Key • keyToString() Key • stringToKey() Key
  6. 6. ) Entity • setProperty(“ ”, Object ) • Object getProperty(“ ”) • boolean hasProperty(“ ”) • <missing> • • HashSet Collection ArrayList • int Long…
  7. 7. ) DatastoreService • • Key put(Entity) • List<Key> put(Iterable<Entity>) • • void delete(Key... keys) • void delete(Iterable<Key>) • Transaction
  8. 8. ) DatastoreService • • Entity get(Key key) • Key EntityNotFoundException • Map<Key, Entity> get( Iterable<Key> key) • Key Map
  9. 9. ) DatastoreService • Transaction , • beginTransaction() • getCurrentTransaction() • ... • Transaction • commit() • rollback() • isActive()
  10. 10. ) DatastoreService • 1.2.5 Key Entity Key 1.2.5 Key • KeyRange allocalteIds( [ Key], Kind, ) • KeyRange • getStart(), getEnd(), iterator()
  11. 11. 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());
  12. 12. 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(); } }
  13. 13. Low-Level API #2
  14. 14. Datastore Service Query • new Query(String kind) • JDO • new Query(String kind, Key ancestorKey) • Kind • new Query(Key ancestorKey) • Kind •
  15. 15. Datastore Service Query • • Query addFilter(String propertyName, FilterOperator operator, Object value) • • Query addSort(String propertyName, SortDirection direction) • • Query setKeysOnly() • Key Entity.KEY_RESERVED_PROPERTY
  16. 16. Datastore Service PreparedQuery • DatastoreService#prepare() Query PreaparedQuery • • int countEntities() • • Entity asSingleResult() • null • Entity TooManyResultsException
  17. 17. Datastore Service PreparedQuery • • List<Entity> asList(FetchOptions) • • Iterator<Entity> asIterator([FetchOptions]) • • Iterable<Entity> asIterable([FetchOptions]) • iterator() Datastore asIterator() • FetchOptions • offset, limit
  18. 18. 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));
  19. 19. 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); ...
  20. 20. 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
  21. 21. 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
  22. 22. Datastore Service JDO MyEntiy(1) Child (A-D) MyEntiy(1)/Child(A) MyEntiy(1)/Child(B) MyEntiy(1)/Child(C) MyEntiy(1)/Child(D)
  23. 23. shin1ogawa@

×