kotoba 0.1.3

GP2-based Graph Rewriting Language - ISO GQL-compliant queries, MVCC+Merkle persistence, and distributed execution
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
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
//! CIDベース永続ストレージシステム
//!
//! このモジュールは、CIDアドレス指定による永続ストレージを実装します。
//! LSMツリー、Merkle DAG、MVCCを統合した高性能ストレージエンジンです。

use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::Arc;
use parking_lot::RwLock;
use crate::types::*;
use crate::cid::*;
use crate::graph::*;
use crate::storage::{lsm::*, merkle::*, mvcc::*};

/// 永続ストレージ設定
#[derive(Debug, Clone)]
pub struct PersistentStorageConfig {
    /// データディレクトリ
    pub data_dir: PathBuf,
    /// MemTableサイズ閾値
    pub memtable_size: usize,
    /// SSTable最大サイズ
    pub sstable_max_size: usize,
    /// 圧縮間隔
    pub compaction_interval: u64,
    /// スナップショット間隔
    pub snapshot_interval: u64,
}

impl Default for PersistentStorageConfig {
    fn default() -> Self {
        Self {
            data_dir: PathBuf::from("./data"),
            memtable_size: 1000,
            sstable_max_size: 10 * 1024 * 1024, // 10MB
            compaction_interval: 3600, // 1時間
            snapshot_interval: 86400, // 24時間
        }
    }
}

/// 永続ストレージエンジン
#[derive(Debug)]
pub struct PersistentStorage {
    /// CIDマネージャー
    cid_manager: Arc<RwLock<CidManager>>,
    /// LSMツリーストレージ
    lsm_tree: Arc<RwLock<LSMTree>>,
    /// Merkle DAG
    merkle_dag: Arc<RwLock<MerkleDAG>>,
    /// MVCCマネージャー
    mvcc_manager: Arc<RwLock<MVCCManager>>,
    /// 設定
    config: PersistentStorageConfig,
}

/// ストレージ操作結果
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum StorageResult<T> {
    Success(T),
    NotFound,
    VersionConflict,
    IntegrityError(String),
    IOError(String),
}

/// グラフ永続化データ
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PersistedGraph {
    /// グラフCID
    pub cid: Cid,
    /// 頂点CIDのリスト
    pub vertex_cids: Vec<Cid>,
    /// エッジCIDのリスト
    pub edge_cids: Vec<Cid>,
    /// メタデータCID
    pub metadata_cid: Option<Cid>,
    /// タイムスタンプ
    pub timestamp: u64,
}

impl PersistentStorage {
    /// 新しい永続ストレージを作成
    pub fn new(config: PersistentStorageConfig) -> Result<Self> {
        // データディレクトリ作成
        std::fs::create_dir_all(&config.data_dir)?;

        let cid_manager = Arc::new(RwLock::new(CidManager::new()));
        let lsm_tree = Arc::new(RwLock::new(LSMTree::new(
            config.data_dir.join("lsm"),
            config.memtable_size,
            config.sstable_max_size,
        )?));
        let merkle_dag = Arc::new(RwLock::new(MerkleDAG::new()));
        let mvcc_manager = Arc::new(RwLock::new(MVCCManager::new()));

        Ok(Self {
            cid_manager,
            lsm_tree,
            merkle_dag,
            mvcc_manager,
            config,
        })
    }

    /// グラフを永続化
    pub fn store_graph(&self, graph: &Graph) -> Result<Cid> {
        let mut cid_manager = self.cid_manager.write();
        let mut merkle_dag = self.merkle_dag.write();

        // グラフのCIDを計算
        let graph_cid = cid_manager.compute_graph_cid(&GraphCore {
            nodes: graph.vertices.values().cloned().collect(),
            edges: graph.edges.values().cloned().collect(),
            boundary: None,
            attrs: None,
        })?;

        // 頂点を個別に格納
        let mut vertex_cids = Vec::new();
        for vertex in graph.vertices.values() {
            let vertex_cid = cid_manager.compute_node_cid(vertex)?;
            let vertex_key = format!("vertex:{}", vertex_cid.as_str());

            let vertex_data = serde_json::to_vec(vertex)?;
            self.store_data(&vertex_key, &vertex_data)?;

            vertex_cids.push(vertex_cid);
        }

        // エッジを個別に格納
        let mut edge_cids = Vec::new();
        for edge in graph.edges.values() {
            let edge_cid = cid_manager.compute_edge_cid(edge)?;
            let edge_key = format!("edge:{}", edge_cid.as_str());

            let edge_data = serde_json::to_vec(edge)?;
            self.store_data(&edge_key, &edge_data)?;

            edge_cids.push(edge_cid);
        }

        // 永続化メタデータを作成
        let persisted_graph = PersistedGraph {
            cid: graph_cid.clone(),
            vertex_cids,
            edge_cids,
            metadata_cid: None,
            timestamp: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_secs(),
        };

        // メタデータを格納
        let metadata_key = format!("graph:{}", graph_cid.as_str());
        let metadata_data = serde_json::to_vec(&persisted_graph)?;
        self.store_data(&metadata_key, &metadata_data)?;

        Ok(graph_cid)
    }

