| 44 | _io = _io # for _commit() |
| 45 | |
| 46 | def __init__(self, filebasename, mode, flag='c'): |
| 47 | filebasename = self._os.fsencode(filebasename) |
| 48 | self._mode = mode |
| 49 | self._readonly = (flag == 'r') |
| 50 | |
| 51 | # The directory file is a text file. Each line looks like |
| 52 | # "%r, (%d, %d)\n" % (key, pos, siz) |
| 53 | # where key is the string key, pos is the offset into the dat |
| 54 | # file of the associated value's first byte, and siz is the number |
| 55 | # of bytes in the associated value. |
| 56 | self._dirfile = filebasename + b'.dir' |
| 57 | |
| 58 | # The data file is a binary file pointed into by the directory |
| 59 | # file, and holds the values associated with keys. Each value |
| 60 | # begins at a _BLOCKSIZE-aligned byte offset, and is a raw |
| 61 | # binary 8-bit string value. |
| 62 | self._datfile = filebasename + b'.dat' |
| 63 | self._bakfile = filebasename + b'.bak' |
| 64 | |
| 65 | # The index is an in-memory dict, mirroring the directory file. |
| 66 | self._index = None # maps keys to (pos, siz) pairs |
| 67 | |
| 68 | # Handle the creation |
| 69 | self._create(flag) |
| 70 | self._update(flag) |
| 71 | |
| 72 | def _create(self, flag): |
| 73 | if flag == 'n': |