1
- This library provides different ways to sort, merge, write,
2
- and read key-value pairs efficiently.
1
+ This library provides tools to sort, merge, write, and read immutable key-value pairs.
2
+ The entries in the grenad files are _ immutable_ and the only way to modify them is by _ creating
3
+ a new file_ with the changes.
3
4
4
- # Example: use the Writer and Reader structs
5
+ # Example: Use the ` Writer ` and ` Reader ` structs
5
6
6
- You can use the [ ` Writer ` ] struct to store key-value pairs
7
- into the specified [ ` std::io::Write ` ] type. The [ ` Reader ` ] type
8
- can then be used to read the entries.
7
+ You can use the [ ` Writer ` ] struct to store key-value pairs into the specified
8
+ [ ` std::io::Write ` ] type. The [ ` Reader ` ] type can then be used to read the entries.
9
+
10
+ The entries provided to the [ ` Writer ` ] struct must be given in lexicographic order.
9
11
10
12
``` rust
11
13
use std :: io :: Cursor ;
@@ -14,24 +16,34 @@ use grenad::{Reader, Writer};
14
16
15
17
# fn main () -> Result <(), Box <dyn std :: error :: Error >> {
16
18
let mut writer = Writer :: memory ();
19
+
20
+ // We insert our key-value pairs in lexicographic order.
17
21
writer . insert (" first-counter" , 119_u32 . to_ne_bytes ())? ;
18
22
writer . insert (" second-counter" , 384_u32 . to_ne_bytes ())? ;
19
23
20
24
// We create a reader from our writer.
21
25
let cursor = writer . into_inner (). map (Cursor :: new )? ;
22
- let mut reader = Reader :: new (cursor )? . into_cursor ()? ;
26
+ let mut cursor = Reader :: new (cursor )? . into_cursor ()? ;
23
27
24
28
// We can see that the sum of u32s is valid here.
25
- assert_eq! (reader . move_on_next ()? , Some ((& b " first-counter" [.. ], & 119_u32 . to_ne_bytes ()[.. ])));
26
- assert_eq! (reader . move_on_next ()? , Some ((& b " second-counter" [.. ], & 384_u32 . to_ne_bytes ()[.. ])));
27
- assert_eq! (reader . move_on_next ()? , None );
29
+ assert_eq! (cursor . move_on_next ()? , Some ((& b " first-counter" [.. ], & 119_u32 . to_ne_bytes ()[.. ])));
30
+ assert_eq! (cursor . move_on_next ()? , Some ((& b " second-counter" [.. ], & 384_u32 . to_ne_bytes ()[.. ])));
31
+ assert_eq! (cursor . move_on_next ()? , None );
32
+
33
+ // We can also jum on any given entry.
34
+ assert_eq! (cursor . move_on_key_greater_than_or_equal_to (" first" )? , Some ((& b " first-counter" [.. ], & 119_u32 . to_ne_bytes ()[.. ])));
35
+ assert_eq! (cursor . move_on_key_equal_to (" second-counter" )? , Some ((& b " second-counter" [.. ], & 384_u32 . to_ne_bytes ()[.. ])));
36
+ assert_eq! (cursor . move_on_key_lower_than_or_equal_to (" abracadabra" )? , None );
28
37
# Ok (()) }
29
38
```
30
39
31
- # Example: use the Merger struct
40
+ # Example: Use the ` Merger ` struct
32
41
33
42
In this example we show how you can merge multiple [ ` Reader ` ] s
34
- by using a merge function when a conflict is encountered.
43
+ by using a _ merge function_ when a conflict is encountered.
44
+
45
+ The entries yielded by the [ ` Merger ` ] struct are returned in lexicographic order,
46
+ a good way to write them back into a new [ ` Writer ` ] .
35
47
36
48
``` rust
37
49
use std :: array :: TryFromSliceError ;
@@ -64,8 +76,8 @@ let mut writera = Writer::memory();
64
76
let mut writerb = Writer :: memory ();
65
77
let mut writerc = Writer :: memory ();
66
78
67
- // We insert our key-value pairs in order and
68
- // mix them between our writers.
79
+ // We insert our key-value pairs in lexicographic order
80
+ // and mix them between our writers.
69
81
writera . insert (" first-counter" , 32_u32 . to_ne_bytes ())? ;
70
82
writera . insert (" second-counter" , 64_u32 . to_ne_bytes ())? ;
71
83
writerb . insert (" first-counter" , 23_u32 . to_ne_bytes ())? ;
@@ -95,11 +107,14 @@ assert_eq!(iter.next()?, None);
95
107
# Ok (()) }
96
108
```
97
109
98
- # Example: use the Sorter struct
110
+ # Example: Use the ` Sorter ` struct
99
111
100
- In this example we show how by defining a merge function,
101
- you can insert multiple entries with the same key and output
102
- them in key-order.
112
+ In this example we show how by defining a _ merge function_ , we can insert
113
+ multiple entries with the same key and output them in lexicographic order.
114
+
115
+ The [ ` Sorter ` ] accepts the entries in any given order, will reorder them in-memory and
116
+ merge them with the _ merge function_ when required. It is authorized to have a memory budget
117
+ during its construction and will try to follow it as closely as possible.
103
118
104
119
``` rust
105
120
use std :: array :: TryFromSliceError ;
@@ -145,6 +160,5 @@ let mut iter = sorter.into_stream_merger_iter()?;
145
160
assert_eq! (iter . next ()? , Some ((& b " first-counter" [.. ], & 119_u32 . to_ne_bytes ()[.. ])));
146
161
assert_eq! (iter . next ()? , Some ((& b " second-counter" [.. ], & 384_u32 . to_ne_bytes ()[.. ])));
147
162
assert_eq! (iter . next ()? , None );
148
-
149
163
# Ok (()) }
150
164
```
0 commit comments