Java Code Examples for com.couchbase.client.core.message.kv.MutationToken

The following examples show how to use com.couchbase.client.core.message.kv.MutationToken. 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: couchbase-jvm-core   Source File: KeyValueMessageTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Helper method to make sure that two consecutive sequences are valid.
 *
 * They are valid if the vbucket uuid is the same and the sequence is higher by one.
 *
 * @param first the first mutation
 * @param second the second mutation
 * @throws Exception
 */
private void assertMetadataSequence(MutationToken first, MutationToken second) throws Exception {
    if (isMutationMetadataEnabled()) {
        assertNotNull(first);
        assertNotNull(second);
        assertTrue(first.vbucketUUID() != 0);
        assertTrue(first.vbucketID() > 0);
        assertTrue(second.vbucketID() > 0);
        assertTrue(first.bucket() != null && first.bucket().equals(bucket()));
        assertEquals(first.bucket(), second.bucket());
        assertEquals(first.vbucketUUID(), second.vbucketUUID());
        assertTrue((first.sequenceNumber()+1) == second.sequenceNumber());
        assertEquals(first.vbucketID(), second.vbucketID());
    } else {
        assertNull(first);
        assertNull(second);
    }
}
 
Example 2
Source Project: couchbase-jvm-core   Source File: SubdocumentMessageTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to assert if the mutation metadata is correct.
 *
 * Note that if mutation metadata is disabled, null is expected.
 *
 * @param token the token to check
 * @throws Exception
 */
private void assertValidMetadata(MutationToken token) throws Exception {
    if (isMutationMetadataEnabled()) {
        assertNotNull(token);
        assertTrue(token.sequenceNumber() > 0);
        assertTrue(token.vbucketUUID() != 0);
        assertTrue(token.vbucketID() > 0);
    } else {
        assertNull(token);
    }
}
 
Example 3
Source Project: couchbase-jvm-core   Source File: KeyValueMessageTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to assert if the mutation metadata is correct.
 *
 * Note that if mutation metadata is disabled, null is expected.
 *
 * @param token the token to check
 * @throws Exception
 */
private void assertValidMetadata(MutationToken token) throws Exception {
    if (isMutationMetadataEnabled()) {
        assertNotNull(token);
        assertTrue(token.sequenceNumber() > 0);
        assertTrue(token.vbucketUUID() != 0);
        assertTrue(token.vbucketID() > 0);
        assertTrue(token.bucket() != null && token.bucket().equals(bucket()));
    } else {
        assertNull(token);
    }
}
 
Example 4
Source Project: couchbase-jvm-core   Source File: Observe.java    License: Apache License 2.0 5 votes vote down vote up
public static Observable<Boolean> call(final ClusterFacade core, final String bucket, final String id,
   final long cas, final boolean remove, MutationToken token, final PersistTo persistTo, final ReplicateTo replicateTo,
   final Delay delay, final RetryStrategy retryStrategy, Span parent) {
    if (token == null) {
        return ObserveViaCAS.call(core, bucket, id, cas, remove, persistTo, replicateTo, delay, retryStrategy, parent);
    } else {
        return ObserveViaMutationToken.call(core, bucket, id, token, persistTo, replicateTo, delay, retryStrategy, parent, cas);
    }
}
 
Example 5
public static ObserveItem from(MutationToken token, FailoverObserveSeqnoResponse response) {
    boolean replicated = response.currentSeqNo() >= token.sequenceNumber();
    boolean persisted = response.lastPersistedSeqNo() >= token.sequenceNumber();

    return new ObserveItem(
            replicated && !response.master() ? 1 : 0,
            persisted ? 1 : 0,
            response.master() && persisted
    );
}
 
Example 6
public static ObserveItem from(MutationToken token, NoFailoverObserveSeqnoResponse response) {
    boolean replicated = response.currentSeqNo() >= token.sequenceNumber();
    boolean persisted = response.lastPersistedSeqNo() >= token.sequenceNumber();

    return new ObserveItem(
        replicated && !response.master() ? 1 : 0,
        persisted ? 1 : 0,
        response.master() && persisted
    );
}
 
Example 7
Source Project: couchbase-jvm-core   Source File: MultiMutationResponse.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates an successful {@link MultiMutationResponse}.
 *
 * @param bucket the bucket on which the request happened.
 * @param request the original {@link BinarySubdocMultiMutationRequest}.
 * @param cas the CAS value of the document after mutations.
 * @param token the {@link MutationToken} of the document after mutations, if available. Null otherwise.
 * @param responses the list of {@link MultiResult MultiResult&lt;Mutation&gt;} for each command. Some may include a value.
 */
