|
This is a combined introduction to Objectify and to the Appengine datastore. So, you want to persist some data. You've probably looked at the datastore documentation and thought "crap, that's complicated!" Entities, query languages, fetch groups, detaching, transactions... hell those things have bloody lifecycles! However, the complexity of JDO is hiding a lot of simplicity under the covers. The first thing you should do is set aside all your preconceived notions about relational databases. The GAE datastore is not an RDBMS. In fact, it acts much more like a HashMap that gives you the additional ability to index and query for values. When you think of the datastore, imagine a persistent HashMap. EntitiesThis document will talk a lot about entities. An entity is an object's worth of data in the datastore. Using Objectify, an entity will correspond to a single POJO class you define. In the datastore, an entity is a HashMap-like object of type Entity. Conceptually, they are the same. Since the datastore is conceptually a HashMap of keys to entities, and an entity is conceptually a HashMap of name/value pairs, your mental model of the datastore should be a HashMap of HashMaps! OperationsThere are only four basic operations in the datastore, and any persistence API must boil operations down to:
KeysAll entities have either a Long id or a String name, but those values are not unique by themselves. In the datastore, entities are identified by the id (or name) and a kind, which corresponds to the type of object you are storing. So, to get Car #959 from the datastore, you need to call something equivalent to get_from_datastore("Car", 959) (not real code yet). By the way, I lied. There is actually a third value which is necessary to uniquely identify an entity, called parent. Parent defines a special kind of relationship, placing the child in the same entity group as the parent. Entity groups will be discussed next in the section on Transactions, but for now what you need to know is that this parent (which is often simply null, creating an unparented, root entity) is also required to uniquely identify an entity. So, to get Car #959 from the datastore, you actually need to call something equivalent to get_from_datastore("Car", 959, null) or get_from_datastore("Car", 959, theOwner). Yeech. Instead of passing three parameters around all the time, the datastore wraps these values into a single object - a Key. That's all a Key is, a holder for the three parts that uniquely identify an entity. The native Datastore Key class is simple and untyped, like the native Entity class. Objectify provides a generified Key that carries type information: Key<Car> rootKey = new Key<Car>(Car.class, 959); Key<Car> keyWithParent = new Key<Car>(parent, Car.class, 959); With a Key you can use the most fundamental method on the Objectify interface, nearly identical to the DatastoreService equivalent. If the generics are confusing, hold on - there will be examples later. <T> T get(Key<? extends T> key) throws EntityNotFoundException; In Objectify, you define your object as a Java class with a mandatory identifier (Long, long, or String) and an optional parent. However, when you look up or reference your object, you do so by Key. In fact, you can (and should) batch together a variety of requests into a single call, even if it will fetch many different kinds of objects: Map<Key<Object>, Object> lotsOfThings = objectify.get(carKey, airplaneKey, chairKey, personKey, yourMamaKey); Actually, I lied again. We don't force you to create keys by hand all the time. There is a convenient shorthand for the very common case of loading a single type of object, but don't forget that this is really just creating an Key and calling get()! Car c = objectify.get(Car.class, 959); Map<Long, Car> cars = objectify.get(Car.class, 959, 911, 944, 924); By the way, Key is used for relationship references as well. Remember that value that defines a parent entity? The type of this parent is Key: public Key(Key<?> parent, Class<? extends T> kind, long id) When you create relationships to other entities in your system, the type of the entity relationship should be Key. TransactionsThe datastore has a lot of odd concepts designed to facilitate building a JTA interface - thread local transactions, implicit transaction management policies, and methods that behave differently whether you pass them a transaction or not. Forget all that. Here's what you need to know: Entity GroupsWhen you put() your entity, it gets stored somewhere in a gigantic farm of thousands of machines. In order to perform an atomic transaction, the datastore (currently) requires that all the data that is a part of that transaction live on the same server. To give you some control over where your data is stored, the datastore has the concept of an entity group. Remember the parent that is part of a Key? If an entity has a parent, it belongs to the same entity group as its parent. If an entity does not have a parent, it is the "root" of an entity group, and may be physically located anywhere in the cluster. Within a transaction, you can only access data from a single entity group. If you try to access data from multiple entity groups, you will get an Exception. This means you must pick your entity groups carefully, usually to correspond to the data associated with a single user. Yes, this severely limits the utility of transactions. Why not store all your data with a common parent, putting it all in a single entity group? You can, but it's a bad idea. Google limits the number of requests per second that can be served by a particular entity group. It should be worth mentioning that the term parent is somewhat misleading. There is no "cascading delete" in the datastore; if you delete the parent entity it will NOT delete the child entities. For that matter, you can create child entites with a parent Key (or any other key as a member field) that points to a nonexistant entity! Parent is only important in that it defines entity groups; if you do not need transactions across several entities, you may wish to use a normal nonparent key relationship - even if the entities have a conceptual parent-child relationship. Executing TransactionsWhen you execute a get(), put(), delete(), or query(), you will either be in a transaction or you will not. If you execute within a transaction:
If you execute without a transaction:
IndexesWhen using a traditional RDBMS, you become accustomed to issuing any ad-hoc SQL query you want and letting the query planner figure out how to obtain the result. It may take twelve hours to linear scan five tables in the database and sort the 8 gigabyte result set in RAM, but eventually you get your result! The appengine datastore does NOT work this way. Appengine only allows you to run efficient queries. The exact meaning of this limitation is somewhat arbitrary and changes as Google rolls out more powerful versions of the query planner, but generally this means:
The datastore query planner really only likes one operation: Find an index and walk it in-order. This means that for any query you perform, the datastore must already contain properly ordered index on the field or fields you want to filter by! And since appengine doesn't do joins, queries are limited to what you can stuff into a single index -- you can't, for example, filter by X > 5 and then sort by Y. Actually, it's not quite true that appengine won't do joins. It will do one kind of join - a "zig-zag" merge join which lets you perform equality filters on multiple separate properties. But this is still an efficient query - it walks each of the property indexes in order without buffering chunks of data in RAM. What you should be getting out of this is that if you want queries, you need indexes tailored to the queries you want to run. To make this easier, the datastore has an innate ability to store each and every (single) property as "indexed" or "unindexed" (Entity.setProperty() vs Entity.setUnindexedProperty(). This allows you to easily issue a queries based on single properties. By default, Objectify defaults to setting all properties as indexed unless you flag the field (or class) with an @Unindexed annotation. To run queries by filtering or sorting against multiple properties (that is, if it can't be satisfied by a zigzag merge on single-property indexes), you must create a multi-value index in your datastore-indexes.xml. There is a great deal written on this subject; we recommend How Entities and Indexes are Stored and Index Building. Note that there are some tricks to creating indexes:
Now that you are familiar with the underlying concepts of the datastore, read the IntroductionToObjectify. | |
Nice, although you wrote this to describe Objectify, it is also one of the most concise explanation of appengine datastore itself I've ever read. Thank you.
Great article. This project looks extremely interesting, from a beginner point of view. Thanks!
Good implementation is nothing without good documentation. Great work, thanks.
This is an excellent primer. Thanks a lot!
Excellent introduction. I've read several other introductions to the datastore, but this no-nonsense, here's-what-you-really-need-to-know approach really cut through the noise for me. I'll look forward to using Objectify. Thanks much.
Great article! I've been using JDO for the past 3 months on my app engine project and I'm fed up with it. I'm eager to try out objectify :)
Great Article. Thanks.
Thanks for piercing the appengine confusion cloud. I've got a GWT/JDO app working but it was not a pleasant experience even though my needs are relatively simple. JDO may be useful for porting massive apps, but for my new app, the datastore needs are simple. After reading your article, I'm looking forward to reviewing your new approach with my app in mind and possibly rewriting the backend of my app completely.
As max said, great piece of documentation even for GAE's Datastore itself.
Very good primer for beginners.......
This is a really nice article, from developers to developers. Thank you very much!
A+ on the documentation. I wish all tool providers did the same.
Simple and nice article !!
To paraphrase John Gierach, "Writing is the art of re-doing a paragraph over and over until it reads as though it were written once, fluidly, from start to finish." Sir, your style of writing is perfectly "impedance matched" to my brain. What a pleasant read.
Really like objectify, great work. One quick note though: while it may be obvious to experienced developers, it took me a little digging to work out how to install (e.g., just copy the jar, add to build path) as well as find some tips about disabling the data nucleus enhancer for IDE performance improvements. While this info may be available somewhere on this site, I didn't find it. Adding an "installation" section as the first item in the wiki would be helpful for new developers.
Woah, best article about the DataStore? I have read so far, congratulations ! I arrived here after reading about the DataStore? on the official documentation. I hope Objectify will be pleasant to use. But I have a question about the indexes. What about indexing everything, every property of every entities ? It should be more convenient to do queries but what is the counterpart of it ?
The downside of maintaining lots of indexes is the expense. It costs in both cpu time and storage. Read about Partial Indexes in the IntroductionToObjectify.
Would you please put some light on relationships Like how do I store a list of child in the datastore. Or If my POJO contains a list of another POJO what will happen Sorry if the question is too much childish..
Thanks.... Clear Concepts in clear words..
Good job!! Very interesting
Finaly a good description of the datastore ! I was crying for it !
Great Job dude ....really was in a Bad need for it
I have a doubt here. If i use the below piece of code twice in the same program and use put. Key<Car> rootKey = new Key<Car>(Car.class, 959); What happens at the datastore side? My understanding is that it creates two root entities. If it is so , how does the get query understand which value to retrieve ?? May be a bit naive or my complete understanding might be wrong.. Can you please clarify??
Creating a Key does not create anything in appengine. It's just part of your code.
I actually create entity and use put after it as explained above. Sorry for the confusion. But just for example i take this code from google simple app. Key guestbookKey = KeyFactory?.createKey("Guestbook", "default"); Entity greeting = new Entity("Greeting", guestbookKey); datastore.put(greeting); Every time i execute this piece of code what exactly happens.
my understanding is. It creates a key which is for Entity Greeting kind.That key can be used every time to insert Greeting Entity under the same root.Please correct me. Hopefully you understand its the key to understand Datastore concepts.Thanks
Thank you for a great document. Is this sentence in this document correct or should it be slightly changed to be more accurate? "And since appengine doesn't do joins, queries are limited to what you can stuff into a single index -- you can't filter by one property and then sort by a different one."
/Ricky
Corrected - thanks!
This explains datastore api concepts better than original google documentation!! Thanks