-
Notifications
You must be signed in to change notification settings - Fork 1
/
nv_relation.c
223 lines (204 loc) · 6.65 KB
/
nv_relation.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
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
#include "nv.h"
//
// Internal
//
NV_ID NV_NodeID_createRel_OnDupUpdate(const NV_ID *from, const NV_ID *rel,
const NV_ID *to,
NV_ID (*find)(const NV_ID *from,
const NV_ID *rel)) {
NV_ID r;
r = find(from, rel);
if (NV_NodeID_isEqual(&r, &NODEID_NOT_FOUND)) {
// 新規
return NV_NodeID_createRelation(from, rel, to);
}
// 既存
NV_NodeID_updateRelationTo(&r, to);
return r;
}
//
// Relation
//
NV_ID NV_NodeID_createRelation(const NV_ID *from, const NV_ID *rel,
const NV_ID *to) {
NV_Relation *reld;
int32_t size = sizeof(NV_Relation);
//
reld = NV_malloc(size);
reld->from = *from;
reld->rel = *rel;
reld->to = *to;
/*
if(IS_DEBUG_MODE()){
printf("Rel created: ");
NV_NodeID_printForDebug(from);
NV_NodeID_printForDebug(rel);
NV_NodeID_printForDebug(to);
printf("\n");
}
*/
return NV_Node_createWithTypeAndData(kRelation, reld, size);
}
NV_ID NV_NodeID_createRelationWithID(const NV_ID *relnid, const NV_ID *from,
const NV_ID *rel, const NV_ID *to) {
NV_Relation *reld;
int32_t size = sizeof(NV_Relation);
//
reld = NV_malloc(size);
reld->from = *from;
reld->rel = *rel;
reld->to = *to;
return NV_Node_createWith_ID_Type_Data_Size(relnid, kRelation, reld, size);
}
NV_ID NV_NodeID_createUniqueIDRelation(const NV_ID *from, const NV_ID *rel,
const NV_ID *to) {
// fromが同一のIDで、
// re1.id === rel2.idとなるような
// relがすでに存在するならば、それのtoをupdateする。
// 存在しないならば、新規作成する。
return NV_NodeID_createRel_OnDupUpdate(from, rel, to,
NV_NodeID_getRelationFrom);
}
NV_ID NV_NodeID_createUniqueEqRelation(const NV_ID *from, const NV_ID *rel,
const NV_ID *to) {
// fromが同一のIDで、
// relがNV_Node_Internal_isEqualInValue()において等価であるような
// relがすでに存在するならば、それのtoをupdateする。
// 存在しないならば、新規作成する。
return NV_NodeID_createRel_OnDupUpdate(from, rel, to,
NV_NodeID_getEqRelationFrom);
}
NV_Node *NV_NodeID_Relation_getLinkFrom(const NV_ID *relnid) {
const NV_Relation *reld;
//
reld = NV_NodeID_getDataAsType(relnid, kRelation);
if (!reld) return NULL;
return NV_NodeID_getNode(&reld->from);
}
NV_ID NV_NodeID_Relation_getIDLinkTo(const NV_ID *relnid) {
const NV_Relation *reld;
//
reld = NV_NodeID_getDataAsType(relnid, kRelation);
if (!reld) return NODEID_NOT_FOUND;
return reld->to;
}
NV_Node *NV_NodeID_Relation_getLinkTo(const NV_ID *relnid) {
NV_ID id;
id = NV_NodeID_Relation_getIDLinkTo(relnid);
return NV_NodeID_getNode(&id);
}
NV_ID NV_NodeID_Relation_getIDLinkRel(const NV_ID *relnid) {
const NV_Relation *reld;
//
reld = NV_NodeID_getDataAsType(relnid, kRelation);
return reld->rel;
}
NV_Node *NV_NodeID_Relation_getLinkRel(const NV_ID *relnid) {
NV_ID id;
id = NV_NodeID_Relation_getIDLinkRel(relnid);
return NV_NodeID_getNode(&id);
}
NV_ID NV_NodeID_updateRelationTo(const NV_ID *relnid, const NV_ID *to) {
// Updateとは言っているが、内容はImmutableにしたいので、
// 既存のものを削除して、新しいノードを生成し、以前と同一の関係性を持たせる。
NV_ID from, rel;
NV_Node *n;
const NV_Relation *reld;
//
n = NV_NodeID_getNode(relnid);
reld = NV_Node_getDataAsType(n, kRelation);
if (!reld || !to) return NODEID_NOT_FOUND;
// 以前の関係性を取り出す
from = reld->from;
rel = reld->rel;
// 古い関係を除去
NV_NodeID_remove(relnid);
// 新しい関係を作成して返す
return NV_NodeID_createRelation(&from, &rel, to);
}
#define REL_CACHE_MASK 0xFF
#define HASH_REL(id) (id->d[0] & REL_CACHE_MASK)
const NV_Node
*relCache[REL_CACHE_MASK + 1][REL_CACHE_MASK + 1]; // [from][rel] -> to
const NV_Node *NV_NodeID_getRelNodeFromWithCmp(const NV_ID *from,
const NV_ID *rel,
int (*cmp)(const NV_ID *p,
const NV_ID *q)) {
const NV_Node *n;
const NV_Relation *reld;
if (!from || !rel || !cmp) return NULL;
// check cache
n = relCache[HASH_REL(from)][HASH_REL(rel)];
if (n) {
reld = NV_Node_getDataAsType(n, kRelation);
if (reld && NV_NodeID_isEqual(&reld->from, from) && cmp(&reld->rel, rel)) {
// hit!
return n;
}
}
// check relCache
n = NV_NodeID_getNode(from);
if (n) {
n = NV_Node_getRelCache(n);
reld = NV_Node_getDataAsType(n, kRelation);
if (reld && NV_NodeID_isEqual(&reld->from, from) && cmp(&reld->rel, rel)) {
return n;
}
}
//
for (n = NV_Node_getNextNode(&nodeRoot); n; n = NV_Node_getNextNode(n)) {
reld = NV_Node_getDataAsType(n, kRelation);
if (!reld) continue;
if (NV_NodeID_isEqual(&reld->from, from) && cmp(&reld->rel, rel)) {
//
relCache[HASH_REL(from)][HASH_REL(rel)] = n;
NV_Node *fn = NV_NodeID_getNode(from);
if (fn) {
NV_Node_setRelCache(fn, n);
}
//
return n;
}
}
return NULL;
}
NV_ID NV_NodeID_getRelationFrom(const NV_ID *from, const NV_ID *rel) {
const NV_Node *n;
//
n = NV_NodeID_getRelNodeFromWithCmp(from, rel, NV_NodeID_isEqual);
if (n) return NV_Node_getID(n);
return NODEID_NOT_FOUND;
}
NV_ID NV_NodeID_getRelatedNodeFrom(const NV_ID *from, const NV_ID *rel) {
const NV_Node *n;
const NV_Relation *reld;
//
n = NV_NodeID_getRelNodeFromWithCmp(from, rel, NV_NodeID_isEqual);
if (n) {
reld = NV_Node_getDataAsType(n, kRelation);
return reld->to;
}
return NODEID_NOT_FOUND;
}
NV_ID NV_NodeID_getEqRelationFrom(const NV_ID *from, const NV_ID *rel) {
const NV_Node *n;
//
n = NV_NodeID_getRelNodeFromWithCmp(from, rel, NV_NodeID_isEqualInValue);
if (n) return NV_Node_getID(n);
return NODEID_NOT_FOUND;
}
NV_ID NV_NodeID_getEqRelatedNodeFrom(const NV_ID *from, const NV_ID *rel) {
const NV_Node *n;
const NV_Relation *reld;
/*
printf("\nNV_NodeID_getEqRelatedNodeFrom: find from #%08X, rel #%08X\n",
from->d[0], rel->d[0]);
*/
//
n = NV_NodeID_getRelNodeFromWithCmp(from, rel, NV_NodeID_isEqualInValue);
if (n) {
reld = NV_Node_getDataAsType(n, kRelation);
return reld->to;
}
return NODEID_NOT_FOUND;
}