|
|
@ -1,9 +1,12 @@ |
|
|
|
#include "fielddb/request.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <deque>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include "leveldb/slice.h"
|
|
|
|
#include "leveldb/status.h"
|
|
|
|
#include "leveldb/write_batch.h"
|
|
|
|
#include "port/port_stdcxx.h"
|
|
|
|
#include "util/mutexlock.h"
|
|
|
|
#include "util/serialize_value.h"
|
|
|
|
#include "fielddb/encode_index.h"
|
|
|
@ -74,7 +77,7 @@ void FieldsReq::ConstructBatch(WriteBatch &KVBatch,WriteBatch &IndexBatch, |
|
|
|
if(DB->index_.count(field_name)) { |
|
|
|
auto [index_status,parent_req] = DB->index_[field_name]; |
|
|
|
if(index_status == IndexStatus::Creating || index_status == IndexStatus::Deleting) { |
|
|
|
parent_req->PendReq(this); |
|
|
|
parent_req->PendReq(this->parent); |
|
|
|
return; |
|
|
|
} else if(index_status == IndexStatus::Exist) { |
|
|
|
HasIndex = true; |
|
|
@ -89,7 +92,7 @@ void FieldsReq::ConstructBatch(WriteBatch &KVBatch,WriteBatch &IndexBatch, |
|
|
|
if(DB->index_.count(field_name)) { |
|
|
|
auto [index_status,parent_req] = DB->index_[field_name]; |
|
|
|
if(index_status == IndexStatus::Creating || index_status == IndexStatus::Deleting) { |
|
|
|
parent_req->PendReq(this); |
|
|
|
parent_req->PendReq(this->parent); |
|
|
|
return; |
|
|
|
} else if(index_status == IndexStatus::Exist) { |
|
|
|
HasOldIndex = true; |
|
|
@ -172,7 +175,7 @@ void DeleteReq::ConstructBatch(WriteBatch &KVBatch,WriteBatch &IndexBatch, |
|
|
|
if(DB->index_.count(field_name)) { |
|
|
|
auto [index_status,parent_req] = DB->index_[field_name]; |
|
|
|
if(index_status == IndexStatus::Creating || index_status == IndexStatus::Deleting) { |
|
|
|
parent_req->PendReq(this); |
|
|
|
parent_req->PendReq(this->parent); |
|
|
|
return; |
|
|
|
} else if(index_status == IndexStatus::Exist) { |
|
|
|
HasIndex = true; |
|
|
@ -214,7 +217,7 @@ void iCreateReq::Prepare(FieldDB *DB) { |
|
|
|
s = Status::OK(); |
|
|
|
} else { |
|
|
|
//如果正在创建或删除,那么进行等待
|
|
|
|
parent->PendReq(this); |
|
|
|
parent->PendReq(this->parent); |
|
|
|
} |
|
|
|
return; |
|
|
|
} |
|
|
@ -320,5 +323,67 @@ void iDeleteReq::Finalize(FieldDB *DB) { |
|
|
|
this->s = Status::OK(); |
|
|
|
} |
|
|
|
|
|
|
|
BatchReq::BatchReq(WriteBatch *Batch,port::Mutex *mu): |
|
|
|
Batch(Batch),Request(BatchReq_t, mu) { |
|
|
|
|
|
|
|
struct BatchHandler : WriteBatch::Handler { |
|
|
|
void Put(const Slice &key, const Slice &value) override { |
|
|
|
//为key和value构造存储的地方,防止由于string的析构造成可能得内存访问错误
|
|
|
|
str_buf->push_back(key.ToString()); |
|
|
|
fa_buf->push_back({{"",value.ToString()}}); |
|
|
|
sub_requests->emplace_back(new FieldsReq(&str_buf->back(),&fa_buf->back(),mu)); |
|
|
|
sub_requests->back()->parent = req; |
|
|
|
} |
|
|
|
void Delete(const Slice &key) override { |
|
|
|
str_buf->push_back(key.ToString()); |
|
|
|
sub_requests->emplace_back(new DeleteReq(&str_buf->back(),mu)); |
|
|
|
sub_requests->back()->parent = req; |
|
|
|
} |
|
|
|
|
|
|
|
BatchReq *req; |
|
|
|
port::Mutex *mu; |
|
|
|
std::deque<std::string> *str_buf; |
|
|
|
std::deque<FieldArray> *fa_buf; |
|
|
|
std::deque<Request*> *sub_requests; |
|
|
|
}; |
|
|
|
|
|
|
|
BatchHandler Handler; |
|
|
|
Handler.req = this; |
|
|
|
Handler.mu = mu; |
|
|
|
Handler.str_buf = &str_buf; |
|
|
|
Handler.fa_buf = &fa_buf; |
|
|
|
Handler.sub_requests = &sub_requests; |
|
|
|
|
|
|
|
Batch->Iterate(&Handler); |
|
|
|
} |
|
|
|
|
|
|
|
BatchReq::~BatchReq() { |
|
|
|
while(!sub_requests.empty()) { |
|
|
|
Request *req = sub_requests.front(); |
|
|
|
sub_requests.pop_front(); |
|
|
|
delete req; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void BatchReq::ConstructBatch(WriteBatch &KVBatch,WriteBatch &IndexBatch, |
|
|
|
WriteBatch &MetaBatch,fielddb::FieldDB *DB,std::unordered_set<std::string *> &batchKeySet) |
|
|
|
{ |
|
|
|
WriteBatch Sub_KVBatch,Sub_IndexBatch,Sub_MetaBatch; |
|
|
|
std::unordered_set<std::string *> Sub_batchKeySet; |
|
|
|
//由于batch是有顺序的,根据我们现在的一个key只处理最开始的算法,这里需要反向迭代
|
|
|
|
for(auto subreq = sub_requests.rbegin(); subreq != sub_requests.rend(); subreq++ ) { |
|
|
|
(*subreq)->ConstructBatch(Sub_KVBatch, Sub_IndexBatch, Sub_MetaBatch, DB, Sub_batchKeySet); |
|
|
|
//所有的对于pendreq的调用传入的参数被改成了this->parent,因此,对于subrequests来说,
|
|
|
|
//pendreq的传参为对应的Batchreq,因此,此处判断batchreq是否pending可以得到subreq是否有冲突
|
|
|
|
if(isPending()) { |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
KVBatch.Append(Sub_KVBatch); |
|
|
|
IndexBatch.Append(Sub_IndexBatch); |
|
|
|
MetaBatch.Append(Sub_MetaBatch); |
|
|
|
batchKeySet.insert(batchKeySet.begin(),batchKeySet.end()); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} // namespace fielddb
|