io.tarantool.driver.protocol.TarantoolRequestBody Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cartridge-driver Show documentation
Show all versions of cartridge-driver Show documentation
Tarantool Cartridge driver for Tarantool versions 1.10+ based on Netty framework
package io.tarantool.driver.protocol;
import org.msgpack.value.IntegerValue;
import org.msgpack.value.Value;
import org.msgpack.value.ValueFactory;
import io.tarantool.driver.mappers.MessagePackObjectMapper;
import io.tarantool.driver.mappers.MessagePackValueMapperException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Represents basic Tarantool request body
*
* @author Alexey Kuzin
*/
public class TarantoolRequestBody implements Packable {
private Map values;
/**
* In rare cases, the body may be empty. Creates a request with empty body
*/
public TarantoolRequestBody() {
values = Collections.emptyMap();
}
/**
* Basic constructor. Takes a typical {@link Map} with {@code Integer} keys and {@code Object} values.
* Converts values into MessagePack entities using the passed instance of {@link MessagePackObjectMapper}.
* See
* https://www.tarantool.io/en/doc/2.3/dev_guide/internals/box_protocol/#binary-protocol-requests
* @param body request body
* @param mapper provides mapping for Java objects to MessagePack entities
* @throws TarantoolProtocolException in case if mapping of body parts to objects failed
*/
public TarantoolRequestBody(Map body, MessagePackObjectMapper mapper)
throws TarantoolProtocolException {
try {
this.values = new HashMap<>(body.size(), 1);
for (Integer key: body.keySet()) {
values.put(ValueFactory.newInteger(key), mapper.toValue(body.get(key)));
}
} catch (MessagePackValueMapperException e) {
throw new TarantoolProtocolException(e);
}
}
@Override
public Value toMessagePackValue(MessagePackObjectMapper mapper) {
return ValueFactory.newMap(values);
}
}