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
use std::{path::PathBuf, sync::atomic::AtomicBool};

use atomic::{Atomic, Ordering};
use zng_clone_move::clmv;
use zng_ext_fs_watcher::WATCHER;
use zng_var::{ReadOnlyArcVar, VarUpdateId, VARS};

use super::*;

/// Config source that auto syncs with file.
///
/// The [`WATCHER.sync`] is used to synchronize with the file, this type implements the binding
/// for each key.
///
/// [`WATCHER.sync`]: WATCHER::sync
pub struct SyncConfig<M: ConfigMap> {
    sync_var: ArcVar<M>,
    status: ReadOnlyArcVar<ConfigStatus>,
    shared: ConfigVars,
}
impl<M: ConfigMap> SyncConfig<M> {
    /// Open write the `file`
    pub fn sync(file: impl Into<PathBuf>) -> Self {
        let (sync_var, status) = WATCHER.sync_status::<_, _, ConfigStatusError, ConfigStatusError>(
            file,
            M::empty(),
            |r| match (|| M::read(r?))() {
                Ok(ok) => Ok(Some(ok)),
                Err(e) => {
                    tracing::error!("sync config read error, {e:?}");
                    Err(vec![Arc::new(e)])
                }
            },
            |map, w| {
                let r = (|| {
                    let mut w = w?;
                    map.write(&mut w)?;
                    w.commit()
                })();
                match r {
                    Ok(()) => Ok(()),
                    Err(e) => {
                        tracing::error!("sync config write error, {e:?}");
                        Err(vec![Arc::new(e)])
                    }
                }
            },
        );

        Self {
            sync_var,
            status,
            shared: ConfigVars::default(),
        }
    }

    fn get_new_raw(sync_var: &ArcVar<M>, key: ConfigKey, default: RawConfigValue, insert: bool) -> BoxedVar<RawConfigValue> {
        // init var to already present value, or default.
        let mut used_default = false;
        let var = match sync_var.with(|m| ConfigMap::get_raw(m, &key)) {
            Ok(raw) => {
                // get ok
                match raw {
                    Some(raw) => var(raw),
                    None => {
                        used_default = true;
                        var(default)
                    }
                }
            }
            Err(e) => {
                // get error
                tracing::error!("sync config get({key:?}) error, {e:?}");
                used_default = true;
                var(default)
            }
        };

        if insert && used_default {
            var.update();
        }

        // bind entry var

        // config -> entry
        let wk_var = var.downgrade();
        let last_update = Atomic::new(VarUpdateId::never());
        let request_update = AtomicBool::new(used_default);
        sync_var
            .hook(clmv!(key, |map| {
                let update_id = VARS.update_id();
                if update_id == last_update.load(Ordering::Relaxed) {
                    return true;
                }
                last_update.store(update_id, Ordering::Relaxed);
                if let Some(var) = wk_var.upgrade() {
                    match map.value().get_raw(&key) {
                        Ok(raw) => {
                            // get ok
                            if let Some(raw) = raw {
                                var.set(raw);
                                if request_update.swap(false, Ordering::Relaxed) {
                                    // restored after reset, var can already have the value,
                                    // but upstream bindings are stale, cause an update.
                                    var.update();
                                }
                            } else {
                                // backend lost entry but did not report as error, probably a reset.
                                request_update.store(true, Ordering::Relaxed);
                            }
                        }
                        Err(e) => {
                            // get error
                            tracing::error!("sync config get({key:?}) error, {e:?}");
                        }
                    }
                    // retain hook
                    true
                } else {
                    // entry var dropped, drop hook
                    false
                }
            }))
            .perm();

        // entry -> config
        let wk_sync_var = sync_var.downgrade();
        let last_update = Atomic::new(VarUpdateId::never());
        var.hook(clmv!(|value| {
            let update_id = VARS.update_id();
            if update_id == last_update.load(Ordering::Relaxed) {
                return true;
            }
            last_update.store(update_id, Ordering::Relaxed);
            if let Some(sync_var) = wk_sync_var.upgrade() {
                let raw = value.value().clone();
                sync_var.modify(clmv!(key, |m| {
                    // set, only if actually changed
                    match ConfigMap::set_raw(m, key.clone(), raw) {
                        Ok(()) => {
                            // set ok
                        }
                        Err(e) => {
                            // set error
                            tracing::error!("sync config set({key:?}) error, {e:?}");
                        }
                    }
                }));

                // retain hook
                true
            } else {
                // config dropped, drop hook
                false
            }
        }))
        .perm();

        var.boxed()
    }