    /// CIDからグラフを復元
    pub fn load_graph(&self, cid: &Cid) -> Result<Graph> {
        let metadata_key = format!("graph:{}", cid.as_str());

        // メタデータを取得
        let metadata_data = match self.load_data(&metadata_key)? {
            StorageResult::Success(data) => data,
            StorageResult::NotFound => return Err(KotobaError::Storage("Graph not found".to_string())),
            _ => return Err(KotobaError::Storage("Failed to load graph metadata".to_string())),
        };

        let persisted_graph: PersistedGraph = serde_json::from_slice(&metadata_data)?;

        // 頂点を復元
        let mut vertices = HashMap::new();
        for vertex_cid in &persisted_graph.vertex_cids {
            let vertex_key = format!("vertex:{}", vertex_cid.as_str());
            let vertex_data = match self.load_data(&vertex_key)? {
                StorageResult::Success(data) => data,
                _ => continue, // 頂点が見つからない場合はスキップ
            };

            let vertex: VertexData = serde_json::from_slice(&vertex_data)?;
            vertices.insert(vertex.id, vertex);
        }

        // エッジを復元
        let mut edges = HashMap::new();
        for edge_cid in &persisted_graph.edge_cids {
            let edge_key = format!("edge:{}", edge_cid.as_str());
            let edge_data = match self.load_data(&edge_key)? {
                StorageResult::Success(data) => data,
                _ => continue, // エッジが見つからない場合はスキップ
            };

            let edge: EdgeData = serde_json::from_slice(&edge_data)?;
            edges.insert(edge.id, edge);
        }

        // グラフを再構築
        let mut graph = Graph::empty();

        // 頂点を追加
        for vertex in vertices.values() {
            graph.add_vertex(vertex.clone());
        }

        // エッジを追加
        for edge in edges.values() {
            graph.add_edge(edge.clone());
        }

        Ok(graph)
    }

    /// データを格納(CIDアドレス指定)
    pub fn store_data(&self, key: &str, data: &[u8]) -> Result<()> {
        let mut lsm_tree = self.lsm_tree.write();
        lsm_tree.put(key.to_string(), data.to_vec());
        Ok(())
    }

    /// データを読み込み(CIDアドレス指定)
    pub fn load_data(&self, key: &str) -> Result<StorageResult<Vec<u8>>> {
        let lsm_tree = self.lsm_tree.read();

        match lsm_tree.get(key)? {
            Some(data) => Ok(StorageResult::Success(data)),
            None => Ok(StorageResult::NotFound),
        }
    }

    /// データを削除
    pub fn delete_data(&self, key: &str) -> Result<()> {
        let mut lsm_tree = self.lsm_tree.write();
        lsm_tree.delete(key.to_string());
        Ok(())
    }

    /// Merkleルートを取得
    pub fn get_merkle_root(&self) -> ContentHash {
        let merkle_dag = self.merkle_dag.read();
        // 簡易版:全ノードのハッシュをまとめて計算
        let mut hasher = sha2::Sha256::new();
        let mut sorted_hashes: Vec<_> = merkle_dag.nodes.keys().collect();
        sorted_hashes.sort();

        for hash in sorted_hashes {
            hasher.update(hash.0.as_bytes());
        }

        ContentHash(format!("{:x}", hasher.finalize()))
    }

    /// データ整合性を検証
    pub fn verify_integrity(&self) -> Result<bool> {
        let merkle_dag = self.merkle_dag.read();

        // Merkle DAGの整合性を検証
        for node in merkle_dag.nodes.values() {
            let mut hasher = sha2::Sha256::new();
            hasher.update(&node.data);
            for child in &node.children {
                hasher.update(child.0.as_bytes());
            }

            let computed_hash = format!("{:x}", hasher.finalize());
            if computed_hash != node.hash.0 {
                return Ok(false);
            }
        }

        Ok(true)
    }

