wedb_standalone 0.1.2

Standalone server foundation: RESP protocol, AOF logic layer, and node service orchestration
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
//! HyperLogLog RESP 命令(对标 libs/server/Resp/HyperLogLog/HyperLogLogCommands.cs
//! 与 libs/server/Storage/Session/MainStore/HyperLogLogOps.cs 的 RMW 语义)
//!
//! PFADD/PFCOUNT/PFMERGE 直读本模块 [`HyperLogLog`] 的稀疏/稠密编码;
//! 载荷按稀疏实际占用截断存储,稠密恒为 12304 字节。

use crate::resp::{
  hyperloglog::hyper_log_log::{HyperLogLog, SPARSE_SIZE_MAX_CAP},
  resp_server_session::RespServerSession,
};

/// WRONGTYPE 错误帧(对标 CmdStrings.RESP_ERR_WRONG_TYPE_HLL)
const RESP_ERR_WRONG_TYPE_HLL: &[u8] =
  b"-WRONGTYPE Key is not a valid HyperLogLog string value.\r\n";

/// 从存储装载 HLL 载荷并校验;缺失返回 Ok(None),非法类型返回 Err(())
fn load_hll<'s>(
  hll: &HyperLogLog,
  store: &wkv::BatchStoreSession<'s, impl wdev::Device>,
  key: &[u8],
) -> Result<Option<Vec<u8>>, ()> {
  match store.try_read_sync(key, |v| v.to_vec()) {
    Ok(Some(Some(raw))) => {
      if hll.is_valid_hyll_len(&raw, raw.len()) {
        Ok(Some(raw))
      } else {
        Err(())
      }
    }
    Ok(Some(None)) | Ok(None) => Ok(None),
    Err(_) => Err(()),
  }
}

/// 稀疏载荷回写(保底下限长度)
fn store_hll(
  hll: &HyperLogLog,
  store: &wkv::BatchStoreSession<impl wdev::Device>,
  key: &[u8],
  blob: &[u8],
) {
  // 长度不得低于初始分配(274B,C# IsValidHLLLength 的下界校验),不足处零填充
  let len = if hll.is_sparse(blob) {
    hll
      .sparse_current_size_in_bytes(blob)
      .max(hll.sparse_bytes())
  } else {
    hll.dense_bytes()
  };
  let _ = store.try_upsert_sync(key, &blob[..len]);
}

