Java Code Examples for com.couchbase.client.java.AsyncBucket

The following examples show how to use com.couchbase.client.java.AsyncBucket. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: tutorials   Source File: ClusterServiceImpl.java    License: MIT License 6 votes vote down vote up
@Override
public List<JsonDocument> getDocumentsAsync(final AsyncBucket asyncBucket, Iterable<String> keys) {
    Observable<JsonDocument> asyncBulkGet = Observable.from(keys).flatMap(new Func1<String, Observable<JsonDocument>>() {
        public Observable<JsonDocument> call(String key) {
            return asyncBucket.get(key);
        }
    });

    final List<JsonDocument> docs = new ArrayList<>();
    try {
        asyncBulkGet.toBlocking().forEach(new Action1<JsonDocument>() {
            public void call(JsonDocument doc) {
                docs.add(doc);
            }
        });
    } catch (Exception e) {
        logger.error("Error during bulk get", e);
    }

    return docs;
}
 
Example 2
Source Project: tutorials   Source File: AbstractCrudService.java    License: MIT License 6 votes vote down vote up
@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;
}
 
Example 3
Source Project: samza   Source File: TestCouchbaseTableWriteFunction.java    License: Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 4
Source Project: samza   Source File: TestCouchbaseTableWriteFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testPutAsyncJsonObjectValue() throws Exception {
  String key = "key";
  JsonObject value = JsonObject.fromJson("{\"id\": 1}");
  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.just(null));
  assertNull(writeFunction.putAsync(key, value).get());
}
 
Example 5
Source Project: samza   Source File: TestCouchbaseTableWriteFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testPutAsyncStringValue() throws Exception {
  String key = "key";
  String value = "value";
  Bucket bucket = mock(Bucket.class);
  AsyncBucket asyncBucket = mock(AsyncBucket.class);
  CouchbaseTableWriteFunction<String> writeFunction =
      createAndInit(String.class, new StringSerde(), bucket, asyncBucket);
  when(asyncBucket.upsert(any(Document.class), anyLong(), any(TimeUnit.class))).thenReturn(Observable.just(null));
  assertNull(writeFunction.putAsync(key, value).get());
}
 
Example 6
Source Project: samza   Source File: TestCouchbaseTableWriteFunction.java    License: Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 7
Source Project: samza   Source File: TestCouchbaseTableWriteFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteAsyncJsonObjectValue() throws Exception {
  String key = "key";
  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.just(null));
  assertNull(writeFunction.deleteAsync(key).get());
}
 
Example 8
Source Project: samza   Source File: TestCouchbaseTableWriteFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteAsyncStringValue() throws Exception {
  String key = "key";
  Bucket bucket = mock(Bucket.class);
  AsyncBucket asyncBucket = mock(AsyncBucket.class);
  CouchbaseTableWriteFunction<String> writeFunction =
      createAndInit(String.class, new StringSerde(), bucket, asyncBucket);
  when(asyncBucket.remove(eq(key), anyLong(), any(TimeUnit.class))).thenReturn(Observable.just(null));
  assertNull(writeFunction.deleteAsync(key).get());
}
 
Example 9
Source Project: samza   Source File: TestCouchbaseTableWriteFunction.java    License: Apache License 2.0 5 votes vote down vote up
private <V> CouchbaseTableWriteFunction<V> createAndInit(Class<V> valueClass, Serde<V> serde, Bucket bucket,
    AsyncBucket asyncBucket) {
  when(bucket.async()).thenReturn(asyncBucket);
  PowerMockito.stub(PowerMockito.method(CouchbaseBucketRegistry.class, "getBucket", String.class, List.class,
      CouchbaseEnvironmentConfigs.class)).toReturn(bucket);
  CouchbaseTableWriteFunction<V> writeFunction =
      new CouchbaseTableWriteFunction<>(DEFAULT_BUCKET_NAME, valueClass, DEFAULT_CLUSTER_NODE).withSerde(serde);
  writeFunction.init(mock(Context.class), mock(AsyncReadWriteTable.class));
  return writeFunction;
}
 
Example 10
Source Project: samza   Source File: TestCouchbaseTableReadFunction.java    License: Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 11
Source Project: samza   Source File: TestCouchbaseTableReadFunction.java    License: Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 12
Source Project: samza   Source File: TestCouchbaseTableReadFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAsyncNullValue() throws Exception {
  String key = "NonExistingKey";
  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.empty());
  assertNull(readFunction.getAsync(key).get());
}
 
Example 13
Source Project: samza   Source File: TestCouchbaseTableReadFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAsyncNonExistingKey() throws Exception {
  String key = "NonExistingKey";
  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.empty());
  assertNull(readFunction.getAsync(key).get());
}
 
Example 14
Source Project: samza   Source File: TestCouchbaseTableReadFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAsyncStringValue() throws Exception {
  String key = "key";
  String value = "value";
  StringSerde stringSerde = new StringSerde();
  Bucket bucket = mock(Bucket.class);
  AsyncBucket asyncBucket = mock(AsyncBucket.class);
  CouchbaseTableReadFunction readFunction = createAndInit(String.class, stringSerde, bucket, asyncBucket);
  when(asyncBucket.get(eq(key), anyObject(), anyLong(), any(TimeUnit.class))).thenReturn(
      Observable.just(BinaryDocument.create(key, Unpooled.wrappedBuffer(stringSerde.toBytes(value)))));
  assertEquals(value, readFunction.getAsync(key).get());
}
 
