| 1 | n/a | """A dumb and slow but simple dbm clone. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | For database spam, spam.dir contains the index (a text file), |
|---|
| 4 | n/a | spam.bak *may* contain a backup of the index (also a text file), |
|---|
| 5 | n/a | while spam.dat contains the data (a binary file). |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | XXX TO DO: |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | - seems to contain a bug when updating... |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | - reclaim free space (currently, space once occupied by deleted or expanded |
|---|
| 12 | n/a | items is never reused) |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | - support concurrent access (currently, if two processes take turns making |
|---|
| 15 | n/a | updates, they can mess up the index) |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | - support efficient access to large databases (currently, the whole index |
|---|
| 18 | n/a | is read when the database is opened, and some updates rewrite the whole index) |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | - support opening for read-only (flag = 'm') |
|---|
| 21 | n/a | |
|---|
| 22 | 1 | """ |
|---|
| 23 | n/a | |
|---|
| 24 | 1 | import os as _os |
|---|
| 25 | 1 | import __builtin__ |
|---|
| 26 | 1 | import UserDict |
|---|
| 27 | n/a | |
|---|
| 28 | 1 | _open = __builtin__.open |
|---|
| 29 | n/a | |
|---|
| 30 | 1 | _BLOCKSIZE = 512 |
|---|
| 31 | n/a | |
|---|
| 32 | 1 | error = IOError # For anydbm |
|---|
| 33 | n/a | |
|---|
| 34 | 2 | class _Database(UserDict.DictMixin): |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | # The on-disk directory and data files can remain in mutually |
|---|
| 37 | n/a | # inconsistent states for an arbitrarily long time (see comments |
|---|
| 38 | n/a | # at the end of __setitem__). This is only repaired when _commit() |
|---|
| 39 | n/a | # gets called. One place _commit() gets called is from __del__(), |
|---|
| 40 | n/a | # and if that occurs at program shutdown time, module globals may |
|---|
| 41 | n/a | # already have gotten rebound to None. Since it's crucial that |
|---|
| 42 | n/a | # _commit() finish successfully, we can't ignore shutdown races |
|---|
| 43 | n/a | # here, and _commit() must not reference any globals. |
|---|
| 44 | 1 | _os = _os # for _commit() |
|---|
| 45 | 1 | _open = _open # for _commit() |
|---|
| 46 | n/a | |
|---|
| 47 | 1 | def __init__(self, filebasename, mode): |
|---|
| 48 | 25 | self._mode = mode |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | # The directory file is a text file. Each line looks like |
|---|
| 51 | n/a | # "%r, (%d, %d)\n" % (key, pos, siz) |
|---|
| 52 | n/a | # where key is the string key, pos is the offset into the dat |
|---|
| 53 | n/a | # file of the associated value's first byte, and siz is the number |
|---|
| 54 | n/a | # of bytes in the associated value. |
|---|
| 55 | 25 | self._dirfile = filebasename + _os.extsep + 'dir' |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | # The data file is a binary file pointed into by the directory |
|---|
| 58 | n/a | # file, and holds the values associated with keys. Each value |
|---|
| 59 | n/a | # begins at a _BLOCKSIZE-aligned byte offset, and is a raw |
|---|
| 60 | n/a | # binary 8-bit string value. |
|---|
| 61 | 25 | self._datfile = filebasename + _os.extsep + 'dat' |
|---|
| 62 | 25 | self._bakfile = filebasename + _os.extsep + 'bak' |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | # The index is an in-memory dict, mirroring the directory file. |
|---|
| 65 | 25 | self._index = None # maps keys to (pos, siz) pairs |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | # Mod by Jack: create data file if needed |
|---|
| 68 | 25 | try: |
|---|
| 69 | 25 | f = _open(self._datfile, 'r') |
|---|
| 70 | 10 | except IOError: |
|---|
| 71 | 10 | f = _open(self._datfile, 'w') |
|---|
| 72 | 10 | self._chmod(self._datfile) |
|---|
| 73 | 25 | f.close() |
|---|
| 74 | 25 | self._update() |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | # Read directory file into the in-memory index dict. |
|---|
| 77 | 1 | def _update(self): |
|---|
| 78 | 25 | self._index = {} |
|---|
| 79 | 25 | try: |
|---|
| 80 | 25 | f = _open(self._dirfile) |
|---|
| 81 | 10 | except IOError: |
|---|
| 82 | 10 | pass |
|---|
| 83 | n/a | else: |
|---|
| 84 | 141 | for line in f: |
|---|
| 85 | 126 | line = line.rstrip() |
|---|
| 86 | 126 | key, pos_and_siz_pair = eval(line) |
|---|
| 87 | 126 | self._index[key] = pos_and_siz_pair |
|---|
| 88 | 15 | f.close() |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | # Write the index dict to the directory file. The original directory |
|---|
| 91 | n/a | # file (if any) is renamed with a .bak extension first. If a .bak |
|---|
| 92 | n/a | # file currently exists, it's deleted. |
|---|
| 93 | 1 | def _commit(self): |
|---|
| 94 | n/a | # CAUTION: It's vital that _commit() succeed, and _commit() can |
|---|
| 95 | n/a | # be called from __del__(). Therefore we must never reference a |
|---|
| 96 | n/a | # global in this routine. |
|---|
| 97 | 115 | if self._index is None: |
|---|
| 98 | 13 | return # nothing to do |
|---|
| 99 | n/a | |
|---|
| 100 | 102 | try: |
|---|
| 101 | 102 | self._os.unlink(self._bakfile) |
|---|
| 102 | 11 | except self._os.error: |
|---|
| 103 | 11 | pass |
|---|
| 104 | n/a | |
|---|
| 105 | 102 | try: |
|---|
| 106 | 102 | self._os.rename(self._dirfile, self._bakfile) |
|---|
| 107 | 2 | except self._os.error: |
|---|
| 108 | 2 | pass |
|---|
| 109 | n/a | |
|---|
| 110 | 102 | f = self._open(self._dirfile, 'w') |
|---|
| 111 | 102 | self._chmod(self._dirfile) |
|---|
| 112 | 1039 | for key, pos_and_siz_pair in self._index.iteritems(): |
|---|
| 113 | 937 | f.write("%r, %r\n" % (key, pos_and_siz_pair)) |
|---|
| 114 | 102 | f.close() |
|---|
| 115 | n/a | |
|---|
| 116 | 1 | sync = _commit |
|---|
| 117 | n/a | |
|---|
| 118 | 1 | def __getitem__(self, key): |
|---|
| 119 | 491 | pos, siz = self._index[key] # may raise KeyError |
|---|
| 120 | 491 | f = _open(self._datfile, 'rb') |
|---|
| 121 | 491 | f.seek(pos) |
|---|
| 122 | 491 | dat = f.read(siz) |
|---|
| 123 | 491 | f.close() |
|---|
| 124 | 491 | return dat |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | # Append val to the data file, starting at a _BLOCKSIZE-aligned |
|---|
| 127 | n/a | # offset. The data file is first padded with NUL bytes (if needed) |
|---|
| 128 | n/a | # to get to an aligned offset. Return pair |
|---|
| 129 | n/a | # (starting offset of val, len(val)) |
|---|
| 130 | 1 | def _addval(self, val): |
|---|
| 131 | 271 | f = _open(self._datfile, 'rb+') |
|---|
| 132 | 271 | f.seek(0, 2) |
|---|
| 133 | 271 | pos = int(f.tell()) |
|---|
| 134 | 271 | npos = ((pos + _BLOCKSIZE - 1) // _BLOCKSIZE) * _BLOCKSIZE |
|---|
| 135 | 271 | f.write('\0'*(npos-pos)) |
|---|
| 136 | 271 | pos = npos |
|---|
| 137 | 271 | f.write(val) |
|---|
| 138 | 271 | f.close() |
|---|
| 139 | 271 | return (pos, len(val)) |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | # Write val to the data file, starting at offset pos. The caller |
|---|
| 142 | n/a | # is responsible for ensuring that there's enough room starting at |
|---|
| 143 | n/a | # pos to hold val, without overwriting some other value. Return |
|---|
| 144 | n/a | # pair (pos, len(val)). |
|---|
| 145 | 1 | def _setval(self, pos, val): |
|---|
| 146 | 173 | f = _open(self._datfile, 'rb+') |
|---|
| 147 | 173 | f.seek(pos) |
|---|
| 148 | 173 | f.write(val) |
|---|
| 149 | 173 | f.close() |
|---|
| 150 | 173 | return (pos, len(val)) |
|---|
| 151 | n/a | |
|---|
| 152 | n/a | # key is a new key whose associated value starts in the data file |
|---|
| 153 | n/a | # at offset pos and with length siz. Add an index record to |
|---|
| 154 | n/a | # the in-memory index dict, and append one to the directory file. |
|---|
| 155 | 1 | def _addkey(self, key, pos_and_siz_pair): |
|---|
| 156 | 122 | self._index[key] = pos_and_siz_pair |
|---|
| 157 | 122 | f = _open(self._dirfile, 'a') |
|---|
| 158 | 122 | self._chmod(self._dirfile) |
|---|
| 159 | 122 | f.write("%r, %r\n" % (key, pos_and_siz_pair)) |
|---|
| 160 | 122 | f.close() |
|---|
| 161 | n/a | |
|---|
| 162 | 1 | def __setitem__(self, key, val): |
|---|
| 163 | 444 | if not type(key) == type('') == type(val): |
|---|
| 164 | 0 | raise TypeError, "keys and values must be strings" |
|---|
| 165 | 444 | if key not in self._index: |
|---|
| 166 | 122 | self._addkey(key, self._addval(val)) |
|---|
| 167 | n/a | else: |
|---|
| 168 | n/a | # See whether the new value is small enough to fit in the |
|---|
| 169 | n/a | # (padded) space currently occupied by the old value. |
|---|
| 170 | 322 | pos, siz = self._index[key] |
|---|
| 171 | 322 | oldblocks = (siz + _BLOCKSIZE - 1) // _BLOCKSIZE |
|---|
| 172 | 322 | newblocks = (len(val) + _BLOCKSIZE - 1) // _BLOCKSIZE |
|---|
| 173 | 322 | if newblocks <= oldblocks: |
|---|
| 174 | 173 | self._index[key] = self._setval(pos, val) |
|---|
| 175 | n/a | else: |
|---|
| 176 | n/a | # The new value doesn't fit in the (padded) space used |
|---|
| 177 | n/a | # by the old value. The blocks used by the old value are |
|---|
| 178 | n/a | # forever lost. |
|---|
| 179 | 149 | self._index[key] = self._addval(val) |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | # Note that _index may be out of synch with the directory |
|---|
| 182 | n/a | # file now: _setval() and _addval() don't update the directory |
|---|
| 183 | n/a | # file. This also means that the on-disk directory and data |
|---|
| 184 | n/a | # files are in a mutually inconsistent state, and they'll |
|---|
| 185 | n/a | # remain that way until _commit() is called. Note that this |
|---|
| 186 | n/a | # is a disaster (for the database) if the program crashes |
|---|
| 187 | n/a | # (so that _commit() never gets called). |
|---|
| 188 | n/a | |
|---|
| 189 | 1 | def __delitem__(self, key): |
|---|
| 190 | n/a | # The blocks used by the associated value are lost. |
|---|
| 191 | 77 | del self._index[key] |
|---|
| 192 | n/a | # XXX It's unclear why we do a _commit() here (the code always |
|---|
| 193 | n/a | # XXX has, so I'm not changing it). _setitem__ doesn't try to |
|---|
| 194 | n/a | # XXX keep the directory file in synch. Why should we? Or |
|---|
| 195 | n/a | # XXX why shouldn't __setitem__? |
|---|
| 196 | 77 | self._commit() |
|---|
| 197 | n/a | |
|---|
| 198 | 1 | def keys(self): |
|---|
| 199 | 5 | return self._index.keys() |
|---|
| 200 | n/a | |
|---|
| 201 | 1 | def has_key(self, key): |
|---|
| 202 | 0 | return key in self._index |
|---|
| 203 | n/a | |
|---|
| 204 | 1 | def __contains__(self, key): |
|---|
| 205 | 0 | return key in self._index |
|---|
| 206 | n/a | |
|---|
| 207 | 1 | def iterkeys(self): |
|---|
| 208 | 5 | return self._index.iterkeys() |
|---|
| 209 | 1 | __iter__ = iterkeys |
|---|
| 210 | n/a | |
|---|
| 211 | 1 | def __len__(self): |
|---|
| 212 | 0 | return len(self._index) |
|---|
| 213 | n/a | |
|---|
| 214 | 1 | def close(self): |
|---|
| 215 | 38 | self._commit() |
|---|
| 216 | 38 | self._index = self._datfile = self._dirfile = self._bakfile = None |
|---|
| 217 | n/a | |
|---|
| 218 | 1 | __del__ = close |
|---|
| 219 | n/a | |
|---|
| 220 | 1 | def _chmod (self, file): |
|---|
| 221 | 234 | if hasattr(self._os, 'chmod'): |
|---|
| 222 | 234 | self._os.chmod(file, self._mode) |
|---|
| 223 | n/a | |
|---|
| 224 | n/a | |
|---|
| 225 | 1 | def open(file, flag=None, mode=0666): |
|---|
| 226 | n/a | """Open the database file, filename, and return corresponding object. |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | The flag argument, used to control how the database is opened in the |
|---|
| 229 | n/a | other DBM implementations, is ignored in the dumbdbm module; the |
|---|
| 230 | n/a | database is always opened for update, and will be created if it does |
|---|
| 231 | n/a | not exist. |
|---|
| 232 | n/a | |
|---|
| 233 | n/a | The optional mode argument is the UNIX mode of the file, used only when |
|---|
| 234 | n/a | the database has to be created. It defaults to octal code 0666 (and |
|---|
| 235 | n/a | will be modified by the prevailing umask). |
|---|
| 236 | n/a | |
|---|
| 237 | n/a | """ |
|---|
| 238 | n/a | # flag argument is currently ignored |
|---|
| 239 | n/a | |
|---|
| 240 | n/a | # Modify mode depending on the umask |
|---|
| 241 | 25 | try: |
|---|
| 242 | 25 | um = _os.umask(0) |
|---|
| 243 | 25 | _os.umask(um) |
|---|
| 244 | 0 | except AttributeError: |
|---|
| 245 | 0 | pass |
|---|
| 246 | n/a | else: |
|---|
| 247 | n/a | # Turn off any bits that are set in the umask |
|---|
| 248 | 25 | mode = mode & (~um) |
|---|
| 249 | n/a | |
|---|
| 250 | 25 | return _Database(file, mode) |
|---|