@peerbit/native-backbone 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,6 +16,7 @@ use crate::documents::{
16
16
  document_index_plain_put_payload_append_commit, DocumentIndexAppendCommit,
17
17
  DocumentIndexValuePrefix,
18
18
  };
19
+ use crate::error::BackboneError;
19
20
  use crate::js_interop::{
20
21
  ensure_same_len, optional_usize_from_js, required_bytes_from_array, strings_from_array,
21
22
  strings_to_array,
@@ -40,7 +41,7 @@ impl NativePeerbitBackbone {
40
41
  self_replicating: bool,
41
42
  resolve_trimmed_entries: bool,
42
43
  ) -> Result<Array, JsValue> {
43
- self.prepare_plain_storage_append_transaction_inner(
44
+ Ok(self.prepare_plain_storage_append_transaction_inner(
44
45
  wall_time,
45
46
  logical,
46
47
  gid,
@@ -57,7 +58,7 @@ impl NativePeerbitBackbone {
57
58
  None,
58
59
  true,
59
60
  None,
60
- )
61
+ )?)
61
62
  }
62
63
 
63
64
  #[allow(clippy::too_many_arguments)]
@@ -77,7 +78,7 @@ impl NativePeerbitBackbone {
77
78
  resolve_trimmed_entries: bool,
78
79
  trim_length_to: usize,
79
80
  ) -> Result<Array, JsValue> {
80
- self.prepare_plain_storage_append_transaction_inner(
81
+ Ok(self.prepare_plain_storage_append_transaction_inner(
81
82
  wall_time,
82
83
  logical,
83
84
  gid,
@@ -94,7 +95,7 @@ impl NativePeerbitBackbone {
94
95
  Some(trim_length_to),
95
96
  true,
96
97
  None,
97
- )
98
+ )?)
98
99
  }
99
100
 
100
101
  #[allow(clippy::too_many_arguments)]
@@ -111,26 +112,26 @@ impl NativePeerbitBackbone {
111
112
  ) -> Result<Array, JsValue> {
112
113
  let trim_length_to = optional_usize_from_js(trim_length_to, "trimLengthTo")?;
113
114
  let profile_enabled = self.append_profile_enabled;
114
- let input_copy_started = profile_enabled.then(js_sys::Date::now);
115
+ let input_copy_started = profile_enabled.then(crate::time::now_ms);
115
116
  let payload_template = payload_data.to_vec();
116
117
  if let Some(started) = input_copy_started {
117
- self.append_profile.input_copy_ms += js_sys::Date::now() - started;
118
+ self.append_profile.input_copy_ms += crate::time::now_ms() - started;
118
119
  }
119
120
  let payload_size = payload_template.len() as u32;
120
121
  let now = wall_time_start.to_string();
121
- let started = js_sys::Date::now();
122
+ let started = crate::time::now_ms();
122
123
  for i in 0..iterations {
123
124
  let wall_time = wall_time_start + i as u64;
124
125
  let logical = i;
125
126
  let gid = format!("native-backbone-loop-{wall_time_start}-{i}");
126
- let storage_append_started = profile_enabled.then(js_sys::Date::now);
127
- let payload_copy_started = profile_enabled.then(js_sys::Date::now);
127
+ let storage_append_started = profile_enabled.then(crate::time::now_ms);
128
+ let payload_copy_started = profile_enabled.then(crate::time::now_ms);
128
129
  let payload_data = payload_template.clone();
129
130
  if let Some(started) = payload_copy_started {
130
- self.append_profile.input_copy_ms += js_sys::Date::now() - started;
131
+ self.append_profile.input_copy_ms += crate::time::now_ms() - started;
131
132
  }
132
133
 
133
- let log_started = profile_enabled.then(js_sys::Date::now);
134
+ let log_started = profile_enabled.then(crate::time::now_ms);
134
135
  let mut log_profile = NativeLogAppendProfile::default();
135
136
  let (entry_facts, trim_hashes) = self
136
137
  .log
@@ -148,13 +149,13 @@ impl NativePeerbitBackbone {
148
149
  profile_enabled.then_some(&mut log_profile),
149
150
  )?;
150
151
  if let Some(started) = log_started {
151
- self.append_profile.log_total_ms += js_sys::Date::now() - started;
152
+ self.append_profile.log_total_ms += crate::time::now_ms() - started;
152
153
  self.append_profile.add_log_profile(&log_profile);
153
154
  }
154
155
 
155
156
  let hash_number = self.hash_number_profiled(&entry_facts.hash_digest_bytes)?;
156
157
 
157
- let coordinate_plan_started = profile_enabled.then(js_sys::Date::now);
158
+ let coordinate_plan_started = profile_enabled.then(crate::time::now_ms);
158
159
  let coordinate_facts = commit_local_append_for_gid_compact_core(
159
160
  &mut self.shared_log,
160
161
  entry_facts.hash.clone(),
@@ -171,10 +172,10 @@ impl NativePeerbitBackbone {
171
172
  true,
172
173
  )?;
173
174
  if let Some(started) = coordinate_plan_started {
174
- self.append_profile.coordinate_plan_ms += js_sys::Date::now() - started;
175
+ self.append_profile.coordinate_plan_ms += crate::time::now_ms() - started;
175
176
  }
176
177
 
177
- let coordinate_core_started = profile_enabled.then(js_sys::Date::now);
178
+ let coordinate_core_started = profile_enabled.then(crate::time::now_ms);
178
179
  self.commit_coordinate_core_from_compact_facts(
179
180
  &coordinate_facts,
180
181
  &[],
@@ -183,10 +184,10 @@ impl NativePeerbitBackbone {
183
184
  entry_facts.meta_bytes.clone(),
184
185
  );
185
186
  if let Some(started) = coordinate_core_started {
186
- self.append_profile.coordinate_core_ms += js_sys::Date::now() - started;
187
+ self.append_profile.coordinate_core_ms += crate::time::now_ms() - started;
187
188
  }
188
189
 
189
- let document_index_started = profile_enabled.then(js_sys::Date::now);
190
+ let document_index_started = profile_enabled.then(crate::time::now_ms);
190
191
  let document_index_commit = use_document_index.then(|| DocumentIndexAppendCommit {
191
192
  key: format!("native-backbone-loop-doc-{wall_time_start}-{i}"),
192
193
  value_prefix: DocumentIndexValuePrefix::Bytes(Vec::new()),
@@ -205,15 +206,15 @@ impl NativePeerbitBackbone {
205
206
  payload_size,
206
207
  )?;
207
208
  if let Some(started) = document_index_started {
208
- self.append_profile.document_index_commit_ms += js_sys::Date::now() - started;
209
+ self.append_profile.document_index_commit_ms += crate::time::now_ms() - started;
209
210
  }
210
211
 
211
212
  if let Some(started) = storage_append_started {
212
- self.append_profile.storage_append_inner_ms += js_sys::Date::now() - started;
213
+ self.append_profile.storage_append_inner_ms += crate::time::now_ms() - started;
213
214
  }
214
215
  }
215
216
  let row = Array::new();
216
- row.push(&JsValue::from_f64(js_sys::Date::now() - started));
217
+ row.push(&JsValue::from_f64(crate::time::now_ms() - started));
217
218
  row.push(&JsValue::from_f64(self.log.len() as f64));
218
219
  row.push(&JsValue::from_f64(self.blocks.len() as f64));
219
220
  row.push(&JsValue::from_f64(self.coordinate_index.len() as f64));
@@ -245,7 +246,7 @@ impl NativePeerbitBackbone {
245
246
  document_projection_encoded_document: JsValue,
246
247
  document_projection_signer: JsValue,
247
248
  ) -> Result<Array, JsValue> {
248
- self.prepare_plain_storage_append_transaction_inner(
249
+ Ok(self.prepare_plain_storage_append_transaction_inner(
249
250
  wall_time,
250
251
  logical,
251
252
  gid,
@@ -271,7 +272,7 @@ impl NativePeerbitBackbone {
271
272
  document_projection_encoded_document,
272
273
  document_projection_signer,
273
274
  )?),
274
- )
275
+ )?)
275
276
  }
276
277
 
277
278
  #[allow(clippy::too_many_arguments)]
@@ -299,7 +300,7 @@ impl NativePeerbitBackbone {
299
300
  document_projection_signer: JsValue,
300
301
  trim_length_to: usize,
301
302
  ) -> Result<Array, JsValue> {
302
- self.prepare_plain_storage_append_transaction_inner(
303
+ Ok(self.prepare_plain_storage_append_transaction_inner(
303
304
  wall_time,
304
305
  logical,
305
306
  gid,
@@ -325,7 +326,7 @@ impl NativePeerbitBackbone {
325
326
  document_projection_encoded_document,
326
327
  document_projection_signer,
327
328
  )?),
328
- )
329
+ )?)
329
330
  }
330
331
 
331
332
  #[allow(clippy::too_many_arguments)]
@@ -351,7 +352,7 @@ impl NativePeerbitBackbone {
351
352
  document_projection_encoded_document: JsValue,
352
353
  document_projection_signer: JsValue,
353
354
  ) -> Result<Array, JsValue> {
354
- self.prepare_plain_storage_append_transaction_inner(
355
+ Ok(self.prepare_plain_storage_append_transaction_inner(
355
356
  wall_time,
356
357
  logical,
357
358
  gid,
@@ -376,7 +377,7 @@ impl NativePeerbitBackbone {
376
377
  document_projection_encoded_document,
377
378
  document_projection_signer,
378
379
  )?),
379
- )
380
+ )?)
380
381
  }
381
382
 
382
383
  #[allow(clippy::too_many_arguments)]
@@ -403,7 +404,7 @@ impl NativePeerbitBackbone {
403
404
  document_projection_signer: JsValue,
404
405
  trim_length_to: usize,
405
406
  ) -> Result<Array, JsValue> {
406
- self.prepare_plain_storage_append_transaction_inner(
407
+ Ok(self.prepare_plain_storage_append_transaction_inner(
407
408
  wall_time,
408
409
  logical,
409
410
  gid,
@@ -428,7 +429,7 @@ impl NativePeerbitBackbone {
428
429
  document_projection_encoded_document,
429
430
  document_projection_signer,
430
431
  )?),
431
- )
432
+ )?)
432
433
  }
433
434
 
434
435
  #[allow(clippy::too_many_arguments)]
@@ -463,7 +464,7 @@ impl NativePeerbitBackbone {
463
464
  JsValue::UNDEFINED,
464
465
  JsValue::UNDEFINED,
465
466
  )?;
466
- self.prepare_plain_committed_no_next_storage_append_document_index_compact_transaction_inner(
467
+ Ok(self.prepare_plain_committed_no_next_storage_append_document_index_compact_transaction_inner(
467
468
  wall_time,
468
469
  logical,
469
470
  gid,
@@ -477,7 +478,7 @@ impl NativePeerbitBackbone {
477
478
  self_replicating,
478
479
  document_index_commit,
479
480
  trim_length_to,
480
- )
481
+ )?)
481
482
  }
482
483
 
483
484
  #[allow(clippy::too_many_arguments)]
@@ -507,7 +508,7 @@ impl NativePeerbitBackbone {
507
508
  document_byte_element_index_limit,
508
509
  document_delete_trimmed_heads,
509
510
  )?;
510
- self.prepare_plain_committed_no_next_storage_append_document_index_compact_transaction_inner(
511
+ Ok(self.prepare_plain_committed_no_next_storage_append_document_index_compact_transaction_inner(
511
512
  wall_time,
512
513
  logical,
513
514
  gid,
@@ -521,7 +522,7 @@ impl NativePeerbitBackbone {
521
522
  self_replicating,
522
523
  document_index_commit,
523
524
  trim_length_to,
524
- )
525
+ )?)
525
526
  }
526
527
 
527
528
  #[allow(clippy::too_many_arguments)]
@@ -587,7 +588,7 @@ impl NativePeerbitBackbone {
587
588
  JsValue::UNDEFINED,
588
589
  )?);
589
590
  }
590
- self.prepare_plain_committed_no_next_storage_append_document_index_compact_batch_transaction_inner(
591
+ Ok(self.prepare_plain_committed_no_next_storage_append_document_index_compact_batch_transaction_inner(
591
592
  wall_times,
592
593
  logicals,
593
594
  gids,
@@ -601,7 +602,7 @@ impl NativePeerbitBackbone {
601
602
  self_replicating,
602
603
  trim_length_to,
603
604
  document_index_commits,
604
- )
605
+ )?)
605
606
  }
606
607
 
607
608
  #[allow(clippy::too_many_arguments)]
@@ -651,7 +652,7 @@ impl NativePeerbitBackbone {
651
652
  document_delete_trimmed_heads,
652
653
  )?);
653
654
  }
