Search in sources :

Example 11 with AsyncBucket

use of com.couchbase.client.java.AsyncBucket in project tutorials by eugenp.

the class AbstractCrudService method readBulk.

@Override
public List<T> readBulk(Iterable<String> ids) {
    final AsyncBucket asyncBucket = bucket.async();
    Observable<JsonDocument> asyncOperation = Observable.from(ids).flatMap(new Func1<String, Observable<JsonDocument>>() {

        public Observable<JsonDocument> call(String key) {
            return asyncBucket.get(key);
        }
    });
    final List<T> items = new ArrayList<T>();
    try {
        asyncOperation.toBlocking().forEach(new Action1<JsonDocument>() {

            public void call(JsonDocument doc) {
                T item = converter.fromDocument(doc);
                items.add(item);
            }
        });
    } catch (Exception e) {
        logger.error("Error during bulk get", e);
    }
    return items;
}
Also used : AsyncBucket(com.couchbase.client.java.AsyncBucket) ArrayList(java.util.ArrayList) JsonDocument(com.couchbase.client.java.document.JsonDocument) Observable(rx.Observable) BackpressureException(com.couchbase.client.core.BackpressureException)

Example 12 with AsyncBucket

use of com.couchbase.client.java.AsyncBucket in project samza by apache.

the class TestCouchbaseTableWriteFunction method testDeleteAsyncException.

@Test
public void testDeleteAsyncException() {
    String key = "throwExceptionKey";
    Bucket bucket = mock(Bucket.class);
    AsyncBucket asyncBucket = mock(AsyncBucket.class);
    CouchbaseTableWriteFunction<JsonObject> writeFunction = createAndInit(bucket, asyncBucket);
    when(asyncBucket.remove(eq(key), anyLong(), any(TimeUnit.class))).thenReturn(Observable.error(new CouchbaseException()));
    assertTrue(writeFunction.deleteAsync(key).isCompletedExceptionally());
}
Also used : CouchbaseException(com.couchbase.client.core.CouchbaseException) AsyncBucket(com.couchbase.client.java.AsyncBucket) Bucket(com.couchbase.client.java.Bucket) AsyncBucket(com.couchbase.client.java.AsyncBucket) JsonObject(com.couchbase.client.java.document.json.JsonObject) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 13 with AsyncBucket

use of com.couchbase.client.java.AsyncBucket in project samza by apache.

the class TestCouchbaseTableWriteFunction method testPutAsyncException.

@Test
public void testPutAsyncException() {
    String key = "throwExceptionKey";
    JsonObject value = JsonObject.create();
    Bucket bucket = mock(Bucket.class);
    AsyncBucket asyncBucket = mock(AsyncBucket.class);
    CouchbaseTableWriteFunction<JsonObject> writeFunction = createAndInit(bucket, asyncBucket);
    when(asyncBucket.upsert(any(Document.class), anyLong(), any(TimeUnit.class))).thenReturn(Observable.error(new CouchbaseException()));
    assertTrue(writeFunction.putAsync(key, value).isCompletedExceptionally());
}
Also used : CouchbaseException(com.couchbase.client.core.CouchbaseException) AsyncBucket(com.couchbase.client.java.AsyncBucket) Bucket(com.couchbase.client.java.Bucket) AsyncBucket(com.couchbase.client.java.AsyncBucket) JsonObject(com.couchbase.client.java.document.json.JsonObject) TimeUnit(java.util.concurrent.TimeUnit) Document(com.couchbase.client.java.document.Document) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 14 with AsyncBucket

use of com.couchbase.client.java.AsyncBucket in project samza by apache.

the class TestCouchbaseTableReadFunction method testGetAsyncException.

@Test
public void testGetAsyncException() {
    String key = "throwExceptionKey";
    Bucket bucket = mock(Bucket.class);
    AsyncBucket asyncBucket = mock(AsyncBucket.class);
    CouchbaseTableReadFunction readFunction = createAndInit(bucket, asyncBucket);
    when(asyncBucket.get(eq(key), anyObject(), anyLong(), any(TimeUnit.class))).thenReturn(Observable.error(new CouchbaseException()));
    assertTrue(readFunction.getAsync(key).isCompletedExceptionally());
}
Also used : CouchbaseException(com.couchbase.client.core.CouchbaseException) AsyncBucket(com.couchbase.client.java.AsyncBucket) Bucket(com.couchbase.client.java.Bucket) AsyncBucket(com.couchbase.client.java.AsyncBucket) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 15 with AsyncBucket

use of com.couchbase.client.java.AsyncBucket in project samza by apache.

the class TestCouchbaseTableReadFunction method testGetAsyncFailedToDeserialize.

@Test
public void testGetAsyncFailedToDeserialize() {
    String key = "key";
    Bucket bucket = mock(Bucket.class);
    AsyncBucket asyncBucket = mock(AsyncBucket.class);
    CouchbaseTableReadFunction readFunction = createAndInit(String.class, new StringSerde(), bucket, asyncBucket);
    when(asyncBucket.get(eq(key), anyObject(), anyLong(), any(TimeUnit.class))).thenReturn(Observable.just(BinaryDocument.create(key, null)));
    assertTrue(readFunction.getAsync(key).isCompletedExceptionally());
}
Also used : StringSerde(org.apache.samza.serializers.StringSerde) AsyncBucket(com.couchbase.client.java.AsyncBucket) Bucket(com.couchbase.client.java.Bucket) AsyncBucket(com.couchbase.client.java.AsyncBucket) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

AsyncBucket (com.couchbase.client.java.AsyncBucket)16 Bucket (com.couchbase.client.java.Bucket)12 TimeUnit (java.util.concurrent.TimeUnit)12 Test (org.junit.Test)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 StringSerde (org.apache.samza.serializers.StringSerde)6 JsonObject (com.couchbase.client.java.document.json.JsonObject)5 JsonDocument (com.couchbase.client.java.document.JsonDocument)4 CouchbaseException (com.couchbase.client.core.CouchbaseException)3 Document (com.couchbase.client.java.document.Document)3 Func1 (rx.functions.Func1)3 BackpressureException (com.couchbase.client.core.BackpressureException)1 ArrayList (java.util.ArrayList)1 Observable (rx.Observable)1