|
@ -4,6 +4,11 @@ |
|
|
|
|
|
|
|
|
#include "leveldb/table.h"
|
|
|
#include "leveldb/table.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <sstream> // For std::istringstream
|
|
|
|
|
|
#include <iomanip> // For std::get_time
|
|
|
|
|
|
#include <ctime> // For std::tm and std::mktime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "leveldb/cache.h"
|
|
|
#include "leveldb/cache.h"
|
|
|
#include "leveldb/comparator.h"
|
|
|
#include "leveldb/comparator.h"
|
|
|
#include "leveldb/env.h"
|
|
|
#include "leveldb/env.h"
|
|
@ -211,36 +216,101 @@ Iterator* Table::NewIterator(const ReadOptions& options) const { |
|
|
&Table::BlockReader, const_cast<Table*>(this), options); |
|
|
&Table::BlockReader, const_cast<Table*>(this), options); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Status Table::InternalGet(const ReadOptions& options, const Slice& k, void* arg,
|
|
|
|
|
|
// void (*handle_result)(void*, const Slice&,
|
|
|
|
|
|
// const Slice&)) {
|
|
|
|
|
|
// Status s;
|
|
|
|
|
|
// Iterator* iiter = rep_->index_block->NewIterator(rep_->options.comparator);
|
|
|
|
|
|
// iiter->Seek(k);
|
|
|
|
|
|
// if (iiter->Valid()) {
|
|
|
|
|
|
// Slice handle_value = iiter->value();
|
|
|
|
|
|
// FilterBlockReader* filter = rep_->filter;
|
|
|
|
|
|
// BlockHandle handle;
|
|
|
|
|
|
// if (filter != nullptr && handle.DecodeFrom(&handle_value).ok() &&
|
|
|
|
|
|
// !filter->KeyMayMatch(handle.offset(), k)) {
|
|
|
|
|
|
// // Not found
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// Iterator* block_iter = BlockReader(this, options, iiter->value());
|
|
|
|
|
|
// block_iter->Seek(k);
|
|
|
|
|
|
// if (block_iter->Valid()) {
|
|
|
|
|
|
// (*handle_result)(arg, block_iter->key(), block_iter->value());
|
|
|
|
|
|
// }
|
|
|
|
|
|
// s = block_iter->status();
|
|
|
|
|
|
// delete block_iter;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// if (s.ok()) {
|
|
|
|
|
|
// s = iiter->status();
|
|
|
|
|
|
// }
|
|
|
|
|
|
// delete iiter;
|
|
|
|
|
|
// return s;
|
|
|
|
|
|
// }
|
|
|
Status Table::InternalGet(const ReadOptions& options, const Slice& k, void* arg, |
|
|
Status Table::InternalGet(const ReadOptions& options, const Slice& k, void* arg, |
|
|
void (*handle_result)(void*, const Slice&, |
|
|
void (*handle_result)(void*, const Slice&, |
|
|
const Slice&)) { |
|
|
const Slice&)) { |
|
|
Status s; |
|
|
|
|
|
Iterator* iiter = rep_->index_block->NewIterator(rep_->options.comparator); |
|
|
|
|
|
iiter->Seek(k); |
|
|
|
|
|
if (iiter->Valid()) { |
|
|
|
|
|
Slice handle_value = iiter->value(); |
|
|
|
|
|
FilterBlockReader* filter = rep_->filter; |
|
|
|
|
|
BlockHandle handle; |
|
|
|
|
|
if (filter != nullptr && handle.DecodeFrom(&handle_value).ok() && |
|
|
|
|
|
!filter->KeyMayMatch(handle.offset(), k)) { |
|
|
|
|
|
// Not found
|
|
|
|
|
|
} else { |
|
|
|
|
|
Iterator* block_iter = BlockReader(this, options, iiter->value()); |
|
|
|
|
|
block_iter->Seek(k); |
|
|
|
|
|
if (block_iter->Valid()) { |
|
|
|
|
|
(*handle_result)(arg, block_iter->key(), block_iter->value()); |
|
|
|
|
|
} |
|
|
|
|
|
s = block_iter->status(); |
|
|
|
|
|
delete block_iter; |
|
|
|
|
|
|
|
|
Status s; |
|
|
|
|
|
Iterator* iiter = rep_->index_block->NewIterator(rep_->options.comparator); |
|
|
|
|
|
iiter->Seek(k); |
|
|
|
|
|
if (iiter->Valid()) { |
|
|
|
|
|
Slice handle_value = iiter->value(); |
|
|
|
|
|
FilterBlockReader* filter = rep_->filter; |
|
|
|
|
|
BlockHandle handle; |
|
|
|
|
|
if (filter != nullptr && handle.DecodeFrom(&handle_value).ok() && |
|
|
|
|
|
!filter->KeyMayMatch(handle.offset(), k)) { |
|
|
|
|
|
// Not found
|
|
|
|
|
|
} else { |
|
|
|
|
|
Iterator* block_iter = BlockReader(this, options, iiter->value()); |
|
|
|
|
|
block_iter->Seek(k); |
|
|
|
|
|
if (block_iter->Valid()) { |
|
|
|
|
|
// 这里获取存储的组合字符串
|
|
|
|
|
|
s = Status::OK(); // 确保状态为 OK
|
|
|
|
|
|
Slice combined_value = block_iter->value(); |
|
|
|
|
|
|
|
|
|
|
|
// 获取实际的值和过期时间
|
|
|
|
|
|
std::string combined_str = combined_value.ToString(); |
|
|
|
|
|
|
|
|
|
|
|
// 假设过期时间是字符串的最后19个字符
|
|
|
|
|
|
std::string expiration_time_str = combined_str.substr(combined_str.size() - 19, 19); // 获取过期时间字符串
|
|
|
|
|
|
std::string actual_value = combined_str.substr(0, combined_str.size() - 19); // 获取实际值
|
|
|
|
|
|
|
|
|
|
|
|
// 解析过期时间为时间戳
|
|
|
|
|
|
std::tm tm = {}; |
|
|
|
|
|
std::istringstream ss(expiration_time_str); |
|
|
|
|
|
ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S"); |
|
|
|
|
|
std::time_t expiration_time = std::mktime(&tm); |
|
|
|
|
|
|
|
|
|
|
|
// 获取当前时间并与过期时间进行比较
|
|
|
|
|
|
auto now = std::chrono::system_clock::now(); |
|
|
|
|
|
auto now_time_t = std::chrono::system_clock::to_time_t(now); |
|
|
|
|
|
|
|
|
|
|
|
// 检查是否过期
|
|
|
|
|
|
if (expiration_time > now_time_t) { |
|
|
|
|
|
// 调用结果处理函数,返回实际值
|
|
|
|
|
|
(*handle_result)(arg, block_iter->key(), Slice(actual_value));//, Slice(expiration_time_str));
|
|
|
|
|
|
s = block_iter->status(); |
|
|
|
|
|
if (! s.ok()) printf("291\n"); |
|
|
|
|
|
} else { |
|
|
|
|
|
// 数据已过期,处理过期情况
|
|
|
|
|
|
s = Status::NotFound("Key has expired"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
s = block_iter->status(); |
|
|
|
|
|
if (! s.ok()) printf("299\n"); |
|
|
|
|
|
} |
|
|
|
|
|
delete block_iter; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if (s.ok()) { |
|
|
|
|
|
s = iiter->status(); |
|
|
|
|
|
} |
|
|
|
|
|
delete iiter; |
|
|
|
|
|
return s; |
|
|
|
|
|
|
|
|
if (s.ok()) { |
|
|
|
|
|
s = iiter->status(); |
|
|
|
|
|
if (! s.ok()) printf("306\n"); |
|
|
|
|
|
} |
|
|
|
|
|
delete iiter; |
|
|
|
|
|
if (! s.ok()) printf("!!!!!!!!!!!"); // 如果不在sstable里
|
|
|
|
|
|
return s; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint64_t Table::ApproximateOffsetOf(const Slice& key) const { |
|
|
uint64_t Table::ApproximateOffsetOf(const Slice& key) const { |
|
|
Iterator* index_iter = |
|
|
Iterator* index_iter = |
|
|
rep_->index_block->NewIterator(rep_->options.comparator); |
|
|
rep_->index_block->NewIterator(rep_->options.comparator); |
|
|