impl RespServerSession {
  /// PFADD key [element ...]:登记元素,寄存器有变更回 :1 否则 :0
  ///
  /// libs/server/Resp/HyperLogLog/HyperLogLogCommands.cs:HyperLogLogAdd
  pub fn hyper_log_log_add<'a, D: wdev::Device>(
    &mut self,
    parse_state: &[&[u8]],
    store: &wkv::BatchStoreSession<'a, D>,
    output: &mut Vec<u8>,
  ) -> wresp::Result<bool> {
    if parse_state.is_empty() {
      output.extend_from_slice(b"-ERR wrong number of arguments for 'PFADD' command\r\n");
      return Ok(true);
    }

    let key = parse_state[0];
    let elements = &parse_state[1..];
    let hll = HyperLogLog::new();

    let existing = match load_hll(&hll, store, key) {
      Ok(existing) => existing,
      Err(()) => {
        output.extend_from_slice(RESP_ERR_WRONG_TYPE_HLL);
        return Ok(true);
      }
    };

    let mut updated = false;
    match existing {
      None => {
        // 新键:按元素数推算初始长度(超上限即稠密),init 内部按长度分派编码
        let initial = hll.sparse_initial_length(elements.len());
        let mut blob = vec![0_u8; initial];
        hll.init(elements, &mut blob);
        updated = true;
        store_hll(&hll, store, key, &blob);
      }
      Some(raw) => {
        if hll.is_dense(&raw) {
          let mut blob = raw;
          hll.update(elements, &mut blob, &mut updated);
          if updated {
            store_hll(&hll, store, key, &blob);
          }
        } else {
          // 稀疏:可原位更新则原位,否则经 update_grow 扩容/稠密化
          let mut blob = raw;
          if !hll.update(elements, &mut blob, &mut updated) {
            let new_len = hll.update_grow(elements.len(), &blob);
            let mut grown = vec![0_u8; new_len];
            hll.copy_update(elements, &blob, &mut grown);
            updated = true;
            store_hll(&hll, store, key, &grown);
          } else if updated {
            store_hll(&hll, store, key, &blob);
          }
        }
      }
    }

    output.extend_from_slice(if updated { b":1\r\n" } else { b":0\r\n" });
    Ok(true)
  }

  /// PFCOUNT key [key ...]:单键直读基数;多键做虚拟并集(不改写存储)
  ///
  /// libs/server/Resp/HyperLogLog/HyperLogLogCommands.cs:HyperLogLogLength
  pub fn hyper_log_log_length<'a, D: wdev::Device>(
    &mut self,
    parse_state: &[&[u8]],
    store: &wkv::BatchStoreSession<'a, D>,
    output: &mut Vec<u8>,
  ) -> wresp::Result<bool> {
    if parse_state.is_empty() {
      output.extend_from_slice(b"-ERR wrong number of arguments for 'PFCOUNT' command\r\n");
      return Ok(true);
    }

    let hll = HyperLogLog::new();

    // 装载全部键的稠密视图(稀疏临时稠密化)
    let mut dense: Option<Vec<u8>> = None;
    for key in parse_state {
      match load_hll(&hll, store, key) {
        Err(()) => {
          output.extend_from_slice(RESP_ERR_WRONG_TYPE_HLL);
          return Ok(true);
        }
        Ok(None) => continue,
        Ok(Some(raw)) => {
          let mut view = vec![0_u8; hll.dense_bytes()];
          if hll.is_sparse(&raw) {
            hll.init_dense(&mut view);
            hll.sparse_to_dense(&raw, &mut view);
          } else {
            view.copy_from_slice(&raw[..hll.dense_bytes()]);
          }

          match &mut dense {
            None => dense = Some(view),
            Some(dst) => {
              hll.dense_to_dense(&view, dst);
            }
          }
        }
      }
    }

    let card = dense.map(|mut d| hll.count(&mut d)).unwrap_or(0);
    let mut buf = itoa::Buffer::new();
    output.push(b':');
    output.extend_from_slice(buf.format(card).as_bytes());
    output.extend_from_slice(b"\r\n");
    Ok(true)
  }

  /// PFMERGE dest src [src ...]:多源择大并入目标,回 +OK
  ///
  /// libs/server/Resp/HyperLogLog/HyperLogLogCommands.cs:HyperLogLogMerge
  pub fn hyper_log_log_merge<'a, D: wdev::Device>(
    &mut self,
    parse_state: &[&[u8]],
    store: &wkv::BatchStoreSession<'a, D>,
    output: &mut Vec<u8>,
  ) -> wresp::Result<bool> {
    if parse_state.len() < 2 {
      output.extend_from_slice(b"-ERR wrong number of arguments for 'PFMERGE' command\r\n");
      return Ok(true);
    }

    let dest = parse_state[0];
    let sources = &parse_state[1..];
    let hll = HyperLogLog::new();

    // 目标载荷(缺失按稀疏初始化)
    let mut dst = match load_hll(&hll, store, dest) {
      Err(()) => {
        output.extend_from_slice(RESP_ERR_WRONG_TYPE_HLL);
        return Ok(true);
      }
      Ok(Some(raw)) => raw,
      Ok(None) => {
        let mut blob = vec![0_u8; SPARSE_SIZE_MAX_CAP];
        hll.init_sparse(&mut blob);
        blob[..hll.sparse_bytes()].to_vec()
      }
    };

    for src_key in sources {
      let src = match load_hll(&hll, store, src_key) {
        Err(()) => {
          output.extend_from_slice(RESP_ERR_WRONG_TYPE_HLL);
          return Ok(true);
        }
        Ok(Some(raw)) => raw,
        Ok(None) => continue,
      };

      // 目标容量不足时按 MergeGrow 迁移(稀疏扩容或稠密化)
      let new_len = hll.merge_grow(&src, &dst);
      if new_len != dst.len() {
        let mut grown = vec![0_u8; new_len];
        let old_len = dst.len();
        hll.copy_update_merge(&src, &dst, &mut grown, old_len, new_len);
        dst = grown;
      } else {
        hll.merge(&src, &mut dst);
        hll.set_card(&mut dst, i64::MIN);
      }
    }

    store_hll(&hll, store, dest, &dst);
    output.extend_from_slice(b"+OK\r\n");
    Ok(true)
  }
}

