source: tags/0.56/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
108  if (this->db_open)
109    return 0;
110
111  if (this->db_name == 0)
112    return this->NOFILENAME;
113
114  char rw='w';
115  switch (rw)
116  {
117    case 'r':
118      do
119      {
120        dbf = gdbm_open(this->db_name,0,GDBM_READER,0644,0);
121        if (dbf == 0)
122          usleep(1000);
123      }
124      while (dbf == 0);
125      break;
126    case 'w':
127      do
128      {
129        dbf = gdbm_open(this->db_name,0,GDBM_WRCREAT|this->sync,0644,0);
130        if (dbf == 0)
131          usleep(1000);
132      }
133      while (dbf == 0);
134    break;
135    default:
136      break;
137  }
138  this->db_open = 1;
139  return 0;
140}
141
142int gdbm_pool::close_db(void)
143{
144  if (db_open) 
145    gdbm_close(this->dbf);
146
147  this->db_open = 0;
148
149  return 0;
150}
151
152int gdbm_pool::get_record(std::string &r, const std::string &k)
153{
154  datum key,content;
155  key.dptr = str_to_charp(k);
156  key.dsize = strlen(key.dptr) + 1;
157  content=gdbm_fetch(this->dbf,key);
158  free(key.dptr);
159  if (content.dptr == 0)
160    return this->FETCHERROR;
161
162  r = content.dptr;
163
164  free(content.dptr);
165
166  return 0;
167}
168
169int gdbm_pool::put_record(const std::string &r, const std::string &k)
170{
171  datum key, content;
172  key.dptr = str_to_charp(k);
173  key.dsize = strlen(key.dptr) + 1;
174  std::string buf;   // wwvv todo buf not necc
175
176  buf = r;
177
178  content.dptr  = str_to_charp(buf); 
179  content.dsize = strlen(content.dptr)+ 1;
180  int rc = gdbm_store(this->dbf, key, content, GDBM_REPLACE);
181  free(content.dptr);
182  free(key.dptr);
183  return rc;
184}
185
186
187int gdbm_pool::remove_record(const stopos_key &key)
188{
189  datum k;
190  int rc;
191  k.dptr = str_to_charp(key.str());
192  k.dsize = strlen(k.dptr) + 1;
193
194  rc = gdbm_delete(this->dbf,k);
195  if (rc !=0)
196    return NOTFOUND;
197  return 0;
198}
199 
200
201bool comprecord(std::string a, std::string b)
202{
203  int ia = StringToNumber<int>(a);
204  int ib = StringToNumber<int>(b);
205
206  return ia < ib;
207}
208
209void gdbm_pool::dump_db(void)
210{
211  datum key, content,key1;
212  key = gdbm_firstkey(this->dbf);
213  std::vector <std::string> vec;
214  while ( key.dptr)
215  {
216    content = gdbm_fetch(this->dbf,key);
217    vec.push_back(std::string(key.dptr)+":"+std::string(content.dptr));
218    key1 = gdbm_nextkey(dbf,key);
219    free(key.dptr);
220    free(content.dptr);
221    key = key1;
222  }
223  std::sort(vec.begin(), vec.end(), ::comprecord);
224  std::vector<std::string>::iterator it;
225  for (it=vec.begin(); it!=vec.end(); ++it)
226    std::cerr << " " << *it << std::endl;
227}
228
Note: See TracBrowser for help on using the repository browser.