wedb_embed 0.1.4

Embedded database engine providing Redis-like APIs, built on fjall / 嵌入式数据库引擎,提供类似 Redis 的接口,底层基于 fjall 开发
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
use std::{thread::sleep, time::Duration};

use aok::Void;
use tempfile::tempdir;
use wedb_embed::{
  Fjall, WeDb,
  hash::{
    ERR_HASH_FIELD_EXPIRATION_LEGACY_ENCODING, ERR_HASH_VALUE_NOT_FLOAT,
    ERR_HASH_VALUE_NOT_INTEGER, ERR_INCREMENT_NAN_OR_INFINITY, ERR_INCREMENT_OVERFLOW,
    ERR_WRONG_TYPE, HASH_EXPIRE_COND_FAILED, HASH_EXPIRE_DELETED, HASH_EXPIRE_SET_OK,
    HASH_FIELD_NOT_FOUND, HASH_FIELD_PERSISTENT, HExpire, HGetEx, HSet, HashItemKeyComposer,
    HashLengthMode, HashMeta, HashSubkeyEncodingMode, RangeLex, compose_hash_meta_key,
  },
  key_composer::KeyComposer,
};

#[ctor::ctor(unsafe)]
fn _log_init() {
  log_init::init();
}

#[test]
fn test_hash_basic_ops() -> Void {
  let dir = tempdir()?;
  let db = WeDb::new(Fjall::open(dir.path())?).ns(0)?.db(0)?;

  let fields = ["test-hash-key-1", "test-hash-key-2", "test-hash-key-3"];
  let values = [
    "hash-test-value-1",
    "hash-test-value-2",
    "hash-test-value-3",
  ];

  let fvs: Vec<(&str, &str)> = fields.iter().copied().zip(values.iter().copied()).collect();

  assert_eq!(db.hset("test_hash->key", &fvs)?, 3);
  assert_eq!(db.hlen("test_hash->key")?, 3);
  assert_eq!(
    db.hlen_with_mode("test_hash->key", HashLengthMode::Approximate)?,
    3
  );

  for (f, v) in &fvs {
    assert_eq!(db.hget("test_hash->key", f)?, Some(v.as_bytes().to_vec()));
    assert!(db.hexists("test_hash->key", f)?);
  }
  assert!(!db.hexists("test_hash->key", "nonexistent_field")?);

  // 重复字段去重覆盖
  assert_eq!(
    db.hset("test_hash->key", &[("test-hash-key-1", "new_val")])?,
    0
  );
  assert_eq!(
    db.hget("test_hash->key", "test-hash-key-1")?,
    Some(b"new_val".to_vec())
  );

  // HSETNX
  assert!(!db.hsetnx("test_hash->key", "test-hash-key-1", "ignored")?);
  assert!(db.hsetnx("test_hash->key", "new_field", "new_val")?);
  assert_eq!(db.hlen("test_hash->key")?, 4);

  // HMGET
  let m_res = db.hmget("test_hash->key", &["test-hash-key-1", "nonexistent"])?;
  assert_eq!(m_res[0], Some(b"new_val".to_vec()));
  assert_eq!(m_res[1], None);

  // HKEYS & HVALS
  let keys = db.hkeys("test_hash->key")?;
  assert_eq!(keys.len(), 4);
  let vals = db.hvals("test_hash->key")?;
  assert_eq!(vals.len(), 4);

  // HDEL
  assert_eq!(
    db.hdel("test_hash->key", &["test-hash-key-1", "nonexistent"])?,
    1
  );
  assert_eq!(db.hlen("test_hash->key")?, 3);

  // HINCRBY & HINCRBYFLOAT
  db.hset("test_hash->key", &[("counter", "10")])?;
  assert_eq!(db.hincrby("test_hash->key", "counter", 5)?, 15);
  assert_eq!(db.hincrby("test_hash->key", "counter", -20)?, -5);

  db.hset("test_hash->key", &[("float", "10.5")])?;
  let f_res = db.hincrbyfloat("test_hash->key", "float", 2.25)?;
  assert!((f_res - 12.75).abs() < 1e-9);

  // HSTRLEN
  assert_eq!(db.hstrlen("test_hash->key", "test-hash-key-2")?, 17);
  assert_eq!(db.hstrlen("test_hash->key", "nonexistent")?, 0);

  // HRANGEBYLEX
  let range_res = db.hrangebylex(
    "test_hash->key",
    RangeLex {
      min: b"hash-".to_vec(),
      max: b"test-hash-key-3".to_vec(),
      maxex: false,
      ..Default::default()
    },
  )?;
  assert!(!range_res.is_empty());

  // HSCAN
  let (cursor, items) = db.hscan("test_hash->key", 0, 10, None)?;
  assert_eq!(cursor, 0);
  assert!(!items.is_empty());

  // HRANDFIELD
  let rand_fields = db.hrandfield("test_hash->key", 2, false)?;
  assert_eq!(rand_fields.len(), 2);

  let rand_fvs = db.hrandfield("test_hash->key", -3, true)?;
  assert_eq!(rand_fvs.len(), 3);

  Ok(())
}

