-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.q
164 lines (138 loc) · 4.17 KB
/
file.q
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
#include <glib.h>
#include "scanner.h"
#include "tagdb.h"
#include "query.h"
#include "query_file.h"
/* The operators used in the "TAG SEARCH" query */
const char *search_operators[2][4] = {
{"/", "\\", "%", NULL},
{"AND", "OR", "ANDN", NULL},
};
set_operation_l oper_fn[] = {
g_list_intersection, g_list_union, g_list_difference
};
/* limiters for use in SEARCH */
const char *search_limiters[] = {"<", "=", ">", NULL};
static set_predicate lim_pred_functions[] = {
value_lt_sp, value_eq_sp, value_gt_sp
};
const char *special_tags[3] = {"@all", "@untagged", NULL};
special_tag_fn special_tag_functions[2] = {
tagdb_all_files, tagdb_untagged_items
};
gboolean value_eq_sp (gpointer key, gpointer lvalue, gpointer value)
{
return tagdb_value_equals((tagdb_value_t*) lvalue, (tagdb_value_t*) value);
}
gboolean value_lt_sp (gpointer key, gpointer lvalue, gpointer value)
{
return (tagdb_value_cmp((tagdb_value_t*) lvalue, (tagdb_value_t*) value) < 0);
}
gboolean value_gt_sp (gpointer key, gpointer lvalue, gpointer value)
{
return (tagdb_value_cmp((tagdb_value_t*) lvalue, (tagdb_value_t*) value) > 0);
}
// returns a hash table for the given tag
GList *_get_tag_table (TagDB *db, char *tag_name)
{
int idx = strv_index(special_tags, tag_name);
// log_msg("Entering _get_tag_table\n");
// log_msg("_get_tag_table tag_name = \"%s\"\n", tag_name);
// log_msg("_get_tag_table idx=%d\n", idx);
if (idx != -1)
{
return special_tag_functions[idx](db);
}
char *tag;
char *sep;
Scanner *scn = scanner_new_v(search_limiters);
scanner_set_quotes(scn, g_list_new("\"", NULL));
scanner_set_str_stream(scn, tag_name);
tag = scanner_next(scn, &sep);
Tag *t = lookup_tag(db, tag);
GList *res = NULL;
if (t)
{
res = file_cabinet_get_drawer_l(db->files, t->id);
idx = strv_index(search_limiters, sep);
if (!scanner_stream_is_empty(scn->stream))
{
int type = t->type;
char *vstring = scanner_next(scn, &sep);
tagdb_value_t *val = tagdb_str_to_value(type, vstring);
GList *tmp = g_list_filter(res, lim_pred_functions[idx], val);
g_list_free(res);
res = tmp;
result_destroy(val);
}
}
return res;
}
/*
Arguments:
- (tag, operator)* :: (STRING, %operator)*
- last_tag :: STRING
Return:
A dict of ids matching the query or NULL :: DICT
*/
q%search 1 DIDSS%
{
if (argc % 2 != 1)
{
qerr%invalid query testing%
}
int i;
int op_idx = -1;
GList *files = NULL;
for (i = 0; i < argc; i += 2 )
{
GList *this_table = _get_tag_table(db, argv[i]);
GList *tmp = NULL;
if (op_idx != -1)
tmp = oper_fn[op_idx](files, this_table, (GCompareFunc) file_id_cmp);
else
tmp = g_list_copy(this_table);
g_list_free(files);
g_list_free(this_table);
files = tmp;
op_idx = strv_index(search_operators[1], argv[i+1]);
}
GList *res = NULL;
LL(files, it)
{
File *f = it->data;
GList *pair = NULL;
GList *tags = NULL;
HL(f->tags, it, k, v)
{
GList *pair = NULL;
Tag *t = retrieve_tag(db, TO_S(k));
char *s = tagdb_value_to_str(v);
pair = g_list_new(g_strdup(t->name), s, NULL);
tags = g_list_prepend(tags, pair);
} HL_END;
tags = g_list_prepend(tags,
g_list_new(g_strdup("name"), g_strdup(f->name)));
pair = g_list_new(TO_SP(f->id), tags, NULL);
res = g_list_prepend(res, pair);
} LL_END;
g_list_free(files);
/* {file_name => {tag_name => tag_value}} */
qr%res%
}
q%list_tags 1 DSS%
{
File *f = retrieve_file(db, atoll(argv[0]));
GList *tags = NULL;
HL(f->tags, it, k, v)
{
GList *pair = NULL;
Tag *t = retrieve_tag(db, TO_S(k));
char *s = tagdb_value_to_str(v);
pair = g_list_new(g_strdup(t->name), s, NULL);
tags = g_list_prepend(tags, pair);
} HL_END;
tags = g_list_prepend(tags,
g_list_new(g_strdup("name"), g_strdup(f->name)));
qr%tags%
}