-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxtc_tree.cc
258 lines (232 loc) · 8.83 KB
/
xtc_tree.cc
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
/** \file xtc_tree.cc
* @brief tree structure for managing metadata.
* Store metadata in tree nodes, including the corresponding xtc2 objects’ position in the xtc2 files. Associate tree nodes to HDF5 objects, the tree structure reflecting the HDF5 data hierarchy.
*
* Created on: Feb 14, 2020
* Author: tonglin
*/
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <iostream>
#include "util_debug.h"
#include "xtc_io_api_cpp.hh"
#include "xtc_tree.hh"
#include "xtc_io_api_cpp.hh"
using namespace std;
xtc_node* new_xtc_node(xtc_object* obj){
assert(obj);
assert(obj->obj_path_abs);
assert(obj->obj_token);
xtc_node* node = (xtc_node*)calloc(1, sizeof(xtc_node));
node->my_obj = obj;
node->path_abs = str_tok(obj->obj_path_abs, "/");
obj->tree_node = (void*)node;
if(node->path_abs.size() == 0)
node->parent = NULL;
(*obj).tree_node = (void*)node;
return node;
}
//make virtual node for xtc tree node that doesn't have a real xtc obj, only serves as a directory node
xtc_node* new_virtual_xtc_node(int fd, vector<string> path_abs, xtc_node* root_node){
assert(path_abs.size() > 0 && root_node);
//printf("New virtual node path len = %d, ", path_abs.size());
//print_path(path_abs);
//printf("\n");
DEBUG_PRINT
xtc_node* node = (xtc_node*)calloc(1, sizeof(xtc_node));
xtc_object* vobj = (xtc_object*)calloc(1, sizeof(xtc_object));
vobj->fd = fd;
vobj->obj_path_abs = strdup(tok_to_str(path_abs).c_str());
vobj->obj_type = XTC_GROUP;
xtc_h5token_new((xtc_token_t**)&(vobj->obj_token), 16);
vobj->location = (xtc_location*)calloc(1, sizeof(xtc_location));
//vobj doesn't have a valid location that is required to do query. So use root location for now.
vobj->location = root_node->my_obj->location;
node->is_virtual = 1;
node->my_obj = vobj;
node->path_abs = path_abs;
vobj->tree_node = node;
DEBUG_PRINT
return node;
}
int add_xtc_node(xtc_node* root, xtc_node* new_node){
assert(root && new_node);
assert(new_node->my_obj);
assert(new_node->my_obj->obj_token);
xtc_location* l = (xtc_location*)new_node->my_obj->location;
DEBUG_PRINT
if(new_node->path_abs.size() < 2){
if(new_node->path_abs.size() == 0){
printf("new_node path len = 0, invalid node.\n");
return -1;
}
if(new_node->path_abs.size() == 1){
DEBUG_PRINT
for(auto i : root->children){
if(!i->path_abs[0].compare(new_node->path_abs[0])){
//printf("new_node has a conflicting path: %s\n", new_node->path_abs[0].c_str());
return -1;
}
}
new_node->parent = root;
root->children.push_back(new_node);
return 0;
}
}
xtc_node* insert_to = root;
int index_n = 0;
bool token_match = false;
while(index_n <= new_node->path_abs.size() - 1){
DEBUG_PRINT
//printf("insert_to path = %s\n", tok_to_str(insert_to->path_abs).c_str());
if(index_n < new_node->path_abs.size() - 1){
if(insert_to->children.size() > 0){
token_match = false;
for(auto i : insert_to->children){
DEBUG_PRINT
// cout<<"New node path = "<< tok_to_str(new_node->path_abs) <<", new node token = "
// <<new_node->path_abs[index_n] << ", child path = " << tok_to_str(i->path_abs)
// << ", index_n = " << index_n
// << endl;
if(0 == new_node->path_abs[index_n].compare(i->path_abs.back())){
//printf("token match: token = %s, index_n = %d\n", i->path_abs.back().c_str(), index_n);
insert_to = i;
index_n++;
token_match = true;
break; //for
} //else continue scan children
}
} else {//empty dir
token_match = false;
}
if(!token_match){//need to add a virtual node as new group
vector<string> new_path = insert_to->path_abs;
new_path.push_back(new_node->path_abs[index_n]);
xtc_node* new_dir_node = new_virtual_xtc_node(root->my_obj->fd, new_path, root);
//printf("Insert new_dir_node (%s) to %s\n", tok_to_str(new_path).c_str(), tok_to_str(insert_to->path_abs).c_str());
new_dir_node->parent = insert_to;
insert_to->children.push_back(new_dir_node);
insert_to = new_dir_node;
index_n++;
}
} else {// == size - 1: last one
// printf("Index_n = %d, reached last new_node token (%s), insert_to = %s, has %d children.\n",
// index_n, new_node->path_abs[index_n].c_str(),
// tok_to_str(insert_to->path_abs).c_str(), insert_to->children.size());
if(insert_to->children.size() > 0){
for(auto i:insert_to->children){
if(new_node->path_abs[index_n].compare(i->path_abs[index_n])==0){
//printf("End token match: invalid insert, path = %s, return.\n", new_node->my_obj->obj_path_abs);
return -1;
}// continue scaning
}//no match, need to insert.
break;//while
} else {// do insert to a empty dir.
break;//while
}
}
}//while
//insert actual node now
new_node->parent = insert_to;
//printf("Insert actual node to : %s\n", tok_to_str(insert_to->path_abs).c_str());
insert_to->children.push_back(new_node);
DEBUG_PRINT
return 0;
}
int print_tree_node(xtc_node* n){
assert(n);
if(n->my_obj->obj_type != XTC_GROUP && n->my_obj->obj_type != XTC_HEAD){
//printf("Can not traverse non-group node, return.\n");
return 0;
}
printf("node path = %s, type = %d, ", tok_to_str(n->path_abs).c_str(), n->my_obj->obj_type);//tok_to_str(n->path_abs).c_str(), n->my_obj->obj_path_abs
if(n->parent)
printf("parent path = %s, %d children:\n", n->parent->my_obj->obj_path_abs, n->children.size());//tok_to_str(n->parent->path_abs).c_str()
else
printf("%d children:\n", n->children.size());
for(auto i:n->children){
printf(" child path = %s, type = %d\n", i->my_obj->obj_path_abs, i->my_obj->obj_type);//tok_to_str(i->path_abs).c_str()
}
printf("---------------\n");
return 0;
}
xtc_object** get_children_obj(xtc_object* group, int* num_out){
assert(group);
if(group->obj_type != XTC_GROUP && group->obj_type != XTC_HEAD){
*num_out = 0;
return NULL;
}
xtc_node* node = (xtc_node*)(group->tree_node);
assert(node);
int n = node->children.size();
*num_out = n;
if(n == 0){
*num_out = 0;
return NULL;
}
xtc_object** children = (xtc_object**)calloc(n, sizeof(xtc_object*));
for(int i = 0; i < n; i++){
assert(node->children[i]->my_obj);
assert(node->children[i]->my_obj->obj_token);
children[i] = node->children[i]->my_obj;
}
return children;
}
xtc_node* find_xtc_node(xtc_node* root, const char* path){//search start from root
assert(root && path);
if(strcmp(path, "/")==0 )//|| strcmp(path, "//")==0
return root;
//assert(root->my_obj && !(root->parent));
assert(root->my_obj && !(root->parent));
vector<string> pv = str_tok(path, "/");
if(pv.size() == 0)
return NULL;
int token_index = 0;
xtc_node* cur = root;
bool token_match = false;
while(token_index < pv.size()){
// cur->parent;
// cur->children;
// cur->my_obj;
// cur->path_abs;
token_match = false;
if(cur->children.size() > 0){
for(auto n : cur->children){
if(pv[token_index].compare(n->path_abs[token_index]) == 0){
token_match = true;
if(token_index == pv.size() - 1)
return n;
token_index++;
cur = n;
break;//for
}
}
} else
token_match = false;
if(token_match){
continue;// while
} else {
break;//while
}
}
return NULL;
}
int print_tree(xtc_node* root){
assert(root);
print_tree_node(root);
//printf("===============================\n");
for(auto i:root->children){
if(i)
print_tree(i);
}
return 0;
}
xtc_node* new_test_node(const char* path) {
xtc_object* obj = (xtc_object*) calloc(1, sizeof(xtc_object));
obj->fd = 5;
obj->obj_path_abs = strdup(path);
obj->obj_type = XTC_GROUP;
xtc_node* node = new_xtc_node(obj);
return node;
}