Skip to content

Commit

Permalink
GORA-424 Cache cursor size to improve performance.
Browse files Browse the repository at this point in the history
- Cache cursor size to improve performance in MongoStore
- Avoid integer division bug
- Added result progress test

This closes #26 PR.

Squashed commit of the following:

commit 8d47c13
Author: Alexander Yastrebov <yastrebov.alex@gmail.com>
Date:   Wed Jun 10 11:37:35 2015 +0300

    Fixed integer division bug. Added result progress test

commit 1ebf813
Author: Alexander Yastrebov <yastrebov.alex@gmail.com>
Date:   Tue Jun 9 19:51:57 2015 +0300

    Cache cursor size
  • Loading branch information
drazzib committed Jun 10, 2015
1 parent 903aeb0 commit bb09d89
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
Apache Gora 0.6.1 Release - 02/03/2015 (dd/mm/yyyy)
Release Report - http://s.apache.org/l69

* GORA-424 Cache cursor size to improve performance (Alexander Yastrebov via drazzib)

* GORA-423 BSONDecorator returns empty string for null field value (Alexander Yastrebov via drazzib)

* GORA-262 Add support for HTTPClient authentication in gora-solr (Furkan KAMACI via lewismc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,32 @@
/**
* MongoDB specific implementation of the {@link org.apache.gora.query.Result}
* interface.
*
*
* @author Fabien Poulard <fpoulard@dictanova.com>
* @author Damien Raude-Morvan <draudemorvan@dictanova.com>
*/
public class MongoDBResult<K, T extends PersistentBase> extends
ResultBase<K, T> {
ResultBase<K, T> {

/**
* Reference to the cursor pointing to the results
*/
private DBCursor cursor;
private int size;

public MongoDBResult(DataStore<K, T> dataStore, Query<K, T> query) {
super(dataStore, query);
}

@Override
public float getProgress() throws IOException {
if (cursor == null)
if (cursor == null) {
return 0;
else if (cursor.size() == 0)
} else if (size == 0) {
return 1;
else
return offset / cursor.size();
} else {
return offset / (float) size;
}
}

@Override
Expand All @@ -73,19 +75,19 @@ protected boolean nextInner() throws IOException {
DBObject obj = cursor.next();
key = (K) obj.get("_id");
persistent = ((MongoStore<K, T>) getDataStore()).newInstance(obj,
getQuery().getFields());
getQuery().getFields());
return persistent != null;
}

/**
* Save the reference to the cursor that holds the actual results.
*
*
* @param cursor
* {@link DBCursor} obtained from a query execution and that holds
* the actual results
*/
public void setCursor(DBCursor cursor) {
this.cursor = cursor;
this.size = cursor.size();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.apache.gora.mongodb.store;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;

import java.io.IOException;

Expand All @@ -33,27 +33,36 @@

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import java.util.Collections;

import org.apache.avro.util.Utf8;
import org.apache.gora.query.Query;
import org.apache.gora.query.Result;
import static org.junit.Assert.assertNotNull;

public abstract class TestMongoStore extends DataStoreTestBase {

private int keySequence;

@Deprecated
@Override
protected DataStore<String, Employee> createEmployeeDataStore()
throws IOException {
throws IOException {
throw new UnsupportedOperationException();
}

@Deprecated
@Override
protected DataStore<String, WebPage> createWebPageDataStore()
throws IOException {
throws IOException {
throw new UnsupportedOperationException();
}

@Before
@Override
public void setUp() throws Exception {
super.setUp();
keySequence = 1;
}

public GoraMongodbTestDriver getTestDriver() {
Expand Down Expand Up @@ -84,7 +93,7 @@ public void testFromMongoList_null() throws Exception {
BasicDBObject noField = new BasicDBObject();
String field = "myField";
Object item = store.fromMongoList(field, null, new BSONDecorator(noField),
null);
null);
assertNotNull(item);
}

Expand All @@ -94,7 +103,7 @@ public void testFromMongoList_empty() throws Exception {
String field = "myField";
BasicDBObject emptyField = new BasicDBObject(field, new BasicDBList());
Object item = store.fromMongoList(field, null,
new BSONDecorator(emptyField), null);
new BSONDecorator(emptyField), null);
assertNotNull(item);
}

Expand All @@ -104,7 +113,7 @@ public void testFromMongoMap_null() throws Exception {
BasicDBObject noField = new BasicDBObject();
String field = "myField";
Object item = store.fromMongoMap(field, null, new BSONDecorator(noField),
null);
null);
assertNotNull(item);
}

Expand All @@ -114,7 +123,62 @@ public void testFromMongoMap_empty() throws Exception {
String field = "myField";
BasicDBObject emptyField = new BasicDBObject(field, new BasicDBObject());
Object item = store.fromMongoMap(field, null,
new BSONDecorator(emptyField), null);
new BSONDecorator(emptyField), null);
assertNotNull(item);
}

@Test
public void testResultProgress() throws Exception {
Query<String, WebPage> q;

// empty
q = webPageStore.newQuery();
assertProgress(q, 1);

addWebPage();

// one
q = webPageStore.newQuery();
assertProgress(q, 0, 1);

addWebPage();

// two
q = webPageStore.newQuery();
assertProgress(q, 0, 0.5f, 1);

addWebPage();

// three
q = webPageStore.newQuery();
assertProgress(q, 0, 0.33f, 0.66f, 1);
}

@Test
public void testResultProgressWithLimit() throws Exception {
for (int i = 0; i < 5; i++) {
addWebPage();
}
Query<String, WebPage> q = webPageStore.newQuery();
q.setLimit(2);

assertProgress(q, 0, 0.5f, 1);
}

private void assertProgress(Query<String, WebPage> q, float... progress) throws Exception {
Result<String, WebPage> r = webPageStore.execute(q);
int i = 0;
do {
assertEquals(progress[i++], r.getProgress(), 0.01f);
} while (r.next());
r.close();
}

private void addWebPage() {
String key = String.valueOf(keySequence++);
WebPage p1 = webPageStore.newPersistent();
p1.setUrl(new Utf8(key));
p1.setHeaders(Collections.singletonMap((CharSequence) "header", (CharSequence) "value"));
webPageStore.put(key, p1);
}
}

0 comments on commit bb09d89

Please sign in to comment.