I am moving from PHP/MySQL to Google App Engine and using JDO as interface with the datastore. What's the recommended way of migrating a Modified Preorder Tree Traversal (MPTT) enabled table to a JDO model?
-
Why MPTT? A list of ancestors is probably a better option for most situations. – Nick Johnson Sep 05 '11 at 04:11
2 Answers
After spending some time searching on the different ways to implement hierarchical data storage on GAE datastore, I decided to try a direct MPTT implementation. The following code snippet presents the model:
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class MPTTObject {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
private String encodedKey;
@Persistent
private String parentEncodedKey;
@Persistent
private int left;
@Persistent
private int right;
The solution works pretty well if the datastore operations are read intensive, but it's quite "heavy" when the operations are write intensive. This fact in combination with the time limitations within which an operation must be completed in GAE, makes this solution less appealing.
Another approach that seems to be more efficient is to store the full list of parents and children for each node in the hierarchy.
Some useful links on hierarchical data storage on GAE datastore are the following:

- 1
- 1

- 3,115
- 2
- 17
- 26
-
You shouldn't store your keys as encoded strings - store them as `Key` objects, instead. – Nick Johnson Sep 05 '11 at 04:11
-
The reason that I use encoded strings is to be able to move the application outside app engine with the less possible modifications. Key objects are app engine unique, while by using the encode strings I still can exploit the indexing app engine provides. – Thanos Makris Sep 05 '11 at 09:26
-
Using encoded strings, though, you're going to encounter other issues instead - such as porting from master/slave to HRD. – Nick Johnson Sep 05 '11 at 09:27
-
Yes, you 're totally right about it, but I did not consider this limitation in my decision, since I started with HRD directly. However, could you point me any other potential problems caused by the use of encoded strings? (My experience with GAE is only one month :( ) Thanks a lot in advance! – Thanos Makris Sep 05 '11 at 09:33
You can take a look at this JPA implementation of MPTT and apply the same ideas to JDO:

- 2,983
- 3
- 26
- 36