javaspringnosqlcouchdbektorp

In-line View Definitions Couch Db Ektorp


I use Couch DB with Ektorp at Spring 3. I read the document and have tried to implement examples. I am so new to these technologies. This is the point where I didn't understand:

@View( name = "all", map = "function(doc) { if (doc.type == 'Sofa' ) emit( null, doc._id )}")
public class SofaRepository extends CouchDbRepositorySupport<Sofa> {

    @View( name = "avg_sofa_size", map = "function(doc) {...}", reduce = "function(doc) {...}")
    public int getAverageSofaSize() {
        ViewResult r = db.queryView(createQuery("avg_sofa_size"));
        return r.getRows().get(0).getValueAsInt();
    }

}

How does that wievs work and how to define them, what happens at that lines?


Solution

  • CouchDbRepositorySupport out of the box provides the following methods to the SofaRepository:

    public void add(Sofa entity);
    public void update(Sofa entity);
    public void remove(Sofa entity);
    public Sofa get(String id);
    public Sofa get(String id, String rev);
    public List<T> getAll();
    public boolean contains(String docId);
    

    By having this inline view annotation for CouchDbRepositorySupport:

    @View( name = "all", map = "function(doc) { if (doc.type == 'Sofa' ) emit( null, doc._id )}") 
    

    You redefine the return from a getAll() method.

    You also adding another method getAverageSofaSize() to your repository, with inline View:

    @View( name = "avg_sofa_size", map = "function(doc) {...}", reduce = "function(doc) {...}")
    

    which explicitly provides a query that db.queryView(createQuery("avg_sofa_size")); undersntad. db here is a CouchDbConnector that is able to create, delete, purge, find, etc..

    Take a look at more documentation about defining in line Views