Skip to content

Commit

Permalink
Fix: ID generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementNerma committed Dec 7, 2023
1 parent d0d27e8 commit 9cc5d90
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/history/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,12 +549,17 @@ mod tests {
{
let (mut hist_a, _) = create_history_at(capacity, &histfile);

assert_eq!(get_all_entry_texts(hist_a.as_ref()), initial_entries);

{
let (mut hist_b, _) = create_history_at(capacity, &histfile);

assert_eq!(get_all_entry_texts(hist_b.as_ref()), initial_entries);

add_text_entries(hist_b.as_mut(), &entries_b);
// As `hist` goes out of scope and get's dropped, its contents are flushed to disk
}

add_text_entries(hist_a.as_mut(), &entries_a);
// As `hist` goes out of scope and get's dropped, its contents are flushed to disk
}
Expand Down
12 changes: 7 additions & 5 deletions src/history/file_backed.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use indexmap::IndexMap;
use rand::{rngs::SmallRng, RngCore, SeedableRng};

use super::{
base::CommandLineSearch, History, HistoryItem, HistoryItemId, SearchDirection, SearchQuery,
Expand Down Expand Up @@ -32,6 +33,7 @@ pub struct FileBackedHistory {
file: Option<PathBuf>,
last_on_disk: Option<HistoryItemId>,
session: Option<HistorySessionId>,
rng: SmallRng,
}

impl Default for FileBackedHistory {
Expand Down Expand Up @@ -63,7 +65,7 @@ fn decode_entry(s: &str) -> std::result::Result<(HistoryItemId, String), &'stati

impl History for FileBackedHistory {
fn generate_id(&mut self) -> HistoryItemId {
HistoryItemId((self.entries.len() + 1) as i64)
HistoryItemId(self.rng.next_u64() as i64)
}

/// only saves a value if it's different than the last value
Expand Down Expand Up @@ -280,10 +282,9 @@ impl History for FileBackedHistory {
.collect::<std::io::Result<IndexMap<_, _>>>()?;

if from_file.len() + own_entries.len() > self.capacity {
(
from_file.split_off(from_file.len() - (self.capacity - own_entries.len())),
true,
)
let start = from_file.len() + own_entries.len() - self.capacity;

(from_file.split_off(start), true)
} else {
(from_file, false)
}
Expand Down Expand Up @@ -361,6 +362,7 @@ impl FileBackedHistory {
file: None,
last_on_disk: None,
session: None,
rng: SmallRng::from_entropy(),
}
}

Expand Down

0 comments on commit 9cc5d90

Please sign in to comment.