Example 15
Source Project: samza   Source File: TestCouchbaseTableReadFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAsyncJsonObjectValue() throws Exception {
  String key = "key";
  JsonObject value = JsonObject.fromJson("{\"id\": 1}");
  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.just(JsonDocument.create(key, value)));
  assertEquals(value, readFunction.getAsync(key).get());
}
 
Example 16
Source Project: samza   Source File: TestCouchbaseTableReadFunction.java    License: Apache License 2.0 5 votes vote down vote up
private <V> CouchbaseTableReadFunction<V> createAndInit(Class<V> valueClass, Serde<V> serde, Bucket bucket,
    AsyncBucket asyncBucket) {
  when(bucket.async()).thenReturn(asyncBucket);
  PowerMockito.stub(PowerMockito.method(CouchbaseBucketRegistry.class, "getBucket", String.class, List.class,
      CouchbaseEnvironmentConfigs.class)).toReturn(bucket);
  CouchbaseTableReadFunction<V> readFunction =
      new CouchbaseTableReadFunction<>(DEFAULT_BUCKET_NAME, valueClass, DEFAULT_CLUSTER_NODE).withSerde(serde);
  readFunction.init(mock(Context.class), mock(AsyncReadWriteTable.class));
  return readFunction;
}
 
Example 17
Source Project: tutorials   Source File: AbstractCrudService.java    License: MIT License 5 votes vote down vote up
@Override
public void createBulk(Iterable<T> items) {
    final AsyncBucket asyncBucket = bucket.async();
    Observable.from(items).flatMap(new Func1<T, Observable<JsonDocument>>() {
        @SuppressWarnings("unchecked")
        @Override
        public Observable<JsonDocument> call(final T t) {
            if (t.getId() == null) {
                t.setId(UUID.randomUUID().toString());
            }
            JsonDocument doc = converter.toDocument(t);
            return asyncBucket.insert(doc).retryWhen(RetryBuilder.anyOf(BackpressureException.class).delay(Delay.exponential(TimeUnit.MILLISECONDS, 100)).max(10).build());
        }
    }).last().toBlocking().single();
}
 
Example 18
Source Project: tutorials   Source File: AbstractCrudService.java    License: MIT License 5 votes vote down vote up
@Override
public void updateBulk(Iterable<T> items) {
    final AsyncBucket asyncBucket = bucket.async();
    Observable.from(items).flatMap(new Func1<T, Observable<JsonDocument>>() {
        @SuppressWarnings("unchecked")
        @Override
        public Observable<JsonDocument> call(final T t) {
            JsonDocument doc = converter.toDocument(t);
            return asyncBucket.upsert(doc).retryWhen(RetryBuilder.anyOf(BackpressureException.class).delay(Delay.exponential(TimeUnit.MILLISECONDS, 100)).max(10).build());
        }
    }).last().toBlocking().single();
}
 
Example 19
Source Project: tutorials   Source File: AbstractCrudService.java    License: MIT License 5 votes vote down vote up
@Override
public void deleteBulk(Iterable<String> ids) {
    final AsyncBucket asyncBucket = bucket.async();
    Observable.from(ids).flatMap(new Func1<String, Observable<JsonDocument>>() {
        @SuppressWarnings("unchecked")
        @Override
        public Observable<JsonDocument> call(String key) {
            return asyncBucket.remove(key).retryWhen(RetryBuilder.anyOf(BackpressureException.class).delay(Delay.exponential(TimeUnit.MILLISECONDS, 100)).max(10).build());
        }
    }).last().toBlocking().single();
}
 
Example 20
Source Project: samza   Source File: TestCouchbaseTableWriteFunction.java    License: Apache License 2.0 4 votes vote down vote up
private CouchbaseTableWriteFunction<JsonObject> createAndInit() {
  return createAndInit(mock(Bucket.class), mock(AsyncBucket.class));
}
 
Example 21
Source Project: samza   Source File: TestCouchbaseTableWriteFunction.java    License: Apache License 2.0 4 votes vote down vote up
private CouchbaseTableWriteFunction<JsonObject> createAndInit(Bucket bucket, AsyncBucket asyncBucket) {
  return createAndInit(JsonObject.class, null, bucket, asyncBucket);
}
 
Example 22
Source Project: samza   Source File: TestCouchbaseTableReadFunction.java    License: Apache License 2.0 4 votes vote down vote up
private CouchbaseTableReadFunction<JsonObject> createAndInit() {
  return createAndInit(mock(Bucket.class), mock(AsyncBucket.class));
}
 
Example 23
Source Project: samza   Source File: TestCouchbaseTableReadFunction.java    License: Apache License 2.0 4 votes vote down vote up
private CouchbaseTableReadFunction<JsonObject> createAndInit(Bucket bucket, AsyncBucket asyncBucket) {
  return createAndInit(JsonObject.class, null, bucket, asyncBucket);
}
 
Example 24
Source Project: datacollector   Source File: CouchbaseConnector.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Returns a reference to the Couchbase bucket
 * @return the instantiated bucket
 */
public AsyncBucket bucket() {
  return this.bucket;
}
 
Example 25
Source Project: tutorials   Source File: ClusterService.java    License: MIT License votes vote down vote up
List<JsonDocument> getDocumentsAsync(AsyncBucket bucket, Iterable<String> keys);