forked from RedisJSON/RedisJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.c
136 lines (120 loc) · 3.49 KB
/
path.c
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
/*
* Copyright (C) 2016-2017 Redis Labs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "path.h"
Node *__pathNode_eval(PathNode *pn, Node *n, PathError *err) {
*err = E_OK;
if (!n) {
goto badtype;
}
if (n->type == N_ARRAY) {
Node *rn = NULL;
if (NT_INDEX == pn->type) {
int index = pn->value.index;
// translate negative values
if (index < 0) index = n->value.arrval.len + index;
int rc = Node_ArrayItem(n, index, &rn);
if (rc != OBJ_OK) {
*err = E_NOINDEX;
}
} else {
goto badtype;
}
return rn;
}
if (n->type == N_DICT) {
if (pn->type != NT_KEY) {
goto badtype;
}
Node *rn = NULL;
int rc = Node_DictGet(n, pn->value.key, &rn);
if (rc != OBJ_OK) {
*err = E_NOKEY;
}
return rn;
}
badtype:
*err = E_BADTYPE;
return NULL;
}
PathError SearchPath_Find(SearchPath *path, Node *root, Node **n) {
Node *current = root;
PathError ret;
for (int i = 0; i < path->len; i++) {
current = __pathNode_eval(&path->nodes[i], current, &ret);
if (ret != E_OK) {
*n = NULL;
return ret;
}
}
*n = current;
return E_OK;
}
PathError SearchPath_FindEx(SearchPath *path, Node *root, Node **n, Node **p, int *errnode) {
Node *current = root;
Node *prev = NULL;
PathError ret;
for (int i = 0; i < path->len; i++) {
prev = current;
current = __pathNode_eval(&path->nodes[i], current, &ret);
if (ret != E_OK) {
*errnode = i;
*p = prev;
*n = NULL;
return ret;
}
}
*p = prev;
*n = current;
return E_OK;
}
SearchPath NewSearchPath(size_t cap) {
return (SearchPath){RedisModule_Calloc(cap, sizeof(PathNode)), 0, cap};
}
void __searchPath_append(SearchPath *p, PathNode pn) {
if (p->len >= p->cap) {
p->cap = p->cap ? MIN(p->cap * 2, 1024) : 1;
p->nodes = RedisModule_Realloc(p->nodes, p->cap * sizeof(PathNode));
}
p->nodes[p->len++] = pn;
}
void SearchPath_AppendIndex(SearchPath *p, int idx) {
PathNode pn;
pn.type = NT_INDEX;
pn.value.index = idx;
__searchPath_append(p, pn);
}
void SearchPath_AppendKey(SearchPath *p, const char *key, const size_t len) {
PathNode pn;
pn.type = NT_KEY;
pn.value.key = rmstrndup(key, len);
__searchPath_append(p, pn);
}
void SearchPath_AppendRoot(SearchPath *p) {
PathNode pn;
pn.type = NT_ROOT;
__searchPath_append(p, pn);
}
void SearchPath_Free(SearchPath *p) {
if (p->nodes) {
for (int i = 0; i < p->len; i++) {
if (p->nodes[i].type == NT_KEY) {
RedisModule_Free((char *)p->nodes[i].value.key);
}
}
}
RedisModule_Free(p->nodes);
}