654
- self.prepare_plain_committed_no_next_storage_append_document_index_compact_batch_transaction_inner(
655
+ Ok(self.prepare_plain_committed_no_next_storage_append_document_index_compact_batch_transaction_inner(
655
656
  wall_times,
656
657
  logicals,
657
658
  gids,
@@ -665,7 +666,7 @@ impl NativePeerbitBackbone {
665
666
  self_replicating,
666
667
  trim_length_to,
667
668
  document_index_commits,
668
- )
669
+ )?)
669
670
  }
670
671
 
671
672
  #[allow(clippy::too_many_arguments)]
@@ -732,7 +733,7 @@ impl NativePeerbitBackbone {
732
733
  document_projection_signers.get(index_u32),
733
734
  )?);
734
735
  }
735
- self.prepare_plain_committed_no_next_storage_append_document_index_compact_batch_transaction_inner(
736
+ Ok(self.prepare_plain_committed_no_next_storage_append_document_index_compact_batch_transaction_inner(
736
737
  wall_times,
737
738
  logicals,
738
739
  gids,
@@ -746,7 +747,7 @@ impl NativePeerbitBackbone {
746
747
  self_replicating,
747
748
  trim_length_to,
748
749
  document_index_commits,
749
- )
750
+ )?)
750
751
  }
