-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdb_mem.cpp
More file actions
354 lines (259 loc) · 6.61 KB
/
Copy pathdb_mem.cpp
File metadata and controls
354 lines (259 loc) · 6.61 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Copyright (C) 2015-2018 Anton Burdinuk
* clark15b@gmail.com
* http://xupnpd.org
*/
#include "db_mem.h"
#include <stdlib.h>
#ifdef _WIN32
#include <windows.h>
#endif /* _WIN32 */
namespace db_mem
{
std::map<int,object_t> media_by_objid;
void __attach_child(object_t& o,int parentid)
{
if(parentid>=0)
{
object_t& p=media_by_objid[parentid];
o.parent=&p;
if(!p.beg)
p.beg=p.end=&o;
else
{
p.end->next=&o;
p.end=&o;
}
}
}
const object_t* __find_object_by_id(int objid)
{
std::map<int,object_t>::const_iterator it=media_by_objid.find(objid);
if(it==media_by_objid.end())
return NULL;
return &it->second;
}
const object_t* __find_object_by_id(const std::string& objid)
{
if(objid.empty())
return NULL;
return __find_object_by_id(atoi(objid.c_str()));
}
const object_t* __skip(const object_t* o,int offset)
{
for(o=o->beg;o && offset>0;o=o->next,offset--);
return o;
}
bool __search_objects(const std::string& from,const std::string& to,int what,int count,int offset,rowset_t& st);
#ifdef _WIN32
CRITICAL_SECTION __db_lock;
#endif /* _WIN32 */
}
bool db_mem::init(void)
{
#ifdef _WIN32
InitializeCriticalSection(&__db_lock);
#endif /* _WIN32 */
return true;
}
void db_mem::done(void)
{
media_by_objid.clear();
#ifdef _WIN32
DeleteCriticalSection(&__db_lock);
#endif /* _WIN32 */
}
void db_mem::begin_scan(void) { media_by_objid.clear(); }
void db_mem::end_scan(void)
{
object_t* p=NULL;
for(std::map<int,object_t>::iterator it=media_by_objid.begin();it!=media_by_objid.end();++it)
{
if(!p)
p=&it->second;
else
{
p->global_next=&it->second;
p=&it->second;
}
}
}
db_mem::locker::locker(void)
{
#ifdef _WIN32
EnterCriticalSection(&__db_lock);
#endif /* _WIN32 */
}
db_mem::locker::~locker(void)
{
#ifdef _WIN32
LeaveCriticalSection(&__db_lock);
#endif /* _WIN32 */
}
bool db_mem::add_container(int contid,int parentid,int childs_num,const std::string& name)
{
object_t& o=media_by_objid[contid];
o.objid=utils::format("%d",contid);
o.parentid=utils::format("%d",parentid);
o.objtype=0;
o.items=utils::format("%d",childs_num);
o.name=name;
__attach_child(o,parentid);
return true;
}
bool db_mem::add_media(int objid,int parentid,int objtype,const std::string& handler,int mimecode,u_int64_t length,const std::string& name,
const std::string& url,const std::string& logo,const std::string& uuid)
{
object_t& o=media_by_objid[objid];
o.objid=utils::format("%d",objid);
o.parentid=utils::format("%d",parentid);
o.objtype=objtype;
o.handler=handler;
o.mimecode=mimecode;
if(length!=(u_int64_t)-1)
o.length=utils::format("%llu",(unsigned long long int)length);
o.name=name;
o.url=url;
o.logo=logo;
__attach_child(o,parentid);
return true;
}
bool db_mem::find_object_by_id(const std::string& objid,object_t& obj)
{
const object_t* o=__find_object_by_id(objid);
if(!o)
return false;
obj=*o;
return true;
}
bool db_mem::find_next_object_by_id(const std::string& objid,const std::string& parentid,object_t& obj)
{
const object_t* o=__find_object_by_id(objid);
if(!o || !o->next)
return false;
obj=*o->next;
return true;
}
int db_mem::get_childs_count(const std::string& parentid)
{
const object_t* o=__find_object_by_id(parentid);
if(!o)
return false;
return atoi(o->items.c_str());
}
bool db_mem::find_objects_by_parent_id(const std::string& parentid,int limit,int offset,rowset_t& st)
{
const object_t* o=__find_object_by_id(parentid);
if(!o || !o->beg)
return false;
st.cur=o->beg;
st.limit=-1;
while(offset>0 && st.__fetch())
offset--;
st.limit=limit>0?limit:-1;
return true;
}
bool db_mem::__search_objects(const std::string& from,const std::string& to,int what,int count,int offset,rowset_t& st)
{
const object_t* o1=__find_object_by_id(from);
const object_t* o2;
if(!o1)
return false;
if(!to.empty())
o2=__find_object_by_id(atoi(to.c_str()));
else
{
// std::map<int,object_t>::const_reverse_iterator it=media_by_objid.rbegin();
std::map<int,object_t>::reverse_iterator it=media_by_objid.rbegin();
if(it==media_by_objid.rend())
return false;
o2=&it->second;
}
st.cur=o1;
st.last=o2;
st.what=what;
st._is_search=true;
st.limit=-1;
while(offset>0 && st.__fetch())
offset--;
if(!st.cur)
return false;
st.limit=count>0?count:-1;
return true;
}
int db_mem::get_objects_count(const std::string& from,const std::string& to,int what)
{
rowset_t st;
if(!__search_objects(from,to,what,0,0,st))
return 0;
int num=0;
while(st.__fetch())
num++;
return num;
}
bool db_mem::search_objects(const std::string& from,const std::string& to,int what,int count,int offset,rowset_t& st)
{
return __search_objects(from,to,what,count,offset,st);
}
bool db_mem::rowset_t::fetch(std::map<std::string,std::string>& row)
{
const object_t* o=__fetch();
if(!o)
return false;
row["objid"]=o->objid;
row["parentid"]=o->parentid;
row["objtype"]=utils::format("%d",o->objtype);
row["items"]=o->items;
row["handler"]=o->handler;
row["mimecode"]=utils::format("%d",o->mimecode);
row["length"]=o->length;
row["name"]=o->name;
row["url"]=o->url;
row["logo"]=o->logo;
return true;
}
bool db_mem::rowset_t::fetch(object_t& row)
{
const object_t* o=__fetch();
if(!o)
return false;
row=*o;
return true;
}
const db_mem::object_t* db_mem::rowset_t::__fetch(void)
{
if(!cur || !limit)
return NULL;
if(_is_search)
return __fetch_search();
const object_t* o=cur;
cur=cur->next;
if(limit!=-1)
limit--;
return o;
}
const db_mem::object_t* db_mem::rowset_t::__fetch_search(void)
{
for(;cur;cur=cur->global_next)
{
if(cur==last)
{ cur=NULL; break ; }
if(!what)
{
if(cur->objtype==0)
continue;
}else
{
if(cur->objtype!=what)
continue;
}
break;
}
const object_t* o=cur;
if(!o)
return NULL;
cur=cur->global_next;
if(limit!=-1)
limit--;
return o;
}