-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_cabinet.c
386 lines (340 loc) · 9.23 KB
/
file_cabinet.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include <stdlib.h>
#include <glib.h>
#include <unistd.h>
#include <assert.h>
#include "sql.h"
#include "log.h"
#include "util.h"
#include "tagdb_util.h"
#include "file.h"
#include "file_cabinet.h"
#include "set_ops.h"
enum {INSERT,
REMOVE,
REMNUL,
GETFIL,
GETUNT,
TAGUNI,
RMTAGU,
RMDRWR,
RALLTU,
RTUDWR,
LOOKUP,
LOOKUT,
TAGUNL,
NUMBER_OF_STMTS
};
#define STMT(_db,_i) ((_db)->stmts[(_i)])
#define STMT_SEM(_db,_i) (&((_db)->stmt_semas[(_i)]))
struct FileCabinet {
/* An index on files. Usually provided to us by tagdb */
GHashTable *files;
/* Indicates whether we created the FILES ourselves */
gboolean own_files;
/* The sqlite database */
sqlite3 *sqlitedb;
/* The sql prepared statements that we use */
sqlite3_stmt *stmts[NUMBER_OF_STMTS];
/* Semaphores to protect prepared statements from being reset while they are executing */
sem_t stmt_semas[NUMBER_OF_STMTS];
};
FileCabinet *file_cabinet_new0 (sqlite3 *db, GHashTable *files)
{
FileCabinet *res = calloc(1,sizeof(FileCabinet));
res->sqlitedb = db;
res->files = files;
return file_cabinet_init(res);
}
FileCabinet *file_cabinet_new (sqlite3 *db)
{
GHashTable *files = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL);
FileCabinet *fc = file_cabinet_new0(db, files);
fc->own_files = TRUE;
return fc;
}
FileCabinet *file_cabinet_init (FileCabinet *res)
{
sqlite3 *db = res->sqlitedb;
assert(db);
for (int i = 0; i < NUMBER_OF_STMTS; i++)
{
sem_init(&(res->stmt_semas[i]), 0, 1);
}
/* insert statement */
sql_prepare(db, "insert into file_tag(file, tag) values(?,?)", STMT(res, INSERT));
/* remove statement */
sql_prepare(db, "delete from file_tag where file=? and tag is ?", STMT(res, REMOVE));
/* remove statement */
sql_prepare(db, "delete from file_tag where tag is ?", STMT(res, RMDRWR));
/* files-with-tag statement */
sql_prepare(db, "select distinct file from file_tag where tag is ?", STMT(res, GETFIL));
sql_prepare(db, "select distinct id from file where id not in (select file from file_tag)", STMT(res, GETUNT));
sql_prepare(db, "select distinct F.id from file_tag Z,file F where Z.tag is ? and Z.file=F.id and F.name=?", STMT(res, LOOKUP));
sql_prepare(db, "select distinct F.id"
" from file F"
" where F.name=?"
" and F.id not in (select file from file_tag)", STMT(res, LOOKUT));
sql_prepare(db, "select distinct b.tag from file_tag a, file_tag b where a.tag=? and a.file=b.file and a.tag!=b.tag", STMT(res, TAGUNL));
return res;
}
void file_cabinet_destroy (FileCabinet *fc)
{
if (fc)
{
for (int i = 0; i < NUMBER_OF_STMTS; i++)
{
sqlite3_finalize(STMT(fc,i));
sem_destroy(&(fc->stmt_semas[i]));
}
if (fc->own_files && fc->files)
{
HL (fc->files, it, k, v)
{
file_destroy_unsafe((File*) v);
} HL_END;
g_hash_table_destroy(fc->files);
}
free(fc);
}
}
GList *_sqlite_getfile_stmt(FileCabinet *fc, file_id_t key);
GList *file_cabinet_get_drawer_l (FileCabinet *fc, file_id_t slot_id)
{
GList *res = _sqlite_getfile_stmt(fc, slot_id);
return res;
}
File *_find_file(FileCabinet *fc, tagdb_key_t key, const char *name)
{
int stmt_code;
sqlite3_stmt *stmt = NULL;
sem_t *stmt_sem;
if (key_is_empty(key))
{
stmt_code = LOOKUT;
}
else
{
stmt_code = LOOKUP;
}
stmt = STMT(fc, stmt_code);
stmt_sem = STMT_SEM(fc, stmt_code);
sem_wait(stmt_sem);
sqlite3_reset(stmt);
if (stmt_code == LOOKUT)
{
sqlite3_bind_text(stmt, 1, name, -1, SQLITE_TRANSIENT);
}
else
{
file_id_t tag_id = key_ref(key, 0);
sqlite3_bind_int(stmt, 1, tag_id);
sqlite3_bind_text(stmt, 2, name, -1, SQLITE_TRANSIENT);
}
while (sql_next_row(stmt) == SQLITE_ROW)
{
int id = sqlite3_column_int(stmt, 0);
File *f = g_hash_table_lookup(fc->files, TO_P(id));
if (f && file_has_tags(f, key))
{
sem_post(stmt_sem);
return f;
}
}
sem_post(stmt_sem);
return NULL;
}
void _sqlite_rm_drawer_stmt(FileCabinet *fc, file_id_t key);
void file_cabinet_remove_drawer (FileCabinet *fc, file_id_t slot_id)
{
_sqlite_rm_drawer_stmt(fc, slot_id);
}
int file_cabinet_drawer_size (FileCabinet *fc, file_id_t key)
{
sqlite3_stmt *stmt = STMT(fc, GETFIL);
sem_wait(STMT_SEM(fc, GETFIL));
sqlite3_reset(stmt);
sqlite3_bind_int(stmt, 1, key);
int sum = 0;
int status;
while ((status = sqlite3_step(stmt)) == SQLITE_ROW)
{
sum++;
}
if (status != SQLITE_DONE)
{
const char* msg = sqlite3_errmsg(fc->sqlitedb);
error("We didn't finish the count SQLite statemnt: %s(%d)", msg, status);
}
sem_post(STMT_SEM(fc, GETFIL));
return sum;
}
GList *_sqlite_getfile_stmt(FileCabinet *fc, file_id_t key)
{
sqlite3_stmt *stmt;
sem_t *stmt_sem;
int status;
int stmt_code;
if (key)
{
stmt = STMT(fc, GETFIL);
stmt_code = GETFIL;
}
else
{
stmt = STMT(fc, GETUNT);
stmt_code = GETUNT;
}
stmt_sem = STMT_SEM(fc, stmt_code);
sem_wait(stmt_sem);
sqlite3_reset(stmt);
if (key)
{
sqlite3_bind_int(stmt, 1, key);
}
GList *res = NULL;
while ((status = sqlite3_step(stmt)) == SQLITE_ROW)
{
int id = sqlite3_column_int(stmt, 0);
/* get the actual file */
File *f = g_hash_table_lookup(fc->files, TO_P(id));
if (f)
{
res = g_list_prepend(res, f);
}
}
if (status != SQLITE_DONE)
{
const char* msg = sqlite3_errmsg(fc->sqlitedb);
error("We didn't finish the getfile SQLite statemnt: %s(%d)", msg, status);
}
sem_post(stmt_sem);
return res;
}
void _sqlite_rm_stmt(FileCabinet *fc, File *f, file_id_t key)
{
int stmt_code;
sqlite3_stmt *stmt = NULL;
sem_t *stmt_sem;
if (key)
{
stmt_code = REMOVE;
}
else
{
stmt_code = REMNUL;
}
stmt = STMT(fc, stmt_code);
stmt_sem = STMT_SEM(fc, stmt_code);
sem_wait(stmt_sem);
sqlite3_reset(stmt);
sqlite3_bind_int(stmt, 1, file_id(f));
if (key)
{
sqlite3_bind_int(stmt, 2, key);
}
sql_step(stmt);
sem_post(stmt_sem);
}
void _sqlite_rm_drawer_stmt(FileCabinet *fc, file_id_t key)
{
if (key)
{
sqlite3_stmt *stmt = STMT(fc, RMDRWR);
sem_wait(STMT_SEM(fc, RMDRWR));
sqlite3_reset(stmt);
sqlite3_bind_int(stmt, 1, key);
sql_step(stmt);
sem_post(STMT_SEM(fc, RMDRWR));
}
}
GList *_sqlite_tag_union_list_stmt(FileCabinet *fc, file_id_t key)
{
sqlite3_stmt *stmt = STMT(fc, TAGUNL);
sem_wait(STMT_SEM(fc,TAGUNL));
sqlite3_reset(stmt);
sqlite3_bind_int(stmt, 1, key);
GList *res = NULL;
while (sql_next_row(stmt) == SQLITE_ROW)
{
file_id_t id = sqlite3_column_int64(stmt, 0);
res = g_list_prepend(res, TO_SP(id));
}
sem_post(STMT_SEM(fc, TAGUNL));
return res;
}
void _sqlite_ins_stmt (FileCabinet *fc, File *f, file_id_t key)
{
sqlite3_stmt *stmt = NULL;
if (key)
{
stmt = STMT(fc, INSERT);
sem_wait(STMT_SEM(fc, INSERT));
sqlite3_reset(stmt);
sqlite3_bind_int(stmt, 1, file_id(f));
sqlite3_bind_int(stmt, 2, key);
sql_step(stmt);
sem_post(STMT_SEM(fc, INSERT));
}
}
void file_cabinet_remove (FileCabinet *fc, file_id_t key, File *f)
{
_sqlite_rm_stmt(fc,f,key);
/* NOTE: Although we always want to insert a file into fc->files on
* insert, we never want to delete the file since it could remain in
* any of the "drawers"
*/
}
void file_cabinet_remove_v (FileCabinet *fc, tagdb_key_t key, File *f)
{
KL(key, i)
{
file_cabinet_remove(fc, key_ref(key,i), f);
} KL_END;
}
void file_cabinet_remove_all (FileCabinet *fc, File *f)
{
tagdb_key_t key = file_extract_key(f);
file_cabinet_remove_v(fc, key, f);
key_destroy(key);
}
GList *file_cabinet_get_untagged_files (FileCabinet *fc)
{
return _sqlite_getfile_stmt(fc, 0);
}
void file_cabinet_delete_file(FileCabinet *fc, File *f)
{
int rem = g_hash_table_remove(fc->files, TO_SP(file_id(f)));
assert(rem);
if (!file_destroy(f))
{
error("Could not destroy file: %s", file_name(f));
}
}
void file_cabinet_insert (FileCabinet *fc, file_id_t key, File *f)
{
if (G_UNLIKELY(fc->own_files))
{
g_hash_table_insert(fc->files, TO_SP(file_id(f)), f);
}
_sqlite_ins_stmt(fc,f,key);
}
void file_cabinet_insert_v (FileCabinet *fc, const tagdb_key_t key, File *f)
{
KL(key, i)
{
file_cabinet_insert(fc, key_ref(key,i), f);
} KL_END
}
gulong file_cabinet_size (FileCabinet *fc)
{
return g_hash_table_size(fc->files);
}
/* Lookup a file with the given name and tags */
File *file_cabinet_lookup_file (FileCabinet *fc, tagdb_key_t key, const char *name)
{
return _find_file(fc, key, name);
}
GList *file_cabinet_get_drawer_tags (FileCabinet *fc, file_id_t slot_id)
{
return _sqlite_tag_union_list_stmt(fc, slot_id);
}