Skip to content

Latest commit

 

History

History
10 lines (7 loc) · 518 Bytes

TarantoolTupleUsage.md

File metadata and controls

10 lines (7 loc) · 518 Bytes

Main page

TarantoolTuple usage

You can use TarantoolTuple for creating tuple which can be sent to the Tarantool instance or can be returned from default tarantool/crud or box methods. You can create TarantoolTuple with this factory TarantoolTupleFactory. See an example below:

TarantoolSpaceOperations<TarantoolTuple, TarantoolResult<TarantoolTuple>> accounts =
client.space("accounts");
// Use TarantoolTupleFactory for instantiating new tuples
TarantoolTupleFactory tupleFactory = new DefaultTarantoolTupleFactory(
client.getConfig().getMessagePackMapper());
// Create a tuple from listed values: [1, null, "credit card", 99.99]
// This tuple contains java values
TarantoolTuple inputTuple = tupleFactory.create(1, null, "credit card", 99.99);
// Insert it in the database
accounts.insert(inputTuple).join();
// This tuple form the database
Conditions conditions = Conditions.equals("id", 1);
TarantoolResult<TarantoolTuple> selectResult = accounts.select(conditions).get();
assertEquals(selectResult.size(), 1);
// This tuple contains messagePack values
TarantoolTuple selectTuple = selectResult.get(0);
assertEquals(selectTuple.size(), 4);
// You can get value from TarantoolTuple by its filedPosition
// If you do not set objectClass default converter will be used for this value
Optional<?> object = selectTuple.getObject(0);
assertEquals(1, object.orElseThrow(NullPointerException::new));
// For example any non-integer number will be converted to Double by default
Optional<?> doubleValue = selectTuple.getObject(3);
assertEquals(99.99, doubleValue.orElseThrow(NullPointerException::new));
assertEquals(Double.class, doubleValue.orElseThrow(NullPointerException::new).getClass());
// But if you need to get Float, you can set objectClass
Optional<?> floatValue = selectTuple.getObject(3, Float.class);
assertEquals(99.99f, floatValue.orElseThrow(NullPointerException::new));
assertEquals(Float.class, floatValue.orElseThrow(NullPointerException::new).getClass());
// You do not have to work with Optional
// Getters for all basic types are available
float floatNumber = selectTuple.getFloat(3);
assertEquals(99.99f, floatNumber);
// Also you can get values by field name
Optional<?> balance = selectTuple.getObject("balance");
assertEquals(99.99, balance.orElseThrow(NullPointerException::new));
String stringValue = selectTuple.getString("name");
assertEquals("credit card", stringValue);