All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.couchbase.client.java.repository.CouchbaseAsyncRepository Maven / Gradle / Ivy

There is a newer version: 3.7.9
Show newest version
/**
 * Copyright (C) 2015 Couchbase, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
 * IN THE SOFTWARE.
 */
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>>() {
                @Override
                public Observable> 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>>() {
                @Override
                public Observable> 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>>() {
                @Override
                public Observable> 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