Skip to content

Commit 6e3835d

Browse files
authored
Merge pull request #278 from meilisearch/combined-lmdb-support
Combined version of LMDB mdb.master and mdb.master3
2 parents 8b01852 + b47b72f commit 6e3835d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+4932
-674
lines changed

.github/workflows/rust.yml

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,33 @@ jobs:
2828
cargo clean
2929
cargo test
3030
31-
check_all_features:
31+
check-heed3:
32+
name: Check the heed3 project
33+
runs-on: ${{ matrix.os }}
34+
strategy:
35+
matrix:
36+
os: [ubuntu-latest, windows-latest, macos-latest]
37+
include:
38+
- os: ubuntu-latest
39+
- os: windows-latest
40+
- os: macos-latest
41+
42+
steps:
43+
- uses: actions/checkout@v2
44+
with:
45+
submodules: recursive
46+
- uses: actions-rs/toolchain@v1
47+
with:
48+
profile: minimal
49+
toolchain: stable
50+
override: true
51+
- name: Run cargo check
52+
run: |
53+
cargo clean
54+
bash convert-to-heed3.sh
55+
cargo check -p heed3
56+
57+
check-all-features:
3258
name: Check all the features of the heed project
3359
runs-on: ${{ matrix.os }}
3460
env:
@@ -51,9 +77,35 @@ jobs:
5177
override: true
5278
- name: Run cargo test
5379
run: |
54-
cd heed
5580
cargo clean
56-
cargo check --all-features
81+
cargo check --all-features -p heed
82+
83+
check-all-features-heed3:
84+
name: Check all the features of the heed3 project
85+
runs-on: ${{ matrix.os }}
86+
env:
87+
RUSTFLAGS: -D warnings
88+
strategy:
89+
matrix:
90+
os: [ubuntu-latest, macos-latest]
91+
include:
92+
- os: ubuntu-latest
93+
- os: macos-latest
94+
95+
steps:
96+
- uses: actions/checkout@v2
97+
with:
98+
submodules: recursive
99+
- uses: actions-rs/toolchain@v1
100+
with:
101+
profile: minimal
102+
toolchain: stable
103+
override: true
104+
- name: Run cargo test
105+
run: |
106+
cargo clean
107+
bash convert-to-heed3.sh
108+
cargo check --all-features -p heed3
57109
58110
examples:
59111
name: Run the heed examples
@@ -81,6 +133,31 @@ jobs:
81133
cargo run --example 2>&1 | grep -E '^ ' | awk '!/rmp-serde/' | xargs -n1 cargo run --example
82134
cargo run --example rmp-serde --features serde-rmp
83135
136+
heed3-examples:
137+
name: Run the heed3 examples
138+
runs-on: ${{ matrix.os }}
139+
strategy:
140+
matrix:
141+
os: [ubuntu-latest, macos-latest]
142+
include:
143+
- os: ubuntu-latest
144+
- os: macos-latest
145+
146+
steps:
147+
- uses: actions/checkout@v2
148+
with:
149+
submodules: recursive
150+
- uses: actions-rs/toolchain@v1
151+
with:
152+
profile: minimal
153+
toolchain: stable
154+
override: true
155+
- name: Run the examples
156+
run: |
157+
cargo clean
158+
bash convert-to-heed3.sh
159+
cargo run --example 2>&1 | grep -E '^ '| xargs -n1 cargo run --example
160+
84161
fmt:
85162
name: Ensure the heed project is formatted
86163
runs-on: ubuntu-latest
@@ -94,3 +171,11 @@ jobs:
94171
components: rustfmt
95172
- name: Run cargo fmt
96173
run: cargo fmt --check
174+
175+
no-heed3-in-heed-folder:
176+
name: Ensure heed3 is not erasing heed
177+
runs-on: ubuntu-latest
178+
steps:
179+
- uses: actions/checkout@v2
180+
- name: Check name is heed with grep
181+
run: grep -q 'name = "heed"' heed/Cargo.toml

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
path = lmdb-master-sys/lmdb
33
url = https://github.com/LMDB/lmdb
44
branch = mdb.master
5+
[submodule "lmdb-master3-sys/lmdb"]
6+
path = lmdb-master3-sys/lmdb
7+
url = https://github.com/LMDB/lmdb
8+
branch = mdb.master3

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[workspace]
2-
members = ["lmdb-master-sys", "heed", "heed-traits", "heed-types"]
2+
members = ["lmdb-master-sys", "lmdb-master3-sys", "heed", "heed-traits", "heed-types"]
33
resolver = "2"

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<p align="center"><img width="280px" src="https://raw.githubusercontent.com/meilisearch/heed/main/assets/heed-pigeon-logo.png"></a></p>
2-
<h1 align="center" >heed</h1>
2+
<h1 align="center" >heed & heed3</h1>
33

