|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +#include "indexer.h" |
| 22 | + |
| 23 | +#include <variant> |
| 24 | + |
| 25 | +#include "storage/redis_metadata.h" |
| 26 | +#include "types/redis_hash.h" |
| 27 | + |
| 28 | +namespace redis { |
| 29 | + |
| 30 | +StatusOr<FieldValueRetriever> FieldValueRetriever::Create(SearchOnDataType type, std::string_view key, |
| 31 | + engine::Storage *storage, const std::string &ns) { |
| 32 | + if (type == SearchOnDataType::HASH) { |
| 33 | + Hash db(storage, ns); |
| 34 | + std::string ns_key = db.AppendNamespacePrefix(key); |
| 35 | + HashMetadata metadata(false); |
| 36 | + auto s = db.GetMetadata(ns_key, &metadata); |
| 37 | + if (!s.ok()) return {Status::NotOK, s.ToString()}; |
| 38 | + return FieldValueRetriever(db, metadata, key); |
| 39 | + } else if (type == SearchOnDataType::JSON) { |
| 40 | + Json db(storage, ns); |
| 41 | + std::string ns_key = db.AppendNamespacePrefix(key); |
| 42 | + JsonMetadata metadata(false); |
| 43 | + JsonValue value; |
| 44 | + auto s = db.read(ns_key, &metadata, &value); |
| 45 | + if (!s.ok()) return {Status::NotOK, s.ToString()}; |
| 46 | + return FieldValueRetriever(value); |
| 47 | + } else { |
| 48 | + assert(false && "unreachable code: unexpected SearchOnDataType"); |
| 49 | + __builtin_unreachable(); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +rocksdb::Status FieldValueRetriever::Retrieve(std::string_view field, std::string *output) { |
| 54 | + if (std::holds_alternative<HashData>(db)) { |
| 55 | + auto &[hash, metadata, key] = std::get<HashData>(db); |
| 56 | + std::string ns_key = hash.AppendNamespacePrefix(key); |
| 57 | + LatestSnapShot ss(hash.storage_); |
| 58 | + rocksdb::ReadOptions read_options; |
| 59 | + read_options.snapshot = ss.GetSnapShot(); |
| 60 | + std::string sub_key = InternalKey(ns_key, field, metadata.version, hash.storage_->IsSlotIdEncoded()).Encode(); |
| 61 | + return hash.storage_->Get(read_options, sub_key, output); |
| 62 | + } else if (std::holds_alternative<JsonData>(db)) { |
| 63 | + auto &value = std::get<JsonData>(db); |
| 64 | + auto s = value.Get(field); |
| 65 | + if (!s.IsOK()) return rocksdb::Status::Corruption(s.Msg()); |
| 66 | + if (s->value.size() != 1) |
| 67 | + return rocksdb::Status::NotFound("json value specified by the field (json path) should exist and be unique"); |
| 68 | + *output = s->value[0].as_string(); |
| 69 | + return rocksdb::Status::OK(); |
| 70 | + } else { |
| 71 | + __builtin_unreachable(); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +StatusOr<IndexUpdater::FieldValues> IndexUpdater::Record(std::string_view key, const std::string &ns) { |
| 76 | + Database db(indexer->storage, ns); |
| 77 | + |
| 78 | + RedisType type = kRedisNone; |
| 79 | + auto s = db.Type(key, &type); |
| 80 | + if (!s.ok()) return {Status::NotOK, s.ToString()}; |
| 81 | + |
| 82 | + if (type != static_cast<RedisType>(on_data_type)) { |
| 83 | + // not the expected type, stop record |
| 84 | + return {Status::NotOK, "this data type cannot be indexed"}; |
| 85 | + } |
| 86 | + |
| 87 | + auto retriever = GET_OR_RET(FieldValueRetriever::Create(on_data_type, key, indexer->storage, ns)); |
| 88 | + |
| 89 | + FieldValues values; |
| 90 | + for (const auto &[field, info] : fields) { |
| 91 | + std::string value; |
| 92 | + auto s = retriever.Retrieve(field, &value); |
| 93 | + if (s.IsNotFound()) continue; |
| 94 | + if (!s.ok()) return {Status::NotOK, s.ToString()}; |
| 95 | + |
| 96 | + values.emplace(field, value); |
| 97 | + } |
| 98 | + |
| 99 | + return values; |
| 100 | +} |
| 101 | + |
| 102 | +void GlobalIndexer::Add(IndexUpdater updater) { |
| 103 | + auto &up = updaters.emplace_back(std::move(updater)); |
| 104 | + for (const auto &prefix : up.prefixes) { |
| 105 | + prefix_map.emplace(prefix, &up); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +StatusOr<IndexUpdater::FieldValues> GlobalIndexer::Record(std::string_view key, const std::string &ns) { |
| 110 | + auto iter = prefix_map.longest_prefix(key); |
| 111 | + if (iter != prefix_map.end()) { |
| 112 | + return iter.value()->Record(key, ns); |
| 113 | + } |
| 114 | + |
| 115 | + return {Status::NoPrefixMatched}; |
| 116 | +} |
| 117 | + |
| 118 | +} // namespace redis |
0 commit comments