#[cfg(test)]
mod tests {
  use std::{iter::once, sync::Arc};

  use tempfile::{TempDir, tempdir};
  use wdev::SegmentedDevice;
  use wkv::{StoreConfig, WedbStore};

  use super::*;

  type TestSession = wkv::StoreSession<SegmentedDevice>;

  /// 临时文件库 + 批处理会话
  fn fixture(tag: &str) -> (TempDir, Arc<WedbStore<SegmentedDevice>>, TestSession) {
    let dir = tempdir().unwrap();
    let device = Arc::new(SegmentedDevice::single_file(dir.path().join(tag)).unwrap());
    let config = StoreConfig::new(16384, 65536, 64, 0.5).unwrap();
    let store = Arc::new(WedbStore::open(config, device).unwrap());
    let session = store.new_session().unwrap();
    (dir, store, session)
  }

  #[test]
  fn pfadd_pfcount_pfmerge_flow() {
    let (_dir, _store, session) = fixture("hll.db");
    let batch = session.enter_batch();
    let mut sess = RespServerSession::default();
    let mut out = Vec::new();

    // PFADD 新键:寄存器变更 → :1
    sess
      .hyper_log_log_add(&[b"h1", b"a", b"b", b"c"], &batch, &mut out)
      .unwrap();
    assert_eq!(out, b":1\r\n");

    // 重复元素:无变更 → :0
    out.clear();
    sess
      .hyper_log_log_add(&[b"h1", b"a", b"b", b"c"], &batch, &mut out)
      .unwrap();
    assert_eq!(out, b":0\r\n");

    // 新增元素:变更 → :1
    out.clear();
    sess
      .hyper_log_log_add(&[b"h1", b"d"], &batch, &mut out)
      .unwrap();
    assert_eq!(out, b":1\r\n");

    // PFCOUNT 单键:精确基数 4
    out.clear();
    sess
      .hyper_log_log_length(&[b"h1"], &batch, &mut out)
      .unwrap();
    assert_eq!(out, b":4\r\n");

    // 缺失键计数为 0
    out.clear();
    sess
      .hyper_log_log_length(&[b"missing"], &batch, &mut out)
      .unwrap();
    assert_eq!(out, b":0\r\n");

    // PFMERGE h1 → h2
    out.clear();
    sess
      .hyper_log_log_merge(&[b"h2", b"h1"], &batch, &mut out)
      .unwrap();
    assert_eq!(out, b"+OK\r\n");

    // PFCOUNT 多键虚拟并集 = 4
    out.clear();
    sess
      .hyper_log_log_length(&[b"h1", b"h2"], &batch, &mut out)
      .unwrap();
    assert_eq!(out, b":4\r\n");

    // WRONGTYPE:非 HLL 载荷
    let _ = batch.try_upsert_sync(b"bad", b"plain-string-value");
    out.clear();
    sess
      .hyper_log_log_add(&[b"bad", b"x"], &batch, &mut out)
      .unwrap();
    assert_eq!(out, RESP_ERR_WRONG_TYPE_HLL);
    out.clear();
    sess
      .hyper_log_log_length(&[b"bad"], &batch, &mut out)
      .unwrap();
    assert_eq!(out, RESP_ERR_WRONG_TYPE_HLL);
    out.clear();
    sess
      .hyper_log_log_merge(&[b"dest", b"bad"], &batch, &mut out)
      .unwrap();
    assert_eq!(out, RESP_ERR_WRONG_TYPE_HLL);

    // 参数不足
    out.clear();
    sess.hyper_log_log_add(&[], &batch, &mut out).unwrap();
    assert_eq!(
      out,
      b"-ERR wrong number of arguments for 'PFADD' command\r\n"
    );
    out.clear();
    sess
      .hyper_log_log_merge(&[b"dest"], &batch, &mut out)
      .unwrap();
    assert_eq!(
      out,
      b"-ERR wrong number of arguments for 'PFMERGE' command\r\n"
    );
  }

