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.dbstream; 22 23 version(VERSION_6) 24 { 25 import berkeleydb.c; 26 import berkeleydb.dbexception; 27 import berkeleydb.dbenv; 28 import berkeleydb.dbt; 29 import std.stdint; 30 import std.string; 31 import std.conv; 32 33 class DbStream 34 { 35 private: 36 DB_STREAM *dbstream = null; 37 DbEnv dbenv; 38 int opened; 39 40 package: 41 @property DB_STREAM *_DB_STREAM() {return dbstream;} 42 43 this(DB_STREAM *dbstream, DbEnv dbenv) 44 { 45 this.dbstream = dbstream; 46 this.dbenv = dbenv; 47 opened = 1; 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 DbStream"); 60 } 61 auto ret = dbstream.close(dbstream, flags); 62 opened = -1; 63 DbRetCodeToException(ret, dbenv); 64 assert(ret == 0); 65 } 66 67 void read(Dbt *data, db_off_t offset, 68 uint32_t size, uint32_t flags = 0) 69 { 70 if (opened < 0) { 71 throw new DbWrongUsingException("Operation on closed DbStream"); 72 } 73 auto ret = dbstream.read(dbstream, &data.dbt, offset, size, flags); 74 DbRetCodeToException(ret, dbenv); 75 assert(ret == 0); 76 } 77 78 db_off_t size(uint32_t flags = 0) 79 { 80 if (opened < 0) { 81 throw new DbWrongUsingException("Operation on closed DbStream"); 82 } 83 db_off_t res; 84 auto ret = dbstream.size(dbstream, &res, flags); 85 DbRetCodeToException(ret, dbenv); 86 assert(ret == 0); 87 return res; 88 } 89 90 void write(Dbt *data, db_off_t offset, uint32_t flags = 0) 91 { 92 if (opened < 0) { 93 throw new DbWrongUsingException("Operation on closed DbStream"); 94 } 95 auto ret = dbstream.write(dbstream, &data.dbt, offset, flags); 96 DbRetCodeToException(ret, dbenv); 97 assert(ret == 0); 98 } 99 } 100 }