Skip to content

Commit d73585f

Browse files
feat: add size method to get size of object in bytes
1 parent fa1e351 commit d73585f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

mapbuffer/mapbuffer.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,23 @@ def get(self, label, *args, **kwargs):
232232

233233
return self.getindex(pos)
234234

235+
def size(self, label:int) -> int:
236+
"""Returns the size of the object in bytes."""
237+
pos = self.find_index_position(label)
238+
index = self.index()
239+
N = index.shape[0]
240+
offset = index[i,1]
241+
242+
crc_compensation = 0
243+
if self.format_version > 0:
244+
crc_compensation = 4
245+
246+
if i < N - 1:
247+
next_offset = index[i+1,1]
248+
return next_offset - offset - crc_compensation
249+
else:
250+
return len(self.buffer) - offset - crc_compensation
251+
235252
def __contains__(self, label):
236253
pos = self.find_index_position(label)
237254
return pos is not None

0 commit comments

Comments
 (0)