751
752
 
752
753
  #[allow(clippy::too_many_arguments)]
@@ -809,7 +810,7 @@ impl NativePeerbitBackbone {
809
810
  )?,
810
811
  );
811
812
  }
812
- self.prepare_plain_committed_no_next_storage_append_document_index_compact_batch_transaction_inner(
813
+ Ok(self.prepare_plain_committed_no_next_storage_append_document_index_compact_batch_transaction_inner(
813
814
  wall_times,
814
815
  logicals,
815
816
  gids,
@@ -823,7 +824,7 @@ impl NativePeerbitBackbone {
823
824
  self_replicating,
824
825
  trim_length_to,
825
826
  document_index_commits,
826
- )
827
+ )?)
827
828
  }
828
829
 
829
830
  #[allow(clippy::too_many_arguments)]
@@ -859,7 +860,7 @@ impl NativePeerbitBackbone {
859
860
  document_projection_encoded_document,
860
861
  document_projection_signer,
861
862
  )?;
862
- self.prepare_plain_committed_no_next_storage_append_document_index_compact_transaction_inner(
863
+ Ok(self.prepare_plain_committed_no_next_storage_append_document_index_compact_transaction_inner(
863
864
  wall_time,
864
865
  logical,
865
866
  gid,
@@ -872,8 +873,8 @@ impl NativePeerbitBackbone {
872
873
  self_hash,
873
874
  self_replicating,
874
875
  document_index_commit,
875
- trim_length_to,
876
- )
876
+ trim_length_to,
877
+ )?)
877
878
  }
