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.dbtxn;
22 
23 import berkeleydb.c;
24 import berkeleydb.dbexception;
25 import berkeleydb.dbenv;
26 import std.stdint;
27 import std.string;
28 import std.conv;
29 
30 class DbTxn
31 {
32 private:
33 	DB_TXN *dbtxn = null;
34     DbEnv dbenv;
35     int opened;
36 
37 package:
38     @property DB_TXN *_DB_TXN() {return dbtxn;}
39 
40 	this(DB_TXN *dbtxn, DbEnv dbenv)
41 	{
42         this.dbtxn = dbtxn;
43         this.dbenv = dbenv;
44         opened = 1;
45 	}
46 
47 public:
48 	~this()
49 	{
50 		if (opened > 0) abort();
51 	}
52 
53 	void abort()
54 	{
55 		if (opened < 0) {
56 			throw new DbWrongUsingException("Aborting committed or discarded/aborted DbTxn");
57 		}
58 		auto ret = dbtxn.abort(dbtxn);
59         opened = -1;
60 		DbRetCodeToException(ret, dbenv);
61         assert(ret == 0);
62 	}
63 
64 	void discard(uint32_t flags = 0)
65 	{
66 		if (opened < 0) {
67 			throw new DbWrongUsingException("Discarding committed or discarded/aborted DbTxn");
68 		}
69 		auto ret = dbtxn.discard(dbtxn, flags);
70         opened = -1;
71 		DbRetCodeToException(ret, dbenv);
72         assert(ret == 0);
73 	}
74 
75 	void commit(uint32_t flags = 0)
76 	{
77 		if (opened < 0) {
78 			throw new DbWrongUsingException("Commiting committed or discarded/aborted DbTxn");
79 		}
80 		auto ret = dbtxn.commit(dbtxn, flags);
81         opened = -1;
82 		DbRetCodeToException(ret, dbenv);
83         assert(ret == 0);
84 	}
85 
86     void prepare(uint8_t[DB_GID_SIZE] gid)
87     {
88 		if (opened < 0) {
89 			throw new DbWrongUsingException("Preparing committed or discarded/aborted DbTxn");
90 		}
91 		auto ret = dbtxn.prepare(dbtxn, gid.ptr);
92 		DbRetCodeToException(ret, dbenv);
93         assert(ret == 0);
94     }
95 
96     uint32_t id()
97     {
98 		if (opened < 0) {
99 			throw new DbWrongUsingException("Operation on committed or discarded/aborted DbTxn");
100 		}
101         return dbtxn.id(dbtxn);
102     }
103 
104     void set_name(string name)
105     {
106 		if (opened < 0) {
107 			throw new DbWrongUsingException("Operation on committed or discarded/aborted DbTxn");
108 		}
109 		auto ret = dbtxn.set_name(dbtxn, name.toStringz());
110 		DbRetCodeToException(ret, dbenv);
111         assert(ret == 0);
112     }
113 
114     string get_name()
115     {
116 		if (opened < 0) {
117 			throw new DbWrongUsingException("Operation on committed or discarded/aborted DbTxn");
118 		}
119         const (char) *res;
120 		auto ret = dbtxn.get_name(dbtxn, &res);
121 		DbRetCodeToException(ret, dbenv);
122         assert(ret == 0);
123         return to!string(res);
124     }
125 
126     void set_priority(uint32_t priority)
127     {
128 		if (opened < 0) {
129 			throw new DbWrongUsingException("Operation on committed or discarded/aborted DbTxn");
130 		}
131 		auto ret = dbtxn.set_priority(dbtxn, priority);
132 		DbRetCodeToException(ret, dbenv);
133         assert(ret == 0);
134     }
135 
136     uint32_t get_priority()
137     {
138 		if (opened < 0) {
139 			throw new DbWrongUsingException("Operation on committed or discarded/aborted DbTxn");
140 		}
141         uint32_t res;
142 		auto ret = dbtxn.get_priority(dbtxn, &res);
143 		DbRetCodeToException(ret, dbenv);
144         assert(ret == 0);
145         return res;
146     }
147     
148     void set_timeout(db_timeout_t timeout, uint32_t flags = 0)
149     {
150 		if (opened < 0) {
151 			throw new DbWrongUsingException("Operation on committed or discarded/aborted DbTxn");
152 		}
153 		auto ret = dbtxn.set_timeout(dbtxn, timeout, flags);
154 		DbRetCodeToException(ret, dbenv);
155         assert(ret == 0);
156     }
157 
158     void set_commit_token(DbTxnToken *buffer)
159     {
160 		if (opened < 0) {
161 			throw new DbWrongUsingException("Operation on committed or discarded/aborted DbTxn");
162 		}
163 		auto ret = dbtxn.set_commit_token(dbtxn, buffer);
164 		DbRetCodeToException(ret, dbenv);
165         assert(ret == 0);
166     }
167 }