drive/util/object_size_info/
document_info.rs1use crate::error::drive::DriveError;
2use crate::error::fee::FeeError;
3use crate::error::Error;
4use crate::util::object_size_info::DriveKeyInfo::{Key, KeySize};
5use crate::util::object_size_info::KeyValueInfo::{KeyRefRequest, KeyValueMaxSize};
6use crate::util::object_size_info::{DriveKeyInfo, KeyValueInfo};
7use crate::util::storage_flags::StorageFlags;
8use crate::util::type_constants::{
9 DEFAULT_HASH_SIZE_U16, DEFAULT_HASH_SIZE_U8, U32_SIZE_U16, U32_SIZE_U8, U64_SIZE_U16,
10 U64_SIZE_U8,
11};
12use dpp::data_contract::document_type::accessors::DocumentTypeV0Getters;
13use dpp::data_contract::document_type::methods::DocumentTypeBasicMethods;
14use dpp::data_contract::document_type::{DocumentTypeRef, IndexLevel};
15use dpp::document::document_methods::DocumentMethodsV0;
16use dpp::document::{Document, DocumentV0Getters};
17use dpp::version::PlatformVersion;
18use grovedb::batch::key_info::KeyInfo;
19use std::borrow::Cow;
20
21#[derive(Clone, Debug)]
23pub enum DocumentInfo<'a> {
24 DocumentOwnedInfo((Document, Option<Cow<'a, StorageFlags>>)),
26 DocumentRefInfo((&'a Document, Option<Cow<'a, StorageFlags>>)),
28 DocumentRefAndSerialization((&'a Document, &'a [u8], Option<Cow<'a, StorageFlags>>)),
30 DocumentAndSerialization((Document, Vec<u8>, Option<Cow<'a, StorageFlags>>)),
32 DocumentEstimatedAverageSize(u32),
34}
35
36pub trait DocumentInfoV0Methods {
38 fn is_document_and_serialization(&self) -> bool;
40 fn is_document_size(&self) -> bool;
42 fn get_borrowed_document(&self) -> Option<&Document>;
44 fn id_key_value_info(&self) -> KeyValueInfo<'_>;
46 fn get_estimated_size_for_document_type(
48 &self,
49 key_path: &str,
50 document_type: DocumentTypeRef,
51 platform_version: &PlatformVersion,
52 ) -> Result<u16, Error>;
53 fn get_raw_for_document_type(
55 &self,
56 key_path: &str,
57 document_type: DocumentTypeRef,
58 owner_id: Option<[u8; 32]>,
59 size_info_with_base_event: Option<(&IndexLevel, [u8; 32])>,
60 platform_version: &PlatformVersion,
61 ) -> Result<Option<DriveKeyInfo<'_>>, Error>;
62 fn get_borrowed_document_and_storage_flags(&self)
64 -> Option<(&Document, Option<&StorageFlags>)>;
65 fn get_storage_flags_ref(&self) -> Option<&StorageFlags>;
67 fn get_document_id_as_slice(&self) -> Option<&[u8]>;
69}
70
71impl DocumentInfoV0Methods for DocumentInfo<'_> {
72 fn is_document_and_serialization(&self) -> bool {
74 matches!(self, DocumentInfo::DocumentRefAndSerialization(..))
75 }
76
77 fn is_document_size(&self) -> bool {
79 matches!(self, DocumentInfo::DocumentEstimatedAverageSize(_))
80 }
81
82 fn get_borrowed_document(&self) -> Option<&Document> {
84 match self {
85 DocumentInfo::DocumentRefAndSerialization((document, _, _))
86 | DocumentInfo::DocumentRefInfo((document, _)) => Some(document),
87 DocumentInfo::DocumentOwnedInfo((document, _))
88 | DocumentInfo::DocumentAndSerialization((document, _, _)) => Some(document),
89 DocumentInfo::DocumentEstimatedAverageSize(_) => None,
90 }
91 }
92
93 fn id_key_value_info(&self) -> KeyValueInfo<'_> {
95 match self {
96 DocumentInfo::DocumentRefAndSerialization((document, _, _))
97 | DocumentInfo::DocumentRefInfo((document, _)) => {
98 KeyRefRequest(document.id_ref().as_slice())
99 }
100 DocumentInfo::DocumentOwnedInfo((document, _))
101 | DocumentInfo::DocumentAndSerialization((document, _, _)) => {
102 KeyRefRequest(document.id_ref().as_slice())
103 }
104 DocumentInfo::DocumentEstimatedAverageSize(document_max_size) => {
105 KeyValueMaxSize((32, *document_max_size))
106 }
107 }
108 }
109
110 fn get_estimated_size_for_document_type(
112 &self,
113 key_path: &str,
114 document_type: DocumentTypeRef,
115 platform_version: &PlatformVersion,
116 ) -> Result<u16, Error> {
117 match key_path {
118 "$ownerId" | "$id" | "$creatorId" => Ok(DEFAULT_HASH_SIZE_U16),
119 "$createdAt" | "$updatedAt" | "$transferredAt" => Ok(U64_SIZE_U16),
120 "$createdAtBlockHeight" | "$updatedAtBlockHeight" | "$transferredAtBlockHeight" => {
121 Ok(U64_SIZE_U16)
122 }
123 "$createdAtCoreBlockHeight"
124 | "$updatedAtCoreBlockHeight"
125 | "$transferredAtCoreBlockHeight" => Ok(U32_SIZE_U16),
126 key_path => {
127 let property = document_type.flattened_properties().get(key_path).ok_or({
128 Error::Fee(FeeError::DocumentTypeFieldNotFoundForEstimation(format!(
129 "incorrect key path [{}] for document type for estimated sizes",
130 key_path
131 )))
132 })?;
133 let estimated_size = property
134 .property_type
135 .middle_byte_size_ceil(platform_version)?
136 .ok_or({
137 Error::Drive(DriveError::CorruptedCodeExecution(
138 "document type must have a max size",
139 ))
140 })?;
141 Ok(estimated_size)
142 }
143 }
144 }
145
146 fn get_raw_for_document_type(
148 &self,
149 key_path: &str,
150 document_type: DocumentTypeRef,
151 owner_id: Option<[u8; 32]>,
152 size_info_with_base_event: Option<(&IndexLevel, [u8; 32])>,
153 platform_version: &PlatformVersion,
154 ) -> Result<Option<DriveKeyInfo<'_>>, Error> {
155 match self {
156 DocumentInfo::DocumentRefAndSerialization((document, _, _))
157 | DocumentInfo::DocumentRefInfo((document, _)) => {
158 let raw_value = document.get_raw_for_document_type(
159 key_path,
160 document_type,
161 owner_id,
162 platform_version,
163 )?;
164 match raw_value {
165 None => Ok(None),
166 Some(value) => Ok(Some(Key(value))),
167 }
168 }
169 DocumentInfo::DocumentOwnedInfo((document, _))
170 | DocumentInfo::DocumentAndSerialization((document, _, _)) => {
171 let raw_value = document.get_raw_for_document_type(
172 key_path,
173 document_type,
174 owner_id,
175 platform_version,
176 )?;
177 match raw_value {
178 None => Ok(None),
179 Some(value) => Ok(Some(Key(value))),
180 }
181 }
182 DocumentInfo::DocumentEstimatedAverageSize(_) => {
183 let (index_level, base_event) = size_info_with_base_event.ok_or(Error::Drive(
184 DriveError::CorruptedCodeExecution("size_info_with_base_event None but needed"),
185 ))?;
186 match key_path {
187 "$ownerId" | "$id" | "$creatorId" => Ok(Some(KeySize(KeyInfo::MaxKeySize {
188 unique_id: document_type
189 .unique_id_for_document_field(index_level, base_event)
190 .to_vec(),
191 max_size: DEFAULT_HASH_SIZE_U8,
192 }))),
193 "$createdAt" | "$updatedAt" | "$transferredAt" => {
194 Ok(Some(KeySize(KeyInfo::MaxKeySize {
195 unique_id: document_type
196 .unique_id_for_document_field(index_level, base_event)
197 .to_vec(),
198 max_size: U64_SIZE_U8,
199 })))
200 }
201 "$createdAtBlockHeight"
202 | "$updatedAtBlockHeight"
203 | "$transferredAtBlockHeight" => Ok(Some(KeySize(KeyInfo::MaxKeySize {
204 unique_id: document_type
205 .unique_id_for_document_field(index_level, base_event)
206 .to_vec(),
207 max_size: U64_SIZE_U8,
208 }))),
209 "$createdAtCoreBlockHeight"
210 | "$updatedAtCoreBlockHeight"
211 | "$transferredAtCoreBlockHeight" => Ok(Some(KeySize(KeyInfo::MaxKeySize {
212 unique_id: document_type
213 .unique_id_for_document_field(index_level, base_event)
214 .to_vec(),
215 max_size: U32_SIZE_U8,
216 }))),
217 key_path => {
218 let property =
219 document_type.flattened_properties().get(key_path).ok_or({
220 Error::Fee(FeeError::DocumentTypeFieldNotFoundForEstimation(
221 format!("incorrect key path [{}] for document type for get_raw_for_document_type", key_path)
222 ))
223 })?;
224
225 let estimated_middle_size = property
226 .property_type
227 .middle_byte_size_ceil(platform_version)?
228 .ok_or({
229 Error::Drive(DriveError::CorruptedCodeExecution(
230 "document type must have a max size",
231 ))
232 })?;
233 if estimated_middle_size > u8::MAX as u16 {
234 return Err(Error::Drive(DriveError::CorruptedCodeExecution(
236 "estimated middle size is too big for a key",
237 )));
238 }
239 Ok(Some(KeySize(KeyInfo::MaxKeySize {
240 unique_id: document_type
241 .unique_id_for_document_field(index_level, base_event)
242 .to_vec(),
243 max_size: estimated_middle_size as u8,
244 })))
245 }
246 }
247 }
248 }
249 }
250
251 fn get_borrowed_document_and_storage_flags(
253 &self,
254 ) -> Option<(&Document, Option<&StorageFlags>)> {
255 match self {
256 DocumentInfo::DocumentRefAndSerialization((document, _, storage_flags))
257 | DocumentInfo::DocumentRefInfo((document, storage_flags)) => {
258 Some((document, storage_flags.as_ref().map(|flags| flags.as_ref())))
259 }
260 DocumentInfo::DocumentOwnedInfo((document, storage_flags))
261 | DocumentInfo::DocumentAndSerialization((document, _, storage_flags)) => {
262 Some((document, storage_flags.as_ref().map(|flags| flags.as_ref())))
263 }
264 DocumentInfo::DocumentEstimatedAverageSize(_) => None,
265 }
266 }
267
268 fn get_storage_flags_ref(&self) -> Option<&StorageFlags> {
270 match self {
271 DocumentInfo::DocumentRefAndSerialization((_, _, storage_flags))
272 | DocumentInfo::DocumentRefInfo((_, storage_flags))
273 | DocumentInfo::DocumentOwnedInfo((_, storage_flags))
274 | DocumentInfo::DocumentAndSerialization((_, _, storage_flags)) => {
275 storage_flags.as_ref().map(|flags| flags.as_ref())
276 }
277 DocumentInfo::DocumentEstimatedAverageSize(_) => {
278 StorageFlags::optional_default_as_ref()
279 }
280 }
281 }
282
283 fn get_document_id_as_slice(&self) -> Option<&[u8]> {
285 match self {
286 DocumentInfo::DocumentRefAndSerialization((document, _, _))
287 | DocumentInfo::DocumentRefInfo((document, _)) => Some(document.id_ref().as_slice()),
288 DocumentInfo::DocumentOwnedInfo((document, _))
289 | DocumentInfo::DocumentAndSerialization((document, _, _)) => {
290 Some(document.id_ref().as_slice())
291 }
292 DocumentInfo::DocumentEstimatedAverageSize(_) => None,
293 }
294 }
295}