prollytree 0.3.2

A prolly (probabilistic) tree for efficient storage, retrieval, and modification of ordered data.
Documentation
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
/*
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::git::metadata::MetadataBackend;
use crate::git::types::*;
use crate::storage::NodeStorage;
use crate::tree::Tree;
use std::path::Path;

impl<const N: usize, S: NodeStorage<N>, M: MetadataBackend> VersionedKvStore<N, S, M>
where
    Self: TreeConfigSaver<N>,
{
    /// Find the git repository root by walking up the directory tree
    pub(super) fn find_git_root<P: AsRef<Path>>(start_path: P) -> Option<std::path::PathBuf> {
        let mut current = start_path.as_ref().to_path_buf();
        loop {
            if current.join(".git").exists() {
                return Some(current);
            }
            if !current.pop() {
                break;
            }
        }
        None
    }

    /// Get the common git directory path, handling worktrees and submodules.
    /// For worktrees, this resolves to the main .git directory (not the per-worktree gitdir)
    /// to ensure shared node storage is placed in a common location.
    pub(super) fn resolve_git_dir<P: AsRef<Path>>(git_root: P) -> std::path::PathBuf {
        let git_path = git_root.as_ref().join(".git");

        // If .git is a file (worktree or submodule), read the gitdir path from it
        let gitdir = if git_path.is_file() {
            if let Ok(content) = std::fs::read_to_string(&git_path) {
                let mut resolved_gitdir = None;
                for line in content.lines() {
                    if let Some(gitdir_str) = line.strip_prefix("gitdir:") {
                        let gitdir_str = gitdir_str.trim();
                        // Handle both absolute and relative paths
                        let gitdir_path = std::path::Path::new(gitdir_str);
                        resolved_gitdir = Some(if gitdir_path.is_absolute() {
                            gitdir_path.to_path_buf()
                        } else {
                            git_root.as_ref().join(gitdir_str)
                        });
                        break;
                    }
                }
                resolved_gitdir.unwrap_or(git_path)
            } else {
                git_path
            }
        } else {
            // Default: .git is a directory
            git_path
        };

        // For linked worktrees, resolve to the common git directory
        // The commondir file contains the path to the main .git directory
        let commondir_path = gitdir.join("commondir");
        if commondir_path.is_file() {
            if let Ok(content) = std::fs::read_to_string(&commondir_path) {
                let commondir_str = content.trim();
                if !commondir_str.is_empty() {
                    let commondir = std::path::Path::new(commondir_str);
                    // Handle both absolute and relative paths
                    let resolved_commondir = if commondir.is_absolute() {
                        commondir.to_path_buf()
                    } else {
                        // Relative path is relative to the gitdir
                        gitdir.join(commondir)
                    };
                    // Canonicalize to resolve .. components
                    if let Ok(canonical) = resolved_commondir.canonicalize() {
                        return canonical;
                    }
                    return resolved_commondir;
                }
            }
        }

        gitdir
    }

    /// Get the prolly directory path inside the git directory
    /// This is where all ProllyTree data is stored to avoid accidental git versioning
    pub(super) fn get_prolly_dir<P: AsRef<Path>>(git_root: P) -> std::path::PathBuf {
        Self::resolve_git_dir(git_root).join("prolly")
    }

    /// Ensure the prolly directory structure exists
    pub(super) fn ensure_prolly_dir<P: AsRef<Path>>(
        git_root: P,
    ) -> Result<std::path::PathBuf, GitKvError> {
        let prolly_dir = Self::get_prolly_dir(&git_root);
        std::fs::create_dir_all(&prolly_dir).map_err(|e| {
            GitKvError::GitObjectError(format!("Failed to create prolly directory: {e}"))
        })?;
        Ok(prolly_dir)
    }

    /// Check if the given path is the git repository root directory
    /// This is used to prevent initializing a dataset at the git root,
    /// which could cause `git add -A .` to stage unrelated files.
    pub(super) fn is_in_git_root<P: AsRef<Path>>(path: P) -> Result<bool, GitKvError> {
        let path = path.as_ref();

        // Try to canonicalize the path. If it doesn't exist, use the parent directory.
        let canonical_path = if path.exists() {
            path.canonicalize()
                .map_err(|e| GitKvError::GitObjectError(format!("Failed to resolve path: {e}")))?
        } else {
            // Path doesn't exist yet (common for init). Use parent + last component.
            let parent = path.parent().ok_or_else(|| {
                GitKvError::GitObjectError("Invalid path: no parent directory".to_string())
            })?;

            // If parent doesn't exist either, we can't proceed
            if !parent.exists() && !parent.as_os_str().is_empty() {
                return Err(GitKvError::GitObjectError(format!(
                    "Parent directory does not exist: {}",
                    parent.display()
                )));
            }

            // Canonicalize parent and append the last component
            let canonical_parent = if parent.as_os_str().is_empty() {
                std::env::current_dir().map_err(|e| {
                    GitKvError::GitObjectError(format!("Failed to get current directory: {e}"))
                })?
            } else {
                parent.canonicalize().map_err(|e| {
                    GitKvError::GitObjectError(format!("Failed to resolve parent path: {e}"))
                })?
            };

            // Append the file name to get the full path
            if let Some(file_name) = path.file_name() {
                canonical_parent.join(file_name)
            } else {
                canonical_parent
            }
        };

        // Find git root from the path (or its parent if path doesn't exist)
        let lookup_path = if path.exists() {
            canonical_path.clone()
        } else {
            canonical_path
                .parent()
                .map(|p| p.to_path_buf())
                .unwrap_or(canonical_path.clone())
        };

        if let Some(git_root) = Self::find_git_root(&lookup_path) {
            let git_root = git_root.canonicalize().map_err(|e| {
                GitKvError::GitObjectError(format!("Failed to resolve git root: {e}"))
            })?;
            Ok(canonical_path == git_root)
        } else {
            Err(GitKvError::GitObjectError(
                "Not inside a git repository. Please run from within a git repository.".to_string(),
            ))
        }
    }

    /// Insert a key-value pair (stages the change).
    ///
    /// # Errors
    ///
    /// Returns [`GitKvError::ValidationError`] if the key is empty, the key
    /// exceeds 64 KB, or the value exceeds 100 MB.
    pub fn insert(&mut self, key: Vec<u8>, value: Vec<u8>) -> Result<(), GitKvError> {
        crate::validation::validate_kv(&key, &value)?;
        self.staging_area.insert(key, Some(value));
        self.save_staging_area()?;
        Ok(())
    }

    /// Update an existing key-value pair (stages the change).
    ///
    /// # Errors
    ///
    /// Returns [`GitKvError::ValidationError`] if the key is empty, the key
    /// exceeds 64 KB, or the value exceeds 100 MB.
    pub fn update(&mut self, key: Vec<u8>, value: Vec<u8>) -> Result<bool, GitKvError> {
        crate::validation::validate_kv(&key, &value)?;
        let exists = self.get(&key).is_some();
        if exists {
            self.staging_area.insert(key, Some(value));
            self.save_staging_area()?;
        }
        Ok(exists)
    }

    /// Delete a key-value pair (stages the change)
    pub fn delete(&mut self, key: &[u8]) -> Result<bool, GitKvError> {
        let exists = self.get(key).is_some();
        if exists {
            self.staging_area.insert(key.to_vec(), None);
            self.save_staging_area()?;
        }
        Ok(exists)
    }

    /// Get a value by key (checks staging area first, then committed data)
    pub fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
        // Check staging area first
        if let Some(staged_value) = self.staging_area.get(key) {
            return staged_value.clone();
        }

        // Check committed data
        self.tree.find(key).and_then(|node| {
            // Find the value in the node
            node.keys
                .iter()
                .position(|k| k == key)
                .map(|index| node.values[index].clone())
        })
    }

    /// List all keys (includes staged changes)
    pub fn list_keys(&self) -> Vec<Vec<u8>> {
        let mut keys = std::collections::HashSet::new();

        // Add keys from the committed ProllyTree
        for key in self.tree.collect_keys() {
            keys.insert(key);
        }

        // Add keys from staging area (overrides committed data)
        for (key, value) in &self.staging_area {
            if value.is_some() {
                keys.insert(key.clone());
            } else {
                keys.remove(key);
            }
        }

        keys.into_iter().collect()
    }

    /// Show current staging area status
    pub fn status(&self) -> Vec<(Vec<u8>, String)> {
        let mut status = Vec::new();

        for (key, value) in &self.staging_area {
            let status_str = match value {
                Some(_) => {
                    if self.tree.find(key).is_some() {
                        "modified".to_string()
                    } else {
                        "added".to_string()
                    }
                }
                None => "deleted".to_string(),
            };
            status.push((key.clone(), status_str));
        }

        status
    }

    /// Commit staged changes
    pub fn commit(&mut self, message: &str) -> Result<gix::ObjectId, GitKvError> {
        // Apply staged changes to the tree
        for (key, value) in self.staging_area.drain() {
            match value {
                Some(v) => {
                    self.tree.insert(key, v);
                }
                None => {
                    self.tree.delete(&key);
                }
            }
        }

        // Persist the tree state (including updating root hash and saving config)
        self.tree.persist_root();

        // For all storage types, also save the tree config to git for historical access
        self.save_tree_config_to_git_internal()?;

        // Get the git root directory using work_dir() for worktree/submodule compatibility
        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()))?;

        // Stage and write tree via metadata backend
        let tree_id = self.metadata.stage_and_write_tree(&git_root)?;

        // Create commit via metadata backend
        let commit_id = self.metadata.write_commit(tree_id, message)?;

        // Update branch ref and HEAD
        self.metadata
            .update_branch(&self.current_branch, commit_id)?;
        self.metadata.update_head(&self.current_branch)?;

        // Clear staging area file since we've committed
        self.save_staging_area()?;

        Ok(commit_id)
    }

    /// Create a new branch
    pub fn branch(&mut self, name: &str) -> Result<(), GitKvError> {
        self.metadata.create_branch(name)
    }

    /// Create a new branch from the current branch and switch to it
    pub fn create_branch(&mut self, name: &str) -> Result<(), GitKvError> {
        // First create the branch
        self.branch(name)?;

        // Then switch to it
        self.staging_area.clear();
        self.save_staging_area()?;

        // Update our internal tracking to the new branch
        self.current_branch = name.to_string();

        // Update HEAD to point to the new branch
        self.metadata.update_head(name)?;

        Ok(())
    }

    // Note: checkout is implemented differently for each storage type
    // GitNodeStorage has its own implementation that reloads tree state

    /// Get current branch name
    pub fn current_branch(&self) -> &str {
        &self.current_branch
    }

    /// List all branches in the repository
    pub fn list_branches(&self) -> Result<Vec<String>, GitKvError> {
        self.metadata.list_branches()
    }

    /// Get commit history
    pub fn log(&self) -> Result<Vec<CommitInfo>, GitKvError> {
        self.metadata.walk_history(100)
    }

    /// Save the staging area to a file
    pub(super) fn save_staging_area(&self) -> Result<(), GitKvError> {
        let staging_file = self.get_staging_file_path()?;

        // Serialize the staging area
        let serialized =
            bincode::serialize(&self.staging_area).map_err(GitKvError::SerializationError)?;

        std::fs::write(staging_file, serialized).map_err(|e| {
            GitKvError::GitObjectError(format!("Failed to write staging area: {e}"))
        })?;

        Ok(())
    }

    /// Load the staging area from a file
    pub(super) fn load_staging_area(&mut self) -> Result<(), GitKvError> {
        let staging_file = self.get_staging_file_path()?;

        if staging_file.exists() {
            let data = std::fs::read(staging_file).map_err(|e| {
                GitKvError::GitObjectError(format!("Failed to read staging area: {e}"))
            })?;

            self.staging_area =
                bincode::deserialize(&data).map_err(GitKvError::SerializationError)?;
        }

        Ok(())
    }

    /// Get the dataset-specific staging file path
    fn get_staging_file_path(&self) -> Result<std::path::PathBuf, GitKvError> {
        // Get the current directory relative to git root
        let current_dir = std::env::current_dir().map_err(|e| {
            GitKvError::GitObjectError(format!("Failed to get current directory: {e}"))
        })?;

        let git_root = Self::find_git_root(&current_dir)
            .ok_or_else(|| GitKvError::GitObjectError("Not in a git repository".to_string()))?;

        // Create a dataset-specific identifier from the relative path
        let relative_path = current_dir
            .strip_prefix(&git_root)
            .map_err(|e| GitKvError::GitObjectError(format!("Failed to get relative path: {e}")))?;

        // Use the relative path to create a unique staging file name
        let path_str = relative_path.to_string_lossy().replace(['/', '\\'], "_");
        let staging_filename = if path_str.is_empty() {
            "PROLLY_STAGING_root".to_string()
        } else {
            format!("PROLLY_STAGING_{path_str}")
        };

        Ok(self.metadata.metadata_dir().join(staging_filename))
    }

    /// Generate a cryptographic proof for a key's existence and value in the tree
    /// This proof can be used to verify the integrity of the key-value pair without
    /// requiring access to the entire tree structure.
    ///
    /// # Parameters
    /// - `key`: The key to generate proof for
    ///
    /// # Returns
    /// - A proof object containing the hash path from root to the target node
    pub fn generate_proof(&self, key: &[u8]) -> crate::proof::Proof<N> {
        self.tree.generate_proof(key)
    }

    /// Verify a cryptographic proof for a key-value pair
    /// This checks that the proof is valid and optionally verifies the expected value
    ///
    /// # Parameters
    /// - `proof`: The proof to verify
    /// - `key`: The key that the proof claims to prove
    /// - `expected_value`: Optional expected value to verify against
    ///
    /// # Returns
    /// - `true` if the proof is valid, `false` otherwise
    pub fn verify(
        &self,
        proof: crate::proof::Proof<N>,
        key: &[u8],
        expected_value: Option<&[u8]>,
    ) -> bool {
        self.tree.verify(proof, key, expected_value)
    }
}

// Generic diff functionality for all storage types
impl<const N: usize, S: NodeStorage<N>, M: MetadataBackend> VersionedKvStore<N, S, M>
where
    Self: HistoricalAccess<N>,
{
    /// Compare two commits or branches and return all keys that are added, updated or deleted
    pub fn diff(&self, from: &str, to: &str) -> Result<Vec<KvDiff>, GitKvError> {
        // Get all keys from both references
        let from_keys = self.get_keys_at_ref(from)?;
        let to_keys = self.get_keys_at_ref(to)?;

        let mut diffs = Vec::new();

        // Check for added or modified keys
        for (key, to_value) in &to_keys {
            match from_keys.get(key) {
                None => {
                    // Key was added
                    diffs.push(KvDiff {
                        key: key.clone(),
                        operation: DiffOperation::Added(to_value.clone()),
                    });
                }
                Some(from_value) => {
                    if from_value != to_value {
                        // Key was modified
                        diffs.push(KvDiff {
                            key: key.clone(),
                            operation: DiffOperation::Modified {
                                old: from_value.clone(),
                                new: to_value.clone(),
                            },
                        });
                    }
                }
            }
        }

        // Check for removed keys
        for (key, from_value) in &from_keys {
            if !to_keys.contains_key(key) {
                diffs.push(KvDiff {
                    key: key.clone(),
                    operation: DiffOperation::Removed(from_value.clone()),
                });
            }
        }

        // Sort diffs by key for consistent output
        diffs.sort_by(|a, b| a.key.cmp(&b.key));

        Ok(diffs)
    }
}

// Generic commit history functionality for all storage types
impl<const N: usize, S: NodeStorage<N>, M: MetadataBackend> VersionedKvStore<N, S, M>
where
    Self: HistoricalCommitAccess<N>,
{
    /// Get all commits that contain changes to a specific key
    /// Returns commits in reverse chronological order (newest first), similar to `git log -- <file>`
    pub fn get_commits(&self, key: &[u8]) -> Result<Vec<CommitInfo>, GitKvError> {
        self.get_commits_for_key(key)
    }

    /// Get the current HEAD commit ID
    pub fn current_commit(&self) -> Result<gix::ObjectId, GitKvError> {
        self.metadata.head_commit_id()
    }
}