This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
forked from hector-client/hector
/
MutatorTest.java
236 lines (199 loc) · 9.21 KB
/
MutatorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package me.prettyprint.cassandra.model;
import static me.prettyprint.hector.api.factory.HFactory.createColumn;
import static me.prettyprint.hector.api.factory.HFactory.createCounterColumn;
import static me.prettyprint.hector.api.factory.HFactory.createColumnQuery;
import static me.prettyprint.hector.api.factory.HFactory.createKeyspace;
import static me.prettyprint.hector.api.factory.HFactory.createMutator;
import static me.prettyprint.hector.api.factory.HFactory.createSuperColumn;
import static me.prettyprint.hector.api.factory.HFactory.getOrCreateCluster;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import me.prettyprint.cassandra.BaseEmbededServerSetupTest;
import me.prettyprint.cassandra.model.thrift.ThriftCounterColumnQuery;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.utils.StringUtils;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.beans.HSuperColumn;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.MutationResult;
import me.prettyprint.hector.api.mutation.Mutator;
import me.prettyprint.hector.api.query.ColumnQuery;
import me.prettyprint.hector.api.query.CounterQuery;
import me.prettyprint.hector.api.query.QueryResult;
import me.prettyprint.hector.api.query.SuperColumnQuery;
import org.apache.cassandra.thrift.ColumnPath;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class MutatorTest extends BaseEmbededServerSetupTest {
private static final StringSerializer se = new StringSerializer();
private Cluster cluster;
private Keyspace keyspace;
@Before
public void setupCase() {
cluster = getOrCreateCluster("Test Cluster", "127.0.0.1:9170");
keyspace = createKeyspace("Keyspace1", cluster);
}
@After
public void teardownCase() {
keyspace = null;
cluster = null;
}
@Test
public void testInsert() {
Mutator<String> m = createMutator(keyspace, se);
MutationResult mr = m.insert("k", "Standard1", createColumn("name", "value", se, se));
assertTrue("Execution time on single insert should be > 0",mr.getExecutionTimeMicro() > 0);
assertTrue("Should have operated on a host", mr.getHostUsed() != null);
assertColumnExists("Keyspace1", "Standard1", "k", "name");
}
@Test
public void testInsertAndDeleteSuper() {
Mutator<String> m = createMutator(keyspace, se);
List<HColumn<String, String>> columnList = new ArrayList<HColumn<String,String>>();
columnList.add(createColumn("name","value",se,se));
HSuperColumn<String, String, String> superColumn = createSuperColumn("super_name", columnList, se, se, se);
// Insert Super Column
MutationResult r = m.insert("sk", "Super1", superColumn);
assertTrue("Execute time should be > 0", r.getExecutionTimeMicro() > 0);
assertTrue("Should have operated on a host", r.getHostUsed() != null);
// Fetch and verify it exists.
SuperColumnQuery<String, String, String, String> scq = HFactory.createSuperColumnQuery(keyspace, se, se, se, se);
scq.setColumnFamily("Super1");
scq.setKey("sk");
scq.setSuperName("super_name");
assertEquals("super_name", scq.execute().get().getName());
// Remove the Super Column
m.superDelete("sk", "Super1", "super_name", se);
// Fetch and verify it exists.
scq = HFactory.createSuperColumnQuery(keyspace, se, se, se, se);
scq.setColumnFamily("Super1");
scq.setKey("sk");
scq.setSuperName("super_name");
assertNull("super_name", scq.execute().get());
}
@Test
public void testSubDelete() {
Mutator<String> m = createMutator(keyspace, se);
List<HColumn<String, String>> columnList = new ArrayList<HColumn<String,String>>();
columnList.add(createColumn("col_1","val_1",se,se));
columnList.add(createColumn("col_2","val_2",se,se));
columnList.add(createColumn("col_3","val_3",se,se));
HSuperColumn<String, String, String> superColumn =
createSuperColumn("super_name", columnList, se, se, se);
MutationResult r = m.insert("sk1", "Super1", superColumn);
SuperColumnQuery<String, String, String, String> scq = HFactory.createSuperColumnQuery(keyspace, se, se, se, se);
scq.setColumnFamily("Super1");
scq.setKey("sk1");
scq.setSuperName("super_name");
assertEquals(3,scq.execute().get().getColumns().size());
m.discardPendingMutations();
m.addSubDelete("sk1", "Super1", "super_name", "col_1", se, se);
m.execute();
assertEquals(2,scq.execute().get().getColumns().size());
}
@Test
public void testSubDeleteHSuperColumn() {
Mutator<String> m = createMutator(keyspace, se);
List<HColumn<String, String>> columnList = new ArrayList<HColumn<String,String>>();
columnList.add(createColumn("col_1","val_1",se,se));
columnList.add(createColumn("col_2","val_2",se,se));
columnList.add(createColumn("col_3","val_3",se,se));
HSuperColumn<String, String, String> superColumn =
createSuperColumn("super_name", columnList, se, se, se);
MutationResult r = m.insert("sk1", "Super1", superColumn);
SuperColumnQuery<String, String, String, String> scq = HFactory.createSuperColumnQuery(keyspace, se, se, se, se);
scq.setColumnFamily("Super1");
scq.setKey("sk1");
scq.setSuperName("super_name");
assertEquals(3,scq.execute().get().getColumns().size());
m.discardPendingMutations();
columnList.remove(1);
columnList.remove(0);
superColumn.setSubcolumns(columnList);
m.addSubDelete("sk1", "Super1", superColumn);
m.execute();
assertEquals(2,scq.execute().get().getColumns().size());
}
@Test
public void testBatchMutationManagement() {
String cf = "Standard1";
Mutator<String> m = createMutator(keyspace, se);
for (int i = 0; i < 5; i++) {
m.addInsertion("k" + i, cf, createColumn("name", "value" + i, se, se));
}
assertEquals(5, m.getPendingMutationCount());
MutationResult r = m.execute();
assertTrue("Execute time should be > 0", r.getExecutionTimeMicro() > 0);
assertTrue("Should have operated on a host", r.getHostUsed() != null);
for (int i = 0; i < 5; i++) {
assertColumnExists("Keyspace1", "Standard1", "k"+i, "name");
}
// Execute an empty mutation
r = m.execute();
assertEquals("Execute time should be 0", 0, r.getExecutionTimeMicro());
// Test discard and then exec an empty mutation
for (int i = 0; i < 5; i++) {
m.addInsertion("k" + i, cf, createColumn("name", "value" + i, se, se));
}
m.discardPendingMutations();
r = m.execute();
assertEquals("Execute time should be 0", 0, r.getExecutionTimeMicro());
assertTrue("Should have operated with a null host", r.getHostUsed() == null);
// cleanup
for (int i = 0; i < 5; i++) {
m.addDeletion("k" + i, cf, "name", se);
}
m.execute();
}
@Test
public void testRowDeletion() {
String cf = "Standard1";
long initialTime = keyspace.createClock();
Mutator<String> m = createMutator(keyspace, se);
for (int i = 0; i < 5; i++) {
m.addInsertion("key" + i, cf, createColumn("name", "value" + i, se, se));
}
m.execute();
// Try to delete the row with key "k0" with a clock previous to the insertion.
// Cassandra will discard this operation.
m.addDeletion("key0", cf, null, se, (initialTime - 100));
m.execute();
// Check that the delete was harmless
QueryResult<HColumn<String, String>> columnResult =
createColumnQuery(keyspace, se, se, se).setColumnFamily(cf).setKey("key0").
setName("name").execute();
assertEquals("value0", columnResult.get().getValue());
for (int i = 0; i < 5; i++) {
m.addDeletion("key" + i, cf, null, se);
}
m.execute();
// Check that the delete took place now
columnResult = createColumnQuery(keyspace, se, se, se).
setColumnFamily(cf).setKey("key0").setName("name").execute();
assertNull(columnResult.get());
}
@Test
public void testInsertCounter() {
Mutator<String> m = createMutator(keyspace, se);
MutationResult mr = m.insertCounter("k", "Counter1", createCounterColumn("name", 5));
assertTrue("Execution time on single counter insert should be > 0", mr.getExecutionTimeMicro() > 0);
assertTrue("Should have operated on a host", mr.getHostUsed() != null);
CounterQuery<String, String> counter = new ThriftCounterColumnQuery<String,String>(keyspace, se, se);
counter.setColumnFamily("Counter1").setKey("k").setName("name");
assertEquals(new Long(5), counter.execute().get().getValue());
}
private void assertColumnExists(String keyspace, String cf, String key, String column) {
ColumnPath cp = new ColumnPath(cf);
cp.setColumn(StringUtils.bytes(column));
Keyspace ks = HFactory.createKeyspace(keyspace, cluster);
ColumnQuery<String, String, String> columnQuery = HFactory.createStringColumnQuery(ks);
assertNotNull(String.format("Should have value for %s.%s[%s][%s]", keyspace, cf, key, column),
columnQuery.setColumnFamily(cf).setKey(key).setName(column).execute().get().getValue());
//client.getKeyspace(keyspace).getColumn(key, cp));
//cluster.releaseClient(client);
}
}