
com.aerospike.client.reactor.AerospikeReactorClient Maven / Gradle / Ivy
/*
* Copyright 2012-2018 Aerospike, Inc.
*
* Portions may be licensed to Aerospike, Inc. under one or more contributor
* license agreements WHICH ARE COMPATIBLE WITH THE APACHE LICENSE, VERSION 2.0.
*
* 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.aerospike.client.reactor;
import com.aerospike.client.*;
import com.aerospike.client.async.EventLoops;
import com.aerospike.client.policy.*;
import com.aerospike.client.query.KeyRecord;
import com.aerospike.client.query.Statement;
import com.aerospike.client.reactor.dto.KeyExists;
import com.aerospike.client.reactor.dto.KeyObject;
import com.aerospike.client.reactor.dto.KeysExists;
import com.aerospike.client.reactor.dto.KeysRecords;
import com.aerospike.client.reactor.listeners.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.List;
/**
* Instantiate an AerospikeReactorClient
object to access an Aerospike
* database cluster and perform database operations reactively.
*
* This client is just wrapper over AerospikeClient async methods that provides Reactor interface
*
* This client is thread-safe. One client instance should be used per cluster.
* Multiple threads should share this cluster instance.
*
* Your application uses this class API to perform database operations such as
* writing and reading records, and selecting sets of records. Write operations
* include specialized functionality such as append/prepend and arithmetic
* addition.
*
* Each record may have multiple bins, unless the Aerospike server nodes are
* configured as "single-bin". In "multi-bin" mode, partial records may be
* written or read by specifying the relevant subset of bins.
*
* @author Sergii Karpenko
*/
public class AerospikeReactorClient implements IAerospikeReactorClient{
private final IAerospikeClient aerospikeClient;
private final EventLoops eventLoops;
public AerospikeReactorClient(IAerospikeClient aerospikeClient, EventLoops eventLoops) {
this.aerospikeClient = aerospikeClient;
this.eventLoops = eventLoops;
}
@Override
public void close(){
aerospikeClient.close();
}
@Override
public final Mono get(Key key) throws AerospikeException {
return get(null, key);
}
@Override
public final Mono get(Policy policy, Key key) throws AerospikeException {
return Mono.create(sink -> aerospikeClient.get(
eventLoops.next(), new ReactorRecordListener(sink), policy, key));
}
@Override
public final Mono get(Key[] keys) throws AerospikeException {
return get(null, keys);
}
@Override
public final Mono get(BatchPolicy policy, Key[] keys) throws AerospikeException {
return Mono.create(sink -> aerospikeClient.get(
eventLoops.next(), new ReactorRecordArrayListener(sink), policy, keys));
}
@Override
public final Mono> get(List records) throws AerospikeException {
return get(null, records);
}
@Override
public final Mono> get(BatchPolicy policy, List records) throws AerospikeException {
return Mono.create(sink -> aerospikeClient.get(
eventLoops.next(), new ReactorBatchListListener(sink), policy, records));
}
@Override
public final Flux getFlux(List records) throws AerospikeException {
return getFlux(null, records);
}
@Override
public final Flux getFlux(BatchPolicy policy, List records) throws AerospikeException {
return Flux.create(sink -> aerospikeClient.get(
eventLoops.next(), new ReactorBatchSequenceListener(sink), policy, records));
}
@Override
public final Flux getFlux(Key[] keys) throws AerospikeException {
return getFlux(null, keys);
}
@Override
public final Flux getFlux(BatchPolicy policy, Key[] keys) throws AerospikeException {
return Flux.create(sink -> aerospikeClient.get(
eventLoops.next(), new ReactorRecordSequenceListener(sink), policy, keys));
}
@Override
public final Mono getHeader(Key key) throws AerospikeException {
return getHeader(null, key);
}
@Override
public final Mono getHeader(Policy policy, Key key) throws AerospikeException {
return Mono.create(sink -> aerospikeClient.getHeader(
eventLoops.next(), new ReactorRecordListener(sink), policy, key));
}
@Override
public final Mono getHeaders(Key[] keys) throws AerospikeException {
return getHeaders(null, keys);
}
@Override
public final Mono getHeaders(BatchPolicy policy, Key[] keys) throws AerospikeException {
return Mono.create(sink -> aerospikeClient.getHeader(
eventLoops.next(), new ReactorRecordArrayListener(sink),
policy, keys));
}
@Override
public final Mono touch(Key key) throws AerospikeException {
return touch(null, key);
}
@Override
public final Mono touch(WritePolicy policy, Key key) throws AerospikeException {
return Mono.create(sink -> aerospikeClient.touch(
eventLoops.next(), new ReactorWriteListener(sink), policy, key));
}
@Override
public final Mono exists(Key key) throws AerospikeException {
return exists(null, key);
}
@Override
public final Mono exists(Policy policy, Key key) throws AerospikeException {
return Mono.create(sink -> aerospikeClient.exists(
eventLoops.next(), new ReactorExistsListener(sink), policy, key));
}
@Override
public final Mono exists(Key[] keys) throws AerospikeException {
return exists(null, keys);
}
@Override
public final Mono exists(BatchPolicy policy, Key[] keys) throws AerospikeException{
return Mono.create(sink -> aerospikeClient.exists(
eventLoops.next(), new ReactorExistsArrayListener(sink), policy, keys));
}
@Override
public final Flux existsFlux(Key[] keys) throws AerospikeException {
return existsFlux(null, keys);
}
@Override
public final Flux existsFlux(BatchPolicy policy, Key[] keys) throws AerospikeException {
return Flux.create(sink -> aerospikeClient.exists(
eventLoops.next(), new ReactorExistsSequenceListener(sink), policy, keys));
}
@Override
public final Mono put(Key key, Bin... bins) throws AerospikeException {
return put(null, key, bins);
}
@Override
public final Mono put(WritePolicy policy, Key key, Bin... bins) throws AerospikeException {
return Mono.create(sink -> aerospikeClient.put(
eventLoops.next(), new ReactorWriteListener(sink), policy, key, bins));
}
@Override
public final Mono append(Key key, Bin... bins) throws AerospikeException {
return append(null, key, bins);
}
@Override
public final Mono append(WritePolicy policy, Key key, Bin... bins) throws AerospikeException {
return Mono.create(sink -> aerospikeClient.append(
eventLoops.next(), new ReactorWriteListener(sink), policy, key, bins));
}
@Override
public final Mono prepend(Key key, Bin... bins) throws AerospikeException {
return prepend(null, key, bins);
}
@Override
public final Mono prepend(WritePolicy policy, Key key, Bin... bins) throws AerospikeException {
return Mono.create(sink -> aerospikeClient.prepend(
eventLoops.next(), new ReactorWriteListener(sink), policy, key, bins));
}
@Override
public final Mono add(Key key, Bin... bins) throws AerospikeException {
return add(null, key, bins);
}
@Override
public final Mono add(WritePolicy policy, Key key, Bin... bins) throws AerospikeException {
return Mono.create(sink -> aerospikeClient.add(
eventLoops.next(), new ReactorWriteListener(sink), policy, key, bins));
}
@Override
public final Mono delete(Key key) throws AerospikeException {
return delete(null, key);
}
@Override
public final Mono delete(WritePolicy policy, Key key) throws AerospikeException {
return Mono.create(sink -> aerospikeClient.delete(
eventLoops.next(), new ReactorDeleteListener(sink), policy, key));
}
@Override
public final Mono operate(Key key, Operation... operations) throws AerospikeException {
return operate(null, key, operations);
}
@Override
public final Mono operate(WritePolicy policy, Key key, Operation... operations) throws AerospikeException {
return Mono.create(sink -> aerospikeClient.operate(
eventLoops.next(), new ReactorRecordListener(sink), policy, key, operations));
}
@Override
public final Flux query(Statement statement) throws AerospikeException {
return query(null, statement);
}
@Override
public final Flux query(QueryPolicy policy, Statement statement) throws AerospikeException {
return Flux.create(sink -> aerospikeClient.query(eventLoops.next(),
new ReactorRecordSequenceListener(sink), policy, statement));
}
@Override
public final Flux scanAll(String namespace, String setName, String... binNames) throws AerospikeException {
return scanAll(null, namespace, setName, binNames);
}
@Override
public final Flux scanAll(ScanPolicy policy, String namespace, String setName, String... binNames) throws AerospikeException {
return Flux.create(sink -> aerospikeClient.scanAll(
eventLoops.next(), new ReactorRecordSequenceListener(sink),
policy, namespace, setName, binNames));
}
@Override
public final Mono execute(Key key, String packageName, String functionName, Value... functionArgs) throws AerospikeException {
return execute(null, key, packageName, functionName, functionArgs);
}
@Override
public final Mono execute(WritePolicy policy, Key key,
String packageName, String functionName, Value... functionArgs) throws AerospikeException {
return Mono.create(sink -> aerospikeClient.execute(
eventLoops.next(), new ReactorExecuteListener(sink),
policy, key, packageName, functionName, functionArgs));
}
}