|
|
@ -1236,67 +1236,79 @@ void DBImpl::ReleaseSnapshot(const Snapshot* snapshot) { |
|
|
|
// Convenience methods
|
|
|
|
Status DBImpl::Put(const WriteOptions& o, const Slice& key, const Slice& val) { |
|
|
|
//ToDo
|
|
|
|
WriteBatch batch; // 创建事务
|
|
|
|
Status s; |
|
|
|
// 遍历fieldWithIndex_,检查是否需要更新索引
|
|
|
|
for (const auto& field : fieldWithIndex_) { |
|
|
|
// 提取字段值
|
|
|
|
size_t field_pos = val.ToString().find(field + ":"); |
|
|
|
if (field_pos != std::string::npos) { |
|
|
|
size_t value_start = field_pos + field.size() + 1; // 跳过 "fieldName:"
|
|
|
|
size_t value_end = val.ToString().find("|", value_start); // 查找下一个分隔符
|
|
|
|
if (value_end == std::string::npos) { |
|
|
|
value_end = val.ToString().size(); |
|
|
|
} |
|
|
|
|
|
|
|
std::string fieldValue = val.ToString().substr(value_start, value_end - value_start); |
|
|
|
// 在主数据库写入数据
|
|
|
|
batch.Put(key, val); |
|
|
|
|
|
|
|
// 遍历fieldWithIndex_,检查是否需要更新索引
|
|
|
|
for (const auto& field : fieldWithIndex_) { |
|
|
|
size_t field_pos = val.ToString().find(field + ":"); |
|
|
|
if (field_pos != std::string::npos) { |
|
|
|
size_t value_start = field_pos + field.size() + 1; // 跳过 "fieldName:"
|
|
|
|
size_t value_end = val.ToString().find("|", value_start); // 查找下一个分隔符
|
|
|
|
if (value_end == std::string::npos) { |
|
|
|
value_end = val.ToString().size(); |
|
|
|
} |
|
|
|
std::string fieldValue = val.ToString().substr(value_start, value_end - value_start); |
|
|
|
|
|
|
|
if (!fieldValue.empty()) { |
|
|
|
// 构建索引键 (例如:fieldValue -> key)
|
|
|
|
std::string indexKey = field + ":" + fieldValue; |
|
|
|
std::string indexValue = key.ToString(); |
|
|
|
|
|
|
|
// 在indexDb_中插入二级索引
|
|
|
|
s = indexDb_->Put(o, Slice(indexKey), Slice(indexValue)); |
|
|
|
if (!s.ok()) { |
|
|
|
return s; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (!fieldValue.empty()) { |
|
|
|
std::string indexKey = field + ":" + fieldValue; |
|
|
|
std::string indexValue = key.ToString(); |
|
|
|
|
|
|
|
// 将索引插入操作加入事务
|
|
|
|
batch.Put(Slice(indexKey), Slice(indexValue)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 使用 `this->Write` 提交事务
|
|
|
|
s = this->Write(o, &batch); |
|
|
|
if (!s.ok()) { |
|
|
|
return s; |
|
|
|
} |
|
|
|
return Status::OK(); |
|
|
|
//ToDo end
|
|
|
|
return DB::Put(o, key, val); |
|
|
|
//return DB::Put(o, key, val);
|
|
|
|
} |
|
|
|
|
|
|
|
Status DBImpl::Delete(const WriteOptions& options, const Slice& key) { |
|
|
|
//ToDo
|
|
|
|
WriteBatch batch; // 创建事务
|
|
|
|
Status s; |
|
|
|
// 遍历fieldWithIndex_,检查是否需要删除索引
|
|
|
|
for (const auto& field : fieldWithIndex_) { |
|
|
|
// 假设key是包含字段的格式,提取字段值
|
|
|
|
size_t field_pos = key.ToString().find(field + ":"); |
|
|
|
if (field_pos != std::string::npos) { |
|
|
|
size_t value_start = field_pos + field.size() + 1; |
|
|
|
size_t value_end = key.ToString().find("|", value_start); |
|
|
|
if (value_end == std::string::npos) { |
|
|
|
value_end = key.ToString().size(); |
|
|
|
} |
|
|
|
|
|
|
|
std::string fieldValue = key.ToString().substr(value_start, value_end - value_start); |
|
|
|
// 在主数据库删除数据
|
|
|
|
batch.Delete(key); |
|
|
|
|
|
|
|
// 遍历fieldWithIndex_,检查是否需要删除索引
|
|
|
|
for (const auto& field : fieldWithIndex_) { |
|
|
|
size_t field_pos = key.ToString().find(field + ":"); |
|
|
|
if (field_pos != std::string::npos) { |
|
|
|
size_t value_start = field_pos + field.size() + 1; |
|
|
|
size_t value_end = key.ToString().find("|", value_start); |
|
|
|
if (value_end == std::string::npos) { |
|
|
|
value_end = key.ToString().size(); |
|
|
|
} |
|
|
|
std::string fieldValue = key.ToString().substr(value_start, value_end - value_start); |
|
|
|
|
|
|
|
if (!fieldValue.empty()) { |
|
|
|
// 构建索引键 (例如:fieldValue -> key)
|
|
|
|
std::string indexKey = field + ":" + fieldValue; |
|
|
|
|
|
|
|
// 从indexDb_中删除二级索引
|
|
|
|
s = indexDb_->Delete(options, Slice(indexKey)); |
|
|
|
if (!s.ok()) { |
|
|
|
return s; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (!fieldValue.empty()) { |
|
|
|
std::string indexKey = field + ":" + fieldValue; |
|
|
|
|
|
|
|
// 将索引删除操作加入事务
|
|
|
|
batch.Delete(Slice(indexKey)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 使用 `this->Write` 提交事务
|
|
|
|
s = this->Write(options, &batch); |
|
|
|
if (!s.ok()) { |
|
|
|
return s; |
|
|
|
} |
|
|
|
return Status::OK(); |
|
|
|
//ToDo end
|
|
|
|
return DB::Delete(options, key); |
|
|
|
//return DB::Delete(options, key);
|
|
|
|
} |
|
|
|
|
|
|
|
Status DBImpl::Write(const WriteOptions& options, WriteBatch* updates) { |
|
|
|