Statistics
| Branch: | Tag: | Revision:

root / lib / filter-kvs.c @ aebfd4cc

History | View | Annotate | Download (3.29 KB)

1
/* SylFilter - a message filter
2
 *
3
 * Copyright (C) 2011 Hiroyuki Yamamoto
4
 * Copyright (C) 2011 Sylpheed Development Team
5
 */
6

    
7
#include <glib.h>
8

    
9
#include "filter-kvs.h"
10

    
11
struct _XFilterKVS
12
{
13
        char *dbfile;
14
        void *dbhandle;
15
};
16

    
17
static XFilterKVSEngine ke;
18

    
19

    
20
int xfilter_kvs_set_engine(XFilterKVSEngine *engine)
21
{
22
        g_return_val_if_fail(engine != NULL, -1);
23

    
24
        ke = *engine;
25

    
26
        return 0;
27
}
28

    
29
XFilterKVS *xfilter_kvs_new(const char *dbfile, void *dbhandle)
30
{
31
        XFilterKVS *kvs;
32

    
33
        kvs = g_new(XFilterKVS, 1);
34
        kvs->dbfile = g_strdup(dbfile);
35
        kvs->dbhandle = dbhandle;
36

    
37
        return kvs;
38
}
39

    
40
const char *xfilter_kvs_get_file(XFilterKVS *kvs)
41
{
42
        g_return_val_if_fail(kvs != NULL, NULL);
43
        return kvs->dbfile;
44
}
45

    
46
void *xfilter_kvs_get_handle(XFilterKVS *kvs)
47
{
48
        g_return_val_if_fail(kvs != NULL, NULL);
49
        return kvs->dbhandle;
50
}
51

    
52
XFilterKVS *xfilter_kvs_open(const char *dbfile)
53
{
54
        g_return_val_if_fail(ke.open != NULL, NULL);
55
        return ke.open(dbfile);
56
}
57

    
58
int xfilter_kvs_close(XFilterKVS *kvs)
59
{
60
        g_return_val_if_fail(kvs != NULL, -1);
61
        return ke.close(kvs);
62
}
63

    
64
int xfilter_kvs_insert(XFilterKVS *kvs, const char *key, void *value, int size)
65
{
66
        g_return_val_if_fail(kvs != NULL, -1);
67
        return ke.insert(kvs, key, value, size);
68
}
69

    
70
int xfilter_kvs_delete(XFilterKVS *kvs, const char *key)
71
{
72
        g_return_val_if_fail(kvs != NULL, -1);
73
        return ke.delete(kvs, key);
74
}
75

    
76
int xfilter_kvs_update(XFilterKVS *kvs, const char *key, void *value, int size)
77
{
78
        g_return_val_if_fail(kvs != NULL, -1);
79
        return ke.update(kvs, key, value, size);
80
}
81

    
82
int xfilter_kvs_fetch(XFilterKVS *kvs, const char *key, void *vbuf, int vsize)
83
{
84
        g_return_val_if_fail(kvs != NULL, -1);
85
        return ke.fetch(kvs, key, vbuf, vsize);
86
}
87

    
88
int xfilter_kvs_get_record_size(XFilterKVS *kvs)
89
{
90
        g_return_val_if_fail(kvs != NULL, -1);
91
        return ke.size(kvs);
92
}
93

    
94
int xfilter_kvs_foreach(XFilterKVS *kvs, XFilterKVSForeachFunc func, void *data)
95
{
96
        g_return_val_if_fail(kvs != NULL, -1);
97
        return ke.foreach(kvs, func, data);
98
}
99

    
100
int xfilter_kvs_fetch_int(XFilterKVS *kvs, const char *key)
101
{
102
        int ival = 0;
103
        int size;
104
        char vbuf[4];
105

    
106
        g_return_val_if_fail(kvs != NULL, -1);
107

    
108
        size = xfilter_kvs_fetch(kvs, key, vbuf, 4);
109
        if (size == 4) {
110
                ival = *(int *)vbuf;
111
                return ival;
112
        }
113

    
114
        return 0;
115
}
116

    
117
int xfilter_kvs_increment(XFilterKVS *kvs, const char *key, int num)
118
{
119
        int ival = 0;
120
        int size;
121
        char vbuf[4];
122

    
123
        g_return_val_if_fail(kvs != NULL, -1);
124

    
125
        size = xfilter_kvs_fetch(kvs, key, vbuf, 4);
126
        if (size == 4) {
127
                ival = *(int *)vbuf;
128
                ival += num;
129
                return xfilter_kvs_update(kvs, key, (char *)&ival, 4);
130
        } else if (size <= 0) {
131
                return xfilter_kvs_insert(kvs, key, (char *)&num, 4);
132
        }
133

    
134
        return -1;
135
}
136

    
137
int xfilter_kvs_decrement(XFilterKVS *kvs, const char *key, int num)
138
{
139
        int ival = 0;
140
        int size;
141
        char vbuf[4];
142

    
143
        g_return_val_if_fail(kvs != NULL, -1);
144

    
145
        size = xfilter_kvs_fetch(kvs, key, vbuf, 4);
146
        if (size == 4) {
147
                ival = *(int *)vbuf;
148
                ival -= num;
149
                if (ival <= 0)
150
                        return xfilter_kvs_delete(kvs, key);
151
                else
152
                        return xfilter_kvs_update(kvs, key, (char *)&ival, 4);
153
        }
154

    
155
        return -1;
156
}
157

    
158
static int count_func(XFilterKVS *kvs, const char *key, void *value, int size, void *data)
159
{
160
        int *sum = (int *)data;
161

    
162
        if (size == 4) {
163
                *sum += *(int *)value;
164
        }
165

    
166
        return 0;
167
}
168

    
169
int xfilter_kvs_count_sum(XFilterKVS *kvs)
170
{
171
        int sum = 0;
172

    
173
        g_return_val_if_fail(kvs != NULL, -1);
174

    
175
        xfilter_kvs_foreach(kvs, count_func, &sum);
176
        return sum;
177
}