|
|
@ -4,14 +4,6 @@ |
|
|
|
|
|
|
|
#include "db/db_impl.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <atomic>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "db/builder.h"
|
|
|
|
#include "db/db_iter.h"
|
|
|
|
#include "db/dbformat.h"
|
|
|
@ -22,11 +14,21 @@ |
|
|
|
#include "db/table_cache.h"
|
|
|
|
#include "db/version_set.h"
|
|
|
|
#include "db/write_batch_internal.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <atomic>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <iostream>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "leveldb/db.h"
|
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "leveldb/status.h"
|
|
|
|
#include "leveldb/table.h"
|
|
|
|
#include "leveldb/table_builder.h"
|
|
|
|
|
|
|
|
#include "port/port.h"
|
|
|
|
#include "table/block.h"
|
|
|
|
#include "table/merger.h"
|
|
|
@ -1162,6 +1164,15 @@ Status DBImpl::Get(const ReadOptions& options, const Slice& key, |
|
|
|
mem->Unref(); |
|
|
|
if (imm != nullptr) imm->Unref(); |
|
|
|
current->Unref(); |
|
|
|
if(s.ok()){ |
|
|
|
auto a = env_->GetCurrentTime(); |
|
|
|
auto b = GetTS(value); |
|
|
|
std::cout<< "read when " << a<<std::endl; |
|
|
|
std::cout<< "read timestamp "<<b << std::endl; |
|
|
|
if(env_->GetCurrentTime() > GetTS(value)){ |
|
|
|
return Status::Expire("Expire",Slice()); |
|
|
|
} |
|
|
|
} |
|
|
|
return s; |
|
|
|
} |
|
|
|
|
|
|
@ -1475,7 +1486,6 @@ void DBImpl::GetApproximateSizes(const Range* range, int n, uint64_t* sizes) { |
|
|
|
MutexLock l(&mutex_); |
|
|
|
Version* v = versions_->current(); |
|
|
|
v->Ref(); |
|
|
|
|
|
|
|
for (int i = 0; i < n; i++) { |
|
|
|
// Convert user_key into a corresponding internal key.
|
|
|
|
InternalKey k1(range[i].start, kMaxSequenceNumber, kValueTypeForSeek); |
|
|
@ -1484,7 +1494,6 @@ void DBImpl::GetApproximateSizes(const Range* range, int n, uint64_t* sizes) { |
|
|
|
uint64_t limit = versions_->ApproximateOffsetOf(v, k2); |
|
|
|
sizes[i] = (limit >= start ? limit - start : 0); |
|
|
|
} |
|
|
|
|
|
|
|
v->Unref(); |
|
|
|
} |
|
|
|
|
|
|
@ -1503,6 +1512,12 @@ void DBImpl::AppendTS(const Slice& val, std::string* val_with_ts,uint64_t ttl) { |
|
|
|
val_with_ts->append(reinterpret_cast<const char*>(&st), sizeof(st)); |
|
|
|
} |
|
|
|
|
|
|
|
uint64_t DBImpl::GetTS(const std::string* val) { |
|
|
|
uint64_t expiration_time; |
|
|
|
memcpy(&expiration_time, val->data() + val->size() - sizeof(uint64_t), sizeof(uint64_t)); |
|
|
|
return expiration_time; |
|
|
|
} |
|
|
|
|
|
|
|
/**
|
|
|
|
* |
|
|
|
* @param options |
|
|
@ -1521,14 +1536,16 @@ Status DB::Put(const WriteOptions& options, const Slice& key, |
|
|
|
|
|
|
|
uint64_t expiration_time = std::chrono::duration_cast<std::chrono::milliseconds>( |
|
|
|
std::chrono::system_clock::now().time_since_epoch()) |
|
|
|
.count(); + ttl; |
|
|
|
.count() + ttl; |
|
|
|
|
|
|
|
// 追加原始 value 到 val_with_ts
|
|
|
|
val_with_ts.append(value.data(), value.size()); |
|
|
|
|
|
|
|
// 将 expiration_time 追加到 val_with_ts
|
|
|
|
val_with_ts.append(reinterpret_cast<const char*>(&expiration_time), sizeof(expiration_time)); |
|
|
|
|
|
|
|
std::cout<<"PUT"<<std::endl; |
|
|
|
std::cout << "timestamp: " << expiration_time << std::endl; |
|
|
|
//"a\323='\277\222\001\000"
|
|
|
|
WriteBatch batch; |
|
|
|
batch.Put(key, Slice(val_with_ts)); |
|
|
|
return Write(options, &batch); |
|
|
|