Skip to content
HN On Hacker News ↗

GitHub - plabayo/venndb: in memory Rust database to query your data like a Venn diagram

▲ 49 points • 4 comments • by gdcbe • 1w ago • HN discussion ↗

Pangram verdict · v3.3

We believe that this entire text is human-written.

0 %

AI likelihood · overall

Human
100% human-written 0% AI-generated
SEGMENTS · HUMAN 1 of 1
SEGMENTS · AI 0 of 1
WORD COUNT 1,712
PEAK AI % 0% · §1
Analyzed
Nov 17
backend: pangram/v3.3
Segments scanned
1 windows
avg 1712 words each
Distribution
100 / 0%
human / AI fraction
Verdict
Human
Pangram v3.3

Article text · 1,712 words · 1 segments analyzed

Human AI-generated
§1 Human · 0%

An append-only in-memory database in Rust for rows queried using bit (flag) columns. This database is designed for a very specific use case where you have mostly static data that you typically load at startup and have to query constantly using very simple filters. Datasets like these can be large and should be both fast and compact. For the limited usecases where venndb can be applied to, it has next to no dependencies and is faster then traditional choices, such as a naive implementation or a more heavy lifted dependency such as Sqlite. See the benchmarks for more information on this topic. This project was developed originally in function of rama, where you can see it being used for example to provide an in-memory (upstream) proxy database. Do let us know in case you use it as well in your project, such that we can assemble a showcase list. 💬 Come join us at Discord on the #venndb public channel. To ask questions, discuss ideas and ask how venndb may be useful for you. Index venndb manual: Usage: quick introduction on how to use venndb; Benchmarks: benchmark results to give you a rough idea how venndb peforms for the use case it is made for (write once, read constantly, using binary filters mostly); Q&A: Frequently Asked Questions (FAQ); Example: the full example (expanded version from Usage), tested and documented; Generated Code Summary: a documented overview of the API that venndb will generate for you when using #[derive(VennDB)] on your named field struct; technical information: Safety Compatibility MSRV (older versions might work but we make no guarantees); Roadmap License: MIT license and Apache 2.0 License misc: Contributing Sponsors Usage Add venndb as a dependency: cargo add venndb and import the derive macro in the module where you want to use it: use venndb::{Any, VennDB}; #[derive(Debug, VennDB)] pub struct Employee { #[venndb(key)] id: u32, name: String, is_manager: Option<bool>, is_admin: bool, #[venndb(skip)] foo: bool, #[venndb(filter, any)] department: Department, #[venndb(filter)] country: Option<String>, } fn main() { let db = EmployeeDB::from_iter(employees()).expect("unique ids"); let mut query = db.query(); let employee = query .is_admin(true) .is_manager(false) .department(Department::Engineering) .execute() .expect("to have found at least one") .any(); println!("non-manager admin engineer: {:?}", employee); } See the full example, the examples (the code in this document is taken from these) or the "Generated Code Summary" chapter below to learn how to use the VennDB and its generated code. Benchmarks Benchmarks displayed here are taken on a dev machine with following specs: Macbook Pro — 16 inch (2024), OS: Tahoe 26.5 Chip: Apple M4 Max, Memory: 64 GB The benchmarks test 4 different implementations of a proxy database, shaped like the one of rama: 7 bool properties and 5 filter maps, of which the state, city and ASN have up to 1 000, 10 000 and 5 000 distinct values. venn: the venndb version (very similar to the example below); roaring: an index of roaring bitmaps (version: 0.12.0), written by hand; sqlite: an in-memory table with an index per filter map (using the sqlite crate (version: 0.37.4)); naive: just a Vec<Proxy>, over which is iterated. Apart from the first two, each row is a query: the filters listed are set on db.query(), followed by any() to pick a random proxy among the matching ones. The bool filters ask for true, the filter maps (pool, country, city) for one value unless more are mentioned, and 1% of their rows match any value. The numbers are the median time. The benchmarks are created by running just bench-full | python3 scripts/bench_tables.py. Database with 100_000 records: venn roaring sqlite naive create the database (from_rows) 12.3 ms 26.52 ms 344 ms - look up by key (get_by_id) 10.52 ns 11.74 ns 1.347 µs 7.041 µs 3 bool filters: tcp + socks5 + datacenter 246.7 ns 1.568 µs 4.135 ms 653.7 µs 1 bool filter, 1 filter map: tcp + country 1.448 µs 2.023 µs 358.4 µs 862 µs 2 bool filters, 2 filter maps: tcp + datacenter + country + city 2.324 µs 3.898 µs 153.9 µs 749.6 µs 1 bool filter, 2 filter maps: mobile + pool + 3 countries 2.862 µs 12.44 µs 1.366 ms 343.6 µs 1 bool filter, 1 filter map: tcp + 5 cities 2.112 µs 6.395 µs 390.1 µs 1.013 ms 3 bool filters and a predicate: tcp + socks5 + datacenter 197.2 ns 134.5 µs - 670.1 µs 3 bool filters and a runtime column: tcp + socks5 + datacenter 207.3 ns - - - memory of the index, at its peak 6.483 MB 7.476 MB - - Database with 1_000_000 records: venn roaring sqlite naive create the database (from_rows) 121.3 ms 390.9 ms 3.838 s - look up by key (get_by_id) 16.46 ns 19.88 ns 1.18 µs 7.062 µs 3 bool filters: tcp + socks5 + datacenter 192 ns 13.79 µs 42.23 ms 6.984 ms 1 bool filter, 1 filter map: tcp + country 2.184 µs 16.69 µs 6.929 ms 8.347 ms 2 bool filters, 2 filter maps: tcp + datacenter + country + city 24.57 µs 30.38 µs 2.295 ms 8.565 ms 1 bool filter, 2 filter maps: mobile + pool + 3 countries 28.55 µs 97.05 µs 12.5 ms 5.92 ms 1 bool filter, 1 filter map: tcp + 5 cities 3.264 µs 49.17 µs 7.098 ms 10.18 ms 3 bool filters and a predicate: tcp + socks5 + datacenter 216.7 ns 3.469 ms - 7.244 ms 3 bool filters and a runtime column: tcp + socks5 + datacenter 222.9 ns - - - memory of the index, at its peak 61.79 MB 59.49 MB - - We are not database nor hardware experts though. Please do open an issue if you think these benchmarks are incorrect or if related improvements can be made. Contributions in the form of Pull requests are welcomed as well, see the Contribution guidelines for more information. Q&A ❓ Why use this over Database X? venndb is not a database, but is close enough for some specific purposes. It shines for long-lived read-only use cases where you need to filter on plenty of binary properties and get a rando matching result. Do not try to replace your usual database needs with it. ❓ Where can I propose a new feature X or some other improvement? Please open an issue and also read the Contribution guidelines. We look forward to hear from you. Alternatively you can also join our Discord and start a conversation / discussion over there. ❓ Can I use whatever type for a #[venndb(filter)] property? Yes, as long as it implements Eq + Hash + Clone. That said, we do recommend that you use enum values if you can, or some other highly restricted form. Using for example a String directly is a bad idea as that would mean that bE != Be != BE != Belgium != Belgique != België. Even though these are really referring all to the same country. In such cases a much better idea is to at the very least create a wrapper type such as struct Country(String), to allow you to enforce sanitization/validation when creating the value and ensuring the hashes will be the same for those values that are conceptually the same. ❓ How do I make a filter optional? Both filters (bool properties) and filter maps (T != bool properties with the #[venndb(filter)] attribute) can be made optional by wrapping the types with Option, resulting in Option<bool> and Option<T>. Rows that have the Option::None value for such an optional column cannot filter on that property, but there is no other consequence beyond that. ❓ Why can do keys have to be unique and non-optional? Within venndb keys are meant to be able to look up, a row which was previously received via filters. As such it makes no sense for such keys to be: duplicate: it would mean: as that can result in multiple rows or the wrong row to be returned; optional: as that would mean the row cannot be looked up when the key is not defined; ❓ How can I allow some rows to match for any value of a certain (filter) column? Filter maps can allow to have a value to match all other values. It is up to you to declare the filter as such, and to also define for that type what the one value to rule them all is. Usage: use venndb::{Any, VennDB}; #[derive(Debug, PartialEq, Eq, Clone, Hash, Any)] pub enum Department { #[any] Any, Hr, Engineering, } #[derive(Debug, VennDB)] pub struct Employee { name: String, #[venndb(filter, any)] department: Department, } let db = EmployeeDB::from_iter([ Employee { name: "Jack".to_owned(), department: Department::Any }, Employee { name: "Derby".to_owned(), department: Department::Hr }, ]); let mut query = db.query(); // will match Jack and Derby, as Jack is marked as Any, meaning it can work for w/e value let hr_employees: Vec<_> = query.department(Department::Hr).execute().unwrap().iter().collect(); assert_eq!(hr_employees.len(), 2); For a type that is no enum you implement venndb::Any yourself, and for a type that is not yours you can point to a function instead: #[venndb(filter, any = is_any)]. ❓ How can a property have multiple values? Mark the filter as a set. A row then matches each of the values in it: #[derive(Debug, VennDB)] pub struct Employee { name: String, #[venndb(filter, set)] skills: HashSet<Skill>, } // everyone with this skill, or with a skill for which the predicate, // called once per distinct skill and not once per row, returns true query.skills(Skill::Rust); query.skills_where(|skill| matches!(skill, Skill::Language { native: true, .. })); It works for HashSet, BTreeSet and any type implementing venndb::FilterSet, not for lists such as Vec. For a type that is not yours use #[venndb(filter, members = path)], with path a function returning an iterator over the values, and item = "Type" if the type of those cannot be told from the property. Every "filter map" property has a <property>_where method, rows with an any value always match it. ❓ How can I filter on something that is not a property, or that changes? Create a column for it and add it to the query: // computed once, for a fact about a row that is no property let seniors = db.column(|employee| employee.years > 10); // set and unset at any time and from any thread, without a lock let on_leave = RuntimeColumn::new(); on_leave.set(db.index_by_id(&35).unwrap(), true);