2009年4月11日星期六

Google AppEngine for java 的JDO测试

研究了一下dataStore文档,并作了一个简单的留言板,没有用DEMO里的那个,自己做的更方便修改,省时间。

有几个心得:

  • 使用eclipse的google插件可以节省很多必要的麻烦,我在前文中已有说明。
  • google的dataStore分为标准api和low api(底层api),标准api分为jdo和jpa两种。底层API看上去比较复杂,文档也少(实际上只有javadoc),而比较jdo和jpa,虽然后者比较新,但显示google对jdo的支持更全面,从文档和DEMO可以看出。
  • 按google的建议,先创建一个PMF工厂,然后使用Query对象构造查询,删除和新增经测试问题都不大,关键是查询和index。
感觉最麻烦的就是索引(index)处理了,与SQL相比,这一部分比较不太一样。eclipse在build项目时会自动处理Query产生的index,并在WEB-INF/appengine-generated/下生成datastore-indexes-auto.xml配置文件,如果有数据更新,还会生成local_db.bin,这实际上就是数据保存的文件了,删除这个文件数据就消失了,这个文件不会上传到googleApp,所以本地调试时生成的数据是不会在上传时与googleApp上的数据同步的。

如果在Query中加入了条件where或order by 语句,都有可能会自动生成index,这时datastore-indexes-auto.xml配置文件生成类似下面的代码:

<datastore-indexes>

<!-- Used 7 times in query history -->
<datastore-index kind="Msg" ancestor="false" source="auto">
<property name="author" direction="asc">
<property name="__key__" direction="desc">
</property>

</property>

这个__key__可能就是主键。
后来发现在改了几下条件后就出现500错误了,再次看了文档,原来eclipse的自动生成index的配置文件是有局限性的,另一些需要手动在datastore-indexes.xml中进行配置,条件如下:

可自动生成的:
  • queries using only equality and ancestor filters 使用的where条件必须是等于条件(=)或是原始的(?这个我理解不到位)。
  • queries using only inequality filters (which can only be of a single property) 如果使用不等的条件(<, <=, >=, >),则只能针对一个字段,也就是非等于条件的where语句,涉及的条件对象只能是一个,比如 age> 10 and age <20>10 and level >0 ;
  • queries with only one sort order on a property, either ascending or descending 在order by 排序语句中只能有一个字段,不是asc的就是desc的。比如 order by age asc是可以的,order by age asc ,id desc 是不行的。
不可自动生成,需要datastore-indexes.xml中进行配置的:
  • queries with multiple sort orders 多重的order by 条件
  • queries with a sort order on keys in descending order 在主键上按倒序排,即order by id desc
  • queries with one or more inequality filters on a property and one or more equality filters over other properties 在一个字段上存在一个或多个不等的where条件(<, <=, >=, >),而且有一个或多个相等条件在其他的字段上
  • queries with inequality filters and ancestor filters 同时存在一个不等条件和ancestor filter条件(这个ancestor filter到底是什么?)
而在Query的写法中,也有很多地方与sql不同,前者有更多的限制:
详情请见:http://code.google.com/intl/en/appengine/docs/java/datastore/queriesandindexes.html#Restrictions_on_Queries

我就不再详细翻译了,有空再进一步说明。

相关主题:

没有评论:

发表评论