    fn get_new<T: ConfigValue>(sync_var: &ArcVar<M>, key: impl Into<ConfigKey>, default: T, insert: bool) -> BoxedVar<T> {
        // init var to already present value, or default.
        let key = key.into();
        let mut used_default = false;
        let var = match sync_var.with(|m| ConfigMap::get::<T>(m, &key)) {
            Ok(value) => match value {
                Some(val) => var(val),
                None => {
                    used_default = true;
                    var(default)
                }
            },
            Err(e) => {
                tracing::error!("sync config get({key:?}) error, {e:?}");
                used_default = true;
                var(default)
            }
        };

        if insert && used_default {
            var.update();
        }

        // bind entry var

        // config -> entry
        let wk_var = var.downgrade();
        sync_var
            .hook(clmv!(key, |map| {
                if let Some(var) = wk_var.upgrade() {
                    match map.value().get::<T>(&key) {
                        Ok(value) => {
                            if let Some(value) = value {
                                var.set(value);
                            }
                        }
                        Err(e) => {
                            tracing::error!("sync config get({key:?}) error, {e:?}");
                        }
                    }
                    true
                } else {
                    false
                }
            }))
            .perm();

        // entry -> config
        let wk_sync_var = sync_var.downgrade();
        var.hook(clmv!(|value| {
            if let Some(sync_var) = wk_sync_var.upgrade() {
                let value = value.value().clone();
                sync_var.modify(clmv!(key, |m| {
                    match ConfigMap::set(m, key.clone(), value) {
                        Ok(()) => {}
                        Err(e) => {
                            tracing::error!("sync config set({key:?}) error, {e:?}");
                        }
                    }
                }));
                true
            } else {
                false
            }
        }))
        .perm();

        var.boxed()
    }
}
impl<M: ConfigMap> AnyConfig for SyncConfig<M> {
    fn get_raw(&mut self, key: ConfigKey, default: RawConfigValue, insert: bool, shared: bool) -> BoxedVar<RawConfigValue> {
        if shared {
            self.shared
                .get_or_bind(key, |key| Self::get_new_raw(&self.sync_var, key.clone(), default, insert))
        } else {
            Self::get_new_raw(&self.sync_var, key, default, insert)
        }
    }

    fn contains_key(&mut self, key: ConfigKey) -> BoxedVar<bool> {
        self.sync_var.map(move |q| q.contains_key(&key)).boxed()
    }

    fn status(&self) -> BoxedVar<ConfigStatus> {
        self.status.clone().boxed()
    }

    fn remove(&mut self, key: &ConfigKey) -> bool {
        let contains = self.sync_var.with(|q| q.contains_key(key));
        if contains {
            self.sync_var.modify(clmv!(key, |m| {
                ConfigMap::remove(m, &key);
            }));
        }
        contains
    }

    fn low_memory(&mut self) {
        self.shared.low_memory();
    }
}
impl<M: ConfigMap> Config for SyncConfig<M> {
    fn get<T: ConfigValue>(&mut self, key: impl Into<ConfigKey>, default: T, insert: bool) -> BoxedVar<T> {
        self.shared
            .get_or_bind(key.into(), |key| Self::get_new(&self.sync_var, key.clone(), default, insert))
    }
}