com.couchbase.client.java.repository.CouchbaseAsyncRepository Maven / Gradle / Ivy
/*
* Copyright (c) 2016 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.couchbase.client.java.repository;
import com.couchbase.client.core.annotations.InterfaceAudience;
import com.couchbase.client.core.annotations.InterfaceStability;
import com.couchbase.client.java.AsyncBucket;
import com.couchbase.client.java.PersistTo;
import com.couchbase.client.java.ReplicaMode;
import com.couchbase.client.java.ReplicateTo;
import com.couchbase.client.java.document.Document;
import com.couchbase.client.java.document.EntityDocument;
import com.couchbase.client.java.document.JsonDocument;
import com.couchbase.client.java.repository.mapping.DefaultEntityConverter;
import com.couchbase.client.java.repository.mapping.EntityConverter;
import rx.Observable;
import rx.functions.Func1;
@InterfaceAudience.Public
@InterfaceStability.Experimental
public class CouchbaseAsyncRepository implements AsyncRepository {
private final EntityConverter converter;
private final AsyncBucket bucket;
public CouchbaseAsyncRepository(AsyncBucket bucket) {
this.bucket = bucket;
converter = new DefaultEntityConverter();
}
@Override
public Observable> get(String id, final Class entityClass) {
return Observable
.just(id)
.flatMap(new Func1>() {
@Override
public Observable call(String id) {
return bucket.get(id);
}
})
.map(new DocumentToType(entityClass));
}
@Override
public Observable> getFromReplica(String id, final ReplicaMode type, Class entityClass) {
return Observable
.just(id)
.flatMap(new Func1>() {
@Override
public Observable call(String id) {
return bucket.getFromReplica(id, type);
}
})
.map(new DocumentToType(entityClass));
}
@Override
public Observable> getAndLock(String id, final int lockTime, Class entityClass) {
return Observable
.just(id)
.flatMap(new Func1>() {
@Override
public Observable call(String id) {
return bucket.getAndLock(id, lockTime);
}
})
.map(new DocumentToType(entityClass));
}
@Override
public Observable> getAndTouch(String id, final int expiry, Class entityClass) {
return Observable
.just(id)
.flatMap(new Func1>() {
@Override
public Observable call(String id) {
return bucket.getAndTouch(id, expiry);
}
})
.map(new DocumentToType(entityClass));
}
@Override
public Observable> upsert(final EntityDocument document) {
return upsert(document, PersistTo.NONE, ReplicateTo.NONE);
}
@Override
public Observable> upsert(EntityDocument document, PersistTo persistTo) {
return upsert(document, persistTo, ReplicateTo.NONE);
}
@Override
public Observable> upsert(EntityDocument document, ReplicateTo replicateTo) {
return upsert(document, PersistTo.NONE, replicateTo);
}
@Override
public Observable> upsert(final EntityDocument document, final PersistTo persistTo, final ReplicateTo replicateTo) {
return Observable
.just(document)
.flatMap(new Func1, Observable extends Document>>>() {
@Override
public Observable extends Document>> call(EntityDocument source) {
Document> converted = converter.fromEntity(source);
return bucket.upsert(converted, persistTo, replicateTo);
}
})
.map(new Func1, EntityDocument>() {
@Override
public EntityDocument call(Document> stored) {
return EntityDocument.create(document.id(), document.expiry(), document.content(), stored.cas());
}
});
}
@Override
public Observable> insert(final EntityDocument document) {
return insert(document, PersistTo.NONE, ReplicateTo.NONE);
}
@Override
public Observable> insert(EntityDocument document, PersistTo persistTo) {
return insert(document, persistTo, ReplicateTo.NONE);
}
@Override
public Observable> insert(EntityDocument document, ReplicateTo replicateTo) {
return insert(document, PersistTo.NONE, replicateTo);
}
@Override
public Observable> insert(final EntityDocument document, final PersistTo persistTo, final ReplicateTo replicateTo) {
return Observable
.just(document)
.flatMap(new Func1, Observable extends Document>>>() {
@Override
public Observable extends Document>> call(EntityDocument source) {
Document> converted = converter.fromEntity(source);
return bucket.insert(converted, persistTo, replicateTo);
}
})
.map(new Func1, EntityDocument>() {
@Override
public EntityDocument call(Document> stored) {
return EntityDocument.create(document.id(), document.expiry(), document.content(), stored.cas());
}
});
}
@Override
public Observable> replace(final EntityDocument document) {
return replace(document, PersistTo.NONE, ReplicateTo.NONE);
}
@Override
public Observable> replace(EntityDocument document, PersistTo persistTo) {
return replace(document, persistTo, ReplicateTo.NONE);
}
@Override
public Observable> replace(EntityDocument document, ReplicateTo replicateTo) {
return replace(document, PersistTo.NONE, replicateTo);
}
@Override
public Observable> replace(final EntityDocument document, final PersistTo persistTo, final ReplicateTo replicateTo) {
return Observable
.just(document)
.flatMap(new Func1, Observable extends Document>>>() {
@Override
public Observable extends Document>> call(EntityDocument source) {
Document> converted = converter.fromEntity(source);
return bucket.replace(converted, persistTo, replicateTo);
}
})
.map(new Func1, EntityDocument>() {
@Override
public EntityDocument call(Document> stored) {
return EntityDocument.create(document.id(), document.expiry(), document.content(), stored.cas());
}
});
}
@Override
public Observable exists(String id) {
return bucket.exists(id);
}
@Override
public Observable exists(EntityDocument document) {
return Observable
.just(document)
.map(new Func1, String>() {
@Override
public String call(EntityDocument source) {
Document> converted = converter.fromEntity(source);
return converted.id();
}
})
.flatMap(new Func1>() {
@Override
public Observable call(String id) {
return exists(id);
}
});
}
@Override
public Observable> remove(EntityDocument document) {
return remove(document, PersistTo.NONE, ReplicateTo.NONE);
}
@Override
public Observable> remove(EntityDocument document, PersistTo persistTo) {
return remove(document, persistTo, ReplicateTo.NONE);
}
@Override
public Observable> remove(EntityDocument document, ReplicateTo replicateTo) {
return remove(document, PersistTo.NONE, replicateTo);
}
@Override
public Observable> remove(final EntityDocument document, final PersistTo persistTo, final ReplicateTo replicateTo) {
return Observable
.just(document)
.map(new Func1, String>() {
@Override
public String call(EntityDocument source) {
Document> converted = converter.fromEntity(source);
return converted.id();
}
})
.flatMap(new Func1>>() {
@Override
@SuppressWarnings("unchecked")
public Observable> call(String id) {
return remove(id, persistTo, replicateTo, (Class) document.content().getClass());
}
});
}
@Override
public Observable> remove(String id, Class entityClass) {
return remove(id, PersistTo.NONE, ReplicateTo.NONE, entityClass);
}
@Override
public Observable> remove(String id, PersistTo persistTo, Class entityClass) {
return remove(id, persistTo, ReplicateTo.NONE, entityClass);
}
@Override
public Observable> remove(String id, ReplicateTo replicateTo, Class entityClass) {
return remove(id, PersistTo.NONE, replicateTo, entityClass);
}
@Override
public Observable> remove(String id, final PersistTo persistTo, final ReplicateTo replicateTo,
Class entityClass) {
return Observable
.just(id)
.flatMap(new Func1>() {
@Override
public Observable call(String id) {
return bucket.remove(id, persistTo, replicateTo);
}
})
.map(new DocumentToType(entityClass));
}
class DocumentToType implements Func1> {
private final Class entityClass;
public DocumentToType(Class entityClass) {
this.entityClass = entityClass;
}
@Override
@SuppressWarnings("unchecked")
public EntityDocument call(JsonDocument document) {
return converter.toEntity(document, entityClass);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy