Metadata-Version: 2.1
Name: ducer
Version: 0.2.1
Summary: Fast and compact maps and sets with Billions of keys, based on finite-state-transducers.
Author-email: Joachim Folz <joachim.folz@dfki.de>
License: Copyright (c) 2024 Joachim Folz
        
        Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
        
Project-URL: Homepage, https://github.com/jfolz/ducer
Project-URL: Documentation, https://github.com/jfolz/ducer/blob/main/README.md
Project-URL: Repository, https://github.com/jfolz/ducer
Project-URL: Issues, https://github.com/jfolz/ducer/issues
Project-URL: Changelog, https://github.com/jfolz/ducer/blob/main/CHANGELOG.md
Keywords: big data,compression,dataset,dict,finite-state-machines,finite-state-transducers,frozen,fst,map,serialization,set,streaming
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Information Technology
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Rust
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE

# Ducer

This package provides Python bindings for the excellent Rust crate
[fst](https://github.com/BurntSushi/fst) by Andrew Gallant.
`Map` and `Set` classes allow building compact representations from sorted
Python iterables.



## Performance

Ducer maps and sets can be built and queried at millions of keys per second.
Consider the following example:

```Python
n = 1_000_000_000
items = ((b"%09d" % i, n-i) for i in range(n))
data = ducer.Map.build(items, ":memory:")
print(len(data))
```

Creating this map takes about 4 minutes on my humble laptop,
which translates to almost 4 Million items per second.
About 3 minutes is spent in Python just creating the item tuples.
This is a pathological scenario, the resulting output is just 464 bytes.
A real-world example with 1.1 Billion keys, where the msgpacked
key-value pairs occupy 21 GiB (without any kind of searchability),
results in a 4.6 GiB file.



## Limitations

Performance is rarely free,
so there are some limitations you should consider before proceeding:

* Keys **must** be `bytes`
* Keys **must** be inserted in lexicographical order
* Map values **must** be positive integers less than 2^64
* Once built, maps and sets **cannot** be altered



## Usage

### Building & opening

Above, we created a map with data stored in memory.
If you want to store your map in a file, you can also give a path instead:

```Python
ducer.Map.build(items, "path/to/my.map")
```

Building a map like this is a very memory-efficient.
You can also stream a map without loading all data into memory,
e.g., using the builtin mmap:

```Python
with open("path/to/my.map", "rb") as f:
    mm = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
    mm.madvise(mmap.MADV_RANDOM)
    m = ducer.Map(mm)
```

### Access

Other than being read-only, maps and sets behave the same as the builtin
Python `dict` and `set`.
Please open an issue if you find something that doesn't work.
For map `m: Map` and key `k: bytes`, the following all work as intended:

```Python
k in m  # bool
m[k]  # value except KeyError
m.get(k)  # value or None
m.get(k, 0)  # value or 0
len(m)  # number of items
for k in m:  # iterate over keys
    pass
for v in m.values():  # iterate over values
    pass
for k, v in m.items():  # iterate over items
    pass
```

### TODO advanced search & operations
