Statistics
| Branch: | Tag: | Revision:

root / lib / filter-kvs-qdbm.c @ aebfd4cc

History | View | Annotate | Download (2.76 KB)

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

    
7
#include <depot.h>
8
#include <stdlib.h>
9

    
10
#include "filter-kvs.h"
11
#include "filter-kvs-qdbm.h"
12

    
13
static XFilterKVS *qdbm_open(const char *dbfile);
14
static int qdbm_close(XFilterKVS *kvs);
15
static int qdbm_insert(XFilterKVS *kvs, const char *key, void *value, int size);
16
static int qdbm_delete(XFilterKVS *kvs, const char *key);
17
static int qdbm_update(XFilterKVS *kvs, const char *key, void *value, int size);
18
static int qdbm_fetch(XFilterKVS *kvs, const char *key, void *vbuf, int vsize);
19
static int qdbm_size(XFilterKVS *kvs);
20
static int qdbm_foreach(XFilterKVS *kvs, XFilterKVSForeachFunc func, void *data);
21

    
22

    
23
int xfilter_kvs_qdbm_set_engine(void)
24
{
25
        XFilterKVSEngine engine = {
26
                qdbm_open,
27
                qdbm_close,
28
                qdbm_insert,
29
                qdbm_delete,
30
                qdbm_update,
31
                qdbm_fetch,
32
                qdbm_size,
33
                qdbm_foreach
34
        };
35

    
36
        return xfilter_kvs_set_engine(&engine);
37
}
38

    
39
static XFilterKVS *qdbm_open(const char *dbfile)
40
{
41
        DEPOT *depot;
42

    
43
        depot = dpopen(dbfile, DP_OREADER|DP_OWRITER|DP_OCREAT, 0);
44
        if (!depot)
45
                return NULL;
46
        return xfilter_kvs_new(dbfile, (void *)depot);
47
}
48

    
49
static int qdbm_close(XFilterKVS *kvs)
50
{
51
        if (!dpclose((DEPOT *)xfilter_kvs_get_handle(kvs)))
52
                return -1;
53
        return 0;
54
}
55

    
56
static int qdbm_insert(XFilterKVS *kvs, const char *key, void *value, int size)
57
{
58
        DEPOT *depot;
59

    
60
        depot = (DEPOT *)xfilter_kvs_get_handle(kvs);
61
        if (!dpput(depot, key, -1, (const char *)value, size, DP_DKEEP))
62
                return -1;
63
        return 0;
64
}
65

    
66
static int qdbm_delete(XFilterKVS *kvs, const char *key)
67
{
68
        DEPOT *depot;
69

    
70
        depot = (DEPOT *)xfilter_kvs_get_handle(kvs);
71
        if (!dpout(depot, key, -1))
72
                return -1;
73

    
74
        return 0;
75
}
76

    
77
static int qdbm_update(XFilterKVS *kvs, const char *key, void *value, int size)
78
{
79
        DEPOT *depot;
80

    
81
        depot = (DEPOT *)xfilter_kvs_get_handle(kvs);
82
        if (!dpput(depot, key, -1, (const char *)value, size, DP_DOVER))
83
                return -1;
84

    
85
        return 0;
86
}
87

    
88
static int qdbm_fetch(XFilterKVS *kvs, const char *key, void *vbuf, int vsize)
89
{
90
        DEPOT *depot;
91
        int size;
92

    
93
        depot = (DEPOT *)xfilter_kvs_get_handle(kvs);
94
        size = dpgetwb(depot, key, -1, 0, vsize, (char *)vbuf);
95
        if (size < 0)
96
                return -1;
97

    
98
        return size;
99
}
100

    
101
static int qdbm_size(XFilterKVS *kvs)
102
{
103
        DEPOT *depot;
104

    
105
        depot = (DEPOT *)xfilter_kvs_get_handle(kvs);
106
        return dprnum(depot);
107
}
108

    
109
static int qdbm_foreach(XFilterKVS *kvs, XFilterKVSForeachFunc func, void *data)
110
{
111
        DEPOT *depot;
112
        char *key;
113
        int ks;
114
        int size;
115
        char vbuf[1024];
116

    
117
        depot = (DEPOT *)xfilter_kvs_get_handle(kvs);
118
        if (!dpiterinit(depot))
119
                return -1;
120
        while ((key = dpiternext(depot, &ks)) != NULL) {
121
                int r;
122

    
123
                size = dpgetwb(depot, key, ks, 0, sizeof(vbuf), vbuf);
124
                if (size < 0) {
125
                        free(key);
126
                        break;
127
                }
128
                r = func(kvs, key, vbuf, size, data);
129
                free(key);
130
                if (r < 0)
131
                        break;
132
        }
133

    
134
        return 0;
135
}