-
-
Notifications
You must be signed in to change notification settings - Fork 578
/
fst.rs
1223 lines (1010 loc) · 43.7 KB
/
fst.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Sonic
//
// Fast, lightweight and schema-less search backend
// Copyright: 2019, Valerian Saliou <valerian@valeriansaliou.name>
// License: Mozilla Public License v2.0 (MPL v2.0)
use fst::set::Stream as FSTStream;
use fst::{
Automaton, Error as FSTError, IntoStreamer, Set as FSTSet, SetBuilder as FSTSetBuilder,
Streamer,
};
use fst_levenshtein::Levenshtein;
use fst_regex::Regex;
use hashbrown::{HashMap, HashSet};
use radix::RadixNum;
use regex_syntax::escape as regex_escape;
use std::collections::VecDeque;
use std::fmt;
use std::fs::{self, File};
use std::io::{self, BufRead, BufReader, BufWriter, Write};
use std::iter::FromIterator;
use std::path::{Path, PathBuf};
use std::str;
use std::sync::{Arc, Mutex, RwLock};
use std::thread;
use std::time::SystemTime;
use super::generic::{
StoreGeneric, StoreGenericActionBuilder, StoreGenericBuilder, StoreGenericPool,
};
use super::keyer::StoreKeyerHasher;
use crate::lexer::ranges::LexerRegexRange;
use crate::APP_CONF;
pub struct StoreFSTPool;
pub struct StoreFSTBuilder;
pub struct StoreFST {
graph: FSTSet,
target: StoreFSTKey,
pending: StoreFSTPending,
last_used: Arc<RwLock<SystemTime>>,
last_consolidated: Arc<RwLock<SystemTime>>,
}
#[derive(Default)]
pub struct StoreFSTPending {
pop: Arc<RwLock<HashSet<Vec<u8>>>>,
push: Arc<RwLock<HashSet<Vec<u8>>>>,
}
pub struct StoreFSTActionBuilder;
pub struct StoreFSTAction {
store: StoreFSTBox,
}
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
pub struct StoreFSTKey {
collection_hash: StoreFSTAtom,
bucket_hash: StoreFSTAtom,
}
pub struct StoreFSTMisc;
#[derive(Copy, Clone)]
enum StoreFSTPathMode {
Permanent,
Temporary,
Backup,
}
type StoreFSTAtom = u32;
type StoreFSTBox = Arc<StoreFST>;
const WORD_LIMIT_LENGTH: usize = 40;
const ATOM_HASH_RADIX: usize = 16;
lazy_static! {
pub static ref GRAPH_ACCESS_LOCK: Arc<RwLock<bool>> = Arc::new(RwLock::new(false));
static ref GRAPH_ACQUIRE_LOCK: Arc<Mutex<bool>> = Arc::new(Mutex::new(false));
static ref GRAPH_REBUILD_LOCK: Arc<Mutex<bool>> = Arc::new(Mutex::new(false));
static ref GRAPH_POOL: Arc<RwLock<HashMap<StoreFSTKey, StoreFSTBox>>> =
Arc::new(RwLock::new(HashMap::new()));
static ref GRAPH_CONSOLIDATE: Arc<RwLock<HashSet<StoreFSTKey>>> =
Arc::new(RwLock::new(HashSet::new()));
}
impl StoreFSTPathMode {
fn extension(&self) -> &'static str {
match self {
StoreFSTPathMode::Permanent => ".fst",
StoreFSTPathMode::Temporary => ".fst.tmp",
StoreFSTPathMode::Backup => ".fst.bck",
}
}
}
impl StoreFSTPool {
pub fn count() -> (usize, usize) {
(
GRAPH_POOL.read().unwrap().len(),
GRAPH_CONSOLIDATE.read().unwrap().len(),
)
}
pub fn acquire<'a, T: Into<&'a str>>(collection: T, bucket: T) -> Result<StoreFSTBox, ()> {
let (collection_str, bucket_str) = (collection.into(), bucket.into());
let pool_key = StoreFSTKey::from_str(collection_str, bucket_str);
// Freeze acquire lock, and reference it in context
// Notice: this prevents two graphs on the same collection to be opened at the same time.
let _acquire = GRAPH_ACQUIRE_LOCK.lock().unwrap();
// Acquire a thread-safe store pool reference in read mode
let graph_pool_read = GRAPH_POOL.read().unwrap();
if let Some(store_fst) = graph_pool_read.get(&pool_key) {
Self::proceed_acquire_cache("fst", collection_str, pool_key, store_fst)
} else {
info!(
"fst store not in pool for collection: {} <{:x?}> / bucket: {} <{:x?}>, opening it",
collection_str, pool_key.collection_hash, bucket_str, pool_key.bucket_hash
);
// Important: we need to drop the read reference first, to avoid dead-locking \
// when acquiring the RWLock in write mode in this block.
drop(graph_pool_read);
Self::proceed_acquire_open("fst", collection_str, pool_key, &*GRAPH_POOL)
}
}
pub fn janitor() {
Self::proceed_janitor(
"fst",
&*GRAPH_POOL,
APP_CONF.store.fst.pool.inactive_after,
&*GRAPH_ACCESS_LOCK,
)
}
pub fn backup(path: &Path) -> Result<(), io::Error> {
debug!("backing up all fst stores to path: {:?}", path);
// Create backup directory (full path)
fs::create_dir_all(path)?;
// Proceed dump action (backup)
Self::dump_action(
"backup",
StoreFSTPathMode::Permanent,
&*APP_CONF.store.fst.path,
path,
&Self::backup_item,
)
}
pub fn restore(path: &Path) -> Result<(), io::Error> {
debug!("restoring all fst stores from path: {:?}", path);
// Proceed dump action (restore)
Self::dump_action(
"restore",
StoreFSTPathMode::Backup,
path,
&*APP_CONF.store.fst.path,
&Self::restore_item,
)
}
pub fn consolidate(force: bool) {
debug!("scanning for fst store pool items to consolidate");
// Notice: we do not consolidate all items at each tick, we try to even out multiple \
// consolidation tasks over time. This lowers the overall HZ of the tasker system for \
// certain heavy tasks, which is better to spread out consolidation steps over time over \
// a large number of very active buckets.
// Acquire rebuild lock, and reference it in context
// Notice: this prevents two consolidate operations to be executed at the same time.
let _rebuild = GRAPH_REBUILD_LOCK.lock().unwrap();
// Exit trap: Register is empty? Abort there.
if GRAPH_CONSOLIDATE.read().unwrap().is_empty() {
info!("no fst store pool items to consolidate in register");
return;
}
// Step 1: List keys to be consolidated
let mut keys_consolidate: Vec<StoreFSTKey> = Vec::new();
{
// Acquire access lock (in blocking write mode), and reference it in context
// Notice: this prevents store to be acquired from any context
let _access = GRAPH_ACCESS_LOCK.write().unwrap();
let graph_consolidate_read = GRAPH_CONSOLIDATE.read().unwrap();
for key in &*graph_consolidate_read {
if let Some(store) = GRAPH_POOL.read().unwrap().get(&key) {
let not_consolidated_for = store
.last_consolidated
.read()
.unwrap()
.elapsed()
.unwrap()
.as_secs();
if force || not_consolidated_for >= APP_CONF.store.fst.graph.consolidate_after {
info!(
"fst key: {} not consolidated for: {} seconds, may consolidate",
key, not_consolidated_for
);
keys_consolidate.push(*key);
} else {
debug!(
"fst key: {} not consolidated for: {} seconds, no consolidate",
key, not_consolidated_for
);
}
}
}
}
// Exit trap: Nothing to consolidate yet? Abort there.
if keys_consolidate.is_empty() {
info!("no fst store pool items need to consolidate at the moment");
return;
}
// Step 2: Clear keys to be consolidated from register
{
// Acquire access lock (in blocking write mode), and reference it in context
// Notice: this prevents store to be acquired from any context
let _access = GRAPH_ACCESS_LOCK.write().unwrap();
let mut graph_consolidate_write = GRAPH_CONSOLIDATE.write().unwrap();
for key in &keys_consolidate {
graph_consolidate_write.remove(key);
debug!("fst key: {} cleared from consolidate register", key);
}
}
// Step 3: Consolidate FSTs, one-by-one (sequential locking; this avoids global locks)
let (mut count_moved, mut count_pushed, mut count_popped) = (0, 0, 0);
{
for key in &keys_consolidate {
{
// As we may be renaming the FST file, ensure no consumer out of this is \
// trying to access the FST file as it gets processed. This also waits for \
// current consumers to finish reading the FST, and prevents any new \
// consumer from opening it while we are not done there.
let _access = GRAPH_ACCESS_LOCK.write().unwrap();
let do_close = if let Some(store) = GRAPH_POOL.read().unwrap().get(key) {
debug!("fst key: {} consolidate started", key);
let consolidate_counts = Self::consolidate_item(store);
count_moved += consolidate_counts.1;
count_pushed += consolidate_counts.2;
count_popped += consolidate_counts.3;
debug!("fst key: {} consolidate complete", key);
// Should close this FST?
consolidate_counts.0
} else {
false
};
// Nuke old opened FST?
// Notice: last consolidated date will be bumped to a new date in the future \
// when a push or pop operation will be done, thus effectively scheduling \
// a consolidation in the future properly.
// Notice: we remove this one early as to release write lock early
if do_close {
GRAPH_POOL.write().unwrap().remove(key);
}
}
// Give a bit of time to other threads before continuing (a consolidate operation \
// must not block all other threads until it completes); this method tells the \
// thread scheduler to give a bit of priority to other threads, and get back \
// to this thread's work when other threads are done. On large setups, this \
// loop can starve other threads due to the locks used (unfortunately they \
// are all necessary).
thread::yield_now();
}
}
info!(
"done scanning for fst store pool items to consolidate (move: {}, push: {}, pop: {})",
count_moved, count_pushed, count_popped
);
}
fn dump_action(
action: &str,
path_mode: StoreFSTPathMode,
read_path: &Path,
write_path: &Path,
fn_item: &Fn(&Path, &Path, &str, &str) -> Result<(), io::Error>,
) -> Result<(), io::Error> {
let fst_extension = path_mode.extension();
let fst_extension_len = fst_extension.len();
// Iterate on FST collections
for collection in fs::read_dir(read_path)? {
let collection = collection?;
// Actual collection found?
match (collection.file_type(), collection.file_name().to_str()) {
(Ok(collection_file_type), Some(collection_name)) => {
if collection_file_type.is_dir() {
debug!("fst collection ongoing {}: {}", action, collection_name);
// Create write folder for collection
fs::create_dir_all(write_path.join(collection_name))?;
// Iterate on FST collection buckets
for bucket in fs::read_dir(read_path.join(collection_name))? {
let bucket = bucket?;
// Actual bucket found?
match (bucket.file_type(), bucket.file_name().to_str()) {
(Ok(bucket_file_type), Some(bucket_file_name)) => {
let bucket_file_name_len = bucket_file_name.len();
if bucket_file_type.is_file()
&& bucket_file_name_len > fst_extension_len
&& bucket_file_name.ends_with(fst_extension)
{
// Acquire bucket name (from full file name)
let bucket_name = &bucket_file_name
[..(bucket_file_name_len - fst_extension_len)];
debug!(
"fst bucket ongoing {}: {}/{}",
action, collection_name, bucket_name
);
fn_item(
write_path,
&bucket.path(),
collection_name,
bucket_name,
)?;
}
}
_ => {}
}
}
}
}
_ => {}
}
}
Ok(())
}
fn backup_item(
backup_path: &Path,
_origin_path: &Path,
collection_name: &str,
bucket_name: &str,
) -> Result<(), io::Error> {
// Acquire access lock (in blocking write mode), and reference it in context
// Notice: this prevents store to be acquired from any context
let _access = GRAPH_ACCESS_LOCK.write().unwrap();
// Generate path to FST backup
let fst_backup_path = backup_path.join(collection_name).join(format!(
"{}{}",
bucket_name,
StoreFSTPathMode::Backup.extension()
));
debug!(
"fst bucket: {}/{} backing up to path: {:?}",
collection_name, bucket_name, fst_backup_path
);
// Erase any previously-existing FST backup
fs::remove_file(&fst_backup_path).ok();
// Stream actual FST data to FST backup
let backup_fst_file = File::create(&fst_backup_path)?;
let mut backup_fst_writer = BufWriter::new(backup_fst_file);
let mut count_words = 0;
// Convert names to hashes (as names are hashes encoded as base-16 strings, but we need \
// them as proper integers)
if let (Ok(collection_radix), Ok(bucket_radix)) = (
RadixNum::from_str(collection_name, ATOM_HASH_RADIX),
RadixNum::from_str(bucket_name, ATOM_HASH_RADIX),
) {
if let (Ok(collection_hash), Ok(bucket_hash)) =
(collection_radix.as_decimal(), bucket_radix.as_decimal())
{
let origin_fst = StoreFSTBuilder::open(
collection_hash as StoreFSTAtom,
bucket_hash as StoreFSTAtom,
)
.or(io_error!("graph open failure"))?;
let mut origin_fst_stream = origin_fst.stream();
while let Some(word) = origin_fst_stream.next() {
count_words += 1;
// Write word, and append a new line
backup_fst_writer.write(word)?;
backup_fst_writer.write("\n".as_bytes())?;
}
info!(
"fst bucket: {}/{} backed up to path: {:?} ({} words)",
collection_name, bucket_name, fst_backup_path, count_words
);
}
}
Ok(())
}
fn restore_item(
_backup_path: &Path,
origin_path: &Path,
collection_name: &str,
bucket_name: &str,
) -> Result<(), io::Error> {
// Acquire access lock (in blocking write mode), and reference it in context
// Notice: this prevents store to be acquired from any context
let _access = GRAPH_ACCESS_LOCK.write().unwrap();
debug!(
"fst bucket: {}/{} restoring from path: {:?}",
collection_name, bucket_name, origin_path
);
// Convert names to hashes (as names are hashes encoded as base-16 strings, but we need \
// them as proper integers)
if let (Ok(collection_radix), Ok(bucket_radix)) = (
RadixNum::from_str(collection_name, ATOM_HASH_RADIX),
RadixNum::from_str(bucket_name, ATOM_HASH_RADIX),
) {
if let (Ok(collection_hash), Ok(bucket_hash)) =
(collection_radix.as_decimal(), bucket_radix.as_decimal())
{
// Force a FST store close
StoreFSTBuilder::close(
collection_hash as StoreFSTAtom,
bucket_hash as StoreFSTAtom,
);
// Generate path to FST
let fst_path = StoreFSTBuilder::path(
StoreFSTPathMode::Permanent,
collection_hash as StoreFSTAtom,
Some(bucket_hash as StoreFSTAtom),
);
// Remove existing FST data?
if fst_path.exists() {
fs::remove_file(&fst_path)?;
}
// Stream backup words to restored FST
let fst_writer = BufWriter::new(File::create(&fst_path)?);
let fst_backup_reader = BufReader::new(File::open(&origin_path)?);
let mut fst_builder = FSTSetBuilder::new(fst_writer)
.or(io_error!("graph restore builder failure"))?;
for word in fst_backup_reader.lines() {
let word = word?;
fst_builder
.insert(word)
.or(io_error!("graph restore word insert failure"))?;
}
fst_builder
.finish()
.or(io_error!("graph restore finish failure"))?;
info!(
"fst bucket: {}/{} restored to path: {:?} from backup: {:?}",
collection_name, bucket_name, fst_path, origin_path
);
}
}
Ok(())
}
fn consolidate_item(store: &StoreFSTBox) -> (bool, usize, usize, usize) {
let (mut should_close, mut count_moved, mut count_pushed, mut count_popped) =
(false, 0, 0, 0);
// Acquire write references to pending sets
let (mut pending_push_write, mut pending_pop_write) = (
store.pending.push.write().unwrap(),
store.pending.pop.write().unwrap(),
);
// Do consolidate? (any change commited)
// Notice: if both pending sets are empty do not consolidate as there may have been a \
// push then a pop of this push, nulling out any commited change.
if pending_push_write.len() > 0 || pending_pop_write.len() > 0 {
// Read old FST (or default to empty FST)
if let Ok(old_fst) =
StoreFSTBuilder::open(store.target.collection_hash, store.target.bucket_hash)
{
// Initialize the new FST (temporary)
let bucket_tmp_path = StoreFSTBuilder::path(
StoreFSTPathMode::Temporary,
store.target.collection_hash,
Some(store.target.bucket_hash),
);
let bucket_tmp_path_parent = bucket_tmp_path.parent().unwrap();
if fs::create_dir_all(&bucket_tmp_path_parent).is_ok() {
// Erase any previously-existing temporary FST (eg. process stopped while \
// writing the temporary FST); there is no guarantee this succeeds.
fs::remove_file(&bucket_tmp_path).ok();
if let Ok(tmp_fst_file) = File::create(&bucket_tmp_path) {
let tmp_fst_writer = BufWriter::new(tmp_fst_file);
// Create a builder that can be used to insert new key-value pairs.
if let Ok(mut tmp_fst_builder) = FSTSetBuilder::new(tmp_fst_writer) {
// Convert push keys to an ordered vector
// Notice: we must go from a Vec to a VecDeque as to sort values, \
// which is a requirement for FST insertions.
let mut ordered_push_vec: Vec<&[u8]> =
Vec::from_iter(pending_push_write.iter().map(|item| item.as_ref()));
ordered_push_vec.sort();
let mut ordered_push: VecDeque<&[u8]> =
VecDeque::from_iter(ordered_push_vec);
// Append words not in pop list to new FST (ie. old words minus pop \
// words)
let mut old_fst_stream = old_fst.stream();
while let Some(old_fst_word) = old_fst_stream.next() {
// Append new words from front? (ie. push words)
// Notice: as an FST is ordered, inserts would fail if they are \
// commited out-of-order. Thus, the only way to check for \
// order is there.
while let Some(push_front_ref) = ordered_push.front() {
if *push_front_ref <= old_fst_word {
// Pop front item and consume it
if let Some(push_front) = ordered_push.pop_front() {
if let Err(err) = tmp_fst_builder.insert(push_front) {
error!(
"failed inserting new word from old in fst: {}",
err
);
}
count_pushed += 1;
// Continue scanning next word (may also come before \
// this FST word in order)
continue;
}
}
// Important: stop loop on next front item (always the same)
break;
}
// Restore old word (if not popped)
if !pending_pop_write.contains(old_fst_word) {
if let Err(err) = tmp_fst_builder.insert(old_fst_word) {
error!("failed inserting old word in fst: {}", err);
}
count_moved += 1;
} else {
count_popped += 1;
}
}
// Complete FST with last pushed items
// Notice: this is necessary if the FST was empty, or if we have push \
// items that come after the last ordered word of the FST.
while let Some(push_front) = ordered_push.pop_front() {
if let Err(err) = tmp_fst_builder.insert(push_front) {
error!(
"failed inserting new word from complete in fst: {}",
err
);
}
count_pushed += 1;
}
// Finish building new FST
if tmp_fst_builder.finish().is_ok() {
// Should close open store reference to old FST
should_close = true;
// Replace old FST with new FST (this nukes the old FST)
// Notice: there is no need to re-open the new FST, as it will be \
// automatically opened on its next access.
let bucket_final_path = StoreFSTBuilder::path(
StoreFSTPathMode::Permanent,
store.target.collection_hash,
Some(store.target.bucket_hash),
);
// Proceed temporary FST to final FST path rename
if std::fs::rename(&bucket_tmp_path, &bucket_final_path).is_ok() {
info!("done consolidate fst at path: {:?}", bucket_final_path);
} else {
error!(
"error consolidating fst at path: {:?}",
bucket_final_path
);
}
} else {
error!(
"error finishing building temporary fst at path: {:?}",
bucket_tmp_path
);
}
} else {
error!(
"error starting building temporary fst at path: {:?}",
bucket_tmp_path
);
}
} else {
error!(
"error initializing temporary fst at path: {:?}",
bucket_tmp_path
);
}
} else {
error!(
"error initializing temporary fst directory at path: {:?}",
bucket_tmp_path_parent
);
}
} else {
error!("error opening old fst");
}
// Reset all pending sets
*pending_push_write = HashSet::new();
*pending_pop_write = HashSet::new();
}
(should_close, count_moved, count_pushed, count_popped)
}
}
impl StoreGenericPool<StoreFSTKey, StoreFST, StoreFSTBuilder> for StoreFSTPool {}
impl StoreFSTBuilder {
fn open(collection_hash: StoreFSTAtom, bucket_hash: StoreFSTAtom) -> Result<FSTSet, FSTError> {
debug!(
"opening finite-state transducer graph for collection: <{:x?}> and bucket: <{:x?}>",
collection_hash, bucket_hash
);
let collection_bucket_path = Self::path(
StoreFSTPathMode::Permanent,
collection_hash,
Some(bucket_hash),
);
if collection_bucket_path.exists() {
// Open graph at path for collection
// Notice: this is unsafe, as loaded memory is a memory-mapped file, that cannot be \
// garanteed not to be muted while we own a read handle to it. Though, we use \
// higher-level locking mechanisms on all callers of this method, so we are safe.
unsafe { FSTSet::from_path(collection_bucket_path) }
} else {
// FST does not exist on disk, generate an empty FST for now; until a consolidation \
// task occurs and populates the on-disk-FST.
let empty_iter: Vec<&str> = Vec::new();
FSTSet::from_iter(empty_iter)
}
}
fn close(collection_hash: StoreFSTAtom, bucket_hash: StoreFSTAtom) {
debug!(
"closing finite-state transducer graph for collection: <{:x?}> and bucket: <{:x?}>",
collection_hash, bucket_hash
);
let bucket_target = StoreFSTKey::from_atom(collection_hash, bucket_hash);
GRAPH_POOL.write().unwrap().remove(&bucket_target);
GRAPH_CONSOLIDATE.write().unwrap().remove(&bucket_target);
}
fn path(
mode: StoreFSTPathMode,
collection_hash: StoreFSTAtom,
bucket_hash: Option<StoreFSTAtom>,
) -> PathBuf {
let mut final_path = APP_CONF
.store
.fst
.path
.join(format!("{:x?}", collection_hash));
if let Some(bucket_hash) = bucket_hash {
final_path = final_path.join(format!("{:x?}{}", bucket_hash, mode.extension()));
}
final_path
}
}
impl StoreGenericBuilder<StoreFSTKey, StoreFST> for StoreFSTBuilder {
fn new(pool_key: StoreFSTKey) -> Result<StoreFST, ()> {
Self::open(pool_key.collection_hash, pool_key.bucket_hash)
.map(|graph| {
let now = SystemTime::now();
StoreFST {
graph,
target: pool_key,
pending: StoreFSTPending::default(),
last_used: Arc::new(RwLock::new(now)),
last_consolidated: Arc::new(RwLock::new(now)),
}
})
.or_else(|err| {
error!("failed opening fst: {}", err);
Err(())
})
}
}
impl StoreFST {
pub fn cardinality(&self) -> usize {
self.graph.len()
}
pub fn lookup_begins(&self, word: &str) -> Result<FSTStream<Regex>, ()> {
// Notice: this regex maps over an unicode range, for speed reasons at scale. \
// We found out that the 'match any' syntax ('.*') was super-slow. Using the restrictive \
// syntax below divided the cost of eg. a search query by 2. The regex below has been \
// found out to be nearly zero-cost to compile and execute, for whatever reason.
// Regex format: '{escaped_word}([{unicode_range}]*)'
let mut regex_str = regex_escape(word);
regex_str.push_str("(");
let write_result = LexerRegexRange::from(word)
.unwrap_or_default()
.write_to(&mut regex_str);
regex_str.push_str("*)");
// Regex write failed? (this should not happen)
if let Err(err) = write_result {
error!(
"could not lookup word in fst via 'begins': {} because regex write failed: {}",
word, err
);
return Err(());
}
// Proceed word lookup
debug!(
"looking-up word in fst via 'begins': {} with regex: {}",
word, regex_str
);
if let Ok(regex) = Regex::new(®ex_str) {
Ok(self.graph.search(regex).into_stream())
} else {
Err(())
}
}
pub fn lookup_typos(
&self,
word: &str,
max_factor: Option<u32>,
) -> Result<FSTStream<Levenshtein>, ()> {
// Allow more typos in word as the word gets longer, up to a maximum limit
let mut typo_factor = match word.len() {
1 | 2 | 3 => 0,
4 | 5 | 6 => 1,
7 | 8 | 9 => 2,
_ => 3,
};
// Cap typo factor to set maximum?
if let Some(max_factor) = max_factor {
if typo_factor > max_factor {
typo_factor = max_factor;
}
}
debug!(
"looking-up word in fst via 'typos': {} with typo factor: {}",
word, typo_factor
);
if let Ok(fuzzy) = Levenshtein::new(word, typo_factor) {
Ok(self.graph.search(fuzzy).into_stream())
} else {
Err(())
}
}
pub fn should_consolidate(&self) {
// Check if not already scheduled
if !GRAPH_CONSOLIDATE.read().unwrap().contains(&self.target) {
// Schedule target for next consolidation tick (ie. collection + bucket tuple)
GRAPH_CONSOLIDATE.write().unwrap().insert(self.target);
// Bump 'last consolidated' time, effectively de-bouncing consolidation to a fixed \
// and predictible tick time in the future.
let mut last_consolidated_value = self.last_consolidated.write().unwrap();
*last_consolidated_value = SystemTime::now();
// Perform an early drop of the lock (frees up write lock early)
drop(last_consolidated_value);
info!("graph consolidation scheduled on pool key: {}", self.target);
} else {
debug!(
"graph consolidation already scheduled on pool key: {}",
self.target
);
}
}
}
impl StoreGeneric for StoreFST {
fn ref_last_used<'a>(&'a self) -> &'a RwLock<SystemTime> {
&self.last_used
}
}
impl StoreFSTActionBuilder {
pub fn access(store: StoreFSTBox) -> StoreFSTAction {
Self::build(store)
}
pub fn erase<'a, T: Into<&'a str>>(collection: T, bucket: Option<T>) -> Result<u32, ()> {
Self::dispatch_erase("fst", collection, bucket)
}
fn build(store: StoreFSTBox) -> StoreFSTAction {
StoreFSTAction { store }
}
}
impl StoreGenericActionBuilder for StoreFSTActionBuilder {
fn proceed_erase_collection(collection_str: &str) -> Result<u32, ()> {
let path_mode = StoreFSTPathMode::Permanent;
let collection_atom = StoreKeyerHasher::to_compact(collection_str);
let collection_path = StoreFSTBuilder::path(path_mode, collection_atom, None);
// Force a FST graph close (on all contained buckets)
// Notice: we first need to scan for opened buckets in-memory, as not all FSTs may be \
// commited to disk; thus some FST stores that exist in-memory may not exist on-disk.
let mut bucket_atoms: Vec<StoreFSTAtom> = Vec::new();
{
let graph_pool_read = GRAPH_POOL.read().unwrap();
for target_key in graph_pool_read.keys() {
if target_key.collection_hash == collection_atom {
bucket_atoms.push(target_key.bucket_hash);
}
}
}
if !bucket_atoms.is_empty() {
debug!(
"will force-close {} fst buckets for collection: {}",
bucket_atoms.len(),
collection_str
);
let (mut graph_pool_write, mut graph_consolidate_write) = (
GRAPH_POOL.write().unwrap(),
GRAPH_CONSOLIDATE.write().unwrap(),
);
for bucket_atom in bucket_atoms {
debug!(
"fst bucket graph force close for bucket: {}/<{:x?}>",
collection_str, bucket_atom
);
let bucket_target = StoreFSTKey::from_atom(collection_atom, bucket_atom);
graph_pool_write.remove(&bucket_target);
graph_consolidate_write.remove(&bucket_target);
}
}
// Remove all FSTs on-disk
if collection_path.exists() {
debug!(
"fst collection store exists, erasing: {}/* at path: {:?}",
collection_str, &collection_path
);
// Remove FST graph storage from filesystem
let erase_result = fs::remove_dir_all(&collection_path);
if erase_result.is_ok() {
debug!("done with fst collection erasure");
Ok(1)
} else {
Err(())
}
} else {
debug!(
"fst collection store does not exist, consider already erased: {}/* at path: {:?}",
collection_str, &collection_path
);
Ok(0)
}
}
fn proceed_erase_bucket(collection_str: &str, bucket_str: &str) -> Result<u32, ()> {
debug!(
"sub-erase on fst bucket: {} for collection: {}",
bucket_str, collection_str
);
let (collection_atom, bucket_atom) = (
StoreKeyerHasher::to_compact(collection_str),
StoreKeyerHasher::to_compact(bucket_str),
);
let bucket_path = StoreFSTBuilder::path(
StoreFSTPathMode::Permanent,
collection_atom,
Some(bucket_atom),
);
// Force a FST graph close
StoreFSTBuilder::close(collection_atom, bucket_atom);
// Remove FST on-disk
if bucket_path.exists() {
debug!(
"fst bucket graph exists, erasing: {}/{} at path: {:?}",
collection_str, bucket_str, &bucket_path
);
// Remove FST graph storage from filesystem
let erase_result = fs::remove_file(&bucket_path);
if erase_result.is_ok() {
debug!("done with fst bucket erasure");
Ok(1)
} else {
Err(())
}
} else {
debug!(
"fst bucket graph does not exist, consider already erased: {}/{} at path: {:?}",
collection_str, bucket_str, &bucket_path
);
Ok(0)
}
}
}
impl StoreFSTAction {