Skip to content

feat: document count index #2516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: v2.0-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 0 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@
},
"required": ["resolution"],
"additionalProperties": false
},
"count": {
"type": "boolean",
"description": "Enables count operations on the index. Adds extra costs for documents storage"
}
},
"required": [
Expand Down
11 changes: 11 additions & 0 deletions packages/rs-dpp/src/data_contract/document_type/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ pub struct Index {
pub null_searchable: bool,
/// Contested indexes are useful when a resource is considered valuable
pub contested_index: Option<ContestedIndexInformation>,
/// Enables count operations on the index
pub count: bool,
}

impl Index {
Expand Down Expand Up @@ -468,6 +470,7 @@ impl TryFrom<&[(Value, Value)]> for Index {
let mut name = None;
let mut contested_index = None;
let mut index_properties: Vec<IndexProperty> = Vec::new();
let mut count = false;

for (key_value, value_value) in index_type_value_map {
let key = key_value.to_str()?;
Expand Down Expand Up @@ -603,6 +606,13 @@ impl TryFrom<&[(Value, Value)]> for Index {
index_properties.push(index_property);
}
}
"count" => {
// This is a boolean value
// If it is not a boolean value, then it is an error
if value_value.is_bool() {
count = value_value.as_bool().expect("confirmed as bool");
}
}
_ => {
return Err(DataContractError::ValueWrongType(
"unexpected property name".to_string(),
Expand All @@ -626,6 +636,7 @@ impl TryFrom<&[(Value, Value)]> for Index {
unique,
null_searchable,
contested_index,
count,
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl Index {
unique,
null_searchable: true,
contested_index: None,
count: false,
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub struct IndexLevelTypeInfo {
pub should_insert_with_all_null: bool,
/// The index type
pub index_type: IndexType,
/// Is this index countable. Uses sum trees to enable count operations
pub count: bool,
}

impl IndexType {
Expand Down Expand Up @@ -214,6 +216,7 @@ impl IndexLevel {
current_level.has_index_with_type = Some(IndexLevelTypeInfo {
should_insert_with_all_null: index.null_searchable,
index_type,
count: index.count,
});
}
}
Expand Down Expand Up @@ -282,6 +285,7 @@ mod tests {
unique: false,
null_searchable: true,
contested_index: None,
count: false,
}];

let old_index_structure =
Expand Down Expand Up @@ -309,6 +313,7 @@ mod tests {
unique: false,
null_searchable: true,
contested_index: None,
count: false,
}];

let new_indices = vec![
Expand All @@ -321,6 +326,7 @@ mod tests {
unique: false,
null_searchable: true,
contested_index: None,
count: false,
},
Index {
name: "test2".to_string(),
Expand All @@ -331,6 +337,7 @@ mod tests {
unique: false,
null_searchable: true,
contested_index: None,
count: false,
},
];

Expand Down Expand Up @@ -367,6 +374,7 @@ mod tests {
unique: false,
null_searchable: true,
contested_index: None,
count: false,
},
Index {
name: "test2".to_string(),
Expand All @@ -377,6 +385,7 @@ mod tests {
unique: false,
null_searchable: true,
contested_index: None,
count: false,
},
];

Expand All @@ -389,6 +398,7 @@ mod tests {
unique: false,
null_searchable: true,
contested_index: None,
count: false,
}];

let old_index_structure =
Expand Down Expand Up @@ -423,6 +433,7 @@ mod tests {
unique: false,
null_searchable: true,
contested_index: None,
count: false,
}];

let new_indices = vec![Index {
Expand All @@ -440,6 +451,7 @@ mod tests {
unique: false,
null_searchable: true,
contested_index: None,
count: false,
}];

let old_index_structure =
Expand Down Expand Up @@ -480,6 +492,7 @@ mod tests {
unique: false,
null_searchable: true,
contested_index: None,
count: false,
}];

let new_indices = vec![Index {
Expand All @@ -491,6 +504,7 @@ mod tests {
unique: false,
null_searchable: true,
contested_index: None,
count: false,
}];

let old_index_structure =
Expand Down
12 changes: 6 additions & 6 deletions packages/rs-drive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ enum-map = { version = "2.0.3", optional = true }
intmap = { version = "3.0.1", features = ["serde"], optional = true }
chrono = { version = "0.4.35", optional = true }
itertools = { version = "0.13", optional = true }
grovedb = { git = "https://github.com/dashpay/grovedb", rev = "f89e03e4e0ac12aa2feea5c94b38c09f4909facc", optional = true, default-features = false }
grovedb-costs = { git = "https://github.com/dashpay/grovedb", rev = "f89e03e4e0ac12aa2feea5c94b38c09f4909facc", optional = true }
grovedb-path = { git = "https://github.com/dashpay/grovedb", rev = "f89e03e4e0ac12aa2feea5c94b38c09f4909facc" }
grovedb-storage = { git = "https://github.com/dashpay/grovedb", rev = "f89e03e4e0ac12aa2feea5c94b38c09f4909facc", optional = true }
grovedb-version = { git = "https://github.com/dashpay/grovedb", rev = "f89e03e4e0ac12aa2feea5c94b38c09f4909facc" }
grovedb-epoch-based-storage-flags = { git = "https://github.com/dashpay/grovedb", rev = "f89e03e4e0ac12aa2feea5c94b38c09f4909facc" }
grovedb = { path = "../../../grovedb/grovedb", optional = true, default-features = false }
grovedb-costs = { path = "../../../grovedb/costs", optional = true }
grovedb-path = { path = "../../../grovedb/path" }
grovedb-storage = { path = "../../../grovedb/storage", optional = true }
grovedb-version = { path = "../../../grovedb/grovedb-version" }
grovedb-epoch-based-storage-flags = { path = "../../../grovedb/grovedb-epoch-based-storage-flags" }

[dev-dependencies]
criterion = "0.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl Drive {
if all_fields_null && !index_type.should_insert_with_all_null {
return Ok(());
}

// unique indexes will be stored under key "0"
// non-unique indices should have a tree at key "0" that has all elements based off of primary key
if !index_type.index_type.is_unique() || any_fields_null {
Expand All @@ -57,12 +58,19 @@ impl Drive {

let path_key_info = key_path_info.add_path_info(index_path_info.clone());

// if index is countable, we should use count trees, so we can get the count of elements
let reference_tree_type = if index_type.count {
TreeType::CountTree
} else {
TreeType::NormalTree
};

let apply_type = if estimated_costs_only_with_layer_info.is_none() {
BatchInsertTreeApplyType::StatefulBatchInsertTree
} else {
BatchInsertTreeApplyType::StatelessBatchInsertTree {
in_tree_type: TreeType::NormalTree,
tree_type: TreeType::NormalTree,
tree_type: reference_tree_type,
flags_len: storage_flags
.map(|s| s.serialized_size())
.unwrap_or_default(),
Expand All @@ -75,7 +83,7 @@ impl Drive {
// a contested resource index
self.batch_insert_empty_tree_if_not_exists(
path_key_info,
TreeType::NormalTree,
reference_tree_type,
*storage_flags,
apply_type,
transaction,
Expand All @@ -94,7 +102,7 @@ impl Drive {
estimated_costs_only_with_layer_info.insert(
index_path_info.clone().convert_to_key_info_path(),
EstimatedLayerInformation {
tree_type: TreeType::NormalTree,
tree_type: reference_tree_type,
estimated_layer_count: PotentiallyAtMaxElements,
estimated_layer_sizes: AllReference(
DEFAULT_HASH_SIZE_U8,
Expand Down
Loading
Loading