org.tarantool.ConnectionState Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of connector Show documentation
Show all versions of connector Show documentation
Tarantool client for java
package org.tarantool;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.EnumMap;
import java.util.Map;
public class ConnectionState {
protected EnumMap header = new EnumMap(Key.class);
protected EnumMap body = new EnumMap(Key.class);
protected ByteBufferStreams buffer = new ByteBufferStreams(ByteBuffer.allocate(32 * 1024), 1.1d);
public ByteBuffer getWelcomeBuffer() {
ByteBuffer buf = buffer.getBuf();
buf.clear();
buf.limit(64);
return buf;
}
public ByteBuffer getLengthReadBuffer() {
ByteBuffer buf = buffer.getBuf();
buf.clear();
buf.limit(5);
return buf;
}
public ByteBuffer getPacketReadBuffer() {
ByteBuffer buf = buffer.getBuf();
buf.limit(buf.position());
buf.rewind();
try {
long size = (Long) MsgPackLite.unpack(buffer.asInputStream(), 0);
buf.clear();
buffer.checkCapacity((int) size);
buf = buffer.getBuf();
buf.position(0);
buf.limit((int)size);
return buf;
} catch (IOException e) {
//this shouldn't happens
throw new IllegalStateException(e);
}
}
public void unpack() {
ByteBuffer buf = buffer.getBuf();
buf.limit(buf.position());
buf.rewind();
body.clear();
header.clear();
try {
toKeyMap(MsgPackLite.unpack(buffer.asInputStream(), MsgPackLite.OPTION_UNPACK_RAW_AS_STRING), header);
if (buf.remaining() > 0) {
toKeyMap(MsgPackLite.unpack(buffer.asInputStream(), MsgPackLite.OPTION_UNPACK_RAW_AS_STRING), body);
}
} catch (IOException e) {
//this shouldn't happens
throw new IllegalStateException(e);
}
}
private void toKeyMap(Object unpack, EnumMap result) {
Map map = (Map) unpack;
for (Map.Entry entry : map.entrySet()) {
Key key = Key.getById(entry.getKey());
if (key != null) {
result.put(key, entry.getValue());
}
}
}
public ByteBuffer pack(Code code, Object[] args) {
return pack(code, null, args);
}
public ByteBuffer pack(Code code, Long syncId, Object[] args) {
beginBody();
if (args != null) {
for (int i = 0, e = args.length; i < e; i += 2) {
Object value = args[i + 1];
put((Key) args[i], value);
}
}
endBody();
return pack(code, syncId, body);
}
public ByteBuffer pack(Code code, Object sync, EnumMap body) {
header.clear();
header.put(Key.CODE, code);
if (sync != null) {
header.put(Key.SYNC, sync);
}
ByteBuffer buf = buffer.getBuf();
buf.clear();
buf.put((byte) 0xce);
buf.putInt(0);
try {
MsgPackLite.pack(header, buffer.asOutputStream());
MsgPackLite.pack(body, buffer.asOutputStream());
} catch (IOException e) {
//this shouldn't happens
throw new IllegalStateException(e);
}
//buffer can be changed during write process
buf = buffer.getBuf();
int size = buf.position();
buf.putInt(1, size - 5);
buf.limit(size);
buf.rewind();
return buf;
}
public ConnectionState beginBody() {
body.clear();
return this;
}
public ConnectionState put(Key key, Object value) {
if (value != null) {
body.put(key, value);
}
return this;
}
public EnumMap endBody() {
return body;
}
public EnumMap getHeader() {
return header;
}
public EnumMap getBody() {
return body;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy