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
//! List 列表操作 (ListTreeOps)
//!
//! - 序号大端符号翻转:`order_idx = ((index as u64) ^ (1 << 63)).to_be_bytes()`
//! - Key: `[TreePrefix::ListIndex as u8: 1B][order_idx: 8B be]` (定长 9 字节)
//! - Val: `element: &[u8]`
//! - `ListStub`: 51 字节(`RangeIndexStub: 35B` + `head: i64` + `tail: i64`)
//! - 接口:`lpush`, `rpush`, `lpop`, `rpop`, `lindex`, `lrange`

use wbftree::{
  BfTreeInsertResult, BfTreeReadResult, BfTreeService, RangeIndexStub, ScanReturnField,
};

use crate::{CollectionError, Result, prefix::TreePrefix};

/// List 存根总字节大小 (35 字节 RangeIndexStub + 8 字节 head + 8 字节 tail)
pub const LIST_STUB_SIZE: usize = 51;

/// 有符号 64 位序号转换为大端保序键 (8 字节)
#[inline]
pub const fn order_idx_from_i64(index: i64) -> [u8; 8] {
  let order = (index as u64) ^ (1 << 63);
  order.to_be_bytes()
}

/// 大端保序键解码为有符号 64 位序号
#[inline]
pub const fn i64_from_order_idx(bytes: [u8; 8]) -> i64 {
  let order = u64::from_be_bytes(bytes);
  (order ^ (1 << 63)) as i64
}

/// 有符号 64 位序号转换为带统一前缀的 9 字节 BfTree 物理键
#[inline(always)]
pub const fn list_key_from_i64(index: i64) -> [u8; 9] {
  let bytes = order_idx_from_i64(index);
  [
    TreePrefix::ListIndex as u8,
    bytes[0],
    bytes[1],
    bytes[2],
    bytes[3],
    bytes[4],
    bytes[5],
    bytes[6],
    bytes[7],
  ]
}

/// 从 9 字节物理键解码为有符号 64 位序号
#[inline(always)]
pub const fn i64_from_list_key(key: [u8; 9]) -> i64 {
  i64_from_order_idx([
    key[1], key[2], key[3], key[4], key[5], key[6], key[7], key[8],
  ])
}

/// 定长 51 字节 List 存根元数据 (组合 35 字节 RangeIndexStub 与双端索引)
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
pub struct ListStub {
  /// 底层 RangeIndexStub
  pub range_stub: RangeIndexStub,
  /// 头部元素当前序号(左闭右开区间 `[head, tail)`)
  pub head: i64,
  /// 尾部元素下一可用序号
  pub tail: i64,
}

/// 将 Redis 风格的 [start, stop] 相对索引标准化为左闭右闭区间 [s, e](基于绝对序号偏移)
#[inline]
pub const fn normalize_range(len: usize, start: i64, stop: i64) -> Option<(usize, usize)> {
  if len == 0 {
    return None;
  }
  let len_i64 = len as i64;
  let s = if start >= 0 {
    start
  } else {
    match len_i64.checked_add(start) {
      Some(v) => v,
      None => 0,
    }
  };
  let s = if s < 0 { 0 } else { s as usize };

  let e = if stop >= 0 {
    stop
  } else {
    match len_i64.checked_add(stop) {
      Some(v) => v,
      None => -1,
    }
  };
  if e < 0 {
    return None;
  }
  let e = if e >= len_i64 { len - 1 } else { e as usize };

  if s > e { None } else { Some((s, e)) }
}

impl ListStub {
  /// 创建新的 List 存根
  #[inline]
  pub fn new(range_stub: RangeIndexStub, head: i64, tail: i64) -> Self {
    Self {
      range_stub,
      head,
      tail,
    }
  }

  /// 当列表为空时重置 head 和 tail 序号为 0,防止双端索引无限漂移
  #[inline]
  pub fn reset_if_empty(&mut self) {
    if self.is_empty() {
      self.head = 0;
      self.tail = 0;
    }
  }

  /// 编码为定长 51 字节数组 (零堆分配)
  #[inline]
  pub const fn encode(&self) -> [u8; LIST_STUB_SIZE] {
    let stub_bytes = self.range_stub.encode();
    let head_bytes = self.head.to_le_bytes();
    let tail_bytes = self.tail.to_le_bytes();
    let mut out = [0u8; LIST_STUB_SIZE];
    let mut i = 0;
    while i < 35 {
      out[i] = stub_bytes[i];
      i += 1;
    }
    let mut j = 0;
    while j < 8 {
      out[35 + j] = head_bytes[j];
      out[43 + j] = tail_bytes[j];
      j += 1;
    }
    out
  }

  /// 编码至输出切片
  #[inline]
  pub fn encode_into(&self, out: &mut [u8]) -> Result<()> {
    if out.len() < LIST_STUB_SIZE {
      return Err(CollectionError::InvalidArgument("输出切片长度不足 51 字节"));
    }
    out[..LIST_STUB_SIZE].copy_from_slice(&self.encode());
    Ok(())
  }

  /// 从切片解码 ListStub (零拷贝)
  #[inline]
  pub const fn decode_opt(bytes: &[u8]) -> Option<Self> {
    if bytes.len() < LIST_STUB_SIZE {
      return None;
    }
    let Some(range_stub) = RangeIndexStub::decode_opt(bytes) else {
      return None;
    };
    let head = i64::from_le_bytes([
      bytes[35], bytes[36], bytes[37], bytes[38], bytes[39], bytes[40], bytes[41], bytes[42],
    ]);
    let tail = i64::from_le_bytes([
      bytes[43], bytes[44], bytes[45], bytes[46], bytes[47], bytes[48], bytes[49], bytes[50],
    ]);
    Some(Self {
      range_stub,
      head,
      tail,
    })
  }

  /// 从切片解码 ListStub (零拷贝,返回 Result)
  #[inline]
  pub fn decode(bytes: &[u8]) -> Result<Self> {
    Self::decode_opt(bytes).ok_or(CollectionError::Corrupted(
      "ListStub 存根数据损坏或长度不足",
    ))
  }

  /// 获取当前列表长度
  #[inline]
  pub const fn len(&self) -> usize {
    if self.tail > self.head {
      (self.tail as u64).wrapping_sub(self.head as u64) as usize
    } else {
      0
    }
  }

  /// 检查列表是否为空
  #[inline]
  pub const fn is_empty(&self) -> bool {
    self.tail <= self.head
  }
}

/// List 树操作 Trait
///
/// 在 garnet 中的相对路径:libs/server/Storage/Session/ObjectStore/ListOps.cs
pub trait ListTreeOps {
  /// 左推入元素 (返回推入后的列表总长度)
  fn lpush(&self, stub: &mut ListStub, element: &[u8]) -> Result<usize>;

  /// 右推入元素 (返回推入后的列表总长度)
  fn rpush(&self, stub: &mut ListStub, element: &[u8]) -> Result<usize>;

  /// 左弹出元素 (空列表返回 Ok(None))
  fn lpop(&self, stub: &mut ListStub) -> Result<Option<Vec<u8>>>;

  /// 右弹出元素 (空列表返回 Ok(None))
  fn rpop(&self, stub: &mut ListStub) -> Result<Option<Vec<u8>>>;

  /// 获取列表当前长度 (O(1) 存根算术直读)
  #[inline]
  fn llen(&self, stub: &ListStub) -> usize {
    stub.len()
  }

  /// 按索引读取元素 (支持负数倒数索引)
  fn lindex(&self, stub: &ListStub, index: i64) -> Result<Option<Vec<u8>>> {
    self.lindex_callback(stub, index, |opt| opt.map(|v| v.to_vec()))
  }

  /// 零拷贝按索引借用读取元素回调 (零堆分配)
  fn lindex_callback<R>(
    &self,
    stub: &ListStub,
    index: i64,
    f: impl FnOnce(Option<&[u8]>) -> R,
  ) -> Result<R>;

  /// 按范围读取元素列表 (闭区间,支持负数索引与边界截断)
  fn lrange(&self, stub: &ListStub, start: i64, stop: i64) -> Result<Vec<Vec<u8>>>;

  /// 按范围流式遍历元素 (闭区间,零分配回调)
  fn lrange_callback<F>(&self, stub: &ListStub, start: i64, stop: i64, on_elem: F) -> Result<usize>
  where
    F: FnMut(&[u8]) -> bool;

  /// 按索引修改元素 (LSET)
  fn lset(&self, stub: &ListStub, index: i64, element: &[u8]) -> Result<()>;

  /// 裁剪列表仅保留指定区间元素 (LTRIM,闭区间,返回裁剪后剩余长度)
  fn ltrim(&self, stub: &mut ListStub, start: i64, stop: i64) -> Result<usize>;
}

impl ListTreeOps for BfTreeService {
  fn lpush(&self, stub: &mut ListStub, element: &[u8]) -> Result<usize> {
    if element.is_empty() {
      return Err(CollectionError::EmptyValue);
    }
    let new_head = stub
      .head
      .checked_sub(1)
      .ok_or(CollectionError::InvalidArgument("list head 下溢"))?;
    let key = list_key_from_i64(new_head);
    match self.insert(&key, element) {
      BfTreeInsertResult::Success => {
        stub.head = new_head;
        Ok(stub.len())
      }
      BfTreeInsertResult::InvalidKV => Err(CollectionError::KeyTooLong),
      _ => Err(CollectionError::InvalidArgument("lpush 插入失败")),
    }
  }

  fn rpush(&self, stub: &mut ListStub, element: &[u8]) -> Result<usize> {
    if element.is_empty() {
      return Err(CollectionError::EmptyValue);
    }
    let new_tail = stub
      .tail
      .checked_add(1)
      .ok_or(CollectionError::InvalidArgument("list tail 上溢"))?;
    let key = list_key_from_i64(stub.tail);
    match self.insert(&key, element) {
      BfTreeInsertResult::Success => {
        stub.tail = new_tail;
        Ok(stub.len())
      }
      BfTreeInsertResult::InvalidKV => Err(CollectionError::KeyTooLong),
      _ => Err(CollectionError::InvalidArgument("rpush 插入失败")),
    }
  }

  fn lpop(&self, stub: &mut ListStub) -> Result<Option<Vec<u8>>> {
    if stub.is_empty() {
      return Ok(None);
    }
    let key = list_key_from_i64(stub.head);
    let next_head = stub
      .head
      .checked_add(1)
      .ok_or(CollectionError::InvalidArgument("list head 上溢"))?;
    let (res, val) = self.read(&key);
    match res {
      BfTreeReadResult::Found => {
        self.delete(&key);
        stub.head = next_head;
        stub.reset_if_empty();
        Ok(val)
      }
      BfTreeReadResult::NotFound | BfTreeReadResult::Deleted => {
        stub.head = next_head;
        stub.reset_if_empty();
        Ok(None)
      }
      _ => Err(CollectionError::InvalidArgument("lpop 读取失败")),
    }
  }

  fn rpop(&self, stub: &mut ListStub) -> Result<Option<Vec<u8>>> {
    if stub.is_empty() {
      return Ok(None);
    }
    let target_idx = stub
      .tail
      .checked_sub(1)
      .ok_or(CollectionError::InvalidArgument("list tail 下溢"))?;
    let key = list_key_from_i64(target_idx);
    let (res, val) = self.read(&key);
    match res {
      BfTreeReadResult::Found => {
        self.delete(&key);
        stub.tail = target_idx;
        stub.reset_if_empty();
        Ok(val)
      }
      BfTreeReadResult::NotFound | BfTreeReadResult::Deleted => {
        stub.tail = target_idx;
        stub.reset_if_empty();
        Ok(None)
      }
      _ => Err(CollectionError::InvalidArgument("rpop 读取失败")),
    }
  }

  fn lindex_callback<R>(
    &self,
    stub: &ListStub,
    index: i64,
    f: impl FnOnce(Option<&[u8]>) -> R,
  ) -> Result<R> {
    let len = stub.len() as i64;
    if len == 0 || index >= len || index < -len {
      return Ok(f(None));
    }
    let actual_offset = if index >= 0 { index } else { len + index };
    let Some(actual_idx) = stub.head.checked_add(actual_offset) else {
      return Ok(f(None));
    };
    let key = list_key_from_i64(actual_idx);
    self.read_callback(&key, |res, bytes| match res {
      BfTreeReadResult::Found => Ok(f(Some(bytes))),
      BfTreeReadResult::NotFound | BfTreeReadResult::Deleted => Ok(f(None)),
      _ => Err(CollectionError::InvalidArgument("lindex 读取失败")),
    })
  }

  fn lrange(&self, stub: &ListStub, start: i64, stop: i64) -> Result<Vec<Vec<u8>>> {
    let Some((s, e)) = normalize_range(stub.len(), start, stop) else {
      return Ok(Vec::new());
    };
    let count = e - s + 1;
    let mut records = Vec::with_capacity(count);
    lrange_normalized_callback(self, stub, s, e, |elem| {
      records.push(elem.to_vec());
      true
    })?;
    Ok(records)
  }

  fn lrange_callback<F>(&self, stub: &ListStub, start: i64, stop: i64, on_elem: F) -> Result<usize>
  where
    F: FnMut(&[u8]) -> bool,
  {
    let Some((s, e)) = normalize_range(stub.len(), start, stop) else {
      return Ok(0);
    };
    lrange_normalized_callback(self, stub, s, e, on_elem)
  }

  fn lset(&self, stub: &ListStub, index: i64, element: &[u8]) -> Result<()> {
    if element.is_empty() {
      return Err(CollectionError::EmptyValue);
    }
    let len = stub.len() as i64;
    if len == 0 || index >= len || index < -len {
      return Err(CollectionError::InvalidArgument("index out of range"));
    }
    let actual_offset = if index >= 0 { index } else { len + index };
    let actual_idx = stub
      .head
      .checked_add(actual_offset)
      .ok_or(CollectionError::InvalidArgument("index overflow"))?;
    let key = list_key_from_i64(actual_idx);
    match self.insert(&key, element) {
      BfTreeInsertResult::Success => Ok(()),
      BfTreeInsertResult::InvalidKV => Err(CollectionError::KeyTooLong),
      _ => Err(CollectionError::InvalidArgument("lset 修改失败")),
    }
  }

  fn ltrim(&self, stub: &mut ListStub, start: i64, stop: i64) -> Result<usize> {
    let len = stub.len();
    if len == 0 {
      return Ok(0);
    }
    let Some((s, e)) = normalize_range(len, start, stop) else {
      // 范围不合法或为空,清空整个列表
      for offset in 0..len {
        if let Some(idx) = stub.head.checked_add(offset as i64) {
          self.delete(&list_key_from_i64(idx));
        }
      }
      stub.head = 0;
      stub.tail = 0;
      return Ok(0);
    };

    // 删除保留区间左侧的元素 [0, s)
    for offset in 0..s {
      if let Some(idx) = stub.head.checked_add(offset as i64) {
        self.delete(&list_key_from_i64(idx));
      }
    }

    // 删除保留区间右侧的元素 (e, len)
    for offset in (e + 1)..len {
      if let Some(idx) = stub.head.checked_add(offset as i64) {
        self.delete(&list_key_from_i64(idx));
      }
    }

    // 更新存根 head 与 tail
    let new_head = stub
      .head
      .checked_add(s as i64)
      .ok_or(CollectionError::InvalidArgument("list head 溢出"))?;
    let new_tail = stub
      .head
      .checked_add(e as i64 + 1)
      .ok_or(CollectionError::InvalidArgument("list tail 溢出"))?;
    stub.head = new_head;
    stub.tail = new_tail;
    stub.reset_if_empty();
    Ok(stub.len())
  }
}

