//
// Created by 马也驰 on 2024/12/29.
//

#ifndef LEVELDB_VLOG_GC_H
#define LEVELDB_VLOG_GC_H


#include <string>
#include <mutex>
#include "../db/slotpage.h"
#include "../db/threadpool.h"
#include "../db/vlog.h"

// 前向声明 VlogSet
class VlogSet;

class VlogGC {
 public:
  VlogGC(SlotPage *s, VlogSet *vs) : slot_page_(s), vlog_set(vs) {}
  void do_gc(size_t old_vlog_num, size_t new_vlog_num);

 private:
  void exec_gc(size_t old_vlog_num, size_t new_vlog_num);

  inline bool value_deleted(uint16_t value_len) {
    return !(value_len >> 15);
  }
  inline uint16_t get_value_len(char *value) {
    uint16_t value_len;
    memcpy(&value_len, value, sizeof(uint16_t));
    return value_len;
  }
  inline size_t get_value_slotnum(char *value) {
    size_t slot_num;
    memcpy(&slot_num, &value[sizeof(uint16_t)], sizeof(size_t));
    return slot_num;
  }

  SlotPage *slot_page_;
  ThreadPool *thread_pool_;
  VlogSet *vlog_set;  // 仅声明为指针，具体定义放在 vlog_gc.cpp
};


#endif  // LEVELDB_VLOG_GC_H