    /// スナップショットを作成
    pub fn create_snapshot(&self) -> Result<String> {
        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();

        let snapshot_id = format!("snapshot_{}", timestamp);

        // LSMツリーのスナップショット
        let lsm_tree = self.lsm_tree.read();
        lsm_tree.create_snapshot(&snapshot_id)?;

        // Merkle DAGのスナップショット
        let merkle_dag = self.merkle_dag.read();
        let merkle_snapshot_path = self.config.data_dir.join(format!("merkle_{}", snapshot_id));
        let merkle_data = serde_json::to_vec(&merkle_dag.nodes)?;
        std::fs::write(merkle_snapshot_path, merkle_data)?;

        Ok(snapshot_id)
    }

    /// スナップショットから復元
    pub fn restore_from_snapshot(&self, snapshot_id: &str) -> Result<()> {
        // LSMツリーの復元
        let mut lsm_tree = self.lsm_tree.write();
        lsm_tree.restore_from_snapshot(snapshot_id)?;

        // Merkle DAGの復元
        let merkle_snapshot_path = self.config.data_dir.join(format!("merkle_{}", snapshot_id));
        if merkle_snapshot_path.exists() {
            let merkle_data = std::fs::read(merkle_snapshot_path)?;
            let nodes: HashMap<ContentHash, MerkleNode> = serde_json::from_slice(&merkle_data)?;
            let mut merkle_dag = self.merkle_dag.write();
            merkle_dag.nodes = nodes;
        }

        Ok(())
    }

    /// 圧縮を実行
    pub fn compact(&self) -> Result<()> {
        let mut lsm_tree = self.lsm_tree.write();
        lsm_tree.compact()?;
        Ok(())
    }

    /// 統計情報を取得
    pub fn get_stats(&self) -> StorageStats {
        let lsm_tree = self.lsm_tree.read();
        let merkle_dag = self.merkle_dag.read();
        let mvcc_manager = self.mvcc_manager.read();

        StorageStats {
            lsm_entries: lsm_tree.stats().total_entries,
            merkle_nodes: merkle_dag.len(),
            active_transactions: mvcc_manager.active_transactions(),
            data_size: lsm_tree.stats().total_size,
        }
    }

    /// クリーンアップ(古いデータを削除)
    pub fn cleanup(&self, max_age_days: u64) -> Result<()> {
        let cutoff_time = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs()
            .saturating_sub(max_age_days * 24 * 3600);

        let mut lsm_tree = self.lsm_tree.write();
        lsm_tree.cleanup(cutoff_time)?;

        Ok(())
    }
}

/// ストレージ統計情報
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageStats {
    /// LSMエントリ数
    pub lsm_entries: usize,
    /// Merkleノード数
    pub merkle_nodes: usize,
    /// アクティブトランザクション数
    pub active_transactions: usize,
    /// データサイズ(バイト)
    pub data_size: u64,
}

/// 分散ストレージマネージャー
#[derive(Debug)]
pub struct DistributedStorageManager {
    /// ローカルストレージ
    local_storage: Arc<PersistentStorage>,
    /// 分散ノード情報
    nodes: HashMap<NodeId, NodeStorageInfo>,
    /// 整合性チェック設定
    consistency_config: ConsistencyConfig,
}

/// 整合性設定
#[derive(Debug, Clone)]
pub struct ConsistencyConfig {
    /// 整合性チェック間隔(秒)
    pub check_interval_secs: u64,
    /// レプリケーション係数
    pub replication_factor: usize,
    /// 読み取り整合性レベル
    pub read_consistency: ConsistencyLevel,
    /// 書き込み整合性レベル
    pub write_consistency: ConsistencyLevel,
}

/// 整合性レベル
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ConsistencyLevel {
    /// 1つのノードのみ
    One,
    /// クォーラム(過半数)
    Quorum,
    /// 全ノード
    All,
}

/// 整合性チェック結果
#[derive(Debug, Clone)]
pub struct ConsistencyCheck {
    /// チェック対象のCID
    pub cid: Cid,
    /// 利用可能なノード数
    pub available_nodes: usize,
    /// 必要なレプリケーション数
    pub required_replicas: usize,
    /// 整合性があるかどうか
    pub is_consistent: bool,
    /// 欠損しているノード
    pub missing_nodes: Vec<NodeId>,
    /// 破損しているノード
    pub corrupted_nodes: Vec<NodeId>,
}