878
879
 
879
880
  #[allow(clippy::too_many_arguments)]
@@ -908,7 +909,7 @@ impl NativePeerbitBackbone {
908
909
  document_projection_plan_id,
909
910
  document_projection_signer,
910
911
  )?;
911
- self.prepare_plain_committed_no_next_storage_append_document_index_compact_transaction_inner(
912
+ Ok(self.prepare_plain_committed_no_next_storage_append_document_index_compact_transaction_inner(
912
913
  wall_time,
913
914
  logical,
914
915
  gid,
@@ -922,7 +923,7 @@ impl NativePeerbitBackbone {
922
923
  self_replicating,
923
924
  document_index_commit,
924
925
  trim_length_to,
925
- )
926
+ )?)
926
927
  }
927
928
  }
928
929
 
@@ -943,9 +944,9 @@ impl NativePeerbitBackbone {
943
944
  self_replicating: bool,
944
945
  trim_length_to: Option<usize>,
945
946
  document_index_commits: Vec<DocumentIndexAppendCommit>,
946
- ) -> Result<Array, JsValue> {
947
+ ) -> Result<Array, BackboneError> {
947
948
  let profile_enabled = self.append_profile_enabled;
948
- let storage_append_started = profile_enabled.then(js_sys::Date::now);
949
+ let storage_append_started = profile_enabled.then(crate::time::now_ms);
949
950
  let batch_len = document_index_commits.len();
950
951
  self.reserve_document_batch(batch_len);
951
952
  let mut pending_appends = Vec::with_capacity(batch_len);
@@ -956,13 +957,13 @@ impl NativePeerbitBackbone {
956
957
  let gid = gids
957
958
  .get(index_u32)
958
959
  .as_string()
959
- .ok_or_else(|| JsValue::from_str("Expected batch gid string"))?;
960
+ .ok_or(BackboneError::ExpectedString("batch gid"))?;
960
961
  let payload_bytes = required_bytes_from_array(&payload_datas, index_u32, "payload")?;
961
962
  let payload_size = payload_bytes.length();
962
963
  let delete_trimmed_document_heads = document_index_commit.delete_trimmed_heads;
963
964
 
964
965
  let (meta_data, payload_data) =
965
- self.copy_append_inputs_profiled(meta_datas.get(index_u32), &payload_bytes);
966
+ self.copy_append_inputs_profiled(meta_datas.get(index_u32), &payload_bytes)?;
966
967
 
967
968
  let (entry_facts, trim_hashes) = self.prepare_no_next_log_append_profiled(
968
969
  wall_times.get_index(index_u32),
@@ -1020,7 +1021,7 @@ impl NativePeerbitBackbone {
1020
1021
 
1021
1022
  let out = Array::new();
1022
1023
  for (pending, coordinate_facts) in pending_appends.into_iter().zip(coordinate_facts) {
1023
- let coordinate_core_started = profile_enabled.then(js_sys::Date::now);
1024
+ let coordinate_core_started = profile_enabled.then(crate::time::now_ms);
1024
1025
  self.commit_coordinate_core_from_compact_facts(
1025
1026
  &coordinate_facts,
1026
1027
  &[],
@@ -1029,7 +1030,7 @@ impl NativePeerbitBackbone {
1029
1030
  pending.entry_facts.meta_bytes.clone(),
1030
1031
  );
1031
1032
  if let Some(started) = coordinate_core_started {
1032
- self.append_profile.coordinate_core_ms += js_sys::Date::now() - started;
1033
+ self.append_profile.coordinate_core_ms += crate::time::now_ms() - started;
1033
1034
  }
1034
1035
 
1035
1036
  let document_trimmed_heads_processed = self.commit_append_document_index_profiled(
@@ -1043,7 +1044,7 @@ impl NativePeerbitBackbone {
1043
1044
  &pending.trim_hashes,
1044
1045
  )?;
1045
1046
 
1046
- let result_row_started = profile_enabled.then(js_sys::Date::now);
1047
+ let result_row_started = profile_enabled.then(crate::time::now_ms);
1047
1048
  let row = no_next_compact_entry_row(
1048
1049
  &self.resolution,
1049
1050
  &pending.entry_facts,
@@ -1062,12 +1063,12 @@ impl NativePeerbitBackbone {
1062
1063
  }
1063
1064
  out.push(&row);
1064
1065
  if let Some(started) = result_row_started {
1065
- self.append_profile.result_row_ms += js_sys::Date::now() - started;
1066
+ self.append_profile.result_row_ms += crate::time::now_ms() - started;
1066
1067
  }
1067
1068
  }
1068
1069
 
1069
1070
  if let Some(started) = storage_append_started {
1070
- self.append_profile.storage_append_inner_ms += js_sys::Date::now() - started;
1071
+ self.append_profile.storage_append_inner_ms += crate::time::now_ms() - started;
1071
1072
  }
1072
1073
  Ok(out)
1073
1074
  }
@@ -1088,13 +1089,14 @@ impl NativePeerbitBackbone {
1088
1089
  self_replicating: bool,
1089
1090
  document_index_commit: DocumentIndexAppendCommit,
1090
1091
  trim_length_to: Option<usize>,
1091
- ) -> Result<Array, JsValue> {
1092
+ ) -> Result<Array, BackboneError> {
1092
1093
  let profile_enabled = self.append_profile_enabled;
1093
- let storage_append_started = profile_enabled.then(js_sys::Date::now);
1094
+ let storage_append_started = profile_enabled.then(crate::time::now_ms);
1094
1095
  let payload_size = payload_data.length();
1095
1096
  let delete_trimmed_document_heads = document_index_commit.delete_trimmed_heads;
1096
1097
 
1097
- let (meta_data, payload_data) = self.copy_append_inputs_profiled(meta_data, &payload_data);
1098
+ let (meta_data, payload_data) =
1099
+ self.copy_append_inputs_profiled(meta_data, &payload_data)?;
1098
1100
 
1099
1101
  let (entry_facts, trim_hashes) = self.prepare_no_next_log_append_profiled(
1100
1102
  wall_time,
@@ -1108,7 +1110,7 @@ impl NativePeerbitBackbone {
1108
1110
 
1109
1111
  let hash_number = self.hash_number_profiled(&entry_facts.hash_digest_bytes)?;
1110
1112
 
1111
- let coordinate_plan_started = profile_enabled.then(js_sys::Date::now);
1113
+ let coordinate_plan_started = profile_enabled.then(crate::time::now_ms);
1112
1114
  let coordinate_facts = commit_local_append_for_gid_compact_core(
1113
1115
  &mut self.shared_log,
1114
1116
  entry_facts.hash.clone(),
@@ -1125,10 +1127,10 @@ impl NativePeerbitBackbone {
1125
1127
  true,
1126
1128
  )?;
1127
1129
  if let Some(started) = coordinate_plan_started {
1128
- self.append_profile.coordinate_plan_ms += js_sys::Date::now() - started;
1130
+ self.append_profile.coordinate_plan_ms += crate::time::now_ms() - started;
1129
1131
  }
1130
1132
 
1131
- let coordinate_core_started = profile_enabled.then(js_sys::Date::now);
1133
+ let coordinate_core_started = profile_enabled.then(crate::time::now_ms);
1132
1134
  self.commit_coordinate_core_from_compact_facts(
1133
1135
  &coordinate_facts,
1134
1136
  &[],
@@ -1137,7 +1139,7 @@ impl NativePeerbitBackbone {
1137
1139
  entry_facts.meta_bytes.clone(),
1138
1140
  );
1139
1141
  if let Some(started) = coordinate_core_started {
1140
- self.append_profile.coordinate_core_ms += js_sys::Date::now() - started;
1142
+ self.append_profile.coordinate_core_ms += crate::time::now_ms() - started;
1141
1143
  }
1142
1144
 
1143
1145
  let document_trimmed_heads_processed = self.commit_append_document_index_profiled(
@@ -1151,14 +1153,14 @@ impl NativePeerbitBackbone {
1151
1153
  &trim_hashes,
1152
1154
  )?;
1153
1155
 
1154
- let result_row_started = profile_enabled.then(js_sys::Date::now);
1156
+ let result_row_started = profile_enabled.then(crate::time::now_ms);
1155
1157
  let out = no_next_compact_entry_row(&self.resolution, &entry_facts, &coordinate_facts);
1156
1158
  push_optional_trim_result(&out, trim_hashes, document_trimmed_heads_processed);
1157
1159
  if let Some(started) = result_row_started {
1158
- self.append_profile.result_row_ms += js_sys::Date::now() - started;
1160
+ self.append_profile.result_row_ms += crate::time::now_ms() - started;
1159
1161
  }
1160
1162
  if let Some(started) = storage_append_started {
1161
- self.append_profile.storage_append_inner_ms += js_sys::Date::now() - started;
1163
+ self.append_profile.storage_append_inner_ms += crate::time::now_ms() - started;
1162
1164
  }
1163
1165
  Ok(out)
1164
1166
  }