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.
34
4- # Example: use the Writer and Reader structs
5+ # Example: Use the ` Writer ` and ` Reader ` structs
56
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.
911
1012``` rust
1113use std :: io :: Cursor ;
@@ -14,24 +16,34 @@ use grenad::{Reader, Writer};
1416
1517# fn main () -> Result <(), Box <dyn std :: error :: Error >> {
1618let mut writer = Writer :: memory ();
19+
20+ // We insert our key-value pairs in lexicographic order.
1721writer . insert (" first-counter" , 119_u32 . to_ne_bytes ())? ;
1822writer . insert (" second-counter" , 384_u32 . to_ne_bytes ())? ;
1923
2024// We create a reader from our writer.
2125let 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 ()? ;
2327
2428// 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 );
2837# Ok (()) }
2938```
3039
31- # Example: use the Merger struct
40+ # Example: Use the ` Merger ` struct
3241
3342In 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 ` ] .
3547
3648``` rust
3749use std :: array :: TryFromSliceError ;
@@ -64,8 +76,8 @@ let mut writera = Writer::memory();
6476let mut writerb = Writer :: memory ();
6577let mut writerc = Writer :: memory ();
6678
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.
6981writera . insert (" first-counter" , 32_u32 . to_ne_bytes ())? ;
7082writera . insert (" second-counter" , 64_u32 . to_ne_bytes ())? ;
7183writerb . insert (" first-counter" , 23_u32 . to_ne_bytes ())? ;
@@ -95,11 +107,14 @@ assert_eq!(iter.next()?, None);
95107# Ok (()) }
96108```
97109
98- # Example: use the Sorter struct
110+ # Example: Use the ` Sorter ` struct
99111
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.
103118
104119``` rust
105120use std :: array :: TryFromSliceError ;
@@ -145,6 +160,5 @@ let mut iter = sorter.into_stream_merger_iter()?;
145160assert_eq! (iter . next ()? , Some ((& b " first-counter" [.. ], & 119_u32 . to_ne_bytes ()[.. ])));
146161assert_eq! (iter . next ()? , Some ((& b " second-counter" [.. ], & 384_u32 . to_ne_bytes ()[.. ])));
147162assert_eq! (iter . next ()? , None );
148-
149163# Ok (()) }
150164```
0 commit comments