44
[![License](https://img.shields.io/badge/license-MIT-green)](#LICENSE)
55
[![Crates.io](https://img.shields.io/crates/v/heed)](https://crates.io/crates/heed)
66
[![Docs](https://docs.rs/heed/badge.svg)](https://docs.rs/heed)
77
[![dependency status](https://deps.rs/repo/github/meilisearch/heed/status.svg)](https://deps.rs/repo/github/meilisearch/heed)
88
[![Build](https://github.com/meilisearch/heed/actions/workflows/rust.yml/badge.svg)](https://github.com/meilisearch/heed/actions/workflows/rust.yml)
99

10-
A Rust-centric [LMDB](https://en.wikipedia.org/wiki/Lightning_Memory-Mapped_Database) abstraction with minimal overhead. This library enables the storage of various Rust types within LMDB, extending support to include Serde-compatible types.
10+
A Rust-centric [LMDB](https://en.wikipedia.org/wiki/Lightning_Memory-Mapped_Database) abstraction with minimal overhead. This library enables the storage of various Rust types within LMDB, extending support to include Serde-compatible types. It not only supports the LMDB `mdb.master` branch but also the `mdb.master3` branch which features encryption-at-rest and checksumming.
1111

1212
## Simple Example Usage
1313

14-
Here is an example on how to store and read entries into LMDB in a safe and ACID way. For usage examples, see [heed/examples/](heed/examples/). To see more advanced usage techniques go check our [Cookbook](https://docs.rs/heed/latest/heed/cookbook/index.html).
14+
Here is an example on how to store and read entries into LMDB in a safe and ACID way. For usage examples, see [examples/](examples/). To see more advanced usage techniques go check our [Cookbook](https://docs.rs/heed/latest/heed/cookbook/index.html).
1515

1616
```rust
1717
use std::fs;
@@ -46,6 +46,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4646
}
4747
```
4848

49+
## Working with two Crates: heed and heed3
50+
51+
The heed and heed3 crates manage a shared codebase. Within the heed3 folder, you can find the Cargo.toml specific to the heed3 crate.
52+
To facilitate work on heed3, utilize the `convert-to-heed3.sh` script.
53+
54+
This script conveniently moves the `heed3/Cargo.toml` file to the `heed/` folder, updates the `heed::` references to `heed3::`, and generates a commit for easy rollback if needed.
55+
4956
## Building from Source
5057

5158
You can use this command to clone the repository:

convert-to-heed3.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env bash
2+
3+
# This script is meant to setup the heed3 crate.
4+
#
5+
6+
if [[ -n $(git status -s) ]]; then
7+
echo "Error: Repository is git dirty, please commit or stash changes before running this script."
8+
exit 1
9+
fi
10+
11+
set -e
12+
13+
# It basically copy the heed3/Cargo.toml file into
14+
# the heed folder...
15+
if [[ "$OSTYPE" == "cygwin" || "$OSTYPE" == "msys" ]]; then
16+
cp heed3\\Cargo.toml heed\\Cargo.toml
17+
else
18+
cp heed3/Cargo.toml heed/Cargo.toml
19+
fi
20+
21+
# ...and replaces the `heed::` string by the `heed3::` one.
22+
for file in $(find heed/src -type f -name "*.rs"); do
23+
if [[ "$OSTYPE" == "darwin"* ]]; then
24+
sed -i '' 's/heed::/heed3::/g' "$file"
25+
else
26+
sed -i 's/heed::/heed3::/g' "$file"
27+
fi
28+
done
29+
30+
# Make it easier to rollback by doing a commit
31+
git config --global user.email "[email protected]"
32+
git config --global user.name "The CI"
33+
git commit -am 'remove-me: heed3 changes generate by the convert-to-heed3.sh script'
34+
35+
echo "Heed3 crate setup completed successfully. Configurations for the heed crate have been copied and modified."
36+
echo "A commit (starting with remove-me) has been generated and must be deleted before merging into the main branch."
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

examples/heed3-all-types.rs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
use std::error::Error;
2+
use std::fs;
3+
use std::path::Path;
4+
5+
use heed3::byteorder::BE;
6+
use heed3::types::*;
7+
use heed3::{Database, EnvOpenOptions};
8+
use serde::{Deserialize, Serialize};
9+
10+
fn main() -> Result<(), Box<dyn Error>> {
11+
let path = Path::new("target").join("heed3.mdb");
12+
13+
fs::create_dir_all(&path)?;
14+
15+
let env = unsafe {
16+
EnvOpenOptions::new()
17+
.map_size(10 * 1024 * 1024) // 10MB
18+
.max_dbs(3000)
19+
.open(path)?
20+
};
21+
22+
// here the key will be an str and the data will be a slice of u8
23+
let mut wtxn = env.write_txn()?;
24+
let db: Database<Str, Bytes> = env.create_database(&mut wtxn, Some("kiki"))?;
25+
26+
db.put(&mut wtxn, "hello", &[2, 3][..])?;
27+
let ret: Option<&[u8]> = db.get(&wtxn, "hello")?;
28+
29+
println!("{:?}", ret);
30+
wtxn.commit()?;
31+
32+
// serde types are also supported!!!
33+
#[derive(Debug, Serialize, Deserialize)]
34+
struct Hello<'a> {
35+
string: &'a str,
36+
}
37+
38+
let mut wtxn = env.write_txn()?;
39+
let db: Database<Str, SerdeBincode<Hello>> =
40+
env.create_database(&mut wtxn, Some("serde-bincode"))?;
41+
42+
let hello = Hello { string: "hi" };
43+
db.put(&mut wtxn, "hello", &hello)?;
44+
45+
let ret: Option<Hello> = db.get(&wtxn, "hello")?;
46+
println!("serde-bincode:\t{:?}", ret);
47+
48+
wtxn.commit()?;
49+
50+
let mut wtxn = env.write_txn()?;
51+
let db: Database<Str, SerdeJson<Hello>> = env.create_database(&mut wtxn, Some("serde-json"))?;
52+
53+
let hello = Hello { string: "hi" };
54+
db.put(&mut wtxn, "hello", &hello)?;
55+
56+
let ret: Option<Hello> = db.get(&wtxn, "hello")?;
57+
println!("serde-json:\t{:?}", ret);
58+
59+
wtxn.commit()?;
60+
61+
// you can ignore the data
62+
let mut wtxn = env.write_txn()?;
63+
let db: Database<Str, Unit> = env.create_database(&mut wtxn, Some("ignored-data"))?;
64+
65+
db.put(&mut wtxn, "hello", &())?;
66+
let ret: Option<()> = db.get(&wtxn, "hello")?;
67+
68+
println!("{:?}", ret);
69+
70+
let ret: Option<()> = db.get(&wtxn, "non-existant")?;
71+
72+
println!("{:?}", ret);
73+
wtxn.commit()?;
74+
75+
// database opening and types are tested in a safe way
76+
//
77+
// we try to open a database twice with the same types
78+
let mut wtxn = env.write_txn()?;
79+
let _db: Database<Str, Unit> = env.create_database(&mut wtxn, Some("ignored-data"))?;
80+
81+
// you can iterate over keys in order
82+
type BEI64 = I64<BE>;
83+
84+
let db: Database<BEI64, Unit> = env.create_database(&mut wtxn, Some("big-endian-iter"))?;
85+
86+
db.put(&mut wtxn, &0, &())?;
87+
db.put(&mut wtxn, &68, &())?;
88+
db.put(&mut wtxn, &35, &())?;
89+
db.put(&mut wtxn, &42, &())?;
90+
91+
let rets: Result<Vec<(i64, _)>, _> = db.iter(&wtxn)?.collect();
92+
93+
println!("{:?}", rets);
94+
95+
// or iterate over ranges too!!!
96+
let range = 35..=42;
97+
let rets: Result<Vec<(i64, _)>, _> = db.range(&wtxn, &range)?.collect();
98+
99+
println!("{:?}", rets);
100+
101+
// delete a range of key
102+
let range = 35..=42;
103+
let deleted: usize = db.delete_range(&mut wtxn, &range)?;
104+
105+
let rets: Result<Vec<(i64, _)>, _> = db.iter(&wtxn)?.collect();
106+
107+
println!("deleted: {:?}, {:?}", deleted, rets);
108+
wtxn.commit()?;
109+
110+
Ok(())
111+
}

0 commit comments

Comments
 (0)