  /// 空稀疏源回归:PFMERGE 全缺失源落下空 HLL 后,将其并入其他键
  /// 不得触发 MergeGrow 的 usize 下溢(debug panic / release 载荷破坏)
  #[test]
  fn pfmerge_empty_source_no_underflow() {
    let (_dir, _store, session) = fixture("hll3.db");
    let batch = session.enter_batch();
    let mut sess = RespServerSession::default();
    let mut out = Vec::new();

    // 全缺失源:目标落为空 HLL
    sess
      .hyper_log_log_merge(&[b"empty", b"no-such-1", b"no-such-2"], &batch, &mut out)
      .unwrap();
    assert_eq!(out, b"+OK\r\n");
    out.clear();
    sess
      .hyper_log_log_length(&[b"empty"], &batch, &mut out)
      .unwrap();
    assert_eq!(out, b":0\r\n");

    // 空源并入新目标 / 并入自身:均成功且计数保持 0
    out.clear();
    sess
      .hyper_log_log_merge(&[b"copy", b"empty"], &batch, &mut out)
      .unwrap();
    assert_eq!(out, b"+OK\r\n");
    out.clear();
    sess
      .hyper_log_log_merge(&[b"empty", b"empty"], &batch, &mut out)
      .unwrap();
    assert_eq!(out, b"+OK\r\n");
    out.clear();
    sess
      .hyper_log_log_length(&[b"copy", b"empty"], &batch, &mut out)
      .unwrap();
    assert_eq!(out, b":0\r\n");

    // 空源并入有值目标:计数不变
    let mut out2 = Vec::new();
    sess
      .hyper_log_log_add(&[b"real", b"x", b"y"], &batch, &mut out2)
      .unwrap();
    out.clear();
    sess
      .hyper_log_log_merge(&[b"real", b"empty"], &batch, &mut out)
      .unwrap();
    assert_eq!(out, b"+OK\r\n");
    out.clear();
    sess
      .hyper_log_log_length(&[b"real"], &batch, &mut out)
      .unwrap();
    assert_eq!(out, b":2\r\n");
  }

  /// 大规模元素:稀疏稠密化后基数仍受控,PFMERGE 后计数不变
  #[test]
  fn dense_upgrade_and_merge_monotonic() {
    let (_dir, _store, session) = fixture("hll2.db");
    let batch = session.enter_batch();
    let mut sess = RespServerSession::default();

    let elements: Vec<Vec<u8>> = (0..1000)
      .map(|i| format!("elem-{i}").into_bytes())
      .collect();
    let args: Vec<&[u8]> = once(b"big".as_slice())
      .chain(elements.iter().map(|e| e.as_slice()))
      .collect();

    let mut out = Vec::new();
    sess.hyper_log_log_add(&args, &batch, &mut out).unwrap();
    assert_eq!(out, b":1\r\n");

    out.clear();
    sess
      .hyper_log_log_length(&[b"big"], &batch, &mut out)
      .unwrap();
    let card: i64 = String::from_utf8_lossy(&out[1..out.len() - 2])
      .parse()
      .unwrap();
    // HLL 标准误差 ~1.1%
    assert!((880..=1120).contains(&card), "card = {card}");

    // merge 到空目标,计数不变
    out.clear();
    sess
      .hyper_log_log_merge(&[b"copy", b"big"], &batch, &mut out)
      .unwrap();
    out.clear();
    sess
      .hyper_log_log_length(&[b"copy"], &batch, &mut out)
      .unwrap();
    let card2: i64 = String::from_utf8_lossy(&out[1..out.len() - 2])
      .parse()
      .unwrap();
    assert_eq!(card, card2);
  }
}