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
use std::{
    any::{Any, TypeId},
    hash::Hasher,
    sync::Arc,
};

use compact_str::CompactString;
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;

use crate::{archive, config::GroupContext, ExportOptions, ImportOptions};

macro_rules! id_type {
    ($id:ident) => {
        #[derive(
            Debug,
            Clone,
            Copy,
            Hash,
            PartialEq,
            Eq,
            PartialOrd,
            Ord,
            derive_more::From,
            derive_more::Display,
            Serialize,
            Deserialize
        )]
        pub struct $id(pub u64);
    };
}

id_type!(PathHash);
id_type!(GroupID);
id_type!(ItemID);

impl PathHash {
    pub fn new<'a>(paths: impl IntoIterator<Item = &'a str>) -> Self {
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        paths.into_iter().for_each(|x| hasher.write(x.as_bytes()));
        Self(hasher.finish())
    }
}

impl GroupID {
    pub(crate) fn new_unique() -> Self {
        static ID_GEN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
        Self(ID_GEN.fetch_add(1, std::sync::atomic::Ordering::Relaxed))
    }
}

impl ItemID {
    pub(crate) fn new_unique() -> Self {
        static ID_GEN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
        Self(ID_GEN.fetch_add(1, std::sync::atomic::Ordering::Relaxed))
    }
}

///
/// ?
///
#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("Storage driver is disposed")]
    ExpiredStorage,

    #[error("Config name is duplicated {0:?}")]
    GroupCreationFailed(Arc<Vec<CompactString>>),

    #[error("Path exist with different type")]
    MismatchedTypeID,

    #[error("Deserialization failed")]
    DeserializationFailed(#[from] erased_serde::Error),

    #[error("Validation failed")]
    ValueValidationFailed,
}

///
///
/// Message type to drive storage
///
pub(crate) enum ControlDirective {
    FromMonitor(MonitorEvent),

    TryGroupRegister(Box<GroupRegisterParam>),

    TryFindGroup {
        path_hash: PathHash,
        type_id: TypeId,

        reply: oneshot::Sender<Result<FoundGroupInfo, GroupFindError>>,
    },

    GroupDisposal(GroupID),

    Fence(oneshot::Sender<()>),

    EntityValueUpdate {
        group_id: GroupID,
        item_id: ItemID,
        silent_mode: bool,
    },

    MonitorRegister {
        reply_to: oneshot::Sender<flume::Receiver<ReplicationEvent>>,
    },

    Import {
        body: archive::Archive,
        option: ImportOptions,
    },

    Export {
        /// If None is specified,
        destination: oneshot::Sender<archive::Archive>,
        option: ExportOptions,
    },

    Close,
}

#[derive(thiserror::Error, Debug)]
pub enum GroupFindError {
    #[error("Given path was not found")]
    PathNotFound,
    #[error("Type ID mismatch from original registration")]
    MismatchedTypeID,
    #[error("The original group was already disposed")]
    ExpiredStorage,
}

/// Contains all necessary information to construct a group
pub(crate) struct FoundGroupInfo {
    pub context: Arc<GroupContext>,

    /// Locked before used, since if we deliver the weak pointer as-is, it might be expired
    /// before the receiver uses it.
    pub unregister_hook: Arc<dyn Any + Send + Sync>,
}

pub(crate) struct GroupRegisterParam {
    pub group_id: GroupID,
    pub context: Arc<GroupContext>,
    pub event_broadcast: async_broadcast::Sender<()>,
    pub reply_success: oneshot::Sender<Result<(), Error>>,
}

pub enum MonitorEvent {
    GroupUpdateNotify { updates: SmallVec<[GroupID; 4]> },
}

///
///
/// Message type to notify backend
///
#[derive(Clone)]
pub enum ReplicationEvent {
    InitialGroups(Vec<(GroupID, Arc<GroupContext>)>),
    GroupAdded(GroupID, Arc<GroupContext>),
    GroupRemoved(GroupID),
    EntityValueUpdated { group_id: GroupID, item_id: ItemID },
}