Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4f71b89
basic sqlite history
phiresky Apr 1, 2022
dd93aba
fix test compilation
phiresky Apr 1, 2022
4c03e68
final touches for MVP
phiresky Apr 1, 2022
a669fb3
better documentation
phiresky Apr 1, 2022
187f867
fix for empty history
phiresky Apr 2, 2022
01b62e7
partial change to non-generic history
phiresky Apr 16, 2022
3e76ae7
mostly working
phiresky Apr 16, 2022
317a69f
fix tests and ci
phiresky Apr 18, 2022
7d4b7ca
fixes, format
phiresky Apr 18, 2022
5fc904e
move history item to new file
phiresky Apr 18, 2022
f756189
fix some comments, fix test compile errors
phiresky Apr 20, 2022
4d60705
ci features matrix
phiresky Apr 20, 2022
13b32b9
fix index creation
phiresky Apr 18, 2022
56a210c
fix file-based tests
phiresky May 8, 2022
7c6b09a
move logic for not saving empty entries to engine
phiresky May 8, 2022
9cab40f
fix update last command on empty, set up application_id and check ver…
phiresky May 8, 2022
37afda0
Merge remote-tracking branch 'origin/main' into sqlite-history-2
phiresky May 8, 2022
556115b
add specific error variants
phiresky May 8, 2022
285ea4b
format
phiresky May 11, 2022
c772fad
fix compile errors
phiresky May 11, 2022
74c8f71
fix fmt
fdncred May 11, 2022
d84fd86
sqlite with bashisms
elferherrera May 21, 2022
a9c9d3a
Merge branch 'main' of https://github.com/nushell/reedline into sqlit…
elferherrera May 21, 2022
428544d
hide with features
elferherrera May 21, 2022
014bd48
cargo fmt
elferherrera May 21, 2022
a8d8703
improve performance of bashisms selectors
phiresky Jun 6, 2022
33b7610
Merge remote-tracking branch 'origin/main' into sqlite-history-2
phiresky Jun 6, 2022
6d803bb
Style: Remove commented out code
sholderbach Jun 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix test compilation
  • Loading branch information
phiresky committed Apr 16, 2022
commit dd93aba23a37bddc6d5df2c4fb7020105719a1d4
91 changes: 53 additions & 38 deletions src/history/sqlite_backed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,29 @@ fn map_sqlite_err(err: rusqlite::Error) -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::Other, err)
}


