1 /*
2    bdb2d is BerkeleyDB for D language
3    It is part of unDE project (http://unde.su)
4 
5    Copyright (C) 2009-2014 Nikolay (unDEFER) Krivchenkov <undefer@gmail.com>
6 
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 module berkeleydb.dbmpoolfile;
22 
23 import berkeleydb.c;
24 import berkeleydb.dbexception;
25 import berkeleydb.dbenv;
26 import berkeleydb.dbt;
27 import berkeleydb.dbtxn;
28 import std.stdint;
29 import std.string;
30 
31 alias DB_CACHE_PRIORITY DbCachePriority;
32 
33 class DbMpoolfile
34 {
35 private:
36 	DB_MPOOLFILE *dbmpoolfile = null;
37     DbEnv dbenv;
38     int opened;
39 
40 package:
41     @property DB_MPOOLFILE *_DB_MPOOLFILE() {return dbmpoolfile;}
42     @property int _opened() {return opened;}
43 
44 	this(DB_MPOOLFILE *dbmpoolfile, DbEnv dbenv)
45 	{
46         this.dbmpoolfile = dbmpoolfile;
47         this.dbenv = dbenv;
48 	}
49 
50 public:
51 	~this()
52 	{
53 		if (opened > 0) close();
54 	}
55 
56 	void close(uint32_t flags = 0)
57 	{
58         if (opened < 0) {
59 			throw new DbWrongUsingException("Closing closed DbMpoolfile");
60 		}
61 		auto ret = dbmpoolfile.close(dbmpoolfile, flags);
62         opened = -1;
63 		DbRetCodeToException(ret, dbenv);
64         assert(ret == 0);
65 	}
66 
67     int get(db_pgno_t *pgnoaddr, DbTxn txnid, uint32_t flags, ref ubyte *page)
68     {
69         if (opened < 0) {
70             throw new DbWrongUsingException("Operation on closed DbMpoolfile");
71         }
72         auto ret = dbmpoolfile.get(dbmpoolfile, pgnoaddr, txnid?txnid._DB_TXN:null, flags, &page);
73         return DbRetCodeToException!"DbMpoolfile.get"(ret, dbenv);
74     }
75 
76     void open(string file, uint32_t flags, int mode, size_t pagesize)
77     {
78         if (opened < 0) {
79             throw new DbWrongUsingException("Operation on closed DbMpoolfile");
80         }
81         auto ret = dbmpoolfile.open(dbmpoolfile, file.toStringz(), flags, mode, pagesize);
82         opened = 1;
83         DbRetCodeToException(ret, dbenv);
84         assert(ret == 0);
85     }
86 
87     void put(void *pgaddr, DbCachePriority priority, uint32_t flags = 0)
88     {
89         if (opened < 0) {
90             throw new DbWrongUsingException("Operation on closed DbMpoolfile");
91         }
92         auto ret = dbmpoolfile.put(dbmpoolfile, pgaddr, priority, flags);
93         DbRetCodeToException(ret, dbenv);
94         assert(ret == 0);
95     }
96 
97     void sync()
98     {
99         if (opened < 0) {
100             throw new DbWrongUsingException("Operation on closed DbMpoolfile");
101         }
102         auto ret = dbmpoolfile.sync(dbmpoolfile);
103         DbRetCodeToException(ret, dbenv);
104         assert(ret == 0);
105     }
106 
107     /* Memory Pool File Configuration */
108     void set_clear_len(uint32_t len)
109     {
110         if (opened < 0) {
111             throw new DbWrongUsingException("Configuration on closed DbMpoolfile");
112         }
113         if (opened > 0) {
114             throw new DbWrongUsingException("Configuration of opened DbMpoolfile");
115         }
116         auto ret = dbmpoolfile.set_clear_len(dbmpoolfile, len);
117         DbRetCodeToException(ret, dbenv);
118         assert(ret == 0);
119     }
120 
121     uint32_t get_clear_len()
122     {
123         if (opened < 0) {
124             throw new DbWrongUsingException("Configuration on closed DbMpoolfile");
125         }
126         uint32_t res;
127         auto ret = dbmpoolfile.get_clear_len(dbmpoolfile, &res);
128         DbRetCodeToException(ret, dbenv);
129         assert(ret == 0);
130         return res;
131     }
132 
133     void set_fileid(uint8_t[] fileid)
134     {
135         if (opened < 0) {
136             throw new DbWrongUsingException("Configuration on closed DbMpoolfile");
137         }
138         if (opened > 0) {
139             throw new DbWrongUsingException("Configuration of opened DbMpoolfile");
140         }
141         if (fileid.length != DB_FILE_ID_LEN)
142         {
143             throw new DbWrongUsingException("Unique file identifiers must be a DB_FILE_ID_LEN length array of bytes");
144         }
145         auto ret = dbmpoolfile.set_fileid(dbmpoolfile, fileid.ptr);
146         DbRetCodeToException(ret, dbenv);
147         assert(ret == 0);
148     }
149 
150     void get_fileid(ref uint8_t[DB_FILE_ID_LEN] fileid)
151     {
152         if (opened < 0) {
153             throw new DbWrongUsingException("Configuration on closed DbMpoolfile");
154         }
155         auto ret = dbmpoolfile.get_fileid(dbmpoolfile, fileid.ptr);
156         DbRetCodeToException(ret, dbenv);
157         assert(ret == 0);
158     }
159 
160     uint8_t[DB_FILE_ID_LEN] get_fileid()
161     {
162         uint8_t[DB_FILE_ID_LEN] res;
163         get_fileid(res);
164         return res;
165     }
166 
167     void set_flags(uint32_t flags, int onoff)
168     {
169         if (opened < 0) {
170             throw new DbWrongUsingException("Configuration on closed DbMpoolfile");
171         }
172         auto ret = dbmpoolfile.set_flags(dbmpoolfile, flags, onoff);
173         DbRetCodeToException(ret, dbenv);
174         assert(ret == 0);
175     }
176 
177     uint32_t get_flags()
178     {
179         if (opened < 0) {
180             throw new DbWrongUsingException("Configuration on closed DbMpoolfile");
181         }
182         uint32_t res;
183         auto ret = dbmpoolfile.get_flags(dbmpoolfile, &res);
184         DbRetCodeToException(ret, dbenv);
185         assert(ret == 0);
186         return res;
187     }
188 
189     void set_ftype(int ftype)
190     {
191         if (opened < 0) {
192             throw new DbWrongUsingException("Configuration on closed DbMpoolfile");
193         }
194         if (opened > 0) {
195             throw new DbWrongUsingException("Configuration of opened DbMpoolfile");
196         }
197         auto ret = dbmpoolfile.set_ftype(dbmpoolfile, ftype);
198         DbRetCodeToException(ret, dbenv);
199         assert(ret == 0);
200     }
201 
202     int get_ftype()
203     {
204         if (opened < 0) {
205             throw new DbWrongUsingException("Configuration on closed DbMpoolfile");
206         }
207         int res;
208         auto ret = dbmpoolfile.get_ftype(dbmpoolfile, &res);
209         DbRetCodeToException(ret, dbenv);
210         assert(ret == 0);
211         return res;
212     }
213 
214     void set_lsn_offset(int32_t lsn_offset)
215     {
216         if (opened < 0) {
217             throw new DbWrongUsingException("Configuration on closed DbMpoolfile");
218         }
219         if (opened > 0) {
220             throw new DbWrongUsingException("Configuration of opened DbMpoolfile");
221         }
222         auto ret = dbmpoolfile.set_lsn_offset(dbmpoolfile, lsn_offset);
223         DbRetCodeToException(ret, dbenv);
224         assert(ret == 0);
225     }
226 
227     int32_t get_lsn_offset()
228     {
229         if (opened < 0) {
230             throw new DbWrongUsingException("Configuration on closed DbMpoolfile");
231         }
232         int32_t res;
233         auto ret = dbmpoolfile.get_lsn_offset(dbmpoolfile, &res);
234         DbRetCodeToException(ret, dbenv);
235         assert(ret == 0);
236         return res;
237     }
238 
239     void set_maxsize(uint32_t gbytes, uint32_t bytes)
240     {
241         if (opened < 0) {
242             throw new DbWrongUsingException("Configuration on closed DbMpoolfile");
243         }
244         auto ret = dbmpoolfile.set_maxsize(dbmpoolfile, gbytes, bytes);
245         DbRetCodeToException(ret, dbenv);
246         assert(ret == 0);
247     }
248 
249     void set_maxsize(uint64_t bytes)
250     {
251         uint32_t _gbytes = cast(uint32_t) bytes/(1024*1024*1024);
252         uint32_t _bytes = bytes%(1024*1024*1024);
253         set_maxsize(_gbytes, _bytes);
254     }
255 
256     void get_maxsize(ref uint32_t gbytes, ref uint32_t bytes)
257     {
258         if (opened < 0) {
259             throw new DbWrongUsingException("Configuration on closed DbMpoolfile");
260         }
261         auto ret = dbmpoolfile.get_maxsize(dbmpoolfile, &gbytes, &bytes);
262         DbRetCodeToException(ret, dbenv);
263         assert(ret == 0);
264     }
265 
266     uint64_t get_maxsize()
267     {
268         uint32_t gbytes;
269         uint32_t bytes;
270         get_maxsize(gbytes, bytes);
271         return (1024UL*1024*1024)*gbytes + bytes;
272     }
273 
274     void set_pgcookie(Dbt *pgcookie)
275     {
276         if (opened < 0) {
277             throw new DbWrongUsingException("Configuration on closed DbMpoolfile");
278         }
279         if (opened > 0) {
280             throw new DbWrongUsingException("Configuration of opened DbMpoolfile");
281         }
282         auto ret = dbmpoolfile.set_pgcookie(dbmpoolfile, &pgcookie.dbt);
283         DbRetCodeToException(ret, dbenv);
284         assert(ret == 0);
285     }
286 
287     void get_pgcookie(Dbt *dbt)
288     {
289         if (opened < 0) {
290             throw new DbWrongUsingException("Configuration on closed DbMpoolfile");
291         }
292         auto ret = dbmpoolfile.get_pgcookie(dbmpoolfile, &dbt.dbt);
293         DbRetCodeToException(ret, dbenv);
294         assert(ret == 0);
295     }
296 
297     Dbt get_pgcookie()
298     {
299         Dbt res;
300         get_pgcookie(&res);
301         return res;
302     }
303 
304     void set_priority(DbCachePriority priority)
305     {
306         if (opened < 0) {
307             throw new DbWrongUsingException("Configuration on closed DbMpoolfile");
308         }
309         auto ret = dbmpoolfile.set_priority(dbmpoolfile, priority);
310         DbRetCodeToException(ret, dbenv);
311         assert(ret == 0);
312     }
313 
314     DbCachePriority get_priority()
315     {
316         if (opened < 0) {
317             throw new DbWrongUsingException("Configuration on closed DbMpoolfile");
318         }
319         DbCachePriority res;
320         auto ret = dbmpoolfile.get_priority(dbmpoolfile, &res);
321         DbRetCodeToException(ret, dbenv);
322         assert(ret == 0);
323         return res;
324     }
325 }