#[test]
fn test_hash_incrby_and_incrbyfloat_errors() -> Void {
  let dir = tempdir()?;
  let db = WeDb::new(Fjall::open(dir.path())?).ns(0)?.db(0)?;

  // 1. 测试 HINCRBY 溢出错误 (i64::MAX + 1 / i64::MIN - 1)
  db.hset("err_hash", &[("max_int", i64::MAX.to_string().as_str())])?;
  let err_over = db.hincrby("err_hash", "max_int", 1).unwrap_err();
  assert!(err_over.to_string().contains(ERR_INCREMENT_OVERFLOW));

  db.hset("err_hash", &[("min_int", i64::MIN.to_string().as_str())])?;
  let err_under = db.hincrby("err_hash", "min_int", -1).unwrap_err();
  assert!(err_under.to_string().contains(ERR_INCREMENT_OVERFLOW));

  // 2. 测试 HINCRBY 非整数解析错误
  db.hset("err_hash", &[("not_int", "12.34")])?;
  let err_not_int = db.hincrby("err_hash", "not_int", 1).unwrap_err();
  assert!(err_not_int.to_string().contains(ERR_HASH_VALUE_NOT_INTEGER));

  db.hset("err_hash", &[("str_val", "abc")])?;
  let err_str = db.hincrby("err_hash", "str_val", 1).unwrap_err();
  assert!(err_str.to_string().contains(ERR_HASH_VALUE_NOT_INTEGER));

  // 3. 测试 HINCRBYFLOAT NaN / Infinity 校验
  db.hset("err_hash", &[("f_val", "1.0")])?;
  let err_nan = db.hincrbyfloat("err_hash", "f_val", f64::NAN).unwrap_err();
  assert!(err_nan.to_string().contains(ERR_INCREMENT_NAN_OR_INFINITY));

  let err_inf = db
    .hincrbyfloat("err_hash", "f_val", f64::INFINITY)
    .unwrap_err();
  assert!(err_inf.to_string().contains(ERR_INCREMENT_NAN_OR_INFINITY));

  // 4. 测试 HINCRBYFLOAT 非浮点值解析错误
  let err_f_str = db.hincrbyfloat("err_hash", "str_val", 1.0).unwrap_err();
  assert!(err_f_str.to_string().contains(ERR_HASH_VALUE_NOT_FLOAT));

  Ok(())
}

