-
Notifications
You must be signed in to change notification settings - Fork 1
/
QueryableCache.cfc
561 lines (467 loc) · 17.4 KB
/
QueryableCache.cfc
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
component accessors = "true" extends = "lib.sql.QueryableCache" {
property name = "importBatchSize" type = "numeric" default = "1000";
property name = "language" type = "string" default = "english";
property name = "maxResults" type = "numeric" default = "1000";
property name = "minPrefixLength" type = "numeric" default = "2";
property name = "name" type = "string" setter = "false";
// https://oss.redislabs.com/redisearch/Stopwords.html
variables.DEFAULT_STOP_WORDS = "a is the an and are as at be but by for if in into it no not of on or such that their then there these they this to was will with";
lib.redis.QueryableCache function init(required lib.redis.ConnectionPool connectionPool, required string name) {
variables.connectionPool = arguments.connectionPool;
variables.name = lCase(arguments.name);
variables.normalizer = createObject("java", "java.text.Normalizer");
variables.normalizerForm = createObject("java", "java.text.Normalizer$Form").NFD;
variables.stopWords = "DEFAULT_STOP_WORDS";
return this;
}
boolean function containsRow() {
local.document = getClient().getDocument(getRowKey(argumentCollection = arguments));
return structKeyExists(local, "document");
}
void function createIndex() {
local.schema = createObject("java", "io.redisearch.Schema");
for(local.field in getQueryable().getFieldList()) {
if(getQueryable().fieldIsFilterable(local.field)) {
if(isRedisNumeric(local.field)) {
local.schema.addSortableNumericField(local.field);
} else {
local.schema.addSortableTextField("_NFD_" & local.field, javaCast("double", 1));
}
}
}
local.indexOptions = createObject("java", "io.redisearch.client.Client$IndexOptions").init(0);
if(len(variables.stopWords) == 0) {
local.indexOptions.setNoStopwords();
} else if(variables.stopWords == "DEFAULT_STOP_WORDS") {
// use the underlying default within redisearch, dont override in the index
variables.stopWords = variables.DEFAULT_STOP_WORDS;
} else {
local.indexOptions.setStopwords(listToArray(variables.stopWords, " "));
}
getClient().createIndex(
local.schema,
local.indexOptions
);
}
void function dropIndex() {
getClient().dropIndex();
}
query function executeSelect(required lib.sql.SelectStatement selectStatement, required numeric limit, required numeric offset) {
// not supported yet...
if(arrayLen(arguments.selectStatement.getAggregates()) > 0) {
throw(type = "lib.redis.UnsupportedOperationException", message = "Aggregates are not supported in this implementation");
}
// https://oss.redislabs.com/redisearch/Query_Syntax.html
local.queryString = arguments.selectStatement.getWhereSQL()
.REReplaceNoCase("^WHERE \(|\)$", "", "all")
.trim();
if(len(local.queryString) == 0) {
local.queryString = "*";
} else {
local.criteria = arguments.selectStatement.getWhereCriteria();
local.parameters = arguments.selectStatement.getParameters();
for(local.i = 1; local.i <= arrayLen(local.criteria); local.i++) {
// reset this on every iteration
structDelete(local, "clause");
structDelete(local, "value");
if(isRedisNumeric(local.criteria[local.i].field)) {
local.clause = "@#local.criteria[local.i].field#:";
if(getQueryable().getFieldSQLType(local.criteria[local.i].field) == "bit") {
local.value = local.parameters[local.i].value ? 1 : 0;
} else if(isRedisDate(local.criteria[local.i].field)) {
local.value = parseDateTime(local.parameters[local.i].value).getTime();
}
if(!structKeyExists(local, "value")) {
local.value = local.parameters[local.i].value;
}
switch(local.criteria[local.i].operator) {
case "!=":
local.clause = "-" & local.clause & "[#local.value# #local.value#]";
break;
case ">=":
local.clause &= "[#local.value# +inf]";
break;
case ">":
local.clause &= "[(#local.value# +inf]";
break;
case "<=":
local.clause &= "[-inf #local.value#]";
break;
case "<":
local.clause &= "[-inf (#local.value#]";
break;
case "IN":
case "NOT IN":
local.clause = listReduce(
local.value,
function(result, item) {
return listAppend(arguments.result, " " & clause & "[#arguments.item# #arguments.item#] ", "|");
},
"",
chr(31)
);
local.clause = "(" & trim(local.clause) & ")";
if(local.criteria[local.i].operator == "NOT IN") {
local.clause = "(-" & local.clause & ")";
}
break;
case "=":
local.clause &= "[#local.value# #local.value#]";
break;
default:
throw(type = "lib.redis.UnsupportedOperatorException", message = "#local.criteria[local.i].operator# is not supported for this field (#local.criteria[local.i].field#)");
break;
}
} else {
local.clause = "@_NFD_#local.criteria[local.i].field#:";
local.value = normalize(local.parameters[local.i].value);
switch(local.criteria[local.i].operator) {
case "LIKE":
// strip out stop words and format the value
local.value = listReduce(
local.value,
function(result, item) {
if(!listFindNoCase(variables.stopWords, arguments.item, " ") && len(arguments.item) >= getMinPrefixLength()) {
arguments.result = listAppend(arguments.result, clause & arguments.item & "*", " ");
}
return arguments.result;
},
"",
" "
);
// our criteria is no longer valid
if(local.value == "") {
local.searchException = true;
}
local.clause = "(" & local.value & ")";
break;
case "!=":
local.clause = "-" & local.clause & '"' & local.value & '"';
break;
case "IN":
case "NOT IN":
local.clause = listReduce(
local.value,
function(result, item) {
if(find(" ", arguments.item)) {
arguments.item = '"' & arguments.item & '"';
}
return listAppend(arguments.result, arguments.item, "|");
},
"",
chr(31)
);
local.clause = "(" & local.clause & ")";
if(local.criteria[local.i].operator == "NOT IN") {
local.clause = "(-" & local.clause & ")";
}
break;
case "=":
local.clause &= '"' & local.value & '"';
break;
default:
throw(type = "lib.redis.UnsupportedOperatorException", message = "#local.criteria[local.i].operator# is not supported for this field (#local.criteria[local.i].field#)");
break;
}
}
// replace our placeholders w/ the cache-friendly values
local.queryString = replace(local.queryString, local.criteria[local.i].statement, local.clause, "one");
}
}
local.docIDs = [];
if(!structKeyExists(local, "searchException")) {
try {
local.queryString = local.queryString
.replace(" AND ", " ", "all")
.replace(" OR ", " | ", "all")
.trim();
// why aggregation? redisearch only supports multiple sorts via the `aggregate` command
local.ab = createObject("java", "io.redisearch.aggregation.AggregationBuilder")
.init("'" & local.queryString & "'")
.apply((isRedisNumeric(getIdentifierField()) ? "@" : "@_NFD_") & getIdentifierField(), "id");
// sort
local.sortedField = createObject("java", "io.redisearch.aggregation.SortedField");
if(arrayLen(arguments.selectStatement.getOrderCriteria()) > 0) {
local.sortFields = arrayReduce(
arguments.selectStatement.getOrderCriteria(),
function(result, item) {
local.f = listFirst(arguments.item, " ");
if(isRedisNumeric(local.f)) {
local.f = "@" & local.f;
} else {
local.f = "@_NFD_" & local.f;
}
local.o = listLast(arguments.item, " ");
if(local.o == "DESC") {
arrayAppend(arguments.result, sortedField.desc(local.f));
} else {
arrayAppend(arguments.result, sortedField.asc(local.f));
}
return arguments.result;
},
[]
);
} else {
local.sortFields = [ local.sortedField.asc("@id") ];
}
local.maxResults = getMaxResults();
if(arguments.limit > 0) {
if(arguments.offset > 0) {
local.maxResults = arguments.offset + arguments.limit;
} else {
local.maxResults = arguments.limit;
}
}
local.ab.sortBy(javaCast("int", local.maxResults), local.sortFields);
// pagination
// look familiar? - this is the same logic as above... just not good way to re-use the condition, as the syntax must be assembled in order
if(arguments.limit > 0) {
if(arguments.offset > 0) {
local.ab.limit(arguments.offset, arguments.limit);
} else {
local.ab.limit(arguments.limit);
}
}
// convert the command to a string for debugging purposes
if(structKeyExists(arguments, "debug") && arguments.debug == "query") {
local.byteArray = [ createObject("java", "java.lang.String").init(javaCast("string", "")).getBytes() ];
local.ab.serializeRedisArgs(local.byteArray);
local.string = arrayReduce(
local.byteArray,
function(result, item) {
return listAppend(arguments.result, charsetEncode(arguments.item, "UTF-8"), " ");
},
""
);
throw(type = "lib.redis.DebugException", message = "ft.aggregate " & getName() & " " & local.string);
}
local.ids = getClient().aggregate(local.ab);
local.resultsLength = local.ids.totalResults;
for(local.i = 0; local.i < local.ids.getResults().size(); local.i++) {
arrayAppend(
local.docIDs,
// strip escape characters back out to get the ID
getRowKey(argumentCollection = { "#getIdentifierField()#": replace(local.ids.getRow(javaCast("int", local.i)).getString("id"), "\-", "-", "all") })
);
}
} catch(redis.clients.jedis.exceptions.JedisDataException e) {
// nada
local.searchException = true;
}
}
// queryNew only supports a subset of the data types that queryparam does
local.fieldSQLTypes = listReduce(
arguments.selectStatement.getSelect(),
function(v, i) {
switch(getFieldSQLType(i)) {
case "char":
return listAppend(arguments.v, "varchar");
break;
case "float":
case "money":
case "numeric":
case "real":
return listAppend(arguments.v, "double");
break;
case "smallint":
case "tinyint":
return listAppend(arguments.v, "integer");
break;
default:
return listAppend(arguments.v, getFieldSQLType(arguments.i));
break;
};
},
""
);
local.query = queryNew(arguments.selectStatement.getSelect(), local.fieldSQLTypes);
if(arrayLen(local.docIDs) > 0) {
local.documents = getClient().getDocuments(local.docIDs);
for(local.i = 0; local.i < local.documents.size(); local.i++) {
local.document = local.documents.get(local.i);
if(!isNull(local.document)) {
queryAddRow(
local.query,
fromRediSearchDocument(local.document, arguments.selectStatement.getSelect())
);
}
}
}
local.query
.getMetadata()
.setExtendedMetadata({
cached: !structKeyExists(local, "searchException"),
engine: "redis",
recordCount: arrayLen(local.docIDs),
totalRecordCount: (structKeyExists(local, "resultsLength") ? local.resultsLength : 0)
});
return local.query;
}
struct function fromRediSearchDocument(required any document, string fieldFilter = "") {
if(arguments.fieldFilter == "") {
arguments.fieldFilter = getFieldList();
}
// backfill any missing values w/ reasonable defaults for data type
return listReduce(
arguments.fieldFilter,
function(result, item) {
if(document.hasProperty(arguments.item)) {
local.value = document.getString(arguments.item);
if(isRedisDate(arguments.item) && isNumeric(val(local.value))) {
arguments.result[arguments.item] = createObject("java", "java.util.Date").init(javaCast("long", val(local.value)));
} else if(isRedisNumeric(arguments.item) && isNumeric(val(local.value))) {
arguments.result[arguments.item] = val(local.value);
} else if(len(local.value) > 0) {
arguments.result[arguments.item] = local.value;
}
}
if(!structKeyExists(arguments.result, arguments.item)) {
arguments.result[arguments.item] = javaCast("null", "");
}
return arguments.result;
},
{}
);
return {};
}
private any function getClient() {
return createObject("java", "io.redisearch.client.Client").init(variables.name, variables.connectionPool.getJedisPool());
}
any function getRow() {
local.document = getClient().getDocument(getRowKey(argumentCollection = arguments));
if(!isNull(local.document)) {
local.result = fromRediSearchDocument(local.document);
for(local.key in getFieldList()) {
if(!structKeyExists(local.result, local.key)) {
local.result[local.key] = "";
}
}
return local.result;
}
}
boolean function isRedisDate(required string field) {
switch(getQueryable().getFieldSQLType(arguments.field)) {
case "date":
case "time":
case "timestamp":
return true;
break;
};
return false;
}
boolean function isRedisNumeric(required string field) {
switch(getQueryable().getFieldSQLType(arguments.field)) {
case "bigint":
case "bit":
case "date":
case "decimal":
case "double":
case "float":
case "integer":
case "money":
case "numeric":
case "real":
case "smallint":
case "time":
case "timestamp":
case "tinyint":
return true;
break;
};
return false;
}
private string function normalize(required string input) {
arguments.input = lCase(arguments.input);
// https://oss.redislabs.com/redisearch/Escaping.html
arguments.input = listReduce(
arguments.input,
function(result, item) {
return listAppend(arguments.result, arguments.item.REReplace("\W+", " ", "all").trim(), chr(31));
},
"",
chr(31)
);
return variables.normalizer.normalize(javaCast("string", arguments.input), variables.normalizerForm).replaceAll("\p{InCombiningDiacriticalMarks}+", "");
}
void function putRow(required struct row) {
getClient().addDocument(
toRediSearchDocument(arguments.row),
createObject("java", "io.redisearch.client.AddOptions")
.setLanguage(getLanguage())
.setReplacementPolicy(createObject("java", "io.redisearch.client.AddOptions$ReplacementPolicy").PARTIAL)
);
}
void function removeRow() {
getClient().deleteDocument(javaCast("string", getRowKey(argumentCollection = arguments)), true);
}
void function seedFromQueryable(boolean overwrite = false) {
local.documents = [];
local.replacementPolicy = createObject("java", "io.redisearch.client.AddOptions$ReplacementPolicy");
local.addOptions = createObject("java", "io.redisearch.client.AddOptions")
.setLanguage(getLanguage())
.setReplacementPolicy(arguments.overwrite ? local.replacementPolicy.FULL : local.replacementPolicy.NONE);
for(local.row in getQueryable().select().execute()) {
arrayAppend(local.documents, toRediSearchDocument(local.row));
if(arrayLen(local.documents) == getImportBatchSize()) {
getClient().addDocuments(local.addOptions, local.documents);
local.documents = [];
}
}
if(arrayLen(local.documents) > 0) {
getClient().addDocuments(local.addOptions, local.documents);
}
}
lib.sql.QueryableCache function setQueryable(required lib.sql.IQueryable queryable) {
if(structKeyExists(variables, "queryable")) {
throw(type = "lib.redis.QueryableDefinedException", message = "an IQueryable has been furnished already");
}
super.setQueryable(arguments.queryable);
if(!structKeyExists(variables, "rowKeyMask")) {
setRowKeyMask(lCase(getName() & ":" & getIdentifierField() & ":{" & getIdentifierField() & "}"));
}
try{
// create the index
createIndex();
} catch(redis.clients.jedis.exceptions.JedisDataException e) {
// index exists already
}
return this;
}
lib.sql.QueryableCache function setStopWords(required string stopWords) {
if(structKeyExists(variables, "queryable")) {
throw(type = "lib.redis.QueryableDefinedException", message = "an IQueryable has been furnished already");
}
variables.stopWords = arguments.stopWords;
return this;
}
any function toRediSearchDocument(required struct row) {
if(!structKeyExists(arguments.row, getIdentifierField())) {
throw(type = "lib.redis.MissingIdentifierException", message = "the identifier field #getIdentifierField()# must be provided");
}
local.document = createObject("java", "io.redisearch.Document").init(getRowKey(argumentCollection = arguments.row));
for(local.field in getQueryable().getFieldList()) {
if(!structKeyExists(arguments.row, local.field)) {
arguments.row[local.field] = "";
}
if(isRedisDate(local.field)) {
if(isDate(arguments.row[local.field])) {
local.document.set(local.field, javaCast("long", arguments.row[local.field].getTime()));
}
} else if(isRedisNumeric(local.field)) {
if(isNumeric(arguments.row[local.field]) || isBoolean(arguments.row[local.field])) {
if(listFindNoCase("bigint,bit,integer,smallint,tinyint", getQueryable().getFieldSQLType(local.field))) {
local.document.set(local.field, javaCast("long", arguments.row[local.field]));
} else {
local.document.set(local.field, javaCast("double", arguments.row[local.field]));
}
}
} else {
local.document.set(local.field, javaCast("string", arguments.row[local.field]));
if(getQueryable().fieldIsFilterable(local.field)) {
local.document.set("_NFD_" & local.field, javaCast("string", normalize(arguments.row[local.field])));
}
}
}
return local.document;
}
}