-
Notifications
You must be signed in to change notification settings - Fork 14
/
IntraOp.java
174 lines (146 loc) · 5.11 KB
/
IntraOp.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
package org.usergrid.vx.experimental;
import com.google.common.base.Preconditions;
import org.apache.cassandra.db.ConsistencyLevel;
import java.io.Serializable;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
public class IntraOp implements Serializable{
public static final String COLUMN= "COLUMN";
public static final String VALUE="VALUE";
// TODO add back access to ID
private final AtomicInteger opid= new AtomicInteger(0);
private static final long serialVersionUID = 4700563595579293663L;
private final Type type;
private Map<String,Object> op;
IntraOp(Type type){
this.type = type;
op = new TreeMap<String,Object>();
}
public IntraOp set(String key, Object value){
op.put(key,value);
return this;
}
public Map<String, Object> getOp() {
return op;
}
public static IntraOp setKeyspaceOp(String keyspace){
checkForBlankStr(keyspace, "keyspace", Type.SETKEYSPACE);
IntraOp i = new IntraOp(Type.SETKEYSPACE);
i.set("keyspace", keyspace);
return i;
}
public static IntraOp setColumnFamilyOp(String columnFamily){
checkForBlankStr(columnFamily, "columnFamily",Type.SETCOLUMNFAMILY);
IntraOp i = new IntraOp(Type.SETCOLUMNFAMILY);
i.set("columnfamily", columnFamily);
return i;
}
public static IntraOp setAutotimestampOp(){
IntraOp i = new IntraOp(Type.AUTOTIMESTAMP);
return i;
}
public static IntraOp setOp(Object rowkey, Object columnName, Object columnValue){
Preconditions.checkArgument(rowkey != null, "The rowkey cannot be null for " + Type.SET);
Preconditions.checkArgument(columnName != null, "The columnName cannot be null for " + Type.SET);
Preconditions.checkArgument(columnValue != null, "Cannot set a column to null for " + Type.SET);
IntraOp i = new IntraOp(Type.SET);
i.set("rowkey", rowkey);
i.set("columnName", columnName);
i.set("value", columnValue);
return i;
}
public static IntraOp getOp(Object rowkey, Object columnName){
Preconditions.checkArgument(rowkey != null, "The rowkey cannot be null for " + Type.GET);
Preconditions.checkArgument(columnName != null, "The columnName cannot be null for " + Type.GET);
IntraOp i = new IntraOp(Type.GET);
i.set("rowkey", rowkey);
i.set("column", columnName);
return i;
}
public static IntraOp getResRefOp(int reference, String wanted){
Preconditions.checkArgument(reference >= 0);
checkForBlankStr(wanted, "wanted", Type.GETREF);
IntraOp i = new IntraOp(Type.GETREF);
i.set("resultref", reference);
i.set("wanted", wanted);
return i;
}
public static IntraOp sliceOp( Object rowkey , Object start, Object end, int size){
Preconditions.checkArgument(rowkey != null,"A row key is required for " + Type.SLICE);
Preconditions.checkArgument(size > 0, "A slice size must be positive integer for " + Type.SLICE);
IntraOp i = new IntraOp(Type.SLICE);
i.set("rowkey", rowkey);
i.set("start", start);
i.set("end", end);
i.set("size", size);
return i;
}
public static IntraOp columnPredicateOp( Object rowkey, Object [] columnList){
Preconditions.checkArgument(columnList != null, "You much provide a columnList array");
IntraOp i = new IntraOp(Type.COLUMNPREDICATE);
i.set("wantedcols", columnList);
return i;
}
public static IntraOp forEachOp( int opRef, IntraOp action){
Preconditions.checkArgument(action != null, "The IntraOp action cannot be null");
IntraOp i = new IntraOp(Type.FOREACH);
i.set("action", action);
return i;
}
public static IntraOp createCfOp(String cfName){
checkForBlankStr(cfName, "columnFamily name", Type.CREATECOLUMNFAMILY);
IntraOp i = new IntraOp(Type.CREATECOLUMNFAMILY);
i.set("name", cfName);
return i;
}
public static IntraOp createKsOp(String ksname, int replication){
checkForBlankStr(ksname, "keyspace name", Type.CREATEKEYSPACE);
Preconditions.checkArgument(replication > 0,
"A value for positive value for 'replication' is required for " + Type.CREATEKEYSPACE);
IntraOp i = new IntraOp(Type.CREATEKEYSPACE);
i.set("name", ksname);
i.set("replication", replication);
return i;
}
public static IntraOp consistencyOp(String name){
// Force an IllegalArgumentException
ConsistencyLevel.valueOf(name);
IntraOp i = new IntraOp(Type.CONSISTENCY);
i.set("level", name);
return i;
}
public static IntraOp listKeyspacesOp(){
IntraOp i = new IntraOp(Type.LISTKEYSPACES);
return i;
}
public static IntraOp listColumnFamilyOp(String keyspace){
checkForBlankStr(keyspace, "Keyspace name", Type.LISTCOLUMNFAMILY);
IntraOp i = new IntraOp(Type.LISTCOLUMNFAMILY);
i.set("keyspace", keyspace);
return i;
}
public Type getType() {
return type;
}
private static void checkForBlankStr(String arg, String msg, Type type) {
Preconditions.checkArgument(arg != null && arg.length() > 0,
"A non-blank '" + msg + "' is required for " + type);
}
public enum Type {
LISTCOLUMNFAMILY,
LISTKEYSPACES,
CONSISTENCY,
CREATEKEYSPACE,
CREATECOLUMNFAMILY,
FOREACH,
COLUMNPREDICATE,
SLICE,
GETREF,
GET,
SET,
AUTOTIMESTAMP,
SETKEYSPACE,
SETCOLUMNFAMILY;
}
}