#[test]
fn test_hash_field_expiration_comprehensive() -> Void {
  let dir = tempdir()?;
  let db = WeDb::new(Fjall::open(dir.path())?).ns(0)?.db(0)?;

  db.hset("hexp", &[("f1", "v1"), ("f2", "v2"), ("f3", "v3")])?;

  // 1. HEXPIRE
  let res = db.hexpire("hexp", &["f1", "f2"], 100, [])?;
  assert_eq!(res, vec![HASH_EXPIRE_SET_OK, HASH_EXPIRE_SET_OK]);

  // 2. HTTL / HPTTL
  let ttls = db.httl("hexp", &["f1", "f2", "f3", "f_none"])?;
  assert!(ttls[0] > 0 && ttls[0] <= 100);
  assert!(ttls[1] > 0 && ttls[1] <= 100);
  assert_eq!(ttls[2], HASH_FIELD_PERSISTENT);
  assert_eq!(ttls[3], HASH_FIELD_NOT_FOUND);

  let pttls = db.hpttl("hexp", &["f1", "f3"])?;
  assert!(pttls[0] > 0);
  assert_eq!(pttls[1], HASH_FIELD_PERSISTENT);

  // 3. HEXPIRETIME / HPEXPIRETIME
  let extimes = db.hexpiretime("hexp", &["f1", "f3"])?;
  assert!(extimes[0] > 0);
  assert_eq!(extimes[1], HASH_FIELD_PERSISTENT);

  let pextimes = db.hpexpiretime("hexp", &["f1", "f3"])?;
  assert!(pextimes[0] > 0);
  assert_eq!(pextimes[1], HASH_FIELD_PERSISTENT);

  // 4. 条件检查 (NX / XX / GT / LT)
  let nx_res = db.hexpire("hexp", &["f1", "f3"], 200, [HExpire::Nx])?;
  assert_eq!(nx_res, vec![HASH_EXPIRE_COND_FAILED, HASH_EXPIRE_SET_OK]); // f1 已有 TTL 不满足 NX,f3 持久满足 NX

  let xx_res = db.hexpire("hexp", &["f1", "f2"], 300, [HExpire::Xx])?;
  assert_eq!(xx_res, vec![HASH_EXPIRE_SET_OK, HASH_EXPIRE_SET_OK]); // f1, f2 均有 TTL 满足 XX

  // GT 条件测试
  let gt_fail = db.hexpire("hexp", &["f1"], 50, [HExpire::Gt])?;
  assert_eq!(gt_fail, vec![HASH_EXPIRE_COND_FAILED]); // 50 < 300, 失败
  let gt_succ = db.hexpire("hexp", &["f1"], 500, [HExpire::Gt])?;
  assert_eq!(gt_succ, vec![HASH_EXPIRE_SET_OK]); // 500 > 300, 成功

  // LT 条件测试
  let lt_fail = db.hexpire("hexp", &["f1"], 600, [HExpire::Lt])?;
  assert_eq!(lt_fail, vec![HASH_EXPIRE_COND_FAILED]); // 600 > 500, 失败
  let lt_succ = db.hexpire("hexp", &["f1"], 400, [HExpire::Lt])?;
  assert_eq!(lt_succ, vec![HASH_EXPIRE_SET_OK]); // 400 < 500, 成功

  // 5. HPERSIST
  let persist_res = db.hpersist("hexp", &["f1", "f2", "f3", "f_none"])?;
  assert_eq!(
    persist_res,
    vec![
      HASH_EXPIRE_SET_OK,
      HASH_EXPIRE_SET_OK,
      HASH_EXPIRE_SET_OK,
      HASH_FIELD_NOT_FOUND
    ]
  );
  let persist_again = db.hpersist("hexp", &["f1", "f2", "f3"])?;
  assert_eq!(
    persist_again,
    vec![
      HASH_FIELD_PERSISTENT,
      HASH_FIELD_PERSISTENT,
      HASH_FIELD_PERSISTENT
    ]
  );

  // 6. 立即过期删除 (seconds <= 0)
  let imm_res = db.hexpire("hexp", &["f1"], 0, [])?;
  assert_eq!(imm_res, vec![HASH_EXPIRE_DELETED]);
  assert_eq!(db.hget("hexp", "f1")?, None);
  assert_eq!(db.hlen("hexp")?, 2);

  Ok(())
}

