11// SPDX-License-Identifier: Apache-2.0
22
3- use std:: collections:: BTreeMap ;
3+ use std:: collections:: HashMap ;
44use std:: fs:: File ;
55use std:: hash:: Hash ;
66use std:: io:: { self , Read , Write } ;
7+ use std:: marker:: PhantomData ;
78use std:: path:: PathBuf ;
89
910use indexmap:: IndexSet ;
1011
1112use crate :: AoraIndex ;
1213
1314// For now, this is just an in-memory read BTree. In the next releases we need to change this.
14- #[ derive( Clone , Debug ) ]
15+ #[ derive( Debug ) ]
1516pub struct FileAoraIndex < K , V , const KEY_LEN : usize = 32 , const VAL_LEN : usize = 32 >
1617where
17- K : Ord + From < [ u8 ; KEY_LEN ] > + Into < [ u8 ; KEY_LEN ] > ,
18- V : Eq + From < [ u8 ; VAL_LEN ] > + Into < [ u8 ; VAL_LEN ] > ,
18+ K : From < [ u8 ; KEY_LEN ] > + Into < [ u8 ; KEY_LEN ] > ,
19+ V : From < [ u8 ; VAL_LEN ] > + Into < [ u8 ; VAL_LEN ] > ,
1920{
2021 path : PathBuf ,
21- cache : BTreeMap < K , IndexSet < V > > ,
22+ cache : HashMap < [ u8 ; KEY_LEN ] , IndexSet < [ u8 ; VAL_LEN ] > > ,
23+ _phantom : PhantomData < ( K , V ) > ,
2224}
2325
2426impl < K , V , const KEY_LEN : usize , const VAL_LEN : usize > FileAoraIndex < K , V , KEY_LEN , VAL_LEN >
2527where
26- K : Ord + From < [ u8 ; KEY_LEN ] > + Into < [ u8 ; KEY_LEN ] > ,
27- V : Eq + From < [ u8 ; VAL_LEN ] > + Into < [ u8 ; VAL_LEN ] > ,
28+ K : From < [ u8 ; KEY_LEN ] > + Into < [ u8 ; KEY_LEN ] > ,
29+ V : From < [ u8 ; VAL_LEN ] > + Into < [ u8 ; VAL_LEN ] > ,
2830{
2931 pub fn create ( path : PathBuf ) -> io:: Result < Self > {
3032 File :: create_new ( & path) ?;
31- Ok ( Self { cache : BTreeMap :: new ( ) , path } )
33+ Ok ( Self { cache : HashMap :: new ( ) , path, _phantom : PhantomData } )
3234 }
3335
3436 pub fn open ( path : PathBuf ) -> io:: Result < Self >
3537 where V : Hash {
36- let mut cache = BTreeMap :: new ( ) ;
38+ let mut cache = HashMap :: new ( ) ;
3739 let mut file = File :: open ( & path) ?;
3840 let mut key_buf = [ 0u8 ; KEY_LEN ] ;
3941 let mut val_buf = [ 0u8 ; VAL_LEN ] ;
4042 while file. read_exact ( & mut key_buf) . is_ok ( ) {
41- let key = K :: from ( key_buf) ;
4243 let mut values = IndexSet :: new ( ) ;
4344 let mut len = [ 0u8 ; 4 ] ;
4445 file. read_exact ( & mut len) . expect ( "cannot read index file" ) ;
4546 let mut len = u32:: from_le_bytes ( len) ;
4647 while len > 0 {
4748 file. read_exact ( & mut val_buf)
4849 . expect ( "cannot read index file" ) ;
49- let res = values. insert ( val_buf. into ( ) ) ;
50+ let res = values. insert ( val_buf) ;
5051 debug_assert ! ( res, "duplicate id in index file" ) ;
5152 len -= 1 ;
5253 }
53- cache. insert ( key , values) ;
54+ cache. insert ( key_buf , values) ;
5455 }
55- Ok ( Self { path, cache } )
56+ Ok ( Self { path, cache, _phantom : PhantomData } )
5657 }
5758
58- pub fn save ( & self ) -> io:: Result < ( ) >
59- where
60- K : Copy ,
61- V : Copy ,
62- {
59+ pub fn save ( & self ) -> io:: Result < ( ) > {
6360 let mut index_file = File :: create ( & self . path ) ?;
6461 for ( key, values) in & self . cache {
65- index_file. write_all ( & ( * key) . into ( ) ) ?;
62+ index_file. write_all ( key) ?;
6663 let len = values. len ( ) as u32 ;
6764 index_file. write_all ( & len. to_le_bytes ( ) ) ?;
6865 for value in values {
69- index_file. write_all ( & ( * value) . into ( ) ) ?;
66+ index_file. write_all ( value) ?;
7067 }
7168 }
7269 Ok ( ( ) )
@@ -76,24 +73,29 @@ where
7673impl < K , V , const KEY_LEN : usize , const VAL_LEN : usize > AoraIndex < K , V , KEY_LEN , VAL_LEN >
7774 for FileAoraIndex < K , V , KEY_LEN , VAL_LEN >
7875where
79- K : Copy + Ord + From < [ u8 ; KEY_LEN ] > + Into < [ u8 ; KEY_LEN ] > ,
80- V : Copy + Eq + Hash + From < [ u8 ; VAL_LEN ] > + Into < [ u8 ; VAL_LEN ] > ,
76+ K : From < [ u8 ; KEY_LEN ] > + Into < [ u8 ; KEY_LEN ] > ,
77+ V : From < [ u8 ; VAL_LEN ] > + Into < [ u8 ; VAL_LEN ] > ,
8178{
82- fn keys ( & self ) -> impl Iterator < Item = K > { self . cache . keys ( ) . copied ( ) }
79+ fn keys ( & self ) -> impl Iterator < Item = K > { self . cache . keys ( ) . copied ( ) . map ( K :: from ) }
8380
84- fn contains_key ( & self , key : & K ) -> bool { self . cache . contains_key ( key) }
81+ fn contains_key ( & self , key : K ) -> bool { self . cache . contains_key ( & key. into ( ) ) }
8582
86- fn value_len ( & self , key : & K ) -> usize { self . cache . get ( key) . map ( |ids| ids. len ( ) ) . unwrap_or ( 0 ) }
83+ fn value_len ( & self , key : K ) -> usize {
84+ self . cache
85+ . get ( & key. into ( ) )
86+ . map ( |ids| ids. len ( ) )
87+ . unwrap_or ( 0 )
88+ }
8789
88- fn get ( & self , key : & K ) -> impl ExactSizeIterator < Item = V > {
89- match self . cache . get ( key) {
90- Some ( ids) => ids. clone ( ) . into_iter ( ) ,
91- None => IndexSet :: new ( ) . into_iter ( ) ,
90+ fn get ( & self , key : K ) -> impl ExactSizeIterator < Item = V > {
91+ match self . cache . get ( & key. into ( ) ) {
92+ Some ( ids) => ids. clone ( ) . into_iter ( ) . map ( V :: from ) ,
93+ None => IndexSet :: new ( ) . into_iter ( ) . map ( V :: from ) ,
9294 }
9395 }
9496
9597 fn push ( & mut self , key : K , val : V ) {
96- self . cache . entry ( key) . or_default ( ) . insert ( val) ;
98+ self . cache . entry ( key. into ( ) ) . or_default ( ) . insert ( val. into ( ) ) ;
9799 self . save ( ) . expect ( "Cannot save index file" ) ;
98100 }
99101}
0 commit comments