|
| 1 | +# Proposal |
| 2 | +As described in Redis Module API modules have more benefints than general LUA scripts. In current environment, lua script locking db for a long period. The main reason of this module: provide non db-locking access for time-consuming operations. |
| 3 | + |
| 4 | +# Design |
| 5 | +* Projects consists from one redis module `filtered_sort` and node.js wrapper `redis-filtered-sort`. |
| 6 | + |
| 7 | +* By default, module going to have 2 build options: Docker based build for redis:5.0-alpine and local system `gcc` |
| 8 | + |
| 9 | +* Exported functions for dev purpose prefixed with "c" prefix |
| 10 | + |
| 11 | +* Current module behaviour copied from existing scripts for compat with lua. |
| 12 | + |
| 13 | +* in Redis Cluster environment: all keys and sets MUST use {prefix} notation, to be stored on same node. Otherwise module won't work correctly. |
| 14 | + |
| 15 | + |
| 16 | +## Expected API and behaviour |
| 17 | +Module will export into Redis command namespace these commands: |
| 18 | +#### cfsort |
| 19 | +Sorts, filters and paginates provided SET `idSetKey` using data from `metaKeyPattern`.`hashKey` using `sortOrder`,`filter` and stores processed data for `expire` seconds |
| 20 | + |
| 21 | +* **Method usage**: `cfsort idSetKey metaKeyPattern hashKey sortOrder filter(jsonFormattedString) curTime:unixtime [offset:int] [limit:int] [expire:int - ttl in seconds] [keyOnly]`; |
| 22 | + - **idSetKey**: Redis SET containing id values |
| 23 | + - **metaKeyPattern**: Pattern to find HASHes of record, "*" is replaced by id from `idSetKey`, eg: _*-metakey_ produce _id-231-metakey_ |
| 24 | + - **hashKey**: field name from HASHkey formed from `metakeyPattern` |
| 25 | + - **filter**: Json string with filters |
| 26 | +* **General behaviour**: During work process function going to store postprocessed data into 2 temporary sets(sorted and filterd) in format `{idSetKey}:{order}:{metaKey}:{hasKey}:{filterString}` and reuse them while `ttl` not past. |
| 27 | + |
| 28 | + Creates index of this sets in ZSET `tempKeysSet`. |
| 29 | + > _Definitely I don't understand why this needed, but we have cacheBuster_ in lua. #SeeLater |
| 30 | +
|
| 31 | + - If `metaKeyPattern` and `hashKey` not provided, function going to use `idSetKey` values for sorting, otherwise fetch data from `metaKeyPattern.gsub("*", idSetMemberValue).providedHashKey` and use them for sort. |
| 32 | + |
| 33 | + - Same as prev behaviour going to be used in filtering process. |
| 34 | + |
| 35 | + - If more than one filter provided: `filterString` will contain only "#" filter; |
| 36 | + |
| 37 | + |
| 38 | +### cfsortBust : |
| 39 | +Checking caches for specified `idSetKey` that was created in result of `cfsort` method work. Deletes outdated tempKeys from ZSET `tempKeysSet` |
| 40 | + |
| 41 | +**Method usage**: `cfsort idSetKey curTime:unixtime [expire:int]` |
| 42 | + |
| 43 | +### cfsortAggregate: |
| 44 | +Groups elements by `aggregateParam` from set `idSet` using meta available under `metaKeyPattern` and reports count of each type of criteria. |
| 45 | + |
| 46 | +Currently supports only `sum`; |
| 47 | + |
| 48 | +* **Method usage**: `cfsortAggregate idSetKey metaKeyPattern aggregateParam` |
| 49 | + - **idSetKey**: Redis SET containing id values |
| 50 | + - **metaKeyPattern**: Pattern to find HASHes of record, "*" is replaced by id from `idSetKey`, eg: _*-metakey_ produce _id-231-metakey_ |
| 51 | + - **aggregateParam** : JsonString containing aggregate criteria. |
| 52 | + ```Javascript |
| 53 | + { |
| 54 | + "fieldName": "sum" |
| 55 | + } |
| 56 | + ``` |
| 57 | + |
| 58 | + |
| 59 | +## Filter Format `filter` parameter: |
| 60 | +Accepting `json` formated object: |
| 61 | +```Javascript |
| 62 | + { |
| 63 | + "fieldToFiler": { |
| 64 | + //Filter params... |
| 65 | + } |
| 66 | + "fieldToFilter": "String to find" |
| 67 | + } |
| 68 | +``` |
| 69 | + |
| 70 | +- **Available criteria**: |
| 71 | + - `lte`,`gte` - (int) value comparison |
| 72 | + - `eq`,`ne` - equal or not |
| 73 | + - `any` - Object containing criteria list to Exclude |
| 74 | + - `some` - Object containing criteria list to Include |
| 75 | + - `match` - String contains |
| 76 | + - `exists` - if value exists |
| 77 | + |
| 78 | +- **MultiField(#multi)**: Reserved field name for assigning criterias to multiple fields from `meta` record(currently supports only _String contains check_) |
| 79 | + ```Javascript |
| 80 | + { |
| 81 | + "#multi": { |
| 82 | + "fields": [ |
| 83 | + //Field list |
| 84 | + ... |
| 85 | + ], |
| 86 | + "match": "criteria" |
| 87 | + } |
| 88 | + } |
| 89 | + ``` |
| 90 | + |
| 91 | + |
| 92 | +**Example** |
| 93 | +```Javascript |
| 94 | + { |
| 95 | + "metaFieldName": "contained string", |
| 96 | + "metaFieldName2": { //Filter params object |
| 97 | + "gte": 12, |
| 98 | + "lte": 13, |
| 99 | + }, |
| 100 | + "metaFieldAnyParams": { |
| 101 | + "any": { |
| 102 | + "0": { |
| 103 | + "gte": 12, |
| 104 | + "lte": 13, |
| 105 | + }, |
| 106 | + "1": { |
| 107 | + "gte": 28, |
| 108 | + "lte": 30, |
| 109 | + } |
| 110 | + } |
| 111 | + }, |
| 112 | +
|
| 113 | +
|
| 114 | + } |
| 115 | + ``` |
| 116 | + |
| 117 | + |
0 commit comments