public MultiMutationResponse(String bucket, BinarySubdocMultiMutationRequest request, long cas, MutationToken token,
                             List<MultiResult<Mutation>> responses) {
    super(ResponseStatus.SUCCESS, KeyValueStatus.SUCCESS.code(), bucket, Unpooled.EMPTY_BUFFER, request);
    this.cas = cas;
    this.mutationToken = token;
    this.firstErrorIndex = -1;
    this.firstErrorStatus = ResponseStatus.SUCCESS;
    this.responses = responses;
}
 
Example 8
Source Project: couchbase-jvm-core   Source File: KeyValueHandler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to decode all simple subdocument response messages.
 *
 * @param request the current request.
 * @param msg the current response message.
 * @param ctx the handler context.
 * @param status the response status code.
 * @return the decoded response or null if none did match.
 */
private static CouchbaseResponse handleSubdocumentResponseMessages(BinaryRequest request, FullBinaryMemcacheResponse msg,
     ChannelHandlerContext ctx, ResponseStatus status, boolean seqOnMutation) {
    if (!(request instanceof BinarySubdocRequest))
        return null;
    BinarySubdocRequest subdocRequest = (BinarySubdocRequest) request;
    long cas = msg.getCAS();
    short statusCode = msg.getStatus();
    String bucket = request.bucket();

    MutationToken mutationToken = null;
    if (msg.getExtrasLength() > 0) {
        mutationToken = extractToken(bucket, seqOnMutation, status.isSuccess(), msg.getExtras(), request.partition());
    }

    ByteBuf fragment;
    if (msg.content() != null && msg.content().readableBytes() > 0) {
        fragment = msg.content();
    } else if (msg.content() != null) {
        msg.content().release();
        fragment = Unpooled.EMPTY_BUFFER;
    } else {
        fragment = Unpooled.EMPTY_BUFFER;
    }

    return new SimpleSubdocResponse(status, statusCode, bucket, fragment, subdocRequest, cas, mutationToken);
}
 
Example 9
Source Project: datacollector   Source File: TestCouchbaseTarget.java    License: Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private static CouchbaseConnector getConnector(Class responseClass) throws Exception {
  ENV = DefaultCouchbaseEnvironment.create();

  CouchbaseCore core = mock(CouchbaseCore.class);
  CouchbaseAsyncBucket asyncBucket = new CouchbaseAsyncBucket(core,
      ENV,
      BUCKET,
      USERNAME,
      PASSWORD,
      Collections.emptyList()
  );

  final CouchbaseRequest requestMock = mock(CouchbaseRequest.class);

  Subject<CouchbaseResponse, CouchbaseResponse> response = AsyncSubject.create();

  if(responseClass == SimpleSubdocResponse.class) {
    final BinarySubdocRequest subdocRequestMock = mock(BinarySubdocRequest.class);
    when(subdocRequestMock.span()).thenReturn(mock(Span.class));

    response.onNext(new SimpleSubdocResponse(ResponseStatus.SUCCESS,
        KeyValueStatus.SUCCESS.code(),
        BUCKET,
        Unpooled.EMPTY_BUFFER,
        subdocRequestMock,
        1234,
        null
    ));

    response.onCompleted();
  } else {

    Constructor con = responseClass.getConstructor(ResponseStatus.class,
        short.class,
        long.class,
        String.class,
        ByteBuf.class,
        MutationToken.class,
        CouchbaseRequest.class
    );

    response.onNext((CouchbaseResponse) con.newInstance(ResponseStatus.SUCCESS,
        KeyValueStatus.SUCCESS.code(),
        1234,
        BUCKET,
        Unpooled.EMPTY_BUFFER,
        null,
        requestMock
    ));
    response.onCompleted();
  }

  when(core.send(any(BinarySubdocRequest.class))).thenReturn(response);
  when(core.send(any())).thenReturn(response);
  when(requestMock.span()).thenReturn(mock(Span.class));

  CouchbaseConnector connector = mock(CouchbaseConnector.class);
  when(connector.getScheduler()).thenReturn(ENV.scheduler());
  when(connector.bucket()).thenReturn(asyncBucket);

  return connector;
}
 
Example 10
Source Project: incubator-gobblin   Source File: CouchbaseWriter.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public TupleDocument newDocument(String id, int expiry, Tuple2<ByteBuf, Integer> content, long cas,
    MutationToken mutationToken) {
  return new TupleDocument(id, expiry, content, cas);
}
 
Example 11
Source Project: incubator-gobblin   Source File: TupleDocument.java    License: Apache License 2.0 4 votes vote down vote up
public TupleDocument(String id, int expiry, Tuple2<ByteBuf, Integer> content, long cas, MutationToken mutationToken)
{
  super(id, expiry, content, cas, mutationToken);
}
 
