diff --git a/db/db_impl.cc b/db/db_impl.cc index 0c01733..1d42842 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -1235,10 +1235,67 @@ void DBImpl::ReleaseSnapshot(const Snapshot* snapshot) { // Convenience methods Status DBImpl::Put(const WriteOptions& o, const Slice& key, const Slice& val) { + //ToDo + 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); + + 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; + } + } + } + } + //ToDo end return DB::Put(o, key, val); } Status DBImpl::Delete(const WriteOptions& options, const Slice& key) { + //ToDo + 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); + + if (!fieldValue.empty()) { + // 构建索引键 (例如:fieldValue -> key) + std::string indexKey = field + ":" + fieldValue; + + // 从indexDb_中删除二级索引 + s = indexDb_->Delete(options, Slice(indexKey)); + if (!s.ok()) { + return s; + } + } + } + } + //ToDo end return DB::Delete(options, key); }