impl<ContextType: HistoryEntryContext> SqliteBackedHistory<ContextType> {
/*pub fn new() -> Self {
SqliteBackedHistory::with_file(":memory:")
with_file_for_test(":memory:")
}*/

/// Creates a new history with an associated history file.
///
/// History file format: commands separated by new lines.
/// If file exists file will be read otherwise empty file will be created.
///
///
/// **Side effects:** creates all nested directories to the file
///
pub fn with_file(file: PathBuf) -> std::io::Result<Self> {
if let Some(base_dir) = file.parent() {
std::fs::create_dir_all(base_dir)?;
}
let db = Connection::open(&file).map_err(map_sqlite_err)?;
Self::from_connection(db)
}
/// Creates a new history in memory
pub fn in_memory() -> std::io::Result<Self> {
Self::from_connection(Connection::open_in_memory().map_err(map_sqlite_err)?)
}
fn from_connection(db: Connection) -> std::io::Result<Self> {
db.pragma_update(None, "journal_mode", "wal").map_err(map_sqlite_err)?;
db.execute(
"
Expand Down Expand Up @@ -184,29 +192,36 @@ mod tests {

use super::*;

fn in_memory_for_test() -> SqliteBackedHistory<()> {
SqliteBackedHistory::in_memory().unwrap()
}
fn with_file_for_test(capacity: i32, file: PathBuf) -> std::io::Result<SqliteBackedHistory<()>> {
SqliteBackedHistory::with_file(file)
}

#[test]
fn accessing_empty_history_returns_nothing() {
let hist = FileBackedHistory::default();
let hist = in_memory_for_test();
assert_eq!(hist.string_at_cursor(), None);
}

#[test]
fn going_forward_in_empty_history_does_not_error_out() {
let mut hist = FileBackedHistory::default();
let mut hist = in_memory_for_test();
hist.forward();
assert_eq!(hist.string_at_cursor(), None);
}

#[test]
fn going_backwards_in_empty_history_does_not_error_out() {
let mut hist = FileBackedHistory::default();
let mut hist = in_memory_for_test();
hist.back();
assert_eq!(hist.string_at_cursor(), None);
}

#[test]
fn going_backwards_bottoms_out() {
let mut hist = FileBackedHistory::default();
let mut hist = in_memory_for_test();
hist.append("command1");
hist.append("command2");
hist.back();
Expand All @@ -219,7 +234,7 @@ mod tests {

#[test]
fn going_forwards_bottoms_out() {
let mut hist = FileBackedHistory::default();
let mut hist = in_memory_for_test();
hist.append("command1");
hist.append("command2");
hist.forward();
Expand All @@ -230,9 +245,9 @@ mod tests {
assert_eq!(hist.string_at_cursor(), None);
}

#[test]
/*#[test]
fn appends_only_unique() {
let mut hist = FileBackedHistory::default();
let mut hist = in_memory_for_test();
hist.append("unique_old");
hist.append("test");
hist.append("test");
Expand All @@ -241,14 +256,14 @@ mod tests {
}
#[test]
fn appends_no_empties() {
let mut hist = FileBackedHistory::default();
let mut hist = in_memory_for_test();
hist.append("");
assert_eq!(hist.entries.len(), 0);
}
}*/

#[test]
fn prefix_search_works() {
let mut hist = FileBackedHistory::default();
let mut hist = in_memory_for_test();
hist.append("find me as well");
hist.append("test");
hist.append("find me");
Expand All @@ -263,7 +278,7 @@ mod tests {

#[test]
fn prefix_search_bottoms_out() {
let mut hist = FileBackedHistory::default();
let mut hist = in_memory_for_test();
hist.append("find me as well");
hist.append("test");
hist.append("find me");
Expand All @@ -281,7 +296,7 @@ mod tests {
}
#[test]
fn prefix_search_returns_to_none() {
let mut hist = FileBackedHistory::default();
let mut hist = in_memory_for_test();
hist.append("find me as well");
hist.append("test");
hist.append("find me");
Expand All @@ -301,7 +316,7 @@ mod tests {

#[test]
fn prefix_search_ignores_consecutive_equivalent_entries_going_backwards() {
let mut hist = FileBackedHistory::default();
let mut hist = in_memory_for_test();
hist.append("find me as well");
hist.append("find me once");
hist.append("test");
Expand All @@ -316,7 +331,7 @@ mod tests {

#[test]
fn prefix_search_ignores_consecutive_equivalent_entries_going_forwards() {
let mut hist = FileBackedHistory::default();
let mut hist = in_memory_for_test();
hist.append("find me once");
hist.append("test");
hist.append("find me once");
Expand All @@ -334,7 +349,7 @@ mod tests {

#[test]
fn substring_search_works() {
let mut hist = FileBackedHistory::default();
let mut hist = in_memory_for_test();
hist.append("substring");
hist.append("don't find me either");
hist.append("prefix substring");
Expand All @@ -360,7 +375,7 @@ mod tests {

#[test]
fn substring_search_with_empty_value_returns_none() {
let mut hist = FileBackedHistory::default();
let mut hist = in_memory_for_test();
hist.append("substring");

hist.set_navigation(HistoryNavigationQuery::SubstringSearch("".to_string()));
Expand All @@ -379,14 +394,14 @@ mod tests {
let entries = vec!["test", "text", "more test text"];

{
let mut hist = FileBackedHistory::with_file(5, histfile.clone()).unwrap();
let mut hist = with_file_for_test(5, histfile.clone()).unwrap();

entries.iter().for_each(|e| hist.append(e));

// As `hist` goes out of scope and get's dropped, its contents are flushed to disk
}

let reading_hist = FileBackedHistory::with_file(5, histfile).unwrap();
let reading_hist = with_file_for_test(5, histfile).unwrap();

let actual: Vec<_> = reading_hist.iter_chronologic().collect();
assert_eq!(entries, actual);
Expand All @@ -409,14 +424,14 @@ mod tests {
];

{
let mut writing_hist = FileBackedHistory::with_file(5, histfile.clone()).unwrap();
let mut writing_hist = with_file_for_test(5, histfile.clone()).unwrap();

entries.iter().for_each(|e| writing_hist.append(e));

// As `hist` goes out of scope and get's dropped, its contents are flushed to disk
}

let reading_hist = FileBackedHistory::with_file(5, histfile).unwrap();
let reading_hist = with_file_for_test(5, histfile).unwrap();

let actual: Vec<_> = reading_hist.iter_chronologic().collect();
assert_eq!(entries, actual);
Expand All @@ -440,7 +455,7 @@ mod tests {

{
let mut writing_hist =
FileBackedHistory::with_file(capacity, histfile.clone()).unwrap();
with_file_for_test(capacity, histfile.clone()).unwrap();

initial_entries.iter().for_each(|e| writing_hist.append(e));

Expand All @@ -449,7 +464,7 @@ mod tests {

{
let mut appending_hist =
FileBackedHistory::with_file(capacity, histfile.clone()).unwrap();
with_file_for_test(capacity, histfile.clone()).unwrap();

appending_entries
.iter()
Expand All @@ -462,7 +477,7 @@ mod tests {

{
let mut truncating_hist =
FileBackedHistory::with_file(capacity, histfile.clone()).unwrap();
with_file_for_test(capacity, histfile.clone()).unwrap();

truncating_entries
.iter()
Expand All @@ -473,7 +488,7 @@ mod tests {
// As `hist` goes out of scope and get's dropped, its contents are flushed to disk
}

let reading_hist = FileBackedHistory::with_file(capacity, histfile).unwrap();
let reading_hist = with_file_for_test(capacity, histfile).unwrap();

let actual: Vec<_> = reading_hist.iter_chronologic().collect();
assert_eq!(expected_truncated_entries, actual);
Expand All @@ -494,7 +509,7 @@ mod tests {
let expected_truncated_entries = vec!["test 4", "test 5", "test 6", "test 7", "test 8"];

{
let mut writing_hist = FileBackedHistory::with_file(10, histfile.clone()).unwrap();
let mut writing_hist = with_file_for_test(10, histfile.clone()).unwrap();

overly_large_previous_entries
.iter()
Expand All @@ -504,14 +519,14 @@ mod tests {
}

{
let truncating_hist = FileBackedHistory::with_file(5, histfile.clone()).unwrap();
let truncating_hist = with_file_for_test(5, histfile.clone()).unwrap();

let actual: Vec<_> = truncating_hist.iter_chronologic().collect();
assert_eq!(expected_truncated_entries, actual);
// As `hist` goes out of scope and get's dropped, its contents are flushed to disk
}

let reading_hist = FileBackedHistory::with_file(5, histfile).unwrap();
let reading_hist = with_file_for_test(5, histfile).unwrap();

let actual: Vec<_> = reading_hist.iter_chronologic().collect();
assert_eq!(expected_truncated_entries, actual);
Expand All @@ -534,18 +549,18 @@ mod tests {

{
let mut writing_hist =
FileBackedHistory::with_file(capacity, histfile.clone()).unwrap();
SqliteBackedHistory::<()>::with_file(histfile.clone()).unwrap();

initial_entries.iter().for_each(|e| writing_hist.append(e));

// As `hist` goes out of scope and get's dropped, its contents are flushed to disk
}

{
let mut hist_a = FileBackedHistory::with_file(capacity, histfile.clone()).unwrap();
let mut hist_a = with_file_for_test(capacity, histfile.clone()).unwrap();

{
let mut hist_b = FileBackedHistory::with_file(capacity, histfile.clone()).unwrap();
let mut hist_b = with_file_for_test(capacity, histfile.clone()).unwrap();

entries_b.iter().for_each(|e| hist_b.append(e));

Expand All @@ -556,7 +571,7 @@ mod tests {
// As `hist` goes out of scope and get's dropped, its contents are flushed to disk
}

let reading_hist = FileBackedHistory::with_file(capacity, histfile).unwrap();
let reading_hist = with_file_for_test(capacity, histfile).unwrap();

let actual: Vec<_> = reading_hist.iter_chronologic().collect();
assert_eq!(expected_entries, actual);
Expand All @@ -578,7 +593,7 @@ mod tests {

{
let mut writing_hist =
FileBackedHistory::with_file(capacity, histfile.clone()).unwrap();
with_file_for_test(capacity, histfile.clone()).unwrap();

initial_entries.for_each(|e| writing_hist.append(&e));

Expand All @@ -590,7 +605,7 @@ mod tests {
let cap = capacity;
let hfile = histfile.clone();
std::thread::spawn(move || {
let mut hist = FileBackedHistory::with_file(cap, hfile).unwrap();
let mut hist = with_file_for_test(cap, hfile).unwrap();
hist.append(&format!("A{}", i));
hist.sync().unwrap();
hist.append(&format!("B{}", i));
Expand All @@ -602,7 +617,7 @@ mod tests {
t.join().unwrap();
}

let reading_hist = FileBackedHistory::with_file(capacity, histfile).unwrap();
let reading_hist = with_file_for_test(capacity, histfile).unwrap();

let actual: Vec<_> = reading_hist.iter_chronologic().collect();

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn main() -> Result<()> {
history.update_context(|mut c| {
c.timestamp = "foo".to_string();
c
});
}).unwrap();
if (buffer.trim() == "exit") || (buffer.trim() == "logout") {
break;
}
Expand Down