#[test]
fn test_hash_legacy_encoding_compatibility() -> Void {
  let dir = tempdir()?;
  let db = WeDb::new(Fjall::open(dir.path())?).ns(0)?.db(0)?;

  // 模拟写入 Legacy 编码哈希
  let legacy_meta = HashMeta::new_with_mode(HashSubkeyEncodingMode::Legacy, 0, 100, 1);
  let kc = KeyComposer::default();
  let meta_k = compose_hash_meta_key(&kc, b"legacy_key");
  let mut composer = HashItemKeyComposer::new(&kc, b"legacy_key");
  let item_k = composer.key_for_field(b"f1");

  let mut batch = db.batch();
  batch.insert_meta(&meta_k, &legacy_meta.encode());
  batch.insert_data(item_k, b"val1");
  batch.commit()?;

  // 1. 常规读取支持 Legacy 编码
  assert_eq!(db.hget("legacy_key", "f1")?, Some(b"val1".to_vec()));
  assert_eq!(db.hlen("legacy_key")?, 1);
  assert!(db.hexists("legacy_key", "f1")?);
  assert_eq!(db.hstrlen("legacy_key", "f1")?, 4);

  // 2. 过期相关命令应拒绝 Legacy 编码并返回错误
  let err_exp = db.hexpire("legacy_key", &["f1"], 10, []).unwrap_err();
  assert!(
    err_exp
      .to_string()
      .contains(ERR_HASH_FIELD_EXPIRATION_LEGACY_ENCODING)
  );

  let err_ttl = db.httl("legacy_key", &["f1"]).unwrap_err();
  assert!(
    err_ttl
      .to_string()
      .contains(ERR_HASH_FIELD_EXPIRATION_LEGACY_ENCODING)
  );

  let err_pttl = db.hpttl("legacy_key", &["f1"]).unwrap_err();
  assert!(
    err_pttl
      .to_string()
      .contains(ERR_HASH_FIELD_EXPIRATION_LEGACY_ENCODING)
  );

  let err_ex_time = db.hexpiretime("legacy_key", &["f1"]).unwrap_err();
  assert!(
    err_ex_time
      .to_string()
      .contains(ERR_HASH_FIELD_EXPIRATION_LEGACY_ENCODING)
  );

  let err_pex_time = db.hpexpiretime("legacy_key", &["f1"]).unwrap_err();
  assert!(
    err_pex_time
      .to_string()
      .contains(ERR_HASH_FIELD_EXPIRATION_LEGACY_ENCODING)
  );

  let err_persist = db.hpersist("legacy_key", &["f1"]).unwrap_err();
  assert!(
    err_persist
      .to_string()
      .contains(ERR_HASH_FIELD_EXPIRATION_LEGACY_ENCODING)
  );

  let err_setex = db
    .hsetex("legacy_key", &[("f1", "val2")], [HSet::Ex(100)])
    .unwrap_err();
  assert!(
    err_setex
      .to_string()
      .contains(ERR_HASH_FIELD_EXPIRATION_LEGACY_ENCODING)
  );

  let err_getex = db
    .hgetex("legacy_key", "f1", [HGetEx::Persist])
    .unwrap_err();
  assert!(
    err_getex
      .to_string()
      .contains(ERR_HASH_FIELD_EXPIRATION_LEGACY_ENCODING)
  );

  Ok(())
}

#[test]
fn test_hash_hgetdel_and_hsetex_hgetex() -> Void {
  let dir = tempdir()?;
  let db = WeDb::new(Fjall::open(dir.path())?).ns(0)?.db(0)?;

  // 1. HSETEX
  assert!(db.hsetex(
    "h_ex_key",
    &[("a", "1"), ("b", "2")],
    [HSet::Ex(10), HSet::Fnx]
  )?);
  assert_eq!(db.hlen("h_ex_key")?, 2);

  // Fnx 遇到已有字段失败
  assert!(!db.hsetex("h_ex_key", &[("a", "1_new")], [HSet::Ex(10), HSet::Fnx])?);

  // 便捷 hsetex 接口
  assert!(db.hsetex("h_ex_key2", &[("c", "100")], [HSet::Ex(30), HSet::Fnx])?);
  assert_eq!(db.hlen("h_ex_key2")?, 1);

  // 2. HGETEX (持久化)
  let g_val = db.hgetex("h_ex_key", "a", [HGetEx::Persist])?;
  assert_eq!(g_val, Some(b"1".to_vec()));
  let g_none = db.hgetex("h_ex_key", "nonexistent", [HGetEx::Persist])?;
  assert_eq!(g_none, None);
  assert_eq!(db.httl("h_ex_key", &["a"])?, vec![HASH_FIELD_PERSISTENT]);

  // 便捷 hgetex 接口
  let g_val = db.hgetex("h_ex_key2", "c", [HGetEx::Persist])?;
  assert_eq!(g_val, Some(b"100".to_vec()));
  assert_eq!(db.httl("h_ex_key2", &["c"])?, vec![HASH_FIELD_PERSISTENT]);

  // 3. HGETDEL
  let del_res = db.hgetdel("h_ex_key", &["a", "b", "c"])?;
  assert_eq!(
    del_res,
    vec![Some(b"1".to_vec()), Some(b"2".to_vec()), None]
  );
  assert_eq!(db.hlen("h_ex_key")?, 0);

  Ok(())
}

