|
8 | 8 | from .exception import AppendNotSupported |
9 | 9 |
|
10 | 10 | class DiskArray(object): |
| 11 | + ''' |
| 12 | + Stores binary data on disk as a memory mapped file |
| 13 | + using numpy.memmap. Allows for growing the disk data |
| 14 | + by appending and extending. |
| 15 | +
|
| 16 | + Links: |
| 17 | + * https://en.wikipedia.org/wiki/Memory-mapped_file |
| 18 | +
|
| 19 | + # FIXME: |
| 20 | + 1. Explain capacity and actual shape |
| 21 | + 2. Explain growby |
| 22 | + 3. Explain not having to specify shape for 1d arrays |
| 23 | + 4. Explain using structured arrays |
| 24 | + 5. Why memory mapping? What does it provide? |
| 25 | + 6. Why not use np.save and np.load? |
| 26 | + ''' |
11 | 27 | GROWBY = 10000 |
12 | 28 |
|
13 | 29 | def __init__(self, fpath, dtype, mode='r+', shape=None, |
@@ -69,10 +85,13 @@ def _shape_bytes(self, shape, dtype_bytes): |
69 | 85 |
|
70 | 86 | def _truncate_if_needed(self): |
71 | 87 | fd = os.open(self._fpath, os.O_RDWR|os.O_CREAT) |
72 | | - dtype_bytes = np.dtype(self._dtype).itemsize |
73 | | - nbytes = self._shape_bytes(self._shape, dtype_bytes) |
74 | | - os.ftruncate(fd, nbytes) |
75 | | - self._capacity_shape = self._shape |
| 88 | + try: |
| 89 | + dtype_bytes = np.dtype(self._dtype).itemsize |
| 90 | + nbytes = self._shape_bytes(self._shape, dtype_bytes) |
| 91 | + os.ftruncate(fd, nbytes) |
| 92 | + self._capacity_shape = self._shape |
| 93 | + finally: |
| 94 | + os.close(fd) |
76 | 95 | self._create_ndarray() |
77 | 96 |
|
78 | 97 | @property |
@@ -184,6 +203,11 @@ def grow(self, n): |
184 | 203 | # FIXME: code |
185 | 204 | pass |
186 | 205 |
|
| 206 | + def close(self): |
| 207 | + self.data._mmap.close() |
| 208 | + del self.data |
| 209 | + del self._fpath |
| 210 | + |
187 | 211 | def truncate(self, n): |
188 | 212 | # FIXME: code |
189 | 213 | pass |
|
0 commit comments