How to make common view for all sorting techniques in android couchbaselite

112 views
Skip to first unread message

PRASANNA KUMAR

unread,
Aug 19, 2015, 8:36:25 AM8/19/15
to Couchbase Mobile
Hi All,

I would to know about how we can do the sorting technique using one view and query. Right now i'm having 3 types sorting technique(LastModifiedDate,DueDate,ResponsibleEmailID)  i do not like write three view and queries. I want to finish in one view and query itself all three sorting techniques.
I will share here how is did till the time,

1) This for sort by lastmodified date

//create view for get all ticket document per project
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) {

Object type = document.get(TYPE);
String archivedState;
try {
archivedState = (String) document.get(ARCHIVED) != null ? ARCHIVED : UNARCHIVED;
}catch (Exception ex){
archivedState = UNARCHIVED;
}
try {
if (type != null && DOCTYPE_TICKET.equals(type)) {
emitter.emit(new Object[]{document.get(PROJECT),archivedState,((HashMap<String, String>) document.get(DATES)).get(LASTMODIFIEDDATE)}, null);
}
} catch (NullPointerException e) {
}
}
}, "5.5");

2)This is for dueDate

//create view for get Tickets for selected map document in project  by using status As Per DueDate
com.couchbase.lite.View viewTicketsPerMapPaginationByStatusAsPerDueDate = database.getView(String.format("%s/%s", D_DOCNAME, TICKET_PER_MAP_BY_STATUS_AS_PER_DUE_DATE));
viewTicketsPerMapPaginationByStatusAsPerDueDate.setMap(new Mapper() {
@Override
public void map(Map<String, Object> document, Emitter emitter) {

Object type = document.get(TYPE);
String archivedState;
try {
archivedState = (String) document.get(ARCHIVED) != null ? ARCHIVED : UNARCHIVED;
}catch (Exception ex){
archivedState = UNARCHIVED;
}

try {
if (type != null && DOCTYPE_TICKET.equals(type)) {
emitter.emit(new Object[]{document.get(PROJECT), document.get(MAP), ((HashMap<String, String>) document.get(STATE)).get(STATE), archivedState,((HashMap<String, String>) document.get(PLAN)).get(DUEDATE)},
null);
}
} catch (NullPointerException e) {
}
}
}, "1.4");

3) This is for sort by responsible emailId

//create view for get all ticket document per project As Per Reponsible
com.couchbase.lite.View viewTicketsPerProjectPaginationAsPerReponsible = database.getView(String.format("%s/%s", D_DOCNAME, TICKETS_PER_PROJECT_PAGINATION_AS_PER_RESPONSIBLE));
viewTicketsPerProjectPaginationAsPerReponsible.setMap(new Mapper() {
@Override
public void map(Map<String, Object> document, Emitter emitter) {

Object type = document.get(TYPE);
String archivedState;
Object sortObject;
try {
archivedState = (String) document.get(ARCHIVED) != null ? ARCHIVED : UNARCHIVED;
}catch (Exception ex){
archivedState = UNARCHIVED;
}
try{
sortObject = ((HashMap<Object, HashMap<Object, String>>) document.get(PARTICIPANTS)).
get(RESPONSIBLE).get(EMAIL);
}catch (Exception ex){
sortObject = null;
}

try {
if (type != null && DOCTYPE_TICKET.equals(type)) {
emitter.emit(new Object[]{document.get(PROJECT), archivedState,sortObject},
null);
}
} catch (NullPointerException e) {
}
}
}, "2.9");

But those are causing the performance because of lot other thinks are there to write view and query, so that inmy project contains more view and query i would to reduce those many thinks.

How we can reduce those many views ? is there any api's to write direct sorting thinks supported by CBL API's 

Please let me know if you need any other information i can provide you  

Thank's
prasanna



PRASANNA KUMAR

unread,
Aug 19, 2015, 9:08:26 AM8/19/15
to Couchbase Mobile
Does android will support the sortDescriptors like ios ? like

Thank's
Prasanna

Jens Alfke

unread,
Aug 19, 2015, 11:37:17 AM8/19/15
to Couchbase Mobile, Hideki Itakura

On Aug 19, 2015, at 5:36 AM, PRASANNA KUMAR <prasannak...@gmail.com> wrote:

I would to know about how we can do the sorting technique using one view and query. Right now i'm having 3 types sorting technique(LastModifiedDate,DueDate,ResponsibleEmailID)  i do not like write three view and queries. I want to finish in one view and query itself all three sorting techniques.
I will share here how is did till the time,

That was too much code for me to read through! But I think the summary is that you have three different fields you need to sort by and you want to do it using one view.

The short answer is that you can’t. A view defines an index, and an index is always sorted by its key, so if you have different sort criteria, you need multiple indexes that are sorted by each key you want to sort on. (The same is true for a SQL database: you’ll need to create multiple indexes. The difference is that SQL will allow you to order by anything you want, but if you want it to be fast you’ll need indexes.)

But those are causing the performance because of lot other thinks are there to write view and query, so that inmy project contains more view and query i would to reduce those many thinks.

What sort of performance problems are you having? (Note: I don’t work on Android, so I don’t know details of its performance. Hopefully Hideki can answer.)

One thing I notice is that your map functions could be faster. Each of them does a bunch of work getting properties from the document before it decides whether or not to emit anything. If you put the “if” test at the start of the function, it’ll be faster to skip over the documents that aren’t tickets.

—Jens

Hideki Itakura

unread,
Aug 19, 2015, 1:12:59 PM8/19/15
to Couchbase Mobile
Hi @PRASANNA KUMAR,

What version of Couchbase LIte for Android? If you are using 1.0.x, I would like to recommend to upgrade to v1.1.0 which contains performance improvement for indexing.

Does you JSON data contains non-ascii characters? If so, it makes indexing slower. We plan to work for this, but not scheduled it yet.

Thanks,
Hideki
Reply all
Reply to author
Forward
0 new messages