Hello,
I am trying to upgrade an existing Java SDK 1.4 code to Java SDK 2.3. I read a particular view with its documents, do some operations on each row and then proceed to the next row. I get the BackpressureException and after some research I found out that an asynchronous operation would be good. I am pretty new to Rx Java hence wanted some guidance in that aspect. my code currently does the following steps:
ViewResult result = client.query(ViewQuery
.from(DESIGN_DOC, SORT_VIEW)
.stale(Stale.TRUE)
.includeDocs(true));
int index = 0;
Iterator<ViewRow> rowIterator = result.rows();
while(rowIterator.hasNext())
{
ViewRow viewRow = rowIterator.next();
Abcd a = gson.fromJson(viewRow.document().toString(), Abcd.class);
AbcdListing aListing = new AbcdListing(a);
aListing.set_id(viewRow.id().substring(5).toUpperCase());
Float x1 = new Float(aListing.getLon());
Float y1 = new Float(aListing.getLat());
Rectangle r = new Rectangle(x1,y1,x1,y1);
spatialIndexTree.add(r, index);
mapSpatialIndexToAListing.put(index, aListing);
index++;
}
the “spatialIndexTree” is a SpatialIndex built on top of an RTree ( http://jsi.sourceforge.net/ ) and mapSpatialIndexToAListing is a Map<Integer, AListing>
I tried changing it to an async model to steer away from the backpressureexception and have come to the following implementation but I am unable to manage the “index” value in the above code. Any help /comments would be great.
final Gson gson = new Gson();
final SpatialIndex spatialIndexTree = new RTree();
spatialIndexTree.init(null);
bucket1
.async()
.query(ViewQuery
.from(DESIGN_DOC, SORT_VIEW)
.stale(Stale.TRUE)
.includeDocs(true))
.flatMap(new Func1<AsyncViewResult, Observable<AsyncViewRow>>() {
@Override
public Observable<AsyncViewRow> call(AsyncViewResult viewResult) {
return viewResult.rows();
}
})
.flatMap(new Func1<AsyncViewRow, Observable<JsonDocument>>() {
@Override
public Observable<JsonDocument> call(AsyncViewRow viewRow) {
return viewRow.document();
}
})
.toBlocking()
.subscribe(new Action1<JsonDocument>() {
@Override
public void call(JsonDocument document) {
Abcd abcd = gson.fromJson(document.toString(), Abcd.class);
AbcdListing aListing = new AbcdListing(a);
aListing.set_id(viewRow.id().substring(5).toUpperCase());
Float x1 = new Float(aListing.getLon());
Float y1 = new Float(aListing.getLat());
Rectangle r = new Rectangle(x1,y1,x1,y1);
spatialIndexTree.add(r, index);
mapSpatialIndexToAListing.put(index, aListing);
}
});
Although this code has the variable “index” but I do not have a way I can increment it. Please comment if this rendition of the above code looks and what can I do abut the “index” variable