1 | #include <iostream> |
---|
2 | #include <string> |
---|
3 | #include <sstream> |
---|
4 | #include <iomanip> |
---|
5 | #include "gdbm_pool.h" |
---|
6 | #include "gdbm.h" |
---|
7 | #include <string.h> |
---|
8 | #include <stdlib.h> |
---|
9 | #include "wutils.h" |
---|
10 | #include <algorithm> |
---|
11 | #include <vector> |
---|
12 | #include "stopos.h" |
---|
13 | |
---|
14 | #include "stopos_pool.h" |
---|
15 | gdbm_pool::gdbm_pool() |
---|
16 | { |
---|
17 | } |
---|
18 | |
---|
19 | gdbm_pool::~gdbm_pool() |
---|
20 | { |
---|
21 | } |
---|
22 | |
---|
23 | int gdbm_pool::create_db(const std::string &dbname) |
---|
24 | { |
---|
25 | int rc; |
---|
26 | |
---|
27 | rc = this->purge_db(); |
---|
28 | if (rc != 0) |
---|
29 | return rc; |
---|
30 | |
---|
31 | // The db_name can be composed of parts separated with '/' |
---|
32 | // we try to create this path until the last '/' |
---|
33 | |
---|
34 | size_t p = dbname.find_last_of('/'); |
---|
35 | rc = mkdir_p(dbname.substr(0,p+1),0755); |
---|
36 | if (rc != 0) |
---|
37 | return CREATEERROR; |
---|
38 | |
---|
39 | this->dbf = gdbm_open(this->db_name,0,GDBM_NEWDB|this->sync,0644,0); |
---|
40 | |
---|
41 | if (this->dbf == 0) |
---|
42 | return this->OPENERROR; |
---|
43 | this->close_db(); |
---|
44 | |
---|
45 | return 0; |
---|
46 | } |
---|
47 | |
---|
48 | int gdbm_pool::purge_db(void) |
---|
49 | { |
---|
50 | if (this->db_name == 0) |
---|
51 | return NOFILENAME; |
---|
52 | |
---|
53 | this->close_db(); |
---|
54 | |
---|
55 | unlink(db_name); |
---|
56 | |
---|
57 | return 0; |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | int gdbm_pool::open_db(void) |
---|
63 | { |
---|
64 | |
---|
65 | if (this->db_open) |
---|
66 | return 0; |
---|
67 | |
---|
68 | if (this->db_name == 0) |
---|
69 | return this->NOFILENAME; |
---|
70 | |
---|
71 | char rw='w'; |
---|
72 | switch (rw) |
---|
73 | { |
---|
74 | case 'r': |
---|
75 | do |
---|
76 | { |
---|
77 | dbf = gdbm_open(this->db_name,0,GDBM_READER,0644,0); |
---|
78 | if (dbf == 0) |
---|
79 | usleep(1000); |
---|
80 | } |
---|
81 | while (dbf == 0); |
---|
82 | break; |
---|
83 | case 'w': |
---|
84 | do |
---|
85 | { |
---|
86 | dbf = gdbm_open(this->db_name,0,GDBM_WRCREAT|this->sync,0644,0); |
---|
87 | if (dbf == 0) |
---|
88 | usleep(1000); |
---|
89 | } |
---|
90 | while (dbf == 0); |
---|
91 | break; |
---|
92 | default: |
---|
93 | break; |
---|
94 | } |
---|
95 | this->db_open = 1; |
---|
96 | return 0; |
---|
97 | } |
---|
98 | |
---|
99 | int gdbm_pool::close_db(void) |
---|
100 | { |
---|
101 | if (db_open) |
---|
102 | gdbm_close(this->dbf); |
---|
103 | |
---|
104 | this->db_open = 0; |
---|
105 | |
---|
106 | return 0; |
---|
107 | } |
---|
108 | |
---|
109 | int gdbm_pool::get_record(std::string &r, const std::string &k, const std::string &l) |
---|
110 | { |
---|
111 | datum key,content; |
---|
112 | key.dptr = str_to_charp(k); |
---|
113 | key.dsize = strlen(key.dptr) + 1; |
---|
114 | content=gdbm_fetch(this->dbf,key); |
---|
115 | free(key.dptr); |
---|
116 | if (content.dptr == 0) |
---|
117 | return this->FETCHERROR; |
---|
118 | |
---|
119 | r = content.dptr; |
---|
120 | |
---|
121 | free(content.dptr); |
---|
122 | |
---|
123 | return 0; |
---|
124 | } |
---|
125 | |
---|
126 | int gdbm_pool::put_record(const std::string &r, const std::string &k, const std::string &l) |
---|
127 | { |
---|
128 | datum key, content; |
---|
129 | key.dptr = str_to_charp(k); |
---|
130 | key.dsize = strlen(key.dptr) + 1; |
---|
131 | std::string buf; // wwvv todo buf not necc |
---|
132 | |
---|
133 | buf = r; |
---|
134 | |
---|
135 | content.dptr = str_to_charp(buf); |
---|
136 | content.dsize = strlen(content.dptr)+ 1; |
---|
137 | int rc = gdbm_store(this->dbf, key, content, GDBM_REPLACE); |
---|
138 | free(content.dptr); |
---|
139 | free(key.dptr); |
---|
140 | return rc; |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | int gdbm_pool::remove_record(const stopos_key &key) |
---|
145 | { |
---|
146 | datum k; |
---|
147 | int rc; |
---|
148 | k.dptr = str_to_charp(key.str()); |
---|
149 | k.dsize = strlen(k.dptr) + 1; |
---|
150 | |
---|
151 | rc = gdbm_delete(this->dbf,k); |
---|
152 | if (rc !=0) |
---|
153 | return NOTFOUND; |
---|
154 | return 0; |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | bool comprecord(std::string a, std::string b) |
---|
159 | { |
---|
160 | int ia = StringToNumber<int>(a); |
---|
161 | int ib = StringToNumber<int>(b); |
---|
162 | |
---|
163 | return ia < ib; |
---|
164 | } |
---|
165 | |
---|
166 | void gdbm_pool::dump_db(void) |
---|
167 | { |
---|
168 | datum key, content,key1; |
---|
169 | key = gdbm_firstkey(this->dbf); |
---|
170 | std::vector <std::string> vec; |
---|
171 | while ( key.dptr) |
---|
172 | { |
---|
173 | content = gdbm_fetch(this->dbf,key); |
---|
174 | vec.push_back(std::string(key.dptr)+":"+std::string(content.dptr)); |
---|
175 | key1 = gdbm_nextkey(dbf,key); |
---|
176 | free(key.dptr); |
---|
177 | free(content.dptr); |
---|
178 | key = key1; |
---|
179 | } |
---|
180 | std::sort(vec.begin(), vec.end(), ::comprecord); |
---|
181 | std::vector<std::string>::iterator it; |
---|
182 | for (it=vec.begin(); it!=vec.end(); ++it) |
---|
183 | std::cerr << " " << *it << std::endl; |
---|
184 | } |
---|
185 | |
---|