|
|
@ -6,7 +6,9 @@ |
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "leveldb/options.h"
|
|
|
|
#include "leveldb/status.h"
|
|
|
|
#include "db/write_batch_internal.h"
|
|
|
|
#include "util/serialize_value.h"
|
|
|
|
#include "fielddb/encode_index.h"
|
|
|
|
|
|
|
|
namespace fielddb { |
|
|
|
using namespace leveldb; |
|
|
@ -18,6 +20,7 @@ Status FieldDB::OpenFieldDB(const Options& options, |
|
|
|
return Status::NotSupported(name, "new a fieldDb first\n"); |
|
|
|
} |
|
|
|
|
|
|
|
//
|
|
|
|
Status status; |
|
|
|
DB *indexdb, *kvdb, *metadb; |
|
|
|
status = Open(options, name+"_indexDB", &indexdb); |
|
|
@ -77,6 +80,89 @@ std::vector FieldDB::FindKeysByField(Field &field) { |
|
|
|
return kvDB_->FindKeysByField(field); |
|
|
|
} |
|
|
|
|
|
|
|
std::vector<std::pair<std::string, std::string>> FieldDB::FindKeysAndValByFieldName ( |
|
|
|
const std::string &fieldName){ |
|
|
|
std::vector<std::pair<std::string, std::string>> result; |
|
|
|
auto iter = kvDB_->NewIterator(ReadOptions()); |
|
|
|
std::string val; |
|
|
|
for(iter->SeekToFirst();iter->Valid();iter->Next()) { |
|
|
|
InternalFieldArray fields(iter->value()); |
|
|
|
val = fields.ValOfName(fieldName); |
|
|
|
if(!val.empty()) { |
|
|
|
result.push_back(std::make_pair(iter->key().ToString(), val)); |
|
|
|
} |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
Status FieldDB::CreateIndexOnField(const std::string& field_name) { |
|
|
|
//taskQueue相关
|
|
|
|
//写锁 是不是只需要给putfields设置一把锁就行
|
|
|
|
|
|
|
|
std::vector<std::pair<std::string, std::string>> keysAndVal = |
|
|
|
FindKeysAndValByFieldName(field_name); |
|
|
|
WriteBatch writeBatch; |
|
|
|
Slice value = Slice(); |
|
|
|
for (auto &kvPair : keysAndVal){ |
|
|
|
std::string indexKey; |
|
|
|
AppendIndexKey(&indexKey, |
|
|
|
ParsedInternalIndexKey(kvPair.first, field_name, kvPair.second)); |
|
|
|
writeBatch.Put(indexKey, value); |
|
|
|
} |
|
|
|
Status s = indexDB_->Write(WriteOptions(), &writeBatch); |
|
|
|
if (!s.ok()) return s; |
|
|
|
|
|
|
|
index_[field_name] = Exist; |
|
|
|
//唤醒taskqueue
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Status FieldDB::DeleteIndex(const std::string &field_name) { |
|
|
|
//taskQueue相关
|
|
|
|
//写锁
|
|
|
|
std::vector<std::pair<std::string, std::string>> keysAndVal = |
|
|
|
FindKeysAndValByFieldName(field_name); |
|
|
|
WriteBatch writeBatch; |
|
|
|
for (auto &kvPair : keysAndVal){ |
|
|
|
std::string indexKey; |
|
|
|
AppendIndexKey(&indexKey, |
|
|
|
ParsedInternalIndexKey(kvPair.first, field_name, kvPair.second)); |
|
|
|
writeBatch.Delete(indexKey); |
|
|
|
} |
|
|
|
Status s = indexDB_->Write(WriteOptions(), &writeBatch); |
|
|
|
if (!s.ok()) return s; |
|
|
|
|
|
|
|
index_.erase(field_name); |
|
|
|
//唤醒taskqueue
|
|
|
|
} |
|
|
|
|
|
|
|
std::vector<std::string> FieldDB::QueryByIndex(const Field &field, Status *s) { |
|
|
|
if (index_.count(field.first) == 0 || index_[field.first] != Exist){ |
|
|
|
*s = Status::NotFound(Slice()); |
|
|
|
return std::vector<std::string>(); |
|
|
|
} |
|
|
|
std::string indexKey; |
|
|
|
AppendIndexKey(&indexKey, |
|
|
|
ParsedInternalIndexKey(Slice(), field.first, field.second)); |
|
|
|
Iterator *indexIterator = indexDB_->NewIterator(ReadOptions()); |
|
|
|
indexIterator->Seek(indexKey); |
|
|
|
|
|
|
|
std::vector<std::string> result; |
|
|
|
for (; indexIterator->Valid(); indexIterator->Next()) { |
|
|
|
ParsedInternalIndexKey iterKey; |
|
|
|
if (ParseInternalIndexKey(indexIterator->key(), &iterKey)){ |
|
|
|
if (iterKey.name_ == field.first && iterKey.val_ == field.second){ |
|
|
|
result.push_back(iterKey.user_key_.ToString()); |
|
|
|
continue; //查到说明在范围里,否则break
|
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
*s = Status::OK(); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
Iterator * FieldDB::NewIterator(const ReadOptions &options) { |
|
|
|
return kvDB_->NewIterator(options); |
|
|
|
} |
|
|
|