Hello,
These are great questions. The implementation of views in the core of
TouchDB relies on native code for that platform. This means that they
are provided to the TouchDB Server at runtime and give a name and
version number. This is in contrast to CouchDB where they are written
in javascript and stored in design documents. One important thing to
note though is that in both cases, the result of the view (the index
that is built) is stored. This means that if you don't change the
definition of your view (signaled to the server through the name and
version) it need not be recreated, even between application restarts.
Also, indirectly related to your question I should mention we have a
Javascript module which adds the capability to run Javascript
map/reduce views. However, this will add about 1MB to your
application.
I'll address the specific questions you have inline below:
CouchDbRepositorySupport - You can use a subset of this functionality.
(I haven't used it, but we have some successful reports of it
working) The subset of functionality that does not work yet revolves
around the designDocumentFacotry. This is what Ektorp uses to
automatically generate map/reduce views to access the data. We'd like
to add support for this in the future. Here is a related issue we're
tracking
https://github.com/couchbaselabs/TouchDB-Android/issues/17
ViewQuery is still used. GrocerySync has an example of how to use it.
The key thing here is that you have to register your view first using
a name that simulates a design document name. So in GrocerySync we
name the view "grocery-local/byDate". Then when we access the view we
use:
new ViewQuery().designDocId("grocery-local").viewName("byDate").descending(true)
The @Views annotation which allows you to define views with in-line
javascript is not supported at this time.
> 2. Or do i have to use TDView class to create and interact with a view? As
> taken from the GrocerySync example.
>
> TDView view = db.getViewNamed(String.format("%s/%s", dDocName,
> byDateViewName));
> view.setMapReduceBlocks(new TDViewMapBlock() {
>
> @Override
> public void map(Map<String, Object> document, TDViewMapEmitBlock
> emitter) {
> Object createdAt = document.get("created_at");
> if(createdAt != null) {
> emitter.emit(createdAt.toString(), document);
> }
>
> }
> }, null, "1.0");
Yes this is the correct way to register a view in TouchDB-Android.
> Couple of questions related to this is,
> Does this view get created permanently on touchdb?
The results of the map function are persisted. The name and version
number of the view are used to let TouchDB know when it can use the
values its already calculated, and when it has to regenerate the
values.
> If not, how do i make it
> exist permanently (under the assumption if it is permanent that the next
> time it is referred, everything will be faster)
The only thing you need to do is pass the same name and version and
the values will be reused. If you change the definition of the view,
you should bump the version number.
> If this is not the right way to do it, is this the correct way to do it?
>
> Create a class which extends CouchdbDocument called X.
> Then create a class which extends Couchdbrepositorysupport<x> and register a
> view in that.
>
> Thanks in advance
Hopefully things are a bit more clear now. I've tried to update the
wiki with some of these questions, please take a look and feel free to
contribute other improvements there.
https://github.com/couchbaselabs/TouchDB-Android/wiki
marty