source: trunk/stopos_pool.h

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

willem

File size: 6.9 KB
Line 
1
2/*
3   Copyright 2013 Willem Vermin, SURFsara
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8
9       http://www.apache.org/licenses/LICENSE-2.0
10
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16 */
17//
18// stopos_pool is the base class responsible for the logic
19// behind adding, removing etc lines in the database.
20//
21// The access to the database itself is the task for the derived
22// class.
23//
24//  In the following: a key is unique, never will two records
25//  have the same slot in the same databse between two calls
26//  of create_db or betwen create_db and purge_db.
27//
28// The derived class must provide the following functions:
29//
30//  std::string key_to_slot(const std::string &k)
31//    given a key k, the function should return
32//    a slot, where a record with key k can be
33//    stored. If the database does not need a
34//    slot (mysql, gdbm), then return "".
35//    This slot will be used in subsequent put_record
36//    and get_record calls.
37//
38//  int get_record(std::string &r,
39//                         const std::string &k,
40//                         const std::string &l)
41//    This function must, given the key k, and the slot l,
42//      return the record in r. Return value = 0 if
43//      everything ok.
44//
45//
46//   int put_record(const std::string &r,
47//                  const std::string &k,
48//                  const std::string &l)
49//     This function shoud store record r, given key k
50//     and slot l. The slot may be ignored.
51//
52//   int remove_record(const std::string &k,
53//                     const std::string &l)
54//     This function should remove the record given key k
55//       and slot l.
56//
57//
58//   int create_db(const std::string &dbname)
59//     This function should create the database. dbname is
60//     a string containing the filename, tablename or
61//     whatever. The caller (stoposserver) should know about the
62//     issues involved.
63//
64//  int purge_db(void)
65//    This function has to remove the database, the name
66//    is in this->db_name  (char *) and
67//          this->sdb_name  (std::string)
68//
69//  int open_db(void)
70//    This function has to open the database, such that
71//    subsequent put_record, get_record, remove_record
72//    calls can function.
73//    See purge() above for the name of the database.
74//
75//  int close_db(void)
76//    This function is called when database operations
77//    are done, normally it should close the database.
78//    See purge() above for the name of the database.
79//
80// Derived classes have no knowledge of the structure of
81// the records, and can add extra information if so
82// desired, as long as a retrieved record is equal
83// to a stored record with the same key/slot combination.
84//
85// Also the "status record" is for usage of the base class only,
86// with one exception: the field "extra" i.e. this->status->extra
87// is not used by the base class and can be used by derived classes
88// at will.
89//
90
91#ifndef STOPOS_POOL_H
92#define STOPOS_POOL_H
93#include <string>
94#include "stopos.h"
95#include "stopos_key.h"
96#include <iostream>
97 
98class datarecord
99{
100  public:
101    datarecord();
102    datarecord(const std::string &s,char sep);
103    stopos_key prev;
104    stopos_key next;
105    longuint retrieved;
106    std::string value;
107    std::string str(char dbsep) const;
108};
109
110class statusrecord
111{
112  public:
113  stopos_key next;      // points to first line for the 'next' command
114  stopos_key first;     // points to first record
115  stopos_key last;      // points to last record
116  stopos_key nextd;     // points to record to be dumped
117  longuint count;       // total number of records added
118  longuint present;     // total number of records now availabe
119  longuint present0;    // ditto, but uncommitted
120  std::string extra;    // string that can be used by derived class
121
122  statusrecord(void);
123  statusrecord(const std::string &s,char dbsep);
124  std::string str(char dbsep);
125  void init(void);
126};
127
128class stopos_pool
129{
130  protected:
131    char* db_name;        // name for database
132    std::string sdb_name; // idem
133    static const unsigned int reclen = 4096;
134    bool kvp; //true if Koot-Vermin paradigm is to be used
135    bool db_open;
136   
137    stopos_key statuskey;
138    stopos_key nextkey;
139    statusrecord status;
140
141    virtual std::string key_to_slot(const std::string &k) = 0;
142    virtual int create_db(const std::string &dbname)      = 0;
143    virtual int get_record(std::string &r,       
144                           const std::string &k, 
145                           const std::string &l)          = 0;
146    virtual int put_record(const std::string &r, 
147                           const std::string &k, 
148                           const std::string &l)          = 0;
149    virtual int remove_record(const std::string &k, 
150                              const std::string &l)       = 0;
151
152    int put_status(void);
153    int get_status(void);
154
155    // the function add_line calls this to get a slot
156    // from the derived class.  If no slot is necessary,
157    // this function may return "".
158    // The subsequent put_record call will have the return
159    // value of this function as parameter.
160
161    int get_record(datarecord &r,const stopos_key &k);
162    int put_record(const datarecord &r,const stopos_key &k);
163    int remove_record(const stopos_key &k);
164
165    std::string whoami;
166
167  public:
168
169    stopos_pool();
170    ~stopos_pool();
171
172    virtual int purge_db(void)  = 0;
173    virtual int open_db(void)   = 0;
174    virtual int close_db(void)  = 0;
175
176
177    int create_db(void);
178
179    virtual void set_db_name(std::string filename);
180    virtual std::string get_db_name(void);
181
182    virtual int add_line(const std::string line,std::string &key);
183    virtual int get_line(std::string &line,longuint &retrieved,std::string &key);
184    virtual int remove_line(const std::string &key);
185
186    virtual void set_kvp(const bool v);
187    virtual bool get_kvp(void);
188
189    int get_counts(longuint &count, 
190                   longuint &present, 
191                   longuint &present0);
192
193    int dump_line(std::string  &line,
194                  longuint     &retrieved,
195                  std::string  &key);
196
197    bool is_statusrecord(const std::string &k);
198
199    const static int ERROR             =  1; 
200    const static int NOFILENAME        =  2;
201    const static int OPENERROR         =  3;
202    const static int STOREERROR        =  4;
203    const static int FETCHERROR        =  5;
204    const static int NOTFOUND          =  6;
205    const static int NOTIMPLEMENTED    =  7;
206    const static int REMOVEERROR       =  8;
207    const static int LOCKERROR         =  9;
208    const static int CLOSEERROR        = 10;
209    const static int UNLOCKERROR       = 11;
210    const static int DBNOTOPEN         = 12;
211    const static int CREATEERROR       = 13;
212    const static int PURGEERROR        = 14;
213    const static int RECORDTOOLONG     = 15;
214
215};
216#endif
Note: See TracBrowser for help on using the repository browser.