/// 经标准化的区间流式读取元素
fn lrange_normalized_callback<F>(
  service: &BfTreeService,
  stub: &ListStub,
  s: usize,
  e: usize,
  mut on_elem: F,
) -> Result<usize>
where
  F: FnMut(&[u8]) -> bool,
{
  let Some(start_idx) = stub.head.checked_add(s as i64) else {
    return Ok(0);
  };
  let Some(end_idx) = stub.head.checked_add(e as i64) else {
    return Ok(0);
  };
  let start_key = list_key_from_i64(start_idx);
  let end_key = list_key_from_i64(end_idx);

  let prefix_u8 = TreePrefix::ListIndex as u8;
  let mut count = 0;
  service.scan_with_end_key_callback(
    &start_key,
    &end_key,
    ScanReturnField::KeyAndValue,
    |k, v| {
      if k.len() < 9 || k[0] != prefix_u8 {
        return false;
      }
      count += 1;
      on_elem(v)
    },
  )?;
  Ok(count)
}

/// ListTree 包装结构体
pub struct ListTree<'a> {
  /// 底层 BfTree 树实例
  pub tree: &'a BfTreeService,
  /// 关联的 List 元数据存根
  pub stub: &'a mut ListStub,
}

impl<'a> ListTree<'a> {
  /// 创建 ListTree 包装
  #[inline]
  pub fn new(tree: &'a BfTreeService, stub: &'a mut ListStub) -> Self {
    Self { tree, stub }
  }

  /// 左推入
  #[inline]
  pub fn lpush(&mut self, element: &[u8]) -> Result<usize> {
    self.tree.lpush(self.stub, element)
  }

  /// 右推入
  #[inline]
  pub fn rpush(&mut self, element: &[u8]) -> Result<usize> {
    self.tree.rpush(self.stub, element)
  }

  /// 左弹出
  #[inline]
  pub fn lpop(&mut self) -> Result<Option<Vec<u8>>> {
    self.tree.lpop(self.stub)
  }

  /// 右弹出
  #[inline]
  pub fn rpop(&mut self) -> Result<Option<Vec<u8>>> {
    self.tree.rpop(self.stub)
  }

  /// 索引读取
  #[inline]
  pub fn lindex(&self, index: i64) -> Result<Option<Vec<u8>>> {
    self.tree.lindex(self.stub, index)
  }

  /// 零拷贝索引读取回调
  #[inline]
  pub fn lindex_callback<R>(&self, index: i64, f: impl FnOnce(Option<&[u8]>) -> R) -> Result<R> {
    self.tree.lindex_callback(self.stub, index, f)
  }

  /// 范围读取
  #[inline]
  pub fn lrange(&self, start: i64, stop: i64) -> Result<Vec<Vec<u8>>> {
    self.tree.lrange(self.stub, start, stop)
  }

  /// 范围流式回调
  #[inline]
  pub fn lrange_callback<F>(&self, start: i64, stop: i64, on_elem: F) -> Result<usize>
  where
    F: FnMut(&[u8]) -> bool,
  {
    self.tree.lrange_callback(self.stub, start, stop, on_elem)
  }

  /// 按索引修改元素 (LSET)
  #[inline]
  pub fn lset(&mut self, index: i64, element: &[u8]) -> Result<()> {
    self.tree.lset(self.stub, index, element)
  }

  /// 裁剪列表仅保留指定区间元素 (LTRIM)
  #[inline]
  pub fn ltrim(&mut self, start: i64, stop: i64) -> Result<usize> {
    self.tree.ltrim(self.stub, start, stop)
  }

  /// 获取列表长度 (O(1) 存根算术直读)
  #[inline]
  pub const fn llen(&self) -> usize {
    self.stub.len()
  }

  /// 长度
  #[inline]
  pub const fn len(&self) -> usize {
    self.stub.len()
  }

  /// 是否为空
  #[inline]
  pub const fn is_empty(&self) -> bool {
    self.stub.is_empty()
  }
}