#[test]
fn test_hash_expired_key_cleanup_and_ttl_preservation() -> Void {
  let dir = tempdir()?;
  let db = WeDb::new(Fjall::open(dir.path())?).ns(0)?.db(0)?;

  // 1. 测试 HINCRBY 保持现有 LiveTTL 字段过期时间
  db.hset("h_inc_ttl", &[("counter", "10")])?;
  db.hexpire("h_inc_ttl", &["counter"], 300, [])?;
  let ttl_before = db.httl("h_inc_ttl", &["counter"])?[0];
  assert!(ttl_before > 0 && ttl_before <= 300);

  let new_val = db.hincrby("h_inc_ttl", "counter", 5)?;
  assert_eq!(new_val, 15);
  let ttl_after = db.httl("h_inc_ttl", &["counter"])?[0];
  assert!(ttl_after > 0 && ttl_after <= 300);

  // 2. 测试 HINCRBYFLOAT 保持现有 LiveTTL 字段过期时间
  db.hset("h_inc_ttl", &[("float_val", "1.5")])?;
  db.hexpire("h_inc_ttl", &["float_val"], 200, [])?;
  let f_val = db.hincrbyfloat("h_inc_ttl", "float_val", 2.25)?;
  assert!((f_val - 3.75).abs() < 1e-6);
  let f_ttl = db.httl("h_inc_ttl", &["float_val"])?[0];
  assert!(f_ttl > 0 && f_ttl <= 200);

  // 3. 测试 HSTRLEN 对不存在或已过期字段返回 0
  assert_eq!(db.hstrlen("h_inc_ttl", "nonexistent")?, 0);
  assert_eq!(db.hstrlen("h_inc_ttl", "counter")?, 2);

  // 立即过期后 HSTRLEN 查不到
  db.hexpire("h_inc_ttl", &["counter"], 0, [])?;
  assert_eq!(db.hstrlen("h_inc_ttl", "counter")?, 0);

  Ok(())
}

#[test]
fn test_hash_scan_and_repair_and_edge_cases() -> Void {
  let dir = tempdir()?;
  let db = WeDb::new(Fjall::open(dir.path())?).ns(0)?.db(0)?;

  // 1. 设置字段并设置短期过期时间 (毫秒级)
  db.hset("h_repair", &[("f1", "v1"), ("f2", "v2"), ("f3", "v3")])?;
  db.hpexpire("h_repair", &["f1", "f2"], 15, [])?; // 15 毫秒过期

  // 极速休眠 25ms 让其物理过期
  sleep(Duration::from_millis(25));

  // 此时 f1, f2 已过期,f3 为持久字段
  // 调用 hlen(Accurate) 触发 scanAndRepair
  let len = db.hlen("h_repair")?;
  assert_eq!(len, 1);
  assert_eq!(db.hget("h_repair", "f1")?, None);
  assert_eq!(db.hget("h_repair", "f2")?, None);
  assert_eq!(db.hget("h_repair", "f3")?, Some(b"v3".to_vec()));

  // 2. 将剩余唯一的持久字段 f3 设置为已过期 (立即过期)
  let imm = db.hexpire("h_repair", &["f3"], 0, [])?;
  assert_eq!(imm, vec![HASH_EXPIRE_DELETED]);
  assert_eq!(db.hlen("h_repair")?, 0);
  assert_eq!(db.hgetall("h_repair")?, Vec::<(Vec<u8>, Vec<u8>)>::new());

  // 3. 空键与单键边界测试
  assert_eq!(db.hdel("nonexistent_hash", &["f1", "f2"])?, 0);
  assert_eq!(db.hget("nonexistent_hash", "f1")?, None);
  assert_eq!(
    db.hmget("nonexistent_hash", &["f1", "f2"])?,
    vec![None, None]
  );
  assert_eq!(
    db.httl("nonexistent_hash", &["f1"])?,
    vec![HASH_FIELD_NOT_FOUND]
  );

  Ok(())
}

