source: tags/0.5/gdbm_pool.cpp @ 9

Last change on this file since 9 was 9, checked in by willem, 11 years ago

willem

File size: 4.1 KB
Line 
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"
15gdbm_pool::gdbm_pool()
16{
17}
18
19gdbm_pool::~gdbm_pool()
20{
21}
22
23int gdbm_pool::create_db(void)
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 separeted with '/'
32  // we try to create this path until the last '/'
33 
34  size_t p = this->sdb_name.find_last_of('/');
35  rc = mkdir_p(this->sdb_name.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
44  this->status.init();
45
46  rc = put_status();
47
48  if (rc != 0)
49  {
50    this->close_db();
51    return this->STOREERROR;
52  }
53  this->close_db();
54  return 0;
55}
56
57int gdbm_pool::purge_db(void)
58{
59  if (this->db_name == 0)
60    return NOFILENAME;
61
62  this->close_db();
63
64  unlink(db_name);
65
66  return 0;
67}
68
69int gdbm_pool::get_status(void)
70{
71  datum key, content;
72  // create record with starting key
73  key.dptr = str_to_charp(this->statuskey.str());
74  key.dsize = strlen(key.dptr)+1;
75
76  content=gdbm_fetch(this->dbf,key);
77  free(key.dptr);
78  if (content.dptr == 0)
79    return FETCHERROR;
80  this->status = statusrecord(content.dptr,dbsep);
81
82  free(content.dptr);
83  return 0;
84}
85
86int gdbm_pool::put_status(void)
87{
88  datum key, content;
89  key.dptr = str_to_charp(this->statuskey.str());
90  key.dsize = strlen(key.dptr)+1;
91  std::string buf;
92
93  buf = this->status.str(dbsep);
94
95  content.dptr  = str_to_charp(buf);
96  content.dsize = strlen(content.dptr) + 1;
97
98  int rc = gdbm_store(this->dbf, key, content, GDBM_REPLACE);
99
100  free(content.dptr);
101  free(key.dptr);
102  return rc;
103}
104
105int gdbm_pool::open_db(void)
106{
107  int rc;
108
109  if (this->db_open)
110    return 0;
111
112  if (this->db_name == 0)
113    return this->NOFILENAME;
114
115  char rw='w';
116  switch (rw)
117  {
118    case 'r':
119      do
120      {
121        dbf = gdbm_open(this->db_name,0,GDBM_READER,0644,0);
122        if (dbf == 0)
123          usleep(1000);
124      }
125      while (dbf == 0);
126      break;
127    case 'w':
128      do
129      {
130        dbf = gdbm_open(this->db_name,0,GDBM_WRCREAT|this->sync,0644,0);
131        if (dbf == 0)
132          usleep(1000);
133      }
134      while (dbf == 0);
135    break;
136    default:
137      break;
138  }
139  this->db_open = 1;
140  return 0;
141}
142
143int gdbm_pool::close_db(void)
144{
145  if (db_open) 
146    gdbm_close(this->dbf);
147
148  this->db_open = 0;
149
150  return 0;
151}
152
153int gdbm_pool::get_record(datarecord &r, const stopos_key &k)
154{
155  datum key,content;
156  key.dptr = str_to_charp(k.str());
157  key.dsize = strlen(key.dptr) + 1;
158  content=gdbm_fetch(this->dbf,key);
159  free(key.dptr);
160  if (content.dptr == 0)
161    return this->FETCHERROR;
162
163  r = datarecord(content.dptr,dbsep);
164
165  free(content.dptr);
166
167  return 0;
168}
169
170int gdbm_pool::put_record(datarecord &r, const stopos_key &k)
171{
172  datum key, content;
173  key.dptr = str_to_charp(k.str());
174  key.dsize = strlen(key.dptr) + 1;
175  std::string buf;
176
177  buf = r.str(dbsep);
178
179  content.dptr  = str_to_charp(buf);
180  content.dsize = strlen(content.dptr)+ 1;
181  int rc = gdbm_store(this->dbf, key, content, GDBM_REPLACE);
182  free(content.dptr);
183  free(key.dptr);
184  return rc;
185}
186
187
188int gdbm_pool::remove_record(const stopos_key &key)
189{
190  datum k;
191  int rc;
192  k.dptr = str_to_charp(key.str());
193  k.dsize = strlen(k.dptr) + 1;
194
195  rc = gdbm_delete(this->dbf,k);
196  if (rc !=0)
197    return NOTFOUND;
198  return 0;
199}
200 
201
202bool comprecord(std::string a, std::string b)
203{
204  int ia = StringToNumber<int>(a);
205  int ib = StringToNumber<int>(b);
206
207  return ia < ib;
208}
209
210void gdbm_pool::dump_db(void)
211{
212  datum key, content,key1;
213  key = gdbm_firstkey(this->dbf);
214  std::vector <std::string> vec;
215  while ( key.dptr)
216  {
217    content = gdbm_fetch(this->dbf,key);
218    vec.push_back(std::string(key.dptr)+":"+std::string(content.dptr));
219    key1 = gdbm_nextkey(dbf,key);
220    free(key.dptr);
221    free(content.dptr);
222    key = key1;
223  }
224  std::sort(vec.begin(), vec.end(), ::comprecord);
225  std::vector<std::string>::iterator it;
226  for (it=vec.begin(); it!=vec.end(); ++it)
227    std::cerr << " " << *it << std::endl;
228}
229
Note: See TracBrowser for help on using the repository browser.