Skip to content

Commit

Permalink
[db] Fix empty DbValueIndex producing invalid storage index #681 (#682)
Browse files Browse the repository at this point in the history
* Update insert_values_test.rs

* Update db_value_index.rs
  • Loading branch information
michaelvlach authored Aug 18, 2023
1 parent 9653223 commit 578c35b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
10 changes: 5 additions & 5 deletions agdb/src/db/db_value_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl DbValueIndex {
}

pub(crate) fn is_value(&self) -> bool {
self.size() != 0
self.size() != 0 || self.index() == 0
}

pub(crate) fn set_type(&mut self, value: u8) {
Expand Down Expand Up @@ -142,7 +142,7 @@ mod tests {
fn value_max() {
let mut index = DbValueIndex::default();
let value = vec![1_u8; 15];
index.set_value(&value);
assert!(index.set_value(&value));

assert_eq!(index.value(), value);
assert_eq!(index.size(), 15);
Expand All @@ -153,11 +153,11 @@ mod tests {
fn value_too_large() {
let mut index = DbValueIndex::default();
let value = vec![1_u8; 16];
index.set_value(&value);
assert!(!index.set_value(&value));

assert_eq!(index.value(), vec![]);
assert_eq!(index.size(), 0);
assert!(!index.is_value());
assert!(index.is_value());
}

#[test]
Expand All @@ -174,7 +174,7 @@ mod tests {
#[test]
fn serialize() {
let mut index = DbValueIndex::new();
index.set_value(&[1_u8, 2_u8, 3_u8]);
assert!(index.set_value(&[1_u8, 2_u8, 3_u8]));

let data = index.serialize();
let other_index = DbValueIndex::deserialize(&data).unwrap();
Expand Down
20 changes: 20 additions & 0 deletions agdb/tests/insert_values_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,23 @@ fn insert_values_overwrite_transaction() {
}],
)
}

#[test]
fn overwrite_empty_value() {
let mut db = TestDb::new();
db.exec_mut(QueryBuilder::insert().nodes().count(1).query(), 1);
db.exec_mut(
QueryBuilder::insert()
.values(vec![vec![("v", "").into()]])
.ids(1)
.query(),
1,
);
db.exec_mut(
QueryBuilder::insert()
.values(vec![vec![("v", "a").into()]])
.ids(1)
.query(),
1,
);
}

0 comments on commit 578c35b

Please sign in to comment.