10
10
import io .tarantool .driver .api .tuple .TarantoolTupleFactory ;
11
11
import io .tarantool .driver .api .tuple .operations .TupleOperations ;
12
12
import org .junit .jupiter .api .BeforeAll ;
13
+ import org .junit .jupiter .api .BeforeEach ;
13
14
import org .junit .jupiter .api .Test ;
14
15
15
16
import java .util .ArrayList ;
16
17
import java .util .Collections ;
17
18
import java .util .List ;
19
+ import java .util .Optional ;
18
20
import java .util .concurrent .ExecutionException ;
19
21
20
22
import static org .junit .jupiter .api .Assertions .*;
21
23
22
24
/**
23
25
* @author Ivan Dneprov
24
26
* <p>
25
- * WARNING: If you updated the code in this file, don't forget to update the docs/ProxyTarantoolClient.md permalinks!
27
+ * WARNING: If you updated the code in this file, don't forget to update the docs/ProxyTarantoolClient.md
28
+ * and docs/TarantoolTupleUsage.md permalinks!
26
29
*/
27
30
public class ProxyTarantoolClientExampleIT extends SharedCartridgeContainer {
28
31
@@ -38,7 +41,6 @@ public class ProxyTarantoolClientExampleIT extends SharedCartridgeContainer {
38
41
public static void setUp () throws Exception {
39
42
startCluster ();
40
43
initClient ();
41
- truncateSpace (SPACE_NAME );
42
44
}
43
45
44
46
public static void initClient () {
@@ -55,8 +57,9 @@ public static void initClient() {
55
57
.build ();
56
58
}
57
59
58
- private static void truncateSpace (String spaceName ) {
59
- client .space (spaceName ).truncate ().join ();
60
+ @ BeforeEach
61
+ public void truncateSpace () {
62
+ client .space (SPACE_NAME ).truncate ().join ();
60
63
}
61
64
62
65
@ Test
@@ -106,4 +109,56 @@ public void clusterInsertSelectDeleteTest() throws ExecutionException, Interrupt
106
109
TarantoolResult <TarantoolTuple > updateResult = space .update (conditions , TupleOperations .set (4 , 10 )).get ();
107
110
assertEquals (10 , updateResult .get (0 ).getInteger (4 ));
108
111
}
112
+
113
+ @ Test
114
+ public void tarantoolTupleUsageExample () throws ExecutionException , InterruptedException , NullPointerException {
115
+ TarantoolSpaceOperations <TarantoolTuple , TarantoolResult <TarantoolTuple >> accounts =
116
+ client .space ("accounts" );
117
+
118
+ // Use TarantoolTupleFactory for instantiating new tuples
119
+ TarantoolTupleFactory tupleFactory = new DefaultTarantoolTupleFactory (
120
+ client .getConfig ().getMessagePackMapper ());
121
+
122
+ // Create a tuple from listed values: [1, null, "credit card", 99.99]
123
+ // This tuple contains java values
124
+ TarantoolTuple inputTuple = tupleFactory .create (1 , null , "credit card" , 99.99 );
125
+
126
+ // Insert it in the database
127
+ accounts .insert (inputTuple ).join ();
128
+
129
+ // This tuple form the database
130
+ Conditions conditions = Conditions .equals ("id" , 1 );
131
+ TarantoolResult <TarantoolTuple > selectResult = accounts .select (conditions ).get ();
132
+ assertEquals (selectResult .size (), 1 );
133
+ // This tuple contains messagePack values
134
+ TarantoolTuple selectTuple = selectResult .get (0 );
135
+ assertEquals (selectTuple .size (), 4 );
136
+
137
+ // You can get value from TarantoolTuple by its filedPosition
138
+ // If you do not set objectClass default converter will be used for this value
139
+ Optional <?> object = selectTuple .getObject (0 );
140
+ assertEquals (1 , object .orElseThrow (NullPointerException ::new ));
141
+
142
+ // For example any non-integer number will be converted to Double by default
143
+ Optional <?> doubleValue = selectTuple .getObject (3 );
144
+ assertEquals (99.99 , doubleValue .orElseThrow (NullPointerException ::new ));
145
+ assertEquals (Double .class , doubleValue .orElseThrow (NullPointerException ::new ).getClass ());
146
+
147
+ // But if you need to get Float, you can set objectClass
148
+ Optional <?> floatValue = selectTuple .getObject (3 , Float .class );
149
+ assertEquals (99.99f , floatValue .orElseThrow (NullPointerException ::new ));
150
+ assertEquals (Float .class , floatValue .orElseThrow (NullPointerException ::new ).getClass ());
151
+
152
+ // You do not have to work with Optional
153
+ // Getters for all basic types are available
154
+ float floatNumber = selectTuple .getFloat (3 );
155
+ assertEquals (99.99f , floatNumber );
156
+
157
+ // Also you can get values by field name
158
+ Optional <?> balance = selectTuple .getObject ("balance" );
159
+ assertEquals (99.99 , balance .orElseThrow (NullPointerException ::new ));
160
+
161
+ String stringValue = selectTuple .getString ("name" );
162
+ assertEquals ("credit card" , stringValue );
163
+ }
109
164
}
0 commit comments