How to make single(view and query) for get the results

30 views
Skip to first unread message

Prasanna Kumar Peddinti

unread,
Feb 8, 2016, 10:40:58 AM2/8/16
to Couchbase Mobile
Hi All,

I would like to know more about what is the best way to write the view and query for get the results using single view and query.
Let me explain my user story here, How i am doing currently.

DataBase Name : KMM Collage.
Roles:Manager,Admin,Principle,HOD,Lecture.
userRole : Manager = 0, Admin = 0,Principle = 0,HOD = 1, Lecture = 2.

Document Information:

{
id : hjd565673fjd847567465,
student : "Prasanna Kumar",
department : "Lecture/HOD"
marks:{
                1:"hhfdh"
                2:"jfjf"
            }

type :"KMM.Type"
}


Filter Pull Replication

> I have filter replication according to login role we have to filter the documents.
> If login user is HOD, i have to filter only his documents.
> else if login user is Lecture, i have to filter only his documents
> else if login user is (Manager,Admin,Principle), i have to filter all documents

Now Pull replication is done according there roles.
Note: The user may switch at any time, if may documents are not available than we have to pull(according his all ).

Views and Queries

com.couchbase.lite.View viewTicketsPerProjectPagination = database.getView(String.format("%s/%s", D_DOCNAME, TICKETS_PER_PROJECT_PAGINATION));
viewTicketsPerProjectPagination.setMap(new Mapper() {
@Override
public void map(Map<String, Object> document, Emitter emitter) {
if (DOCTYPE_TICKET.equals(document.get(TYPE))) {
emitter.emit(new Object[]{userRole == 0?document.get(TYPE):document.get("
department")}, null);
        }
}
}, "1." + userRole);

Query Part:

com.couchbase.lite.View viewPerGetAllTickets = AppConstants.database.getView(String.format("%s/%s", D_DOCNAME,TICKETS_PER_PROJECT_PAGINATION);
queryForGetTickets = viewPerGetAllTickets.createQuery().toLiveQuery();
queryForGetTickets.setStartKey(new Object[]{userRole == 0?"KMM.Type": Lecture});
queryForGetTickets.setEndKey(new Object[]{userRole == 0?"KMM.Type": Lecture});
queryForGetTickets.run();

Note : The index will be change according user role.

Questions :

1) Is this best way to do the thinks, like making reindex views again and again when user role is changed.
2) Between if i gonna write multiple views and query there is big problem like performance wise.
3) Even though if i do like point number 1, there is also performance issue will i thinks(when it is bigger database)
4) How i can do the best out of the above user story. Irrespective of the Performance and reducing the un wanted views 

Note : Reindexing view in multiple times, is that good idea for large databases

Please help me out how i can solve this problem, thanks in advance.

if any clarification or any information please revert me back.  

Thank You,
P Prasanna Kumar.

Jens Alfke

unread,
Feb 8, 2016, 11:18:24 AM2/8/16
to mobile-c...@googlegroups.com

> On Feb 8, 2016, at 4:25 AM, Prasanna Kumar Peddinti <peddinti...@gmail.com> wrote:
>
> 1) Is this best way to do the thinks, like making reindex views again and again when user role is changed.

No. Generally you shouldn’t be changing the map function of the view based on query parameters. The role of the map function is to generate an index, and you want the index to be useable for all queries on this view. Any time you change the map function, the index is wiped out, so you’re losing the benefit of the index.

I don’t have time to review your code or design in depth (not unless you want to pay us for consulting ;) but it looks like your map function only has two cases: one where userRole==0 and one otherwise. So just make it into two views, one for each case.

—Jens
Reply all
Reply to author
Forward
0 new messages