/// 競合解決結果
#[derive(Debug, Clone)]
pub struct ConflictResolution {
    /// 解決されたCID
    pub cid: Cid,
    /// 使用されたバージョン
    pub resolved_version: Cid,
    /// 競合していたバージョン
    pub conflicting_versions: Vec<Cid>,
    /// 解決方法
    pub resolution_method: ConflictResolutionMethod,
}

/// 競合解決方法
#[derive(Debug, Clone)]
pub enum ConflictResolutionMethod {
    /// 最新のタイムスタンプを使用
    LatestTimestamp,
    /// 最も古いタイムスタンプを使用
    OldestTimestamp,
    /// マージ(可能な場合)
    Merge,
    /// 手動解決が必要
    Manual,
}

/// ノードストレージ情報
#[derive(Debug, Clone)]
pub struct NodeStorageInfo {
    /// ノードID
    pub node_id: NodeId,
    /// アドレス
    pub address: String,
    /// 保持するCID範囲
    pub cid_ranges: Vec<CidRange>,
    /// 最終同期時刻
    pub last_sync: u64,
}

impl DistributedStorageManager {
    /// 新しい分散ストレージマネージャーを作成
    pub fn new(local_storage: Arc<PersistentStorage>) -> Self {
        Self {
            local_storage,
            nodes: HashMap::new(),
            consistency_config: ConsistencyConfig {
                check_interval_secs: 300, // 5分
                replication_factor: 3,
                read_consistency: ConsistencyLevel::Quorum,
                write_consistency: ConsistencyLevel::Quorum,
            },
        }
    }

    /// 整合性設定を更新
    pub fn with_consistency_config(mut self, config: ConsistencyConfig) -> Self {
        self.consistency_config = config;
        self
    }

    /// CIDの整合性をチェック
    pub async fn check_consistency(&self, cid: &Cid) -> Result<ConsistencyCheck> {
        let responsible_nodes = self.get_responsible_nodes(cid);
        let mut available_nodes = 0;
        let mut missing_nodes = Vec::new();
        let mut corrupted_nodes = Vec::new();

        // 各ノードからデータを取得して比較
        let mut reference_data: Option<Vec<u8>> = None;
        let mut data_found = false;

        for node_info in &responsible_nodes {
            match self.fetch_data_from_node(node_info, cid).await {
                Ok(data) => {
                    available_nodes += 1;
                    data_found = true;

                    if let Some(ref ref_data) = reference_data {
                        if *ref_data != data {
                            corrupted_nodes.push(node_info.node_id.clone());
                        }
                    } else {
                        reference_data = Some(data);
                    }
                }
                Err(_) => {
                    missing_nodes.push(node_info.node_id.clone());
                }
            }
        }

        let required_replicas = self.consistency_config.replication_factor;
        let is_consistent = data_found &&
                           available_nodes >= required_replicas &&
                           corrupted_nodes.is_empty() &&
                           missing_nodes.len() <= (responsible_nodes.len() - required_replicas);

        Ok(ConsistencyCheck {
            cid: cid.clone(),
            available_nodes,
            required_replicas,
            is_consistent,
            missing_nodes,
            corrupted_nodes,
        })
    }

    /// データをレプリケート
    pub async fn replicate_data(&self, cid: &Cid, data: &[u8], replication_factor: usize) -> Result<()> {
        let responsible_nodes = self.get_responsible_nodes(cid);

        for node_info in responsible_nodes.iter().take(replication_factor) {
            self.send_data_to_node(node_info, cid, data).await?;
        }

        Ok(())
    }

    /// 競合を解決
    pub async fn resolve_conflicts(&self, cid: &Cid, versions: &[Cid]) -> Result<ConflictResolution> {
        if versions.is_empty() {
            return Err(KotobaError::Storage("No versions provided".to_string()));
        }

        if versions.len() == 1 {
            return Ok(ConflictResolution {
                cid: cid.clone(),
                resolved_version: versions[0].clone(),
                conflicting_versions: vec![],
                resolution_method: ConflictResolutionMethod::LatestTimestamp,
            });
        }

        // 各バージョンのタイムスタンプを取得
        let mut version_info = Vec::new();
        for version in versions {
            if let Ok(data) = self.local_storage.load_data(&format!("cid:{}", version.as_str())) {
                if let StorageResult::Success(data_bytes) = data {
                    // 簡易版:データサイズをタイムスタンプの代わりに使用
                    version_info.push((version.clone(), data_bytes.len() as u64));
                }
            }
        }

        // 最新のタイムスタンプを持つバージョンを選択
        version_info.sort_by_key(|(_, timestamp)| *timestamp);
        let resolved_version = version_info.last().unwrap().0.clone();

        let conflicting_versions = versions.iter()
            .filter(|v| *v != &resolved_version)
            .cloned()
            .collect();

        Ok(ConflictResolution {
            cid: cid.clone(),
            resolved_version,
            conflicting_versions,
            resolution_method: ConflictResolutionMethod::LatestTimestamp,
        })
    }

    /// 読み取り操作の整合性を確保
    pub async fn ensure_read_consistency(&self, cid: &Cid) -> Result<Option<Vec<u8>>> {
        match self.consistency_config.read_consistency {
            ConsistencyLevel::One => {
                // 1つのノードから読み取り
                self.local_storage.load_data(&format!("cid:{}", cid.as_str()))
                    .map(|result| match result {
                        StorageResult::Success(data) => Some(data),
                        _ => None,
                    })
            }
            ConsistencyLevel::Quorum => {
                // クォーラムから読み取り
                let check = self.check_consistency(cid).await?;
                if check.is_consistent && check.available_nodes >= check.required_replicas {
                    self.local_storage.load_data(&format!("cid:{}", cid.as_str()))
                        .map(|result| match result {
                            StorageResult::Success(data) => Some(data),
                            _ => None,
                        })
                } else {
                    Ok(None)
                }
            }
            ConsistencyLevel::All => {
                // 全ノードから読み取り
                let responsible_nodes = self.get_responsible_nodes(cid);
                let total_nodes = responsible_nodes.len();
                let check = self.check_consistency(cid).await?;

                if check.available_nodes == total_nodes && check.is_consistent {
                    self.local_storage.load_data(&format!("cid:{}", cid.as_str()))
                        .map(|result| match result {
                            StorageResult::Success(data) => Some(data),
                            _ => None,
                        })
                } else {
                    Ok(None)
                }
            }
        }
    }

    /// 書き込み操作の整合性を確保
    pub async fn ensure_write_consistency(&self, cid: &Cid, data: &[u8]) -> Result<()> {
        // データをローカルに書き込み
        self.local_storage.store_data(&format!("cid:{}", cid.as_str()), data)?;

        match self.consistency_config.write_consistency {
            ConsistencyLevel::One => {
                // ローカル書き込みのみ
                Ok(())
            }
            ConsistencyLevel::Quorum | ConsistencyLevel::All => {
                // 指定された数のノードにレプリケート
                let replication_count = match self.consistency_config.write_consistency {
                    ConsistencyLevel::Quorum => (self.nodes.len() / 2) + 1,
                    ConsistencyLevel::All => self.nodes.len(),
                    _ => unreachable!(),
                };

                self.replicate_data(cid, data, replication_count.max(1)).await?;
                Ok(())
            }
        }
    }

    /// ヘルパーメソッド:CIDを担当するノードを取得
    fn get_responsible_nodes(&self, cid: &Cid) -> Vec<&NodeStorageInfo> {
        let mut responsible = Vec::new();

        for node_info in self.nodes.values() {
            for range in &node_info.cid_ranges {
                let hash = cid.as_str().as_bytes();
                let hash_value = hash.iter().fold(0u64, |acc, &b| acc.wrapping_add(b as u64));

                if hash_value >= range.start && hash_value <= range.end {
                    responsible.push(node_info);
                    break;
                }
            }
        }

        responsible
    }

    /// ヘルパーメソッド:ノードからデータを取得(簡易版)
    async fn fetch_data_from_node(&self, node_info: &NodeStorageInfo, cid: &Cid) -> Result<Vec<u8>> {
        // 実際の実装ではネットワーク通信を行う
        // ここではローカルノードのみをチェック
        if node_info.node_id.0 == "local" {
            match self.local_storage.load_data(&format!("cid:{}", cid.as_str()))? {
                StorageResult::Success(data) => Ok(data),
                _ => Err(KotobaError::Storage("Data not found".to_string())),
            }
        } else {
            Err(KotobaError::Storage("Remote node communication not implemented".to_string()))
        }
    }

    /// ヘルパーメソッド:ノードにデータを送信(簡易版)
    async fn send_data_to_node(&self, node_info: &NodeStorageInfo, cid: &Cid, data: &[u8]) -> Result<()> {
        // 実際の実装ではネットワーク通信を行う
        // ここではローカルノードのみをサポート
        if node_info.node_id.0 == "local" {
            self.local_storage.store_data(&format!("cid:{}", cid.as_str()), data)?;
            Ok(())
        } else {
            Err(KotobaError::Storage("Remote node communication not implemented".to_string()))
        }
    }

    /// CIDの担当ノードを決定
    pub fn get_responsible_node(&self, cid: &Cid) -> Option<&NodeStorageInfo> {
        // CIDハッシュに基づいて担当ノードを決定
        let hash = cid.as_str().as_bytes();
        let hash_value = hash.iter().fold(0u64, |acc, &b| acc.wrapping_add(b as u64));

        for node_info in self.nodes.values() {
            for range in &node_info.cid_ranges {
                if hash_value >= range.start && hash_value <= range.end {
                    return Some(node_info);
                }
            }
        }

        None
    }

    /// データをレプリケート
    pub async fn replicate_data(&self, cid: &Cid, data: &[u8], replication_factor: usize) -> Result<()> {
        // 担当ノードを取得
        let responsible_nodes: Vec<_> = (0..replication_factor)
            .filter_map(|_| self.get_responsible_node(cid))
            .collect();

        // 各ノードにデータを送信(簡易版)
        for node_info in responsible_nodes {
            println!("Replicating {} to node {}", cid.as_str(), node_info.node_id.0);
            // 実際の実装ではネットワーク通信を行う
        }

        Ok(())
    }

    /// データの整合性を検証
    pub async fn verify_consistency(&self, cid: &Cid) -> Result<bool> {
        // 複数のノードから同じCIDのデータを取得して比較
        // 簡易版:ローカルのみチェック
        match self.local_storage.load_data(&format!("cid:{}", cid.as_str()))? {
            StorageResult::Success(_) => Ok(true),
            _ => Ok(false),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    fn create_test_config() -> PersistentStorageConfig {
        let temp_dir = tempdir().unwrap();
        PersistentStorageConfig {
            data_dir: temp_dir.path().to_path_buf(),
            memtable_size: 10,
            sstable_max_size: 1024,
            compaction_interval: 3600,
            snapshot_interval: 86400,
        }
    }

    fn create_test_graph() -> Graph {
        let mut graph = Graph::empty();

        let v1 = graph.add_vertex(VertexData {
            id: VertexId::new("v1").unwrap(),
            labels: vec!["Person".to_string()],
            props: HashMap::new(),
        });

        let v2 = graph.add_vertex(VertexData {
            id: VertexId::new("v2").unwrap(),
            labels: vec!["Person".to_string()],
            props: HashMap::new(),
        });

        graph.add_edge(EdgeData {
            id: EdgeId::new("e1").unwrap(),
            src: v1,
            dst: v2,
            label: "FOLLOWS".to_string(),
            props: HashMap::new(),
        });

        graph
    }

    #[test]
    fn test_store_and_load_graph() {
        let config = create_test_config();
        let storage = PersistentStorage::new(config).unwrap();
        let test_graph = create_test_graph();

        // グラフを格納
        let cid = storage.store_graph(&test_graph).unwrap();

        // グラフを読み込み
        let loaded_graph = storage.load_graph(&cid).unwrap();

        // 比較
        assert_eq!(loaded_graph.vertices.len(), test_graph.vertices.len());
        assert_eq!(loaded_graph.edges.len(), test_graph.edges.len());
    }

    #[test]
    fn test_data_operations() {
        let config = create_test_config();
        let storage = PersistentStorage::new(config).unwrap();

        let key = "test_key";
        let data = b"test_data";

        // データを格納
        storage.store_data(key, data).unwrap();

        // データを読み込み
        match storage.load_data(key).unwrap() {
            StorageResult::Success(loaded_data) => assert_eq!(loaded_data, data),
            _ => panic!("Data not found"),
        }

        // データを削除
        storage.delete_data(key).unwrap();

        // 削除されたことを確認
        match storage.load_data(key).unwrap() {
            StorageResult::NotFound => {} // OK
            _ => panic!("Data should be deleted"),
        }
    }

    #[test]
    fn test_integrity_verification() {
        let config = create_test_config();
        let storage = PersistentStorage::new(config).unwrap();

        // 初期状態では整合性がある
        assert!(storage.verify_integrity().unwrap());
    }

    #[test]
    fn test_storage_stats() {
        let config = create_test_config();
        let storage = PersistentStorage::new(config).unwrap();

        let stats = storage.get_stats();
        assert_eq!(stats.lsm_entries, 0);
        assert_eq!(stats.merkle_nodes, 0);
        assert_eq!(stats.active_transactions, 0);
    }
}