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.dbsite;
22 
23 import berkeleydb.c;
24 import berkeleydb.dbexception;
25 import berkeleydb.dbenv;
26 import berkeleydb.dbt;
27 import std.stdint;
28 import std.string;
29 import std.conv;
30 
31 class DbSite
32 {
33 private:
34 	DB_SITE *dbsite = null;
35     DbEnv dbenv;
36     int opened;
37 
38 package:
39     @property DB_SITE *_DB_SITE() {return dbsite;}
40 
41 	this(DB_SITE *dbsite, DbEnv dbenv)
42 	{
43         this.dbsite = dbsite;
44         this.dbenv = dbenv;
45         opened = 1;
46 	}
47 
48 public:
49 	~this()
50 	{
51 		if (opened > 0) close();
52 	}
53 
54 	void close()
55 	{
56 		if (opened < 0) {
57 			throw new DbWrongUsingException("Closing closed/removed DbSite");
58 		}
59 		auto ret = dbsite.close(dbsite);
60         opened = -1;
61 		DbRetCodeToException(ret, dbenv);
62         assert(ret == 0);
63 	}
64     
65     void get_address(ref string host, ref uint port)
66     {
67 		if (opened < 0) {
68 			throw new DbWrongUsingException("Operation on closed/removed DbSite");
69 		}
70         const (char) *_host;
71 		auto ret = dbsite.get_address(dbsite, &_host, &port);
72 		DbRetCodeToException(ret, dbenv);
73         assert(ret == 0);
74         host = to!string(_host);
75     }
76 
77     int get_eid()
78     {
79 		if (opened < 0) {
80 			throw new DbWrongUsingException("Operation on closed/removed DbSite");
81 		}
82         int res;
83 		auto ret = dbsite.get_eid(dbsite, &res);
84 		DbRetCodeToException(ret, dbenv);
85         assert(ret == 0);
86         return res;
87     }
88 
89     void remove()
90     {
91 		if (opened < 0) {
92 			throw new DbWrongUsingException("Removing closed/removed DbSite");
93 		}
94 		auto ret = dbsite.remove(dbsite);
95         opened = -1;
96 		DbRetCodeToException(ret, dbenv);
97         assert(ret == 0);
98     }
99 
100     void set_config(uint32_t which, uint32_t value)
101     {
102 		if (opened < 0) {
103 			throw new DbWrongUsingException("Operation on closed/removed DbSite");
104 		}
105 		auto ret = dbsite.set_config(dbsite, which, value);
106 		DbRetCodeToException(ret, dbenv);
107         assert(ret == 0);
108     }
109 
110     uint32_t get_config(uint32_t which)
111     {
112 		if (opened < 0) {
113 			throw new DbWrongUsingException("Operation on closed/removed DbSite");
114 		}
115         uint32_t res;
116 		auto ret = dbsite.get_config(dbsite, which, &res);
117 		DbRetCodeToException(ret, dbenv);
118         assert(ret == 0);
119         return res;
120     }
121 }