summaryrefslogtreecommitdiff
path: root/src/hdb/db.h
blob: 66219449f1995eca75ca9e5f78b85b8fa9409ed7 (plain)
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
/*************************************************************************
 *
 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef DBH
#define DBH

#include <hdb/sortable.h>
#include <mt/msmap.h>

////////////////////////////////////////////////////
//
// OK for MT and MP
//

class dbmap {
public:
  dbmap();
  ~dbmap();

  filemap::head_t* head() { return file->head(); }
  const filemap::head_t* head() const { return file->head(); }

  int remap();
  
  int size() const { return recs.size(); }
  int empty() const { return recs.empty(); }
  
  const sref operator()(int row,int col) const throw(merror_t) {
    check(); return recs[row].rec->at(col);
  }
  const sref at(int row,int col) const throw(merror_t) {
    check(); return recs[row].rec->at(col);
  }
  
  ///////////////////////////////////////////////////////

  void Get(int row,spile& list) throw(merror_t);
  void Set(int row,int col,const sref& s) throw(merror_t);
  void Set(int row,const spile& list) throw(merror_t);

  ///////////////////////////////////////////////////////

  void Rem(int row) throw(merror_t);
  int Insert(const spile& list,int unique) throw(merror_t);

  ///////////////////////////////////////////////////////
  
  int Tables() const { return head()->tables.get(); }
  const sref Table(int col) const { check(); return head()->get(col); }

  ///////////////////////////////////////////////////////

  void SetTable(int col,const sref& name) throw(merror_t);
  void RemTable(int col) throw(merror_t);
  void InsertTable(int col,const sref& name) throw(merror_t);

  ///////////////////////////////////////////////////////

  int Index() const { return index; }
  int FindIndex(const sref& tab) const { return tmap.get(tab); }
  int Index(const sref& tab) const throw(merror_t) {
    int p=FindIndex(tab);
    if(p<0) THROW("db: table ["<<tab<<"] does not exist");
    return p;
  }

  ///////////////////////////////////////////////////////

  const mstring& Compname() const { return compname; }

  void Sort(const sref& tab,const sref& cmp) throw(merror_t);
  void Order(const sref& tab,const sref& cmp) throw(merror_t);

  int Find(const sref& key) const { return recs.get(key); }

  ///////////////////////////////////////////////////////

  void Open(const mstring& path,int update=0,int prot=0600) throw(merror_t);
  void Saveas(const mstring& path,int prot=0600) throw(merror_t);
  time_t Lastmod() const { return file->Lastmod(); }
  void Close();

  static void Sync();

protected:
  dbmap(const dbmap& db) {}
  void operator=(const dbmap& db) {}

  void check() const {
    if(fatal!=head()->fatal.get()) THROW("db: fatal state change")
  }

  void remap_recs();
  void remap_tabs();

  static void setup();
  static const op_t* CompOperator(const sref& s);

  static filemap* flist;
  static mutex_t mutex;
  static opmap M;
private:
  int wok,minor,major,fatal,index;
  mstring compname;
  filemap* file;
  rimap tmap;
  indexmap recs;
};

void Convert(dbmap& db,const char* path);

#endif