Search in sources :

Example 6 with ByteBuf

use of com.couchbase.client.deps.io.netty.buffer.ByteBuf in project incubator-gobblin by apache.

the class AvroToCouchbaseTupleConverter method convertRecord.

@Override
public Iterable<TupleDocument> convertRecord(String outputSchema, GenericRecord inputRecord, WorkUnitState workUnit) throws DataConversionException {
    String key = inputRecord.get(keyField).toString();
    GenericRecord data = (GenericRecord) inputRecord.get(dataRecordField);
    ByteBuffer dataBytes = (ByteBuffer) data.get(valueField);
    Integer flags = (Integer) data.get(flagsField);
    ByteBuf buffer = Unpooled.copiedBuffer(dataBytes);
    return new SingleRecordIterable<>(new TupleDocument(key, Tuple.create(buffer, flags)));
}
Also used : SingleRecordIterable(org.apache.gobblin.converter.SingleRecordIterable) GenericRecord(org.apache.avro.generic.GenericRecord) ByteBuf(com.couchbase.client.deps.io.netty.buffer.ByteBuf) TupleDocument(org.apache.gobblin.couchbase.common.TupleDocument) ByteBuffer(java.nio.ByteBuffer)

Example 7 with ByteBuf

use of com.couchbase.client.deps.io.netty.buffer.ByteBuf in project components by Talend.

the class CouchbaseStreamingConnectionTest method testStartStopStreaming.

@Test
public void testStartStopStreaming() throws InterruptedException {
    Mockito.when(client.initializeState(StreamFrom.BEGINNING, StreamTo.NOW)).thenReturn(Completable.complete());
    Mockito.when(client.startStreaming(Mockito.<Short[]>anyVararg())).thenReturn(Completable.complete());
    Mockito.when(client.stopStreaming(Mockito.<Short[]>anyVararg())).thenReturn(Completable.complete());
    SessionState sessionState = Mockito.mock(SessionState.class);
    Mockito.when(sessionState.isAtEnd()).thenReturn(false, true);
    Mockito.when(client.sessionState()).thenReturn(sessionState);
    BlockingQueue<ByteBuf> resultsQueue = new ArrayBlockingQueue<>(4);
    resultsQueue.put(Mockito.mock(ByteBuf.class));
    resultsQueue.put(Mockito.mock(ByteBuf.class));
    resultsQueue.put(Mockito.mock(ByteBuf.class));
    resultsQueue.put(Mockito.mock(ByteBuf.class));
    Mockito.when(client.disconnect()).thenReturn(Completable.complete());
    streamingConnection.startStreaming(resultsQueue);
    streamingConnection.stopStreaming();
    Thread.sleep(1500);
    Mockito.verify(client, Mockito.times(2)).sessionState();
    Mockito.verify(client, Mockito.times(1)).disconnect();
}
Also used : SessionState(com.couchbase.client.dcp.state.SessionState) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ByteBuf(com.couchbase.client.deps.io.netty.buffer.ByteBuf) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 8 with ByteBuf

use of com.couchbase.client.deps.io.netty.buffer.ByteBuf in project components by Talend.

the class CouchbaseReader method advance.

@Override
public boolean advance() throws IOException {
    while (true) {
        ByteBuf event;
        try {
            event = resultsQueue.poll(1, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            LOG.error("Failed to poll event from the results queue", e);
            return false;
        }
        if (event != null) {
            currentRecord = converter.convertToAvro(event);
            connection.acknowledge(event);
            event.release();
            recordCount++;
            return true;
        }
        if (!connection.isStreaming() && resultsQueue.isEmpty()) {
            break;
        }
    }
    return false;
}
Also used : ByteBuf(com.couchbase.client.deps.io.netty.buffer.ByteBuf)

Example 9 with ByteBuf

use of com.couchbase.client.deps.io.netty.buffer.ByteBuf in project components by Talend.

the class CouchbaseEventGenericRecordConverterTest method testConvertToAvroMutation.

@Test
public void testConvertToAvroMutation() {
    ByteBuf buffer = Mockito.mock(ByteBuf.class);
    // Mocking key object
    Mockito.when(buffer.getByte(0)).thenReturn(MessageUtil.MAGIC_REQ);
    Mockito.when(buffer.getByte(1)).thenReturn(MessageUtil.DCP_MUTATION_OPCODE);
    Mockito.when(buffer.getByte(4)).thenReturn(OFFSET);
    Mockito.when(buffer.getShort(2)).thenReturn(LENGTH);
    ByteBuf key = Mockito.mock(ByteBuf.class);
    Mockito.when(key.readableBytes()).thenReturn(4);
    Mockito.when(buffer.slice(29, 10)).thenReturn(key);
    // Mocking content object.
    Mockito.when(buffer.getByte(4)).thenReturn(OFFSET);
    Mockito.when(buffer.getShort(2)).thenReturn(LENGTH);
    Mockito.when(buffer.getInt(8)).thenReturn(100);
    ByteBuf content = Mockito.mock(ByteBuf.class);
    // Content byte array length.
    Mockito.when(content.readableBytes()).thenReturn(85);
    Mockito.when(buffer.slice(39, 85)).thenReturn(content);
    IndexedRecord record = converter.convertToAvro(buffer);
    Assert.assertEquals(schema, record.getSchema());
    assertIndexedRecordResult(record, "mutation", new byte[85]);
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) ByteBuf(com.couchbase.client.deps.io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 10 with ByteBuf

use of com.couchbase.client.deps.io.netty.buffer.ByteBuf in project components by Talend.

the class CouchbaseEventGenericRecordConverterTest method testConvertToAvroExpiration.

@Test
public void testConvertToAvroExpiration() {
    ByteBuf buffer = Mockito.mock(ByteBuf.class);
    // Mocking key object
    Mockito.when(buffer.getByte(0)).thenReturn(MessageUtil.MAGIC_REQ);
    Mockito.when(buffer.getByte(1)).thenReturn(MessageUtil.DCP_EXPIRATION_OPCODE);
    Mockito.when(buffer.getByte(4)).thenReturn(OFFSET);
    Mockito.when(buffer.getShort(2)).thenReturn(LENGTH);
    ByteBuf key = Mockito.mock(ByteBuf.class);
    Mockito.when(key.readableBytes()).thenReturn(4);
    Mockito.when(buffer.slice(29, 10)).thenReturn(key);
    IndexedRecord record = converter.convertToAvro(buffer);
    assertIndexedRecordResult(record, "expiration", null);
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) ByteBuf(com.couchbase.client.deps.io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

ByteBuf (com.couchbase.client.deps.io.netty.buffer.ByteBuf)12 Test (org.junit.Test)8 IndexedRecord (org.apache.avro.generic.IndexedRecord)5 SessionState (com.couchbase.client.dcp.state.SessionState)2 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 CouchbaseException (com.couchbase.client.core.CouchbaseException)1 Bucket (com.couchbase.client.java.Bucket)1 PersistTo (com.couchbase.client.java.PersistTo)1 ReplicateTo (com.couchbase.client.java.ReplicateTo)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 SingleRecordIterable (org.apache.gobblin.converter.SingleRecordIterable)1 TupleDocument (org.apache.gobblin.couchbase.common.TupleDocument)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 ComponentLog (org.apache.nifi.logging.ComponentLog)1