Binary Data (Blobs)¶
Blobs provide efficient binary data storage with typed layouts and zero-copy NumPy integration.
When to use: Use blobs for binary data like images, meshes, and raw buffers.
A builder fills the bytes, build() seals them into a ValueBlob, and a
reader reads them: BlobArray flat and binary, BlobView element by
element, BlobPack region by region.
Quick Start¶
>>> from dsviper import (BlobLayout, BlobArray, BlobArrayBuilder, BlobPack,
... BlobPackBuilder, BlobPackDescriptor)
>>> import numpy as np
A blob’s bytes never change once it is made, so writing happens on a builder that owns them: here 100 positions of 3 floats each.
>>> layout = BlobLayout('float', 3)
>>> builder = BlobArrayBuilder(layout, 100)
Its buffer is writable and flat — 300 floats, not a 100x3 matrix. Reshape it to address elements:
>>> flat = np.asarray(builder)
>>> flat.shape
(300,)
>>> flat.reshape(100, 3)[0] = [1.0, 2.0, 3.0]
build() hands the bytes over, and the value reads back read-only:
>>> del flat
>>> positions = builder.build()
>>> np.frombuffer(positions, dtype=np.float32)[:3]
array([1., 2., 3.], dtype=float32)
>>> np.frombuffer(positions, dtype=np.float32).flags.writeable
False
A BlobPack holds several regions in one blob, and carries their table:
>>> desc = BlobPackDescriptor()
>>> desc.add_region('positions', BlobLayout('float', 3), 100)
>>> desc.add_region('normals', BlobLayout('float', 3), 100)
>>> desc.add_region('indices', BlobLayout('uint', 3), 50)
>>> pack_builder = BlobPackBuilder(desc)
>>> with pack_builder['positions'] as region:
... np.asarray(region).reshape(100, 3)[0] = [1.0, 2.0, 3.0]
>>> mesh = BlobPack.from_blob(pack_builder.build())
>>> np.frombuffer(mesh['positions'], dtype=np.float32).shape
(300,)
Choosing the Right Class¶
Data Type |
Class |
Example |
|---|---|---|
Raw bytes |
|
|
Typed array |
|
|
Multiple regions |
|
Mesh with pos/normals/indices |
Bytes, before they are a value |
|
|
Database reference |
|
SHA-1 hash reference |
Large files (>2GB) |
|
Streaming upload |
Core Classes¶
Describes the layout of one element in a blob: a data type and a component count. |
|
Reads blob as a flat array of blob_layout.data_type. |
|
Fills the bytes of one typed array, then seals them into a ValueBlob. |
|
Several memory regions packed into one blob, described by a BlobPackDescriptor. |
|
Fills the regions a BlobPackDescriptor describes, then seals them into a ValueBlob a BlobPack reads. |
|
Describes the binary regions a BlobPack will hold. |
|
One region of a BlobPack - its name, the layout of its elements, how many it holds, and where it sits in the pack's blob. |
Blob I/O¶
The handle on a blob being written to a database past 2 GB, in chunks. |
|
Builds a blob element by element: write(value) appends, end_encoding() returns the finished blob. |
|
A BlobLayout together with the Viper types it reads as. |
|
Interprets the bytes of a blob through its layout. |
|
The bytes of one blob, with the blob_id and layout that go with them. |
Database Integration¶
Retrieves blobs from a persistence layer. |
|
What a store knows about one blob without reading it: its id, layout, size, SQLite rowid, and whether it is chunked (over 2 GB). |
|
How many blobs a store holds and how big they are: count, total, smallest, largest, in bytes. |