Skip to content

Instantly share code, notes, and snippets.

@bashnesnos
Created December 21, 2015 15:01
Show Gist options
  • Save bashnesnos/18b0423f9eb29159698c to your computer and use it in GitHub Desktop.
Save bashnesnos/18b0423f9eb29159698c to your computer and use it in GitHub Desktop.
TarantoolConnection16Pool
import org.tarantool.TarantoolConnection16;
import org.tarantool.TarantoolConnection16Impl;
import org.tarantool.TarantoolGenericConnection16;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class TarantoolConnection16Pool implements TarantoolConnection16 {
private final BlockingQueue<TarantoolConnection16> pool;
private final long acquireTimeout;
private final TimeUnit acquireTimeoutUnit;
public TarantoolConnection16Pool(int size, long acquireTimeout, TimeUnit acquireTimeoutUnit) {
this.acquireTimeout = acquireTimeout;
this.acquireTimeoutUnit = acquireTimeoutUnit;
pool = new ArrayBlockingQueue<TarantoolConnection16>(size);
for (int i = 0; i < size; i++) {
TarantoolConnection16 con;
try {
con = new TarantoolConnection16Impl("localhost", 3301);
} catch (IOException e) {
throw new RuntimeException(e);
}
con.auth("test", "test");
pool.add(con);
}
}
@Override
public List select(int i, int i1, Object o, int i2, int i3, int i4) {
try {
TarantoolConnection16 con = pool.poll(acquireTimeout, acquireTimeoutUnit);
List result = con.select(i, i1, o, i2, i3, i4);
pool.offer(con);
return result;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public List insert(int i, Object o) {
try {
TarantoolConnection16 con = pool.poll(acquireTimeout, acquireTimeoutUnit);
List result = con.insert(i, o);
pool.offer(con);
return result;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public List replace(int i, Object o) {
try {
TarantoolConnection16 con = pool.poll(acquireTimeout, acquireTimeoutUnit);
List result = con.replace(i, o);
pool.offer(con);
return result;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public List update(int i, Object o, Object... objects) {
try {
TarantoolConnection16 con = pool.poll(acquireTimeout, acquireTimeoutUnit);
List result = con.update(i, o, objects);
pool.offer(con);
return result;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public void upsert(int i, Object o, Object o1, Object... objects) {
try {
TarantoolConnection16 con = pool.poll(acquireTimeout, acquireTimeoutUnit);
con.upsert(i, o, o1, objects);
pool.offer(con);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public List delete(int i, Object o) {
try {
TarantoolConnection16 con = pool.poll(acquireTimeout, acquireTimeoutUnit);
List result = con.delete(i, o);
pool.offer(con);
return result;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public List call(String s, Object... objects) {
try {
TarantoolConnection16 con = pool.poll(acquireTimeout, acquireTimeoutUnit);
List result = con.call(s, objects);
pool.offer(con);
return result;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public List eval(String s, Object... objects) {
try {
TarantoolConnection16 con = pool.poll(acquireTimeout, acquireTimeoutUnit);
List result = con.eval(s, objects);
pool.offer(con);
return result;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public void auth(String s, String s1) {
throw new UnsupportedOperationException("Auth is done on init");
}
@Override
public <T> T schema(T t) {
try {
TarantoolConnection16 con = pool.poll(acquireTimeout, acquireTimeoutUnit);
T schema = con.schema(t);
pool.offer(con);
return schema;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public void ping() {
}
@Override
public void close() {
List<TarantoolConnection16> connections = new ArrayList<TarantoolConnection16>();
pool.drainTo(connections);
for (TarantoolConnection16 con : connections) {
con.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment