Skip to main content

A library that automatically offloads large pickle-able objects stored in a dict/list to SQLite, reducing memory usage. It can be used with the same syntax as a regular dict/list.

Project description

English · 日本語 (Japanese)

swapcollection

A library that automatically offloads large objects stored in a dict/list to SQLite, reducing memory usage. It can be used with the same syntax as a regular dict/list.

Quick Start

pip install swapcollection

SwapDict

from swapcollection import SwapDict

# Values whose pickled size >= size_threshold are spilled to SQLite
d = SwapDict(size_threshold=100)

# Small values stay in-memory
d["name"] = "alice"
d.data  # → {'name': 'alice'}

# Large values are transparently spilled to SQLite
d["blob"] = b"x" * 200
d.data  # → {'name': 'alice', 'blob': 'swapdict_<uuid>'}  (ID stored in memory)

# Retrieval works transparently — same as a normal dict
d["blob"]  # → b'xxxxxxxx...'

# All standard dict methods work
d.update({"a": 1, "b": 2})
len(d)             # → 4
list(d.keys())     # → ['name', 'blob', 'a', 'b']

d["name"]          # → 'alice'
d.get("missing")   # → None
d.pop("name")      # → 'alice'
"name" in d        # → False

d.setdefault("c", 99)  # → 99
list(d.items())
# → [('blob', b'xxx...'), ('a', 1), ('b', 2), ('c', 99)]

# repr() also resolves spilled values
repr(d)  # → "{'blob': b'xxx...', 'a': 1, 'b': 2, 'c': 99}"

# Class method
SwapDict.fromkeys(["x", "y"], 0)
# → {'x': 0, 'y': 0}

d.clear()
len(d)             # → 0

SwapList

from swapcollection import SwapList

# Same threshold concept
xs = SwapList(size_threshold=100)

xs.append(42)
xs.append(b"y" * 200)
xs[0]    # → 42
xs[1]    # → b'yyyyyy...'

xs.insert(1, "hello")
list(xs)  # → [42, 'hello', b'yyyyyy...']

xs.extend([1.5, b"z" * 200])
len(xs)   # → 5

# Slice access resolves spilled values
xs[1:3]   # → ['hello', b'yyyyyy...']

# Slice assignment with large values works
xs[2:4] = [99, b"w" * 200]

# contains / index / count
42 in xs             # → True
xs.index(42)         # → 0
xs.count(42)         # → 1

# pop
val = xs.pop()       # → b'zzzzzz...'
len(xs)              # → 4

# reverse
xs.reverse()

# sort (including spilled values)
ys = SwapList(size_threshold=50)
ys.extend([b"c" * 20, b"a" * 20, b"b" * 20])
ys.sort()
list(ys)  # → [b'aaaa...', b'bbbb...', b'cccc...']

# comparison works transparently
zs = SwapList([1, 2])
zs == [1, 2]   # → True
zs == [1, 3]   # → False

# concatenation / repetition
zs + [3, 4]    # → [1, 2, 3, 4]
zs * 3         # → [1, 2, 1, 2, 1, 2]

xs.clear()
len(xs)        # → 0

How It Works

SwapDict / SwapList automatically offloads values whose pickled size meets or exceeds size_threshold to a SQLite database. Any pickle-able object (not just bytes) is eligible.

Value Storage
Pickle size < size_threshold In-memory
Pickle size >= size_threshold SQLite (transparent)

Spilled values are retrieved, updated, and deleted using the same dict/list interface — no special API calls needed.

Integrity

Each spilled value is stored alongside a SHA-256 hash of its pickled bytes, embedded directly in the spill ID. On retrieval, the hash is verified:

  • If the database row was tampered with or corrupted after storage, a RuntimeError("hash mismatch") is raised immediately.
  • This protects against undetected SQLite corruption or external manipulation of stored values — the integrity of the data you store is checked every time you access it.

Caveats

  • size_threshold is the pickle-size threshold in bytes; values >= this size are spilled.
  • size_threshold cannot be changed after instantiation.
  • Any pickle-able object type (not only bytes) can be spilled.
  • Deleting the SQLite database will cause errors on previously spilled items.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

swapcollection-1.1.0.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

swapcollection-1.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file swapcollection-1.1.0.tar.gz.

File metadata

  • Download URL: swapcollection-1.1.0.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for swapcollection-1.1.0.tar.gz
Algorithm Hash digest
SHA256 5da835233f42c3a9ab4b5a77ce67e95699c228af68d3bb2cb47a663e2e844b0a
MD5 ac13bc7a20c7a95daefcbbef7f1b9cc4
BLAKE2b-256 5e835bf217c1bee4998e1e1d57368944580f353189659d01cd3f6b627164a2d2

See more details on using hashes here.

File details

Details for the file swapcollection-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: swapcollection-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for swapcollection-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c898f7fc7121fa3ac27bd0062548f2ebc0fe15575f49c392132eb5ffd546eed1
MD5 94e7ed37f378964dd942d496a976dc2b
BLAKE2b-256 cb065f7028aeffc15ff9ad43fd6fa549d4b1977b5e9e6e87f1daff3593236d60

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page