This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
/
Index.java
446 lines (402 loc) · 14.4 KB
/
Index.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* Copyright 2017 Pilosa Corp.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
package com.pilosa.client.orm;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pilosa.client.Validator;
import com.pilosa.client.exceptions.ValidationException;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import java.util.HashMap;
import java.util.Map;
/**
* The purpose of the Index is to represent a data namespace.
* <p>
* You cannot perform cross-index queries. Column-level attributes are global to the Index.
*
* @see <a href="https://www.pilosa.com/docs/data-model/">Data Model</a>
* @see <a href="https://www.pilosa.com/docs/query-language/">Query Language</a>
*/
public class Index {
/**
* Creates an index with a name.
*
* @param name index name
* @return a Index object
* @throws ValidationException if the passed index name is not valid
*/
public static Index create(String name) {
return create(name, IndexOptions.withDefaults());
}
/**
* Creates an index with a name and options.
*
* @param name index name
* @param options index options
* @return a Index object
* @throws ValidationException if the passed index name is not valid
*/
public static Index create(String name, IndexOptions options) {
Validator.ensureValidIndexName(name);
return new Index(name, options);
}
public String getName() {
return this.name;
}
/**
* Returns a field object with the specified name and defaults.
*
* @param name field name
* @return a Field object
* @throws ValidationException if the passed field name is not valid
*/
public Field field(String name) {
return this.field(name, FieldOptions.withDefaults());
}
/**
* Returns a field with the specified name and options.
*
* @param name field name
* @param options field options
* @return a Field object
* @throws ValidationException if the passed field name is not valid
*/
public Field field(String name, FieldOptions options) {
if (this.fields.containsKey(name)) {
return this.fields.get(name);
}
Field field = Field.create(this, name, options);
this.fields.put(name, field);
return field;
}
public Field field(String name, Map<String, Object> options) {
return field(name, FieldOptions.fromMap(options));
}
/**
* Copies other field to this index and returns the new field
*
* @param other field
* @return copied field
*/
public Field field(Field other) {
return field(other.getName(), other.getOptions());
}
/**
* Checks whether the index has the given field.
*
* @param fieldName
* @return <code>true</code> if the field exists.
*/
public boolean hasField(String fieldName) {
return this.fields.containsKey(fieldName);
}
/**
* Creates a batch query.
*
* @return batch query
*/
public PqlBatchQuery batchQuery() {
return new PqlBatchQuery(this);
}
/**
* Creates a batch query which has the specified query count pre-allocated.
* <p>
* If the number of queries in the batch is known beforehand, calling this constructor
* is more efficient than calling it without the query count.
*
* Note that <code>queryCount</code> is not the limit of queries in the batch; the batch can
* grow beyond that.
*
* @param queryCount number of queries expected to be in the batch
* @return batch query
*/
@SuppressWarnings("WeakerAccess")
public PqlBatchQuery batchQuery(int queryCount) {
return new PqlBatchQuery(this, queryCount);
}
/**
* Creates a batch query with the given queries
*
* @param queries the queries in the batch
* @return PqlBatchQuery
*/
public PqlBatchQuery batchQuery(PqlQuery... queries) {
return new PqlBatchQuery(this, queries);
}
/**
* Creates a raw query.
* <p>
* Note that the query is not validated before sending to the server.
*
* @param query raw query
* @return a Pql query
*/
public PqlBaseQuery rawQuery(String query) {
PqlBaseQuery q = new PqlBaseQuery(query, this);
// raw queries are always assumed to have keys
q.query.setWriteKeys(true);
return q;
}
/**
* Creates a Union query.
* <p>
* Union performs a logical OR on the results of each ROW_CALL query passed to it.
*
* @param rows 2 or more rows to union
* @return a PQL query
* @see <a href="https://www.pilosa.com/docs/query-language/#union">Union Query</a>
*/
@SuppressWarnings("WeakerAccess")
public PqlRowQuery union(PqlRowQuery... rows) {
return rowOperation("Union", rows);
}
/**
* Creates an Intersect query.
* <p>
* Intersect performs a logical AND on the results of each ROW_CALL query passed to it.
*
* @param rows 2 or more rows to intersect
* @return a PQL query
* @throws IllegalArgumentException if the number of rows is less than 1
* @see <a href="https://www.pilosa.com/docs/query-language/#intersect">Intersect Query</a>
*/
@SuppressWarnings("WeakerAccess")
public PqlRowQuery intersect(PqlRowQuery... rows) {
if (rows.length < 1) {
throw new IllegalArgumentException("Intersect operation requires at least 1 row");
}
return rowOperation("Intersect", rows);
}
/**
* Creates a Difference query.
* <p>
* Difference returns all of the bits from the first ROW_CALL argument
* passed to it, without the bits from each subsequent ROW_CALL.
*
* @param rows 1 or more rows to differentiate
* @return a PQL query
* @throws IllegalArgumentException if the number of rows is less than 1
* @see <a href="https://www.pilosa.com/docs/query-language/#difference">Difference Query</a>
*/
@SuppressWarnings("WeakerAccess")
public PqlRowQuery difference(PqlRowQuery... rows) {
if (rows.length < 1) {
throw new IllegalArgumentException("Difference operation requires at least 1 row");
}
return rowOperation("Difference", rows);
}
/**
* Creates an Xor query.
*
* @param rows 2 or more rows to xor
* @return a PQL query
* @throws IllegalArgumentException if the number of rows is less than 2
* @see <a href="https://www.pilosa.com/docs/query-language/#xor">Xor Query</a>
*/
public PqlRowQuery xor(PqlRowQuery... rows) {
if (rows.length < 2) {
throw new IllegalArgumentException("Difference operation requires at least 2 rows");
}
return rowOperation("Xor", rows);
}
public PqlRowQuery not(PqlRowQuery row) {
return pqlRowQuery(String.format("Not(%s)", row.serialize().getQuery()));
}
/**
* Creates a Count query.
* <p>
* Count returns the number of set bits in the ROW_CALL passed in.
*
* @param row the row query
* @return a PQL query
* @see <a href="https://www.pilosa.com/docs/query-language/#count">Count Query</a>
*/
public PqlBaseQuery count(PqlRowQuery row) {
return pqlQuery(String.format("Count(%s)", row.serialize().getQuery()), false);
}
/**
* Creates a SetColumnAttrs query.
* <p>
* SetColumnAttrs associates arbitrary key/value pairs with a column in an index.
* <p>
* Following object types are accepted:
* <ul>
* <li>Long</li>
* <li>String</li>
* <li>Boolean</li>
* <li>Double</li>
* </ul>
*
* @param id column ID
* @param attributes column attributes
* @return a PQL query
* @see <a href="https://www.pilosa.com/docs/query-language/#setcolumnattrs">SetColumnAttrs Query</a>
*/
public PqlBaseQuery setColumnAttrs(long id, Map<String, Object> attributes) {
String attributesString = Util.createAttributesString(mapper, attributes);
boolean hasKeys = this.getOptions().isKeys();
return pqlQuery(String.format("SetColumnAttrs(%d,%s)", id, attributesString), hasKeys);
}
/**
* Creates a SetColumnAttrs query. (Enterprise version)
* <p>
* SetColumnAttrs associates arbitrary key/value pairs with a column in an index.
* <p>
* Following object types are accepted:
* <ul>
* <li>Long</li>
* <li>String</li>
* <li>Boolean</li>
* <li>Double</li>
* </ul>
*
* @param key column key
* @param attributes column attributes
* @return a PQL query
* @see <a href="https://www.pilosa.com/docs/query-language/#setcolumnattrs">SetColumnAttrs Query</a>
*/
public PqlBaseQuery setColumnAttrs(String key, Map<String, Object> attributes) {
String attributesString = Util.createAttributesString(mapper, attributes);
String text = String.format("SetColumnAttrs('%s',%s)", key, attributesString);
return pqlQuery(text, this.getOptions().isKeys());
}
public PqlBaseQuery options(PqlRowQuery row, OptionsOptions opts) {
String text = String.format("Options(%s,%s)", row.serialize().getQuery(), opts.serialize());
return pqlQuery(text, false);
}
public PqlBaseQuery groupBy(PqlRowsQuery... queries) {
return groupBy(0, null, queries);
}
public PqlBaseQuery groupBy(Integer limit, PqlRowsQuery... queries) {
return groupBy(limit, null, queries);
}
public PqlBaseQuery groupBy(PqlRowQuery filterQuery, PqlRowsQuery... queries) {
return groupBy(0, filterQuery, queries);
}
public PqlBaseQuery groupBy(Integer limit, PqlRowQuery filterQuery, PqlRowsQuery... queries) {
if (queries.length < 1) {
throw new IllegalArgumentException("there should be at least one rows query");
}
StringBuilder builder = new StringBuilder("GroupBy(");
builder.append(joinQueries(queries));
if (limit > 0) {
builder.append(",limit=");
builder.append(limit);
}
if (filterQuery != null) {
builder.append(",filter=");
builder.append(filterQuery.serialize().getQuery());
}
builder.append(")");
return pqlQuery(builder.toString(), false);
}
public Map<String, Field> getFields() {
return this.fields;
}
public IndexOptions getOptions() {
return this.options;
}
public long getShardWidth() {
return this.shardWidth;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Index)) {
return false;
}
if (obj == this) {
return true;
}
Index rhs = (Index) obj;
return rhs.name.equals(this.name) &&
rhs.fields.equals(this.fields) &&
rhs.getOptions().equals(this.options);
}
@Override
public int hashCode() {
// note that we don't include fields in the hash
return new HashCodeBuilder(31, 47)
.append(this.name)
.toHashCode();
}
PqlBaseQuery pqlQuery(String query, boolean hasKeys) {
PqlBaseQuery q = new PqlBaseQuery(query, this);
if (hasKeys) {
q.query.setWriteKeys(true);
}
return q;
}
PqlRowQuery pqlRowQuery(String query) {
return new PqlRowQuery(query, this);
}
PqlRowsQuery pqlRowsQuery(String query) {
return new PqlRowsQuery(query, this);
}
Index(Index index) {
this(index.name, index.options);
for (Map.Entry<String, Field> entry : index.fields.entrySet()) {
// we don't copy field options, since FieldOptions has no mutating methods
this.field(entry.getKey(), entry.getValue().getOptions());
}
}
Index setShardWidth(long shardWidth) {
this.shardWidth = shardWidth;
return this;
}
private PqlRowQuery rowOperation(String name, PqlRowQuery... rows) {
String text = String.format("%s(%s)", name, joinQueries(rows));
return pqlRowQuery(text);
}
private static String joinQueries(PqlBaseQuery... queries) {
if (queries.length == 0) {
return "";
}
StringBuilder builder = new StringBuilder(queries.length - 1);
SerializedQuery q = queries[0].serialize();
builder.append(q.getQuery());
for (int i = 1; i < queries.length; i++) {
builder.append(",");
q = queries[i].serialize();
builder.append(q.getQuery());
}
return builder.toString();
}
private Index(String name, IndexOptions options) {
this.name = name;
this.options = options;
}
private String name;
private IndexOptions options;
private static ObjectMapper mapper = new ObjectMapper();
private Map<String, Field> fields = new HashMap<>();
private long shardWidth;
}