Example 12
Source Project: couchbase-jvm-core   Source File: Observe.java    License: Apache License 2.0 4 votes vote down vote up
public static Observable<Boolean> call(final ClusterFacade core, final String bucket, final String id,
    final long cas, final boolean remove, MutationToken token, final PersistTo persistTo, final ReplicateTo replicateTo,
    final Delay delay, final RetryStrategy retryStrategy) {
    return call(core, bucket, id, cas, remove, token, persistTo, replicateTo, delay, retryStrategy, null);
}
 
Example 13
Source Project: couchbase-jvm-core   Source File: SimpleSubdocResponse.java    License: Apache License 2.0 4 votes vote down vote up
public SimpleSubdocResponse(ResponseStatus status, short serverStatusCode, String bucket, ByteBuf content,
                            BinarySubdocRequest request, long cas, MutationToken mutationToken) {
    super(status, serverStatusCode, bucket, content, request);
    this.cas = cas;
    this.mutationToken = mutationToken;
}
 
Example 14
Source Project: couchbase-jvm-core   Source File: SimpleSubdocResponse.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @return the {@link MutationToken} corresponding to a mutation of the document, if it was mutated and tokens are activated.
 */
public MutationToken mutationToken() {
    return mutationToken;
}
 
Example 15
Source Project: couchbase-jvm-core   Source File: MultiMutationResponse.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @return the {@link MutationToken} corresponding to a mutation of the document, if it was mutated and tokens are activated.
 */
public MutationToken mutationToken() {
    return mutationToken;
}
 
Example 16
Source Project: couchbase-jvm-core   Source File: KeyValueHandler.java    License: Apache License 2.0 4 votes vote down vote up
private static MutationToken extractToken(String bucket, boolean seqOnMutation, boolean success, ByteBuf extras, long vbid) {
    if (success && seqOnMutation) {
        return new MutationToken(vbid, extras.readLong(), extras.readLong(), bucket);
    }
    return null;
}
 
Example 17
Source Project: couchbase-jvm-core   Source File: MultiMutationResponse.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Creates a {@link MultiMutationResponse} that failed at subdocument level. The status, expected to be
 * {@link ResponseStatus#SUBDOC_MULTI_PATH_FAILURE}, denotes that at least one {@link MutationCommand} failed.
 *
 * @param status the status of the request (SUBDOC_MULTI_PATH_FAILURE).
 * @param serverStatusCode the status code of the whole request.
 * @param bucket the bucket on which the request happened.
 * @param firstErrorIndex the zero-based index of the first {@link MutationCommand} that failed (in case failure is
 *                        due to one or more commands).
 * @param firstErrorStatusCode the status code for the first {@link MutationCommand} that failed (in case failure is
 *                        due to one or more commands).
 * @param request the original {@link BinarySubdocMultiMutationRequest}.
 * @param cas the CAS value of the document after mutations.
 * @param mutationToken the {@link MutationToken} of the document after mutations, if available. Null otherwise.
 */
public MultiMutationResponse(ResponseStatus status, short serverStatusCode, String bucket, int firstErrorIndex, short firstErrorStatusCode,
                            BinarySubdocMultiMutationRequest request, long cas, MutationToken mutationToken) { //do cas and muto make sense here?
    super(status, serverStatusCode, bucket, Unpooled.EMPTY_BUFFER, request);
    this.cas = cas;
    this.mutationToken = mutationToken;
    this.firstErrorIndex = firstErrorIndex;
    if (firstErrorIndex == -1) {
        this.firstErrorStatus = ResponseStatus.FAILURE;
    } else {
        this.firstErrorStatus = ResponseStatusConverter.fromBinary(firstErrorStatusCode);
    }
    this.responses = Collections.emptyList();
}
 
Example 18
Source Project: couchbase-jvm-core   Source File: MultiMutationResponse.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Creates a unsuccessful {@link MultiMutationResponse} that failed at document level.
 *
 * First error index is set to -1 and first error status is set to {@link ResponseStatus#FAILURE}.
 *
 * @param status the failed status of the request.
 * @param serverStatusCode the status code of the whole request.
 * @param bucket the bucket on which the request happened.
 * @param request the original {@link BinarySubdocMultiMutationRequest}.
 * @param cas the CAS value of the document after mutations.
 * @param mutationToken the {@link MutationToken} of the document after mutations, if available. Null otherwise.
 */
public MultiMutationResponse(ResponseStatus status, short serverStatusCode, String bucket,
                             BinarySubdocMultiMutationRequest request, long cas, MutationToken mutationToken) { //do cas and muto make sense here?
    super(status, serverStatusCode, bucket, Unpooled.EMPTY_BUFFER, request);
    this.cas = cas;
    this.mutationToken = mutationToken;
    this.firstErrorIndex = -1;
    this.firstErrorStatus = ResponseStatus.FAILURE;
    this.responses = Collections.emptyList();
}