#[test]
fn test_hash_binary_safe_keys_and_fields() -> Void {
  let dir = tempdir()?;
  let db = WeDb::new(Fjall::open(dir.path())?).ns(0)?.db(0)?;

  // 非 UTF-8 二进制键和字段
  let bin_key = b"\xff\xfe\x00\x01binary_hash";
  let bin_field1 = b"\x00\x01\x02field";
  let bin_field2 = b"\xaa\xbb\xcc\xdd";
  let bin_val1 = b"\xde\xad\xbe\xef";
  let bin_val2 = b"\x01\x02\x03\x04\x05";

  assert_eq!(
    db.hset(
      bin_key,
      &[
        (bin_field1.as_slice(), bin_val1.as_slice()),
        (bin_field2.as_slice(), bin_val2.as_slice())
      ]
    )?,
    2
  );
  assert_eq!(db.hlen(bin_key)?, 2);
  assert_eq!(db.hget(bin_key, bin_field1)?, Some(bin_val1.to_vec()));
  assert_eq!(db.hget(bin_key, bin_field2)?, Some(bin_val2.to_vec()));

  // 二进制安全过期
  let exp_res = db.hexpire(bin_key, &[bin_field1.as_slice()], 60, [])?;
  assert_eq!(exp_res, vec![HASH_EXPIRE_SET_OK]);
  let ttl_res = db.httl(bin_key, &[bin_field1.as_slice(), bin_field2.as_slice()])?;
  assert!(ttl_res[0] > 0);
  assert_eq!(ttl_res[1], HASH_FIELD_PERSISTENT);

  // 二进制安全删除
  assert_eq!(db.hdel(bin_key, &[bin_field1.as_slice()])?, 1);
  assert_eq!(db.hlen(bin_key)?, 1);

  Ok(())
}

#[test]
fn test_hash_wrongtype_handling() -> Void {
  let dir = tempdir()?;
  let db = WeDb::new(Fjall::open(dir.path())?).ns(0)?.db(0)?;

  // 1. 先将键设置为 String 类型
  db.set("str_conflict_key", "string_value", [])?;

  // 2. Hash 命令操作此 String 键,必须返回 WRONGTYPE 错误
  let err_hset = db.hset("str_conflict_key", &[("f1", "v1")]).unwrap_err();
  assert!(err_hset.to_string().contains(ERR_WRONG_TYPE));

  let err_hget = db.hget("str_conflict_key", "f1").unwrap_err();
  assert!(err_hget.to_string().contains(ERR_WRONG_TYPE));

  let err_hlen = db.hlen("str_conflict_key").unwrap_err();
  assert!(err_hlen.to_string().contains(ERR_WRONG_TYPE));

  let err_hexists = db.hexists("str_conflict_key", "f1").unwrap_err();
  assert!(err_hexists.to_string().contains(ERR_WRONG_TYPE));

  let err_hdel = db.hdel("str_conflict_key", &["f1"]).unwrap_err();
  assert!(err_hdel.to_string().contains(ERR_WRONG_TYPE));

  let err_hincrby = db.hincrby("str_conflict_key", "f1", 1).unwrap_err();
  assert!(err_hincrby.to_string().contains(ERR_WRONG_TYPE));

  let err_hincrbyfloat = db.hincrbyfloat("str_conflict_key", "f1", 1.0).unwrap_err();
  assert!(err_hincrbyfloat.to_string().contains(ERR_WRONG_TYPE));

  let err_hkeys = db.hkeys("str_conflict_key").unwrap_err();
  assert!(err_hkeys.to_string().contains(ERR_WRONG_TYPE));

  let err_hvals = db.hvals("str_conflict_key").unwrap_err();
  assert!(err_hvals.to_string().contains(ERR_WRONG_TYPE));

  let err_hgetall = db.hgetall("str_conflict_key").unwrap_err();
  assert!(err_hgetall.to_string().contains(ERR_WRONG_TYPE));

  let err_hstrlen = db.hstrlen("str_conflict_key", "f1").unwrap_err();
  assert!(err_hstrlen.to_string().contains(ERR_WRONG_TYPE));

  let err_hexpire = db.hexpire("str_conflict_key", &["f1"], 10, []).unwrap_err();
  assert!(err_hexpire.to_string().contains(ERR_WRONG_TYPE));

  let err_httl = db.httl("str_conflict_key", &["f1"]).unwrap_err();
  assert!(err_httl.to_string().contains(ERR_WRONG_TYPE));

  let err_hpersist = db.hpersist("str_conflict_key", &["f1"]).unwrap_err();
  assert!(err_hpersist.to_string().contains(ERR_WRONG_TYPE));

  let err_hrange = db
    .hrangebylex("str_conflict_key", RangeLex::default())
    .unwrap_err();
  assert!(err_hrange.to_string().contains(ERR_WRONG_TYPE));

  let err_hscan = db.hscan("str_conflict_key", 0, 10, None).unwrap_err();
  assert!(err_hscan.to_string().contains(ERR_WRONG_TYPE));

  let err_hiter = db.hiter("str_conflict_key", |_, _| true).unwrap_err();
  assert!(err_hiter.to_string().contains(ERR_WRONG_TYPE));

  Ok(())
}

