2 Unix SMB/CIFS implementation.
6 Copyright (C) 2004-2006 Tim Potter <tpot@samba.org>
7 Copyright (C) 2007 Jelmer Vernooij <jelmer@samba.org>
9 ** NOTE! The following LGPL license applies to the tdb
10 ** library. This does NOT imply that all of Samba is released
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, see <http://www.gnu.org/licenses/>.
31 /* This symbol is used in both includes.h and Python.h which causes an
32 annoying compiler warning. */
38 /* Include tdb headers */
44 typedef TDB_CONTEXT tdb;
47 /* The tdb functions will crash if a NULL tdb context is passed */
52 %typemap(check,noblock=1) TDB_CONTEXT* {
54 SWIG_exception(SWIG_ValueError,
55 "tdb context must be non-NULL");
58 /* In and out typemaps for the TDB_DATA structure. This is converted to
59 and from the Python string type which can contain arbitrary binary
62 %typemap(in,noblock=1) TDB_DATA {
63 if ($input == Py_None) {
66 } else if (!PyString_Check($input)) {
67 PyErr_SetString(PyExc_TypeError, "string arg expected");
70 $1.dsize = PyString_Size($input);
71 $1.dptr = (uint8_t *)PyString_AsString($input);
75 %typemap(out,noblock=1) TDB_DATA {
76 if ($1.dptr == NULL && $1.dsize == 0) {
79 $result = PyString_FromStringAndSize((const char *)$1.dptr, $1.dsize);
84 /* Treat a mode_t as an unsigned integer */
87 /* flags to tdb_store() */
88 %constant int REPLACE = TDB_REPLACE;
89 %constant int INSERT = TDB_INSERT;
90 %constant int MODIFY = TDB_MODIFY;
92 /* flags for tdb_open() */
93 %constant int DEFAULT = TDB_DEFAULT;
94 %constant int CLEAR_IF_FIRST = TDB_CLEAR_IF_FIRST;
95 %constant int INTERNAL = TDB_INTERNAL;
96 %constant int NOLOCK = TDB_NOLOCK;
97 %constant int NOMMAP = TDB_NOMMAP;
98 %constant int CONVERT = TDB_CONVERT;
99 %constant int BIGENDIAN = TDB_BIGENDIAN;
109 TDB_ERR_LOCK_TIMEOUT,
115 %rename(lock_all) tdb_context::lockall;
116 %rename(unlock_all) tdb_context::unlockall;
118 %rename(read_lock_all) tdb_context::lockall_read;
119 %rename(read_unlock_all) tdb_context::unlockall_read;
121 %typemap(default,noblock=1) int tdb_flags {
125 %typemap(default,noblock=1) int open_flags {
129 %typemap(default,noblock=1) int hash_size {
133 %typemap(default,noblock=1) mode_t mode {
137 %typemap(default,noblock=1) int flag {
142 %typemap(out,noblock=1) tdb * {
143 /* Throw an IOError exception from errno if tdb_open() returns NULL */
145 PyErr_SetFromErrno(PyExc_IOError);
148 $result = SWIG_NewPointerObj($1, $1_descriptor, 0);
151 typedef struct tdb_context {
153 tdb(const char *name, int hash_size, int tdb_flags, int flags, mode_t mode) {
154 return tdb_open(name, hash_size, tdb_flags, flags, mode);
156 enum TDB_ERROR error();
157 ~tdb() { tdb_close($self); }
159 int append(TDB_DATA key, TDB_DATA new_dbuf);
160 const char *errorstr();
162 TDB_DATA fetch(TDB_DATA key);
163 int delete(TDB_DATA key);
164 int store(TDB_DATA key, TDB_DATA dbuf, int flag);
165 int exists(TDB_DATA key);
167 TDB_DATA nextkey(TDB_DATA key);
171 int unlockall_read();
173 int transaction_start();
174 int transaction_commit();
175 int transaction_cancel();
176 int transaction_recover();
180 void set_max_dead(int max_dead);
188 # Random access to keys, values
189 def __getitem__(self, key):
190 result = self.get(key)
192 raise KeyError, '%s: %s' % (key, self.errorstr())
195 def __setitem__(self, key, item):
196 if self.store(key, item) == -1:
197 raise IOError, self.errorstr()
199 def __delitem__(self, key):
200 if not self.exists(key):
201 raise KeyError, '%s: %s' % (key, self.errorstr())
204 def __contains__(self, key):
205 return self.exists(key) != 0
207 def has_key(self, key):
208 return self.exists(key) != 0
210 def fetch_uint32(self, key):
215 return struct.unpack("<L", data)[0]
217 def fetch_int32(self, key):
222 return struct.unpack("<l", data)[0]
226 def __init__(self, tdb):
235 self.key = self.tdb.firstkey()
240 self.key = self.tdb.nextkey(self.key)
246 return self.TdbIterator(self)
248 # Implement other dict functions using TdbIterator
251 return [k for k in iter(self)]
254 return [self[k] for k in iter(self)]
257 return [(k, self[k]) for k in iter(self)]
260 return len(self.keys())
266 # TODO: iterkeys, itervalues, iteritems
268 # TODO: any other missing methods for container types