MCPcopy Create free account
hub / github.com/antirez/ds4 / raxCompare

Function raxCompare

rax.c:2220–2246  ·  view source on GitHub ↗

Compare the key currently pointed by the iterator to the specified * key according to the specified operator. Returns 1 if the comparison is * true, otherwise 0 is returned. */

Source from the content-addressed store, hash-verified

2218 * key according to the specified operator. Returns 1 if the comparison is
2219 * true, otherwise 0 is returned. */
2220int raxCompare(raxIterator *iter, const char *op, unsigned char *key, size_t key_len) {
2221 int eq = 0, lt = 0, gt = 0;
2222
2223 if (op[0] == '=' || op[1] == '=') eq = 1;
2224 if (op[0] == '>') gt = 1;
2225 else if (op[0] == '<') lt = 1;
2226 else if (op[1] != '=') return 0; /* Syntax error. */
2227
2228 size_t minlen = key_len < iter->key_len ? key_len : iter->key_len;
2229 int cmp = memcmp(iter->key,key,minlen);
2230
2231 /* Handle == */
2232 if (lt == 0 && gt == 0) return cmp == 0 && key_len == iter->key_len;
2233
2234 /* Handle >, >=, <, <= */
2235 if (cmp == 0) {
2236 /* Same prefix: longer wins. */
2237 if (eq && key_len == iter->key_len) return 1;
2238 else if (lt) return iter->key_len < key_len;
2239 else if (gt) return iter->key_len > key_len;
2240 else return 0; /* Avoid warning, just 'eq' is handled before. */
2241 } else if (cmp > 0) {
2242 return gt ? 1 : 0;
2243 } else /* (cmp < 0) */ {
2244 return lt ? 1 : 0;
2245 }
2246}
2247
2248/* Update the data associated to the element currently selected by the
2249 * iterator. The operation works both for regular key nodes and for inline

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected