File size: 1,642 Bytes
84d2a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use std::path::{Path, PathBuf};

use common::tar_ext;
use io::file_operations::{atomic_save_json, read_json};
use serde::{Deserialize, Serialize};

use crate::operations::types::CollectionResult;
use crate::shards::shard::PeerId;

pub const SHARD_CONFIG_FILE: &str = "shard_config.json";

#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
pub enum ShardType {
    Local,                      // Deprecated
    Remote { peer_id: PeerId }, // Deprecated
    Temporary,                  // Deprecated
    ReplicaSet,
}

#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
pub struct ShardConfig {
    pub r#type: ShardType,
}

impl ShardConfig {
    pub fn get_config_path(shard_path: &Path) -> PathBuf {
        shard_path.join(SHARD_CONFIG_FILE)
    }

    pub fn new_replica_set() -> Self {
        Self {
            r#type: ShardType::ReplicaSet,
        }
    }

    pub fn load(shard_path: &Path) -> CollectionResult<Option<Self>> {
        let config_path = Self::get_config_path(shard_path);
        if !config_path.exists() {
            log::info!("Detected missing shard config file in {:?}", shard_path);
            return Ok(None);
        }
        Ok(Some(read_json(&config_path)?))
    }

    pub fn save(&self, shard_path: &Path) -> CollectionResult<()> {
        let config_path = Self::get_config_path(shard_path);
        Ok(atomic_save_json(&config_path, self)?)
    }

    pub async fn save_to_tar(&self, tar: &tar_ext::BuilderExt) -> CollectionResult<()> {
        let bytes = serde_json::to_vec(self)?;
        tar.append_data(bytes, Path::new(SHARD_CONFIG_FILE)).await?;
        Ok(())
    }
}