1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
use super::{HistoricalAccess, HistoricalCommitAccess, TreeConfigSaver, VersionedKvStore};
use crate::config::TreeConfig;
use crate::diff::{ConflictResolver, IgnoreConflictsResolver};
use crate::digest::ValueDigest;
use crate::git::metadata::{GitMetadataBackend, MetadataBackend};
use crate::git::types::*;
use crate::storage::{FileNodeStorage, GitNodeStorage, InMemoryNodeStorage};
use crate::tree::{ProllyTree, Tree};
use gix::prelude::*;
use std::collections::HashMap;
use std::path::Path;
#[cfg(feature = "rocksdb_storage")]
use crate::storage::RocksDBNodeStorage;
// Implement TreeConfigSaver for GitNodeStorage
impl<const N: usize> TreeConfigSaver<N>
for VersionedKvStore<N, GitNodeStorage<N>, GitMetadataBackend>
{
fn save_tree_config_to_git_internal(&self) -> Result<(), GitKvError> {
self.save_tree_config_to_git()
}
}
// Storage-specific implementations
impl<const N: usize> VersionedKvStore<N, GitNodeStorage<N>, GitMetadataBackend> {
/// Get access to the git repository (for internal use and backward compatibility)
pub fn git_repo(&self) -> &gix::Repository {
self.metadata.repo()
}
/// Save both tree config and hash mappings to git for GitNodeStorage
fn save_tree_config_to_git(&self) -> Result<(), GitKvError> {
// For GitNodeStorage, we need to ensure the config and hash mappings are
// available in the dataset directory so they can be committed to git
// Get the current tree configuration
let config = self.tree.config.clone();
// Serialize the configuration to JSON
let config_json = serde_json::to_string_pretty(&config)
.map_err(|e| GitKvError::GitObjectError(format!("Failed to serialize config: {e}")))?;
// Write config to the dataset directory
let config_path = self
.tree
.storage
.dataset_dir()
.join("prolly_config_tree_config");
std::fs::write(&config_path, config_json)
.map_err(|e| GitKvError::GitObjectError(format!("Failed to write config file: {e}")))?;
// Get hash mappings from storage and save them
let mappings = self.tree.storage.get_hash_mappings();
let mut mappings_content = String::new();
for (hash, object_id) in mappings {
// Convert hash bytes to hex manually
let hash_hex: String = hash.as_bytes().iter().map(|b| format!("{b:02x}")).collect();
mappings_content.push_str(&format!("{hash_hex}:{object_id}\n"));
}
// Write mappings to the dataset directory
let mappings_path = self.tree.storage.dataset_dir().join("prolly_hash_mappings");
std::fs::write(&mappings_path, mappings_content).map_err(|e| {
GitKvError::GitObjectError(format!("Failed to write mappings file: {e}"))
})?;
Ok(())
}
/// Git-specific checkout that reloads tree state from the target commit
pub fn checkout(&mut self, branch_or_commit: &str) -> Result<(), GitKvError> {
// Call the generic checkout to handle HEAD reference update
// Clear staging area
self.staging_area.clear();
self.save_staging_area()?;
// Update HEAD to point to the new branch/commit
let target_ref = if branch_or_commit.starts_with("refs/") {
branch_or_commit.to_string()
} else {
format!("refs/heads/{branch_or_commit}")
};
// Check if the reference exists
match self.metadata.repo().refs.find(&target_ref) {
Ok(_reference) => {
// Update our internal tracking
self.current_branch = branch_or_commit.to_string();
// Update HEAD to point to the resolved reference
let head_file = self.metadata.repo().path().join("HEAD");
let head_content = format!("ref: {target_ref}\n");
std::fs::write(&head_file, head_content).map_err(|e| {
GitKvError::GitObjectError(format!("Failed to update HEAD: {e}"))
})?;
}
Err(_) => {
return Err(GitKvError::BranchNotFound(branch_or_commit.to_string()));
}
}
// Git-specific: Reload the tree from the HEAD commit of the target branch
self.reload_tree_from_head()?;
Ok(())
}
/// Merge another branch into the current branch with configurable conflict resolution
///
/// This method performs a three-way merge using the ProllyTree merge functionality.
/// It merges changes from the source branch into the current (destination) branch.
///
/// # Arguments
///
/// * `source_branch` - The branch to merge from
/// * `resolver` - Conflict resolver to handle merge conflicts
///
/// # Returns
///
/// Returns Ok(commit_id) if merge succeeded, or Err with unresolved conflicts
pub fn merge<R: ConflictResolver>(
&mut self,
source_branch: &str,
resolver: &R,
) -> Result<gix::ObjectId, GitKvError> {
// Get current branch name
let dest_branch = self.current_branch.clone();
// Find common base commit
let base_commit = self.find_merge_base(&dest_branch, source_branch)?;
// Note: We don't need the tree root hashes for key-value level merge
// let base_root = self.get_tree_root_at_commit(&base_commit)?;
// let source_root = self.get_tree_root_at_branch(source_branch)?;
// let dest_root = self.tree.get_root_hash().unwrap(); // Current tree
// Perform the merge at the key-value level instead of tree level
// Get the actual key-value data from each branch
let base_kv = self.collect_keys_at_commit(&base_commit)?;
let source_kv = self.collect_keys_at_commit(&self.get_branch_commit(source_branch)?)?;
let mut dest_kv = HashMap::new();
// Collect current tree data
for key in self.tree.collect_keys() {
if let Some(value) = self.get(&key) {
dest_kv.insert(key, value);
}
}
// Perform three-way merge at key-value level
let mut merge_results = Vec::new();
let mut all_keys = std::collections::HashSet::new();
// Collect all keys from all three states
all_keys.extend(base_kv.keys().cloned());
all_keys.extend(source_kv.keys().cloned());
all_keys.extend(dest_kv.keys().cloned());
for key in all_keys {
let base_value = base_kv.get(&key);
let source_value = source_kv.get(&key);
let dest_value = dest_kv.get(&key);
match (base_value, source_value, dest_value) {
// Key exists in all three - check for modifications
(Some(base), Some(source), Some(dest)) => {
if base == source && base == dest {
// No changes, skip
continue;
} else if base == dest && base != source {
// Only source changed - take source value
merge_results.push(crate::diff::MergeResult::Modified(key, source.clone()));
} else if base == source && base != dest {
// Only dest changed - keep dest value (no-op)
continue;
} else if source == dest {
// Both changed to same value - keep it (no-op)
continue;
} else {
// Conflict: both branches modified differently
let conflict = crate::diff::MergeConflict {
key: key.clone(),
base_value: Some(base.clone()),
source_value: Some(source.clone()),
destination_value: Some(dest.clone()),
};
merge_results.push(crate::diff::MergeResult::Conflict(conflict));
}
}
// Key added in source, not in base or dest
(None, Some(source), None) => {
merge_results.push(crate::diff::MergeResult::Added(key, source.clone()));
}
// Key added in dest, not in base or source - keep it (no-op)
(None, None, Some(_dest)) => {
continue;
}
// Key added in both source and dest - potential conflict
(None, Some(source), Some(dest)) => {
if source == dest {
// Both added same value - keep it (no-op)
continue;
} else {
// Conflict: both branches added different values
let conflict = crate::diff::MergeConflict {
key: key.clone(),
base_value: None,
source_value: Some(source.clone()),
destination_value: Some(dest.clone()),
};
merge_results.push(crate::diff::MergeResult::Conflict(conflict));
}
}
// Key deleted in source, still exists in dest
(Some(_base), None, Some(_dest)) => {
// Source deleted it - apply deletion
merge_results.push(crate::diff::MergeResult::Removed(key));
}
// Key deleted in dest, still exists in source - keep deletion (no-op)
(Some(_base), Some(_source), None) => {
continue;
}
// Key deleted in both - no-op
(Some(_base), None, None) => {
continue;
}
// All other cases - no action needed
_ => continue,
}
}
// Apply conflict resolution
let mut resolved_results = Vec::new();
let mut unresolved_conflicts = Vec::new();
for result in merge_results {
match result {
crate::diff::MergeResult::Conflict(conflict) => {
if let Some(resolved_result) = resolver.resolve_conflict(&conflict) {
resolved_results.push(resolved_result);
} else {
unresolved_conflicts.push(conflict);
}
}
other => resolved_results.push(other),
}
}
// If there are unresolved conflicts, return them
if !unresolved_conflicts.is_empty() {
return Err(GitKvError::MergeConflictError(unresolved_conflicts));
}
// Apply resolved merge results directly to current tree
for result in resolved_results {
match result {
crate::diff::MergeResult::Added(key, value) => {
self.tree.insert(key, value);
}
crate::diff::MergeResult::Modified(key, value) => {
self.tree.insert(key, value); // insert overwrites existing
}
crate::diff::MergeResult::Removed(key) => {
self.tree.delete(&key);
}
crate::diff::MergeResult::Conflict(_) => {
// Should not happen since we handled conflicts above
unreachable!("Conflicts should have been resolved");
}
}
}
// Clear staging area since we've applied changes directly to tree
self.staging_area.clear();
self.save_staging_area()?;
// Create merge commit
let message = format!("Merge branch '{source_branch}' into '{dest_branch}'");
let merge_commit_id = self.create_merge_commit(&message, source_branch)?;
Ok(merge_commit_id)
}
/// Convenience method to merge with default IgnoreConflictsResolver
pub fn merge_ignore_conflicts(
&mut self,
source_branch: &str,
) -> Result<gix::ObjectId, GitKvError> {
self.merge(source_branch, &IgnoreConflictsResolver)
}
/// Find the merge base (common ancestor) of two branches
fn find_merge_base(&self, branch1: &str, branch2: &str) -> Result<gix::ObjectId, GitKvError> {
// Get commit IDs for both branches
let commit1 = self.get_branch_commit(branch1)?;
let commit2 = self.get_branch_commit(branch2)?;
// For now, use a simple approach: find common ancestor by walking history
// This is a simplified implementation - a real merge-base algorithm would be more sophisticated
let mut visited1 = std::collections::HashSet::new();
let mut queue1 = std::collections::VecDeque::new();
queue1.push_back(commit1);
// Collect all ancestors of branch1
while let Some(commit_id) = queue1.pop_front() {
if visited1.contains(&commit_id) {
continue;
}
visited1.insert(commit_id);
// Add parents
if let Ok(parents) = self.get_commit_parents(&commit_id) {
for parent in parents {
if !visited1.contains(&parent) {
queue1.push_back(parent);
}
}
}
}
// Walk branch2 and find first common ancestor
let mut visited2 = std::collections::HashSet::new();
let mut queue2 = std::collections::VecDeque::new();
queue2.push_back(commit2);
while let Some(commit_id) = queue2.pop_front() {
if visited2.contains(&commit_id) {
continue;
}
visited2.insert(commit_id);
// Check if this commit is in branch1's ancestors
if visited1.contains(&commit_id) {
return Ok(commit_id);
}
// Add parents
if let Ok(parents) = self.get_commit_parents(&commit_id) {
for parent in parents {
if !visited2.contains(&parent) {
queue2.push_back(parent);
}
}
}
}
Err(GitKvError::GitObjectError(
"No common ancestor found".to_string(),
))
}
/// Get commit ID for a branch
fn get_branch_commit(&self, branch: &str) -> Result<gix::ObjectId, GitKvError> {
let branch_ref = format!("refs/heads/{branch}");
match self.metadata.repo().refs.find(&branch_ref) {
Ok(reference) => match reference.target.try_id() {
Some(commit_id) => Ok(commit_id.to_owned()),
None => Err(GitKvError::GitObjectError(format!(
"Branch {branch} does not point to a commit"
))),
},
Err(_) => Err(GitKvError::BranchNotFound(branch.to_string())),
}
}
/// Get parents of a commit
fn get_commit_parents(
&self,
commit_id: &gix::ObjectId,
) -> Result<Vec<gix::ObjectId>, GitKvError> {
let mut buffer = Vec::new();
let commit_obj = self
.metadata
.repo()
.objects
.find(commit_id, &mut buffer)
.map_err(|e| GitKvError::GitObjectError(format!("Failed to find commit: {e}")))?;
let parents = match commit_obj.decode() {
Ok(gix::objs::ObjectRef::Commit(commit)) => commit.parents().collect(),
_ => {
return Err(GitKvError::GitObjectError(
"Object is not a commit".to_string(),
))
}
};
Ok(parents)
}
/// Create a merge commit with two parents (current HEAD + source branch)
pub(crate) fn create_merge_commit(
&mut self,
message: &str,
source_branch: &str,
) -> Result<gix::ObjectId, GitKvError> {
// Get both parent commits
let current_commit = self.metadata.head_commit_id()?;
let source_commit = self.get_branch_commit(source_branch)?;
// Save current tree state
self.tree.persist_root();
self.tree
.save_config()
.map_err(|e| GitKvError::GitObjectError(format!("Failed to save config: {e}")))?;
// Save tree config to git
self.save_tree_config_to_git_internal()?;
// Stage files and write tree via metadata backend
let dataset_dir = self
.dataset_dir
.as_ref()
.ok_or_else(|| GitKvError::GitObjectError("Dataset directory not set".into()))?;
let git_root = self
.metadata
.work_dir()
.or_else(|| Self::find_git_root(dataset_dir))
.ok_or_else(|| GitKvError::GitObjectError("Could not find git root".into()))?;
let tree_id = self.metadata.stage_and_write_tree(&git_root)?;
// Create merge commit with two parents using gix (bypasses shell hooks)
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_err(|e| GitKvError::GitObjectError(format!("System time error: {e}")))?
.as_secs() as i64;
let (name, email) = self.metadata.user_config();
let signature = gix::actor::Signature {
name: name.into(),
email: email.into(),
time: gix::date::Time {
seconds: now,
offset: 0,
sign: gix::date::time::Sign::Plus,
},
};
let commit = gix::objs::Commit {
tree: tree_id,
parents: vec![current_commit, source_commit].into(),
author: signature.clone(),
committer: signature,
encoding: None,
message: message.as_bytes().into(),
extra_headers: vec![],
};
let commit_id = self
.metadata
.repo()
.objects
.write(&commit)
.map_err(|e| GitKvError::GitObjectError(format!("Failed to write commit: {e}")))?;
// Update branch ref and HEAD
self.metadata
.update_branch(&self.current_branch, commit_id)?;
self.metadata.update_head(&self.current_branch)?;
Ok(commit_id)
}
/// Reload the tree state from the current HEAD commit
fn reload_tree_from_head(&mut self) -> Result<(), GitKvError> {
// Get the current HEAD commit
let head = self
.metadata
.repo()
.head()
.map_err(|e| GitKvError::GitObjectError(format!("Failed to get HEAD: {e}")))?;
let head_commit_id = head.id().ok_or_else(|| {
GitKvError::GitObjectError("HEAD does not point to a commit".to_string())
})?;
// Convert gix::Id to gix::ObjectId
let head_object_id = head_commit_id.detach();
// Load all key-value pairs from the HEAD commit
let keys_at_head = self.collect_keys_at_commit(&head_object_id)?;
// Clear the current tree and rebuild it with the data from HEAD
// Important: Create tree with config that already has the correct root hash to avoid triggering persist_root
let mut config = self.tree.config.clone();
// Get the config from the commit to get the correct root hash
if let Ok(commit_config) = self.read_tree_config_from_commit(&head_object_id) {
config.root_hash = commit_config.root_hash;
}
self.tree = ProllyTree::new(self.tree.storage.clone(), config);
// Insert all the key-value pairs from the HEAD commit
// Note: These insertions may still trigger auto-save in the tree
for (key, value) in keys_at_head {
self.tree.insert(key, value);
}
// Note: We don't save_config() or persist_root() here because this is a read operation.
// The config files should only be saved during write operations (commit).
// Saving here would overwrite the existing files with potentially stale data.
Ok(())
}
/// Initialize a new versioned KV store with Git storage (default)
pub fn init<P: AsRef<Path>>(path: P) -> Result<Self, GitKvError> {
let path = path.as_ref();
// Safety check: prevent initializing at git root to avoid `git add -A .` staging all files
if Self::is_in_git_root(path)? {
return Err(GitKvError::GitObjectError(
"Cannot initialize git-prolly in git root directory. \
Please use a subdirectory to create a dataset, or the commit operation \
may accidentally stage all files in the repository."
.to_string(),
));
}
// Find the git repository
let git_root = Self::find_git_root(path).ok_or_else(|| {
GitKvError::GitObjectError(
"Not inside a git repository. Please run from within a git repository.".to_string(),
)
})?;
// For GitVersionedKvStore, use the user-provided path as the dataset directory
// This allows config files to be versioned in git commits
let dataset_dir = path.to_path_buf();
std::fs::create_dir_all(&dataset_dir).map_err(|e| {
GitKvError::GitObjectError(format!("Failed to create dataset directory: {e}"))
})?;
// Check if the store is already initialized by looking for config files
let config_path = dataset_dir.join("prolly_config_tree_config");
let mappings_path = dataset_dir.join("prolly_hash_mappings");
if config_path.exists() || mappings_path.exists() {
// Store already exists, use open instead to load existing configuration
return Self::open(path);
}
// Open the existing git repository
let git_repo = gix::open(&git_root).map_err(|e| GitKvError::GitOpenError(Box::new(e)))?;
// Create GitNodeStorage with user-provided path for versioned files
let storage = GitNodeStorage::new(git_repo.clone(), dataset_dir.clone())?;
// Create ProllyTree with default config
let config: TreeConfig<N> = TreeConfig::default();
let tree = ProllyTree::new(storage, config);
let mut store = VersionedKvStore {
tree,
metadata: GitMetadataBackend::new(git_repo),
staging_area: HashMap::new(),
current_branch: "main".to_string(),
storage_backend: StorageBackend::Git,
dataset_dir: Some(dataset_dir),
};
// Save initial configuration
let _ = store.tree.save_config();
// Create initial commit (which will include prolly metadata files)
store.commit("Initial commit")?;
Ok(store)
}
/// Open an existing versioned KV store with Git storage (default)
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, GitKvError> {
let path = path.as_ref();
// Safety check: prevent opening at git root to avoid `git add -A .` staging all files
if Self::is_in_git_root(path)? {
return Err(GitKvError::GitObjectError(
"Cannot open git-prolly in git root directory. \
Please use a subdirectory for your dataset, or the commit operation \
may accidentally stage all files in the repository."
.to_string(),
));
}
// Find the git repository
let git_root = Self::find_git_root(path).ok_or_else(|| {
GitKvError::GitObjectError(
"Not inside a git repository. Please run from within a git repository.".to_string(),
)
})?;
// For GitVersionedKvStore, use the user-provided path as the dataset directory
// This allows config files to be versioned in git commits
let dataset_dir = path.to_path_buf();
// Open existing Git repository
let git_repo = gix::open(&git_root).map_err(|e| GitKvError::GitOpenError(Box::new(e)))?;
// Create GitNodeStorage with user-provided path for versioned files
let storage = GitNodeStorage::new(git_repo.clone(), dataset_dir.clone())?;
// Load tree configuration from dataset directory
let config_path = dataset_dir.join("prolly_config_tree_config");
if !config_path.exists() {
return Err(GitKvError::GitObjectError(
"Config file not found. The store may not be initialized. \
Call init() to create a new store."
.to_string(),
));
}
let config_data = std::fs::read_to_string(&config_path)
.map_err(|e| GitKvError::GitObjectError(format!("Failed to read config file: {e}")))?;
let config: TreeConfig<N> = serde_json::from_str(&config_data)
.map_err(|e| GitKvError::GitObjectError(format!("Failed to parse config file: {e}")))?;
// Try to load existing tree from storage
let tree = if let Some(existing_tree) =
ProllyTree::load_from_storage(storage.clone(), config.clone())
{
existing_tree
} else if config.root_hash.is_some() {
// We have a saved root hash but failed to load the tree
// This could be due to missing hash mappings or git objects
// For read-only operations, we should try to work with what we have
// rather than creating a new empty tree that would overwrite the config
eprintln!("Warning: Failed to load tree from saved root hash. This may indicate missing git objects or corrupted hash mappings.");
eprintln!("Attempting to create tree with saved config to avoid data loss...");
ProllyTree::new(storage, config)
} else {
// No saved root hash - this is a genuinely new/empty tree
ProllyTree::new(storage, config)
};
// Get current branch
let current_branch = git_repo
.head_ref()
.map_err(|e| GitKvError::GitObjectError(format!("Failed to get head ref: {e}")))?
.map(|r| r.name().shorten().to_string())
.unwrap_or_else(|| "main".to_string());
let mut store = VersionedKvStore {
tree,
metadata: GitMetadataBackend::new(git_repo),
staging_area: HashMap::new(),
current_branch,
storage_backend: StorageBackend::Git,
dataset_dir: Some(dataset_dir),
};
// Load staging area from file if it exists
store.load_staging_area()?;
// Note: We intentionally do NOT call reload_tree_from_head() here
// because git-prolly commands should read from the current directory's
// prolly_config_tree_config and mapping files, not from git HEAD.
// The tree was already loaded from local storage by load_from_storage() above.
Ok(store)
}
/// Get a reference to the underlying ProllyTree
pub fn tree(&self) -> &ProllyTree<N, GitNodeStorage<N>> {
&self.tree
}
/// Get a mutable reference to the underlying ProllyTree
pub fn tree_mut(&mut self) -> &mut ProllyTree<N, GitNodeStorage<N>> {
&mut self.tree
}
/// Collect all key-value pairs from the tree at a specific commit
pub(crate) fn collect_keys_at_commit(
&self,
commit_id: &gix::ObjectId,
) -> Result<HashMap<Vec<u8>, Vec<u8>>, GitKvError> {
// Build relative paths for the prolly files
let dataset_dir = self.tree.storage.dataset_dir();
let git_root = self
.metadata
.work_dir()
.or_else(|| Self::find_git_root(dataset_dir))
.ok_or_else(|| GitKvError::GitObjectError("Could not find git root".to_string()))?;
let dataset_relative_path = dataset_dir
.strip_prefix(&git_root)
.map_err(|e| GitKvError::GitObjectError(format!("Failed to get relative path: {e}")))?;
let relative_path_str = dataset_relative_path
.components()
.map(|c| c.as_os_str().to_string_lossy())
.collect::<Vec<_>>()
.join("/");
let config_path = format!("{}/prolly_config_tree_config", relative_path_str);
let mapping_path = format!("{}/prolly_hash_mappings", relative_path_str);
let config_result = self.metadata.read_file_at_commit(commit_id, &config_path);
let mapping_result = self.metadata.read_file_at_commit(commit_id, &mapping_path);
// If files are not found, this might be an initial empty commit, return empty
if config_result.is_err() || mapping_result.is_err() {
return Ok(HashMap::new());
}
let config_data = config_result?;
let config: TreeConfig<N> = serde_json::from_slice(&config_data).map_err(|e| {
GitKvError::GitObjectError(format!("Failed to deserialize config: {e}"))
})?;
// Load the hash mappings from the tree as string format and parse
let mapping_data = mapping_result?;
let mapping_str = String::from_utf8(mapping_data)
.map_err(|e| GitKvError::GitObjectError(format!("Invalid UTF-8 in mappings: {e}")))?;
// Check if this is a simple key-value mapping (for InMemory storage)
// or a hash mapping (for Git storage)
let mut key_values = HashMap::new();
let mut hash_mappings = HashMap::new();
let mut is_simple_mapping = false;
for line in mapping_str.lines() {
if let Some((prefix, rest)) = line.split_once(':') {
if prefix == "key" {
// This is a simple key-value mapping from InMemory storage
is_simple_mapping = true;
if let Some((key_hex, value_hex)) = rest.split_once(':') {
if let (Ok(key), Ok(value)) = (hex::decode(key_hex), hex::decode(value_hex))
{
key_values.insert(key, value);
}
}
} else {
// This is a hash mapping from Git storage
let hash_hex = prefix;
let object_hex = rest;
if hash_hex.len() == N * 2 {
let mut hash_bytes = Vec::new();
for i in 0..N {
if let Ok(byte) = u8::from_str_radix(&hash_hex[i * 2..i * 2 + 2], 16) {
hash_bytes.push(byte);
} else {
break;
}
}
if hash_bytes.len() == N {
if let Ok(object_id) = gix::ObjectId::from_hex(object_hex.as_bytes()) {
let hash = ValueDigest::raw_hash(&hash_bytes);
hash_mappings.insert(hash, object_id);
}
}
}
}
}
}
if is_simple_mapping {
// For InMemory storage, return the directly stored key-value pairs
return Ok(key_values);
}
// For Git storage, reconstruct the tree from hash mappings
if hash_mappings.is_empty() {
return Ok(HashMap::new());
}
// Create a temporary storage with the loaded mappings
let temp_storage = GitNodeStorage::with_mappings(
self.metadata.clone_repo(),
self.tree.storage.dataset_dir().to_path_buf(),
hash_mappings,
)?;
// Load the tree with the config
let tree = ProllyTree::load_from_storage(temp_storage, config).ok_or_else(|| {
GitKvError::GitObjectError("Failed to load tree from storage".to_string())
})?;
// Collect all key-value pairs
let mut result_key_values = HashMap::new();
for key in tree.collect_keys() {
if let Some(node) = tree.find(&key) {
// Find the value in the node
if let Some(index) = node.keys.iter().position(|k| k == &key) {
result_key_values.insert(key, node.values[index].clone());
}
}
}
Ok(result_key_values)
}
}
// Implement HistoricalAccess for GitNodeStorage
impl<const N: usize> HistoricalAccess<N>
for VersionedKvStore<N, GitNodeStorage<N>, GitMetadataBackend>
{
fn get_keys_at_ref(&self, reference: &str) -> Result<HashMap<Vec<u8>, Vec<u8>>, GitKvError> {
let commit_id = self.resolve_commit(reference)?;
self.collect_keys_at_commit(&commit_id)
}
}
// Implement HistoricalCommitAccess for GitNodeStorage
impl<const N: usize> HistoricalCommitAccess<N>
for VersionedKvStore<N, GitNodeStorage<N>, GitMetadataBackend>
{
fn get_commits_for_key(&self, key: &[u8]) -> Result<Vec<CommitInfo>, GitKvError> {
let mut commit_history = self.get_commit_history()?;
// Reverse to process in chronological order (oldest first)
commit_history.reverse();
let mut commits_with_key_changes = Vec::new();
let mut previous_value: Option<Vec<u8>> = None; // None = key not present, Some(val) = key present with value
for commit in commit_history {
// Get the key value at this commit
let current_value = self.collect_keys_at_commit(&commit.id)?.get(key).cloned();
// Check if the value changed from the previous commit
let value_changed = previous_value != current_value;
if value_changed {
commits_with_key_changes.push(commit);
}
previous_value = current_value;
}
// Reverse back to newest first for the final result
commits_with_key_changes.reverse();
Ok(commits_with_key_changes)
}
fn get_commit_history(&self) -> Result<Vec<CommitInfo>, GitKvError> {
// Reuse the existing log method
self.log()
}
}
impl<const N: usize> VersionedKvStore<N, InMemoryNodeStorage<N>, GitMetadataBackend> {
/// Initialize a new versioned KV store with InMemory storage
pub fn init<P: AsRef<Path>>(path: P) -> Result<Self, GitKvError> {
let path = path.as_ref();
// Safety check: prevent initializing at git root to avoid `git add -A .` staging all files
if Self::is_in_git_root(path)? {
return Err(GitKvError::GitObjectError(
"Cannot initialize in-memory store in git root directory. \
Please use a subdirectory to create a dataset, or the commit operation \
may accidentally stage all files in the repository."
.to_string(),
));
}
// Find the git repository
let git_root = Self::find_git_root(path).ok_or_else(|| {
GitKvError::GitObjectError(
"Not inside a git repository. Please run from within a git repository.".to_string(),
)
})?;
// Open the existing git repository
let git_repo = gix::open(&git_root).map_err(|e| GitKvError::GitOpenError(Box::new(e)))?;
// Create dataset directory for config files
let dataset_dir = path.to_path_buf();
std::fs::create_dir_all(&dataset_dir).map_err(|e| {
GitKvError::GitObjectError(format!("Failed to create dataset directory: {e}"))
})?;
// Create InMemoryNodeStorage
let storage = InMemoryNodeStorage::<N>::new();
// Create ProllyTree with default config
let config: TreeConfig<N> = TreeConfig::default();
let tree = ProllyTree::new(storage, config);
let mut store = VersionedKvStore {
tree,
metadata: GitMetadataBackend::new(git_repo),
staging_area: HashMap::new(),
current_branch: "main".to_string(),
storage_backend: StorageBackend::InMemory,
dataset_dir: Some(dataset_dir),
};
// Create initial commit
store.commit("Initial commit")?;
Ok(store)
}
/// Open an existing versioned KV store with InMemory storage
/// Note: InMemory storage is volatile, so this creates a new empty store
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, GitKvError> {
// For InMemory storage, "opening" is the same as initializing
// since data is not persistent
Self::init(path)
}
}
// Implement HistoricalAccess for InMemoryNodeStorage
impl<const N: usize> HistoricalAccess<N>
for VersionedKvStore<N, InMemoryNodeStorage<N>, GitMetadataBackend>
{
fn get_keys_at_ref(&self, reference: &str) -> Result<HashMap<Vec<u8>, Vec<u8>>, GitKvError> {
// Resolve the reference to a commit ID
let commit_id = self.resolve_commit(reference)?;
// Get the tree config from the commit to extract root hash
let tree_config = self.read_tree_config_from_commit(&commit_id)?;
// Reconstruct the tree state from storage using the root hash
self.collect_keys_from_config(&tree_config)
}
}
// Implement HistoricalCommitAccess for InMemoryNodeStorage
impl<const N: usize> HistoricalCommitAccess<N>
for VersionedKvStore<N, InMemoryNodeStorage<N>, GitMetadataBackend>
{
fn get_commits_for_key(&self, key: &[u8]) -> Result<Vec<CommitInfo>, GitKvError> {
self.get_commits_for_key_generic(key)
}
fn get_commit_history(&self) -> Result<Vec<CommitInfo>, GitKvError> {
self.get_commit_history_generic()
}
}
impl<const N: usize> VersionedKvStore<N, FileNodeStorage<N>, GitMetadataBackend> {
/// Initialize a new versioned KV store with File storage
///
/// Nodes are stored in `.git/prolly/nodes/files/` (shared, content-addressed).
/// Config is stored in the dataset directory (committed to git for history).
pub fn init<P: AsRef<Path>>(path: P) -> Result<Self, GitKvError> {
let path = path.as_ref();
// Safety check: prevent initializing at git root to avoid `git add -A .` staging all files
if Self::is_in_git_root(path)? {
return Err(GitKvError::GitObjectError(
"Cannot initialize file store in git root directory. \
Please use a subdirectory to create a dataset, or the commit operation \
may accidentally stage all files in the repository."
.to_string(),
));
}
// Find the git repository
let git_root = Self::find_git_root(path).ok_or_else(|| {
GitKvError::GitObjectError(
"Not inside a git repository. Please run from within a git repository.".to_string(),
)
})?;
// Create prolly directory inside .git for node storage
let prolly_dir = Self::ensure_prolly_dir(&git_root)?;
// Create dataset directory for config files (will be committed to git)
let dataset_dir = path.to_path_buf();
std::fs::create_dir_all(&dataset_dir).map_err(|e| {
GitKvError::GitObjectError(format!("Failed to create dataset directory: {e}"))
})?;
// Open the existing git repository
let git_repo = gix::open(&git_root).map_err(|e| GitKvError::GitOpenError(Box::new(e)))?;
// Create FileNodeStorage inside .git/prolly/nodes/files/ (shared across datasets)
let file_storage_path = prolly_dir.join("nodes").join("files");
std::fs::create_dir_all(&file_storage_path).map_err(|e| {
GitKvError::GitObjectError(format!("Failed to create file storage directory: {e}"))
})?;
let storage = FileNodeStorage::<N>::new(file_storage_path).map_err(|e| {
GitKvError::GitObjectError(format!("Failed to create file storage: {e}"))
})?;
// Create ProllyTree with default config
let config: TreeConfig<N> = TreeConfig::default();
let tree = ProllyTree::new(storage, config);
let mut store = VersionedKvStore {
tree,
metadata: GitMetadataBackend::new(git_repo),
staging_area: HashMap::new(),
current_branch: "main".to_string(),
storage_backend: StorageBackend::File,
dataset_dir: Some(dataset_dir),
};
// Create initial commit (which will save config to dataset_dir)
store.commit("Initial commit")?;
Ok(store)
}
/// Open an existing versioned KV store with File storage
///
/// Nodes are loaded from `.git/prolly/nodes/files/` (shared, content-addressed).
/// Config is loaded from the dataset directory.
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, GitKvError> {
let path = path.as_ref();
let dataset_dir = path.to_path_buf();
// Safety check: prevent opening at git root to avoid `git add -A .` staging all files
if Self::is_in_git_root(path)? {
return Err(GitKvError::GitObjectError(
"Cannot open file store in git root directory. \
Please use a subdirectory for your dataset, or the commit operation \
may accidentally stage all files in the repository."
.to_string(),
));
}
// Check if the dataset directory exists
if !dataset_dir.exists() {
return Err(GitKvError::GitObjectError(
"Dataset directory not found. Call init() first to create the store.".to_string(),
));
}
// Find the git repository
let git_root = Self::find_git_root(path).ok_or_else(|| {
GitKvError::GitObjectError(
"Not inside a git repository. Please run from within a git repository.".to_string(),
)
})?;
// Get prolly directory inside .git
let prolly_dir = Self::get_prolly_dir(&git_root);
// Open existing Git repository
let git_repo = gix::open(&git_root).map_err(|e| GitKvError::GitOpenError(Box::new(e)))?;
// Open FileNodeStorage inside .git/prolly/nodes/files/
let file_storage_path = prolly_dir.join("nodes").join("files");
// Check if the file storage directory exists - if not, the store hasn't been initialized
if !file_storage_path.exists() {
return Err(GitKvError::GitObjectError(
"File store not initialized. Call init() first to create the store.".to_string(),
));
}
let storage = FileNodeStorage::<N>::new(file_storage_path.clone()).map_err(|e| {
GitKvError::GitObjectError(format!("Failed to create file storage: {e}"))
})?;
// Load tree configuration from dataset directory (not from storage)
// Config file must exist for open() - use init() to create new stores
let config_path = dataset_dir.join("prolly_config_tree_config");
if !config_path.exists() {
return Err(GitKvError::GitObjectError(
"Config file not found. The store may not be initialized. \
Call init() to create a new store."
.to_string(),
));
}
let config_data = std::fs::read_to_string(&config_path)
.map_err(|e| GitKvError::GitObjectError(format!("Failed to read config file: {e}")))?;
let config: TreeConfig<N> = serde_json::from_str(&config_data)
.map_err(|e| GitKvError::GitObjectError(format!("Failed to parse config file: {e}")))?;
// Try to load existing tree from storage using the config's root hash
let tree =
if let Some(existing_tree) = ProllyTree::load_from_storage(storage, config.clone()) {
existing_tree
} else {
// Create new storage instance since the original was consumed
let new_storage = FileNodeStorage::<N>::new(file_storage_path).map_err(|e| {
GitKvError::GitObjectError(format!("Failed to create file storage: {e}"))
})?;
ProllyTree::new(new_storage, config)
};
// Get current branch
let current_branch = git_repo
.head_ref()
.map_err(|e| GitKvError::GitObjectError(format!("Failed to get head ref: {e}")))?
.map(|r| r.name().shorten().to_string())
.unwrap_or_else(|| "main".to_string());
let mut store = VersionedKvStore {
tree,
metadata: GitMetadataBackend::new(git_repo),
staging_area: HashMap::new(),
current_branch,
storage_backend: StorageBackend::File,
dataset_dir: Some(dataset_dir),
};
// Load staging area from file if it exists
store.load_staging_area()?;
Ok(store)
}
}
// Implement HistoricalAccess for FileNodeStorage
impl<const N: usize> HistoricalAccess<N>
for VersionedKvStore<N, FileNodeStorage<N>, GitMetadataBackend>
{
fn get_keys_at_ref(&self, reference: &str) -> Result<HashMap<Vec<u8>, Vec<u8>>, GitKvError> {
// Resolve the reference to a commit ID
let commit_id = self.resolve_commit(reference)?;
// Get the tree config from the commit to extract root hash
let tree_config = self.read_tree_config_from_commit(&commit_id)?;
// Reconstruct the tree state from storage using the root hash
self.collect_keys_from_config(&tree_config)
}
}
// Implement HistoricalCommitAccess for FileNodeStorage
impl<const N: usize> HistoricalCommitAccess<N>
for VersionedKvStore<N, FileNodeStorage<N>, GitMetadataBackend>
{
fn get_commits_for_key(&self, key: &[u8]) -> Result<Vec<CommitInfo>, GitKvError> {
self.get_commits_for_key_generic(key)
}
fn get_commit_history(&self) -> Result<Vec<CommitInfo>, GitKvError> {
self.get_commit_history_generic()
}
}
#[cfg(feature = "rocksdb_storage")]
impl<const N: usize> VersionedKvStore<N, RocksDBNodeStorage<N>, GitMetadataBackend> {
/// Initialize a new versioned KV store with RocksDB storage
///
/// Nodes are stored in `.git/prolly/nodes/rocksdb/` (shared, content-addressed).
/// Config is stored in the dataset directory (committed to git for history).
pub fn init<P: AsRef<Path>>(path: P) -> Result<Self, GitKvError> {
let path = path.as_ref();
// Safety check: prevent initializing at git root to avoid `git add -A .` staging all files
if Self::is_in_git_root(path)? {
return Err(GitKvError::GitObjectError(
"Cannot initialize RocksDB store in git root directory. \
Please use a subdirectory to create a dataset, or the commit operation \
may accidentally stage all files in the repository."
.to_string(),
));
}
// Find the git repository
let git_root = Self::find_git_root(path).ok_or_else(|| {
GitKvError::GitObjectError(
"Not inside a git repository. Please run from within a git repository.".to_string(),
)
})?;
// Create prolly directory inside .git for node storage
let prolly_dir = Self::ensure_prolly_dir(&git_root)?;
// Create dataset directory for config files (will be committed to git)
let dataset_dir = path.to_path_buf();
std::fs::create_dir_all(&dataset_dir).map_err(|e| {
GitKvError::GitObjectError(format!("Failed to create dataset directory: {e}"))
})?;
// Open the existing git repository
let git_repo = gix::open(&git_root).map_err(|e| GitKvError::GitOpenError(Box::new(e)))?;
// Create RocksDBNodeStorage inside .git/prolly/nodes/rocksdb/ (shared across datasets)
let rocksdb_path = prolly_dir.join("nodes").join("rocksdb");
std::fs::create_dir_all(&rocksdb_path).map_err(|e| {
GitKvError::GitObjectError(format!("Failed to create RocksDB directory: {e}"))
})?;
let storage = RocksDBNodeStorage::<N>::new(rocksdb_path)
.map_err(|e| GitKvError::GitObjectError(format!("RocksDB creation failed: {e}")))?;
// Create ProllyTree with default config
let config: TreeConfig<N> = TreeConfig::default();
let tree = ProllyTree::new(storage, config);
let mut store = VersionedKvStore {
tree,
metadata: GitMetadataBackend::new(git_repo),
staging_area: HashMap::new(),
current_branch: "main".to_string(),
storage_backend: StorageBackend::RocksDB,
dataset_dir: Some(dataset_dir),
};
// Create initial commit (which will save config to dataset_dir)
store.commit("Initial commit")?;
Ok(store)
}
/// Open an existing versioned KV store with RocksDB storage
///
/// Nodes are loaded from `.git/prolly/nodes/rocksdb/` (shared, content-addressed).
/// Config is loaded from the dataset directory.
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, GitKvError> {
let path = path.as_ref();
let dataset_dir = path.to_path_buf();
// Safety check: prevent opening at git root to avoid `git add -A .` staging all files
if Self::is_in_git_root(path)? {
return Err(GitKvError::GitObjectError(
"Cannot open RocksDB store in git root directory. \
Please use a subdirectory for your dataset, or the commit operation \
may accidentally stage all files in the repository."
.to_string(),
));
}
// Check if the dataset directory exists
if !dataset_dir.exists() {
return Err(GitKvError::GitObjectError(
"Dataset directory not found. Call init() first to create the store.".to_string(),
));
}
// Find the git repository
let git_root = Self::find_git_root(path).ok_or_else(|| {
GitKvError::GitObjectError(
"Not inside a git repository. Please run from within a git repository.".to_string(),
)
})?;
// Get prolly directory inside .git
let prolly_dir = Self::get_prolly_dir(&git_root);
// Open existing Git repository
let git_repo = gix::open(&git_root).map_err(|e| GitKvError::GitOpenError(Box::new(e)))?;
// Open RocksDBNodeStorage inside .git/prolly/nodes/rocksdb/
let rocksdb_path = prolly_dir.join("nodes").join("rocksdb");
// Check if the RocksDB directory exists - if not, the store hasn't been initialized
if !rocksdb_path.exists() {
return Err(GitKvError::GitObjectError(
"RocksDB store not initialized. Call init() first to create the store.".to_string(),
));
}
let storage = RocksDBNodeStorage::<N>::new(rocksdb_path)
.map_err(|e| GitKvError::GitObjectError(format!("RocksDB creation failed: {e}")))?;
// Load tree configuration from dataset directory (not from storage)
let config_path = dataset_dir.join("prolly_config_tree_config");
if !config_path.exists() {
return Err(GitKvError::GitObjectError(
"Config file not found. The store may not be initialized. \
Call init() to create a new store."
.to_string(),
));
}
let config_data = std::fs::read_to_string(&config_path)
.map_err(|e| GitKvError::GitObjectError(format!("Failed to read config file: {e}")))?;
let config: TreeConfig<N> = serde_json::from_str(&config_data)
.map_err(|e| GitKvError::GitObjectError(format!("Failed to parse config file: {e}")))?;
// Try to load existing tree from storage using the config's root hash
let tree = ProllyTree::load_from_storage(storage.clone(), config.clone())
.unwrap_or_else(|| ProllyTree::new(storage, config));
// Get current branch
let current_branch = git_repo
.head_ref()
.map_err(|e| GitKvError::GitObjectError(format!("Failed to get head ref: {e}")))?
.map(|r| r.name().shorten().to_string())
.unwrap_or_else(|| "main".to_string());
let mut store = VersionedKvStore {
tree,
metadata: GitMetadataBackend::new(git_repo),
staging_area: HashMap::new(),
current_branch,
storage_backend: StorageBackend::RocksDB,
dataset_dir: Some(dataset_dir),
};
// Load staging area from file if it exists
store.load_staging_area()?;
Ok(store)
}
}
// Implement HistoricalAccess for RocksDBNodeStorage
#[cfg(feature = "rocksdb_storage")]
impl<const N: usize> HistoricalAccess<N>
for VersionedKvStore<N, RocksDBNodeStorage<N>, GitMetadataBackend>
{
fn get_keys_at_ref(&self, reference: &str) -> Result<HashMap<Vec<u8>, Vec<u8>>, GitKvError> {
// Resolve the reference to a commit ID
let commit_id = self.resolve_commit(reference)?;
// Get the tree config from the commit to extract root hash
let tree_config = self.read_tree_config_from_commit(&commit_id)?;
// Reconstruct the tree state from storage using the root hash
self.collect_keys_from_config(&tree_config)
}
}
// Implement HistoricalCommitAccess for RocksDBNodeStorage
#[cfg(feature = "rocksdb_storage")]
impl<const N: usize> HistoricalCommitAccess<N>
for VersionedKvStore<N, RocksDBNodeStorage<N>, GitMetadataBackend>
{
fn get_commits_for_key(&self, key: &[u8]) -> Result<Vec<CommitInfo>, GitKvError> {
self.get_commits_for_key_generic(key)
}
fn get_commit_history(&self) -> Result<Vec<CommitInfo>, GitKvError> {
self.get_commit_history_generic()
}
}
// Implement TreeConfigSaver for InMemoryNodeStorage
impl<const N: usize> TreeConfigSaver<N>
for VersionedKvStore<N, InMemoryNodeStorage<N>, GitMetadataBackend>
{
fn save_tree_config_to_git_internal(&self) -> Result<(), GitKvError> {
self.save_tree_config_to_git()
}
}
// Specialized implementation for InMemoryNodeStorage
impl<const N: usize> VersionedKvStore<N, InMemoryNodeStorage<N>, GitMetadataBackend> {
/// Save tree config to git for InMemoryNodeStorage
///
/// Writes only the config (with root hash) to dataset directory.
/// The config is committed to git for historical access.
/// Nodes are kept in memory - historical access works within the same session
/// by loading nodes from the in-memory storage using the root hash.
/// After restart, nodes are lost (expected for in-memory storage).
fn save_tree_config_to_git(&self) -> Result<(), GitKvError> {
let dataset_dir = self
.dataset_dir
.as_ref()
.ok_or_else(|| GitKvError::GitObjectError("Dataset directory not set".to_string()))?;
// Get the current tree configuration (includes root_hash)
let config = self.tree.config.clone();
// Serialize the config to JSON
let config_json = serde_json::to_string_pretty(&config)
.map_err(|e| GitKvError::GitObjectError(format!("Failed to serialize config: {e}")))?;
// Write only the config file to the dataset directory
// No need for prolly_hash_mappings - nodes are in memory and accessed by root_hash
// Historical access uses root_hash to traverse the tree from in-memory storage
let config_path = dataset_dir.join("prolly_config_tree_config");
std::fs::write(&config_path, config_json)
.map_err(|e| GitKvError::GitObjectError(format!("Failed to write config file: {e}")))?;
Ok(())
}
}
// Implement TreeConfigSaver for FileNodeStorage
impl<const N: usize> TreeConfigSaver<N>
for VersionedKvStore<N, FileNodeStorage<N>, GitMetadataBackend>
{
fn save_tree_config_to_git_internal(&self) -> Result<(), GitKvError> {
self.save_tree_config_to_git()
}
}
// Specialized implementation for FileNodeStorage
impl<const N: usize> VersionedKvStore<N, FileNodeStorage<N>, GitMetadataBackend> {
/// Save tree config to git for FileNodeStorage
///
/// Writes only the config (with root hash) to dataset directory.
/// The config is committed to git for historical access.
/// Nodes remain in .git/prolly/nodes/files/ (shared, content-addressed).
/// Historical access reconstructs tree by loading nodes from storage using root hash.
fn save_tree_config_to_git(&self) -> Result<(), GitKvError> {
let dataset_dir = self
.dataset_dir
.as_ref()
.ok_or_else(|| GitKvError::GitObjectError("Dataset directory not set".to_string()))?;
// Get the current tree configuration (includes root_hash)
let config = self.tree.config.clone();
// Serialize the config to JSON
let config_json = serde_json::to_string_pretty(&config)
.map_err(|e| GitKvError::GitObjectError(format!("Failed to serialize config: {e}")))?;
// Write only the config file to the dataset directory
// No need for prolly_hash_mappings - nodes are content-addressed in .git/prolly/nodes/files/
// Historical access uses root_hash to traverse the tree from node storage
let config_path = dataset_dir.join("prolly_config_tree_config");
std::fs::write(&config_path, config_json)
.map_err(|e| GitKvError::GitObjectError(format!("Failed to write config file: {e}")))?;
Ok(())
}
}
// Implement TreeConfigSaver for RocksDBNodeStorage
#[cfg(feature = "rocksdb_storage")]
impl<const N: usize> TreeConfigSaver<N>
for VersionedKvStore<N, RocksDBNodeStorage<N>, GitMetadataBackend>
{
fn save_tree_config_to_git_internal(&self) -> Result<(), GitKvError> {
self.save_tree_config_to_git()
}
}
// Specialized implementation for RocksDBNodeStorage
#[cfg(feature = "rocksdb_storage")]
impl<const N: usize> VersionedKvStore<N, RocksDBNodeStorage<N>, GitMetadataBackend> {
/// Save tree config to git for RocksDBNodeStorage
///
/// Writes only the config (with root hash) to dataset directory.
/// The config is committed to git for historical access.
/// Nodes remain in .git/prolly/nodes/rocksdb/ (shared, content-addressed).
/// Historical access reconstructs tree by loading nodes from storage using root hash.
fn save_tree_config_to_git(&self) -> Result<(), GitKvError> {
let dataset_dir = self
.dataset_dir
.as_ref()
.ok_or_else(|| GitKvError::GitObjectError("Dataset directory not set".to_string()))?;
// Get the current tree configuration (includes root_hash)
let config = self.tree.config.clone();
// Serialize the config to JSON
let config_json = serde_json::to_string_pretty(&config)
.map_err(|e| GitKvError::GitObjectError(format!("Failed to serialize config: {e}")))?;
// Write only the config file to the dataset directory
// No need for prolly_hash_mappings - nodes are content-addressed in .git/prolly/nodes/rocksdb/
// Historical access uses root_hash to traverse the tree from node storage
let config_path = dataset_dir.join("prolly_config_tree_config");
std::fs::write(&config_path, config_json)
.map_err(|e| GitKvError::GitObjectError(format!("Failed to write config file: {e}")))?;
Ok(())
}
}