This repository was archived by the owner on Oct 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcddb.c
More file actions
177 lines (127 loc) · 2.78 KB
/
mcddb.c
File metadata and controls
177 lines (127 loc) · 2.78 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
#include <db.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "mcddb.h"
static DB *db;
int open_db(char *dbpath)
{
int ret;
char *defpath = "./mcd.db";
if (!dbpath){
if ((dbpath = malloc(strlen(defpath) + 1)) != NULL) {
strcpy(dbpath, defpath);
}
else {
return -1;
}
}
ret = db_create(&db, NULL, 0);
ret = db->open(db, NULL, dbpath, NULL, DB_BTREE, DB_CREATE, 0664);
if (ret != 0) {
return ret;
}
return 0;
}
int add_rec(char *kbuf, char *vbuf)
{
DBT key, value;
int ret;
memset(&key, 0, sizeof(DBT));
memset(&value, 0, sizeof(DBT));
key.data = kbuf;
key.size = strlen(kbuf);
value.data = vbuf;
value.size = strlen(vbuf);
ret = db->put(db, NULL, &key, &value, DB_NOOVERWRITE);
if (ret == DB_KEYEXIST)
return 1;
else if (ret != 0) {
perror("test");
return ret;
}
db->sync(db, 0);
return 0;
}
int del_rec(char *kbuf)
{
int ret;
DBT key;
memset(&key, 0, sizeof(DBT));
key.data = kbuf;
key.size = strlen(kbuf);
ret = db->del(db, NULL, &key, 0);
if (ret != 0) {
if (ret == DB_NOTFOUND) {
return 1;
}
else
return ret;
}
db->sync(db, 0);
return 0;
}
int find_rec(char *kbuf, DBT *value)
{
int ret;
DBT key;
DBC *dbc = NULL;
ret = db->cursor(db, NULL, &dbc, 0);
if (ret != 0)
return ret;
memset(&key, 0, sizeof(DBT));
key.data = kbuf;
key.size = strlen(kbuf);
while ((ret = dbc->c_get(dbc, &key, value, DB_NEXT)) != DB_NOTFOUND) {
if (!strncmp(key.data, kbuf, strlen(kbuf))) {
if (key.size == strlen(kbuf)) {
break;
}
}
}
if (ret == DB_NOTFOUND) {
return 1;
}
return 0;
}
int get_rec(char *kbuf, DBT *value)
{
int ret;
DBT key;
memset(&key, 0, sizeof(DBT));
key.data = kbuf;
key.size = strlen(kbuf);
ret = db->get(db, NULL, &key, value, 0);
switch(ret) {
case 0:
return 0;
case DB_NOTFOUND:
return 1;
default:
return ret;
}
}
int count_recs(void)
{
int ret, cnt = 0;
DBT key, value;
DBC *dbc = NULL;
ret = db->cursor(db, NULL, &dbc, 0);
if (ret != 0)
return ret;
memset(&key, 0, sizeof(DBT));
memset(&value, 0, sizeof(DBT));
while ((dbc->c_get(dbc, &key, &value, DB_NEXT)) != DB_NOTFOUND) {
++cnt;
}
return cnt;
}
int list_recs(char **keys, char **values)
{
int ret, cnt = 0;
DBT key, value;
DBC *dbc = NULL;
ret = db->cursor(db, NULL, &dbc, 0);
if (ret != 0) {
}
}