#[test]
fn test_hash_iter() -> Void {
  let dir = tempdir()?;
  let db = WeDb::new(Fjall::open(dir.path())?).ns(0)?.db(0)?;

  // 1. 未初始化的 Key 遍历返回 Ok(())
  let mut count = 0;
  db.hiter("nonexistent_hash", |_, _| {
    count += 1;
    true
  })?;
  assert_eq!(count, 0);

  // 2. 正常遍历
  let pairs = [
    ("field1", "val1"),
    ("field2", "val2"),
    ("field3", "val3"),
    ("field4", "val4"),
  ];
  db.hset("h_iter_test", &pairs)?;

  let mut collected = Vec::new();
  db.hiter("h_iter_test", |f, v| {
    collected.push((f.to_vec(), v.to_vec()));
    true
  })?;
  assert_eq!(collected.len(), 4);
  assert_eq!(collected[0], (b"field1".to_vec(), b"val1".to_vec()));
  assert_eq!(collected[1], (b"field2".to_vec(), b"val2".to_vec()));
  assert_eq!(collected[2], (b"field3".to_vec(), b"val3".to_vec()));
  assert_eq!(collected[3], (b"field4".to_vec(), b"val4".to_vec()));

  // 3. 提前终止遍历(返回 false)
  let mut early_stopped = Vec::new();
  db.hiter("h_iter_test", |f, v| {
    early_stopped.push((f.to_vec(), v.to_vec()));
    early_stopped.len() < 2
  })?;
  assert_eq!(early_stopped.len(), 2);
  assert_eq!(early_stopped[0].0, b"field1");
  assert_eq!(early_stopped[1].0, b"field2");

  // 4. 字段过期自动过滤
  db.hset("h_iter_expire", &[("f1", "v1"), ("f2", "v2")])?;
  db.hexpire("h_iter_expire", &["f1"], 1, [])?;
  sleep(Duration::from_millis(1100));

  let mut valid_fields = Vec::new();
  db.hiter("h_iter_expire", |f, v| {
    valid_fields.push((f.to_vec(), v.to_vec()));
    true
  })?;
  assert_eq!(valid_fields.len(), 1);
  assert_eq!(valid_fields[0].0, b"f2");
  assert_eq!(valid_fields[0].1, b"v2");

  // 5. 二进制安全遍历
  let bin_pairs = [
    (vec![0u8, 1, 2], vec![255u8, 254]),
    (vec![3u8, 4, 5], vec![100u8, 200]),
  ];
  db.hset("h_bin_iter", &bin_pairs)?;

  let mut bin_collected = Vec::new();
  db.hiter("h_bin_iter", |f, v| {
    bin_collected.push((f.to_vec(), v.to_vec()));
    true
  })?;
  assert_eq!(bin_collected.len(), 2);
  assert_eq!(bin_collected[0].0, vec![0u8, 1, 2]);
  assert_eq!(bin_collected[0].1, vec![255u8, 254]);
  assert_eq!(bin_collected[1].0, vec![3u8, 4, 5]);
  assert_eq!(bin_collected[1].1, vec![100u8, 200]);

  Ok(())
}