CouchbasePersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(objectToSave.getClass());
final CouchbasePersistentProperty versionProperty = persistentEntity.getVersionProperty();
final Long version = versionProperty != null ? beanWrapper.getProperty(versionProperty, Long.class) : null;
maybeEmitEvent(new BeforeConvertEvent<Object>(objectToSave));
final CouchbaseDocument converted = new CouchbaseDocument();
couchbaseConverter.write(objectToSave, converted);
maybeEmitEvent(new BeforeSaveEvent<Object>(objectToSave, converted));
execute(new BucketCallback<Boolean>() {
@Override
public Boolean doInBucket() throws InterruptedException, ExecutionException {
if (version == null) {
OperationFuture<Boolean> setFuture = client.set(converted.getId(), converted.getExpiration(),
translateEncode(converted), persistTo, replicateTo);
boolean future = setFuture.get();
if (!future) {
handleWriteResultError("Saving document failed: " + setFuture.getStatus().getMessage());
}
return future;
} else {
OperationFuture<CASResponse> casFuture = client.asyncCas(converted.getId(), version,
converted.getExpiration(), translateEncode(converted), persistTo, replicateTo);
CASResponse cas = casFuture.get();
if (cas == CASResponse.EXISTS) {
throw new OptimisticLockingFailureException("Saving document with version value failed: " + cas);
} else {
long newCas = casFuture.getCas();