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.dbchannel; 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 DbChannel 32 { 33 private: 34 DB_CHANNEL *dbchannel = null; 35 DbEnv dbenv; 36 int opened; 37 static DbChannel[DB_CHANNEL *] dbchannel_map; 38 39 package: 40 static DbChannel from_DB_CHANNEL(const DB_CHANNEL *_dbchannel) 41 { 42 return dbchannel_map[_dbchannel]; 43 } 44 45 @property DB_CHANNEL *_DB_CHANNEL() {return dbchannel;} 46 47 this(DB_CHANNEL *dbchannel, DbEnv dbenv) 48 { 49 this.dbchannel = dbchannel; 50 this.dbenv = dbenv; 51 opened = 1; 52 } 53 54 public: 55 ~this() 56 { 57 if (opened > 0) close(); 58 } 59 60 void close(uint32_t flags = 0) 61 { 62 if (opened < 0) { 63 throw new DbWrongUsingException("Closing closed DbChannel"); 64 } 65 auto ret = dbchannel.close(dbchannel, flags); 66 opened = -1; 67 DbRetCodeToException(ret, dbenv); 68 assert(ret == 0); 69 } 70 71 void send_msg(Dbt *msg, uint32_t nmsg, uint32_t flags = 0) 72 { 73 if (opened < 0) { 74 throw new DbWrongUsingException("Operation on closed DbChannel"); 75 } 76 auto ret = dbchannel.send_msg(dbchannel, &msg.dbt, nmsg, flags); 77 DbRetCodeToException(ret, dbenv); 78 assert(ret == 0); 79 } 80 81 void send_request(Dbt[] request, Dbt *response, 82 db_timeout_t timeout, uint32_t flags = 0) 83 { 84 if (opened < 0) { 85 throw new DbWrongUsingException("Operation on closed DbChannel"); 86 } 87 auto ret = dbchannel.send_request(dbchannel, 88 cast(DBT*)request.ptr, cast(uint)request.length, 89 &response.dbt, timeout, flags); 90 DbRetCodeToException(ret, dbenv); 91 assert(ret == 0); 92 } 93 94 void set_timeout(db_timeout_t timeout) 95 { 96 if (opened < 0) { 97 throw new DbWrongUsingException("Operation on closed DbChannel"); 98 } 99 auto ret = dbchannel.set_timeout(dbchannel, timeout); 100 DbRetCodeToException(ret, dbenv); 101 assert(ret == 0); 102 } 103 }