|  | 
|  | 1 | +/** | 
|  | 2 | + * Copyright 2024 AntGroup CO., Ltd. | 
|  | 3 | + * | 
|  | 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | + * you may not use this file except in compliance with the License. | 
|  | 6 | + * You may obtain a copy of the License at | 
|  | 7 | + * | 
|  | 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | + * | 
|  | 10 | + * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | + */ | 
|  | 14 | + | 
|  | 15 | +#pragma once | 
|  | 16 | + | 
|  | 17 | +#include <atomic> | 
|  | 18 | +#include <exception> | 
|  | 19 | +#include <unordered_map> | 
|  | 20 | + | 
|  | 21 | +#include "core/field_data_helper.h" | 
|  | 22 | +#include "core/data_type.h" | 
|  | 23 | +#include "core/iterator_base.h" | 
|  | 24 | +#include "core/kv_store.h" | 
|  | 25 | +#include "core/kv_table_comparators.h" | 
|  | 26 | +#include "core/type_convert.h" | 
|  | 27 | +#include "core/value.h" | 
|  | 28 | + | 
|  | 29 | +namespace lgraph { | 
|  | 30 | + | 
|  | 31 | +class CompositeIndexValue { | 
|  | 32 | +    friend class CompositeIndex; | 
|  | 33 | + | 
|  | 34 | +    Value v_; | 
|  | 35 | + | 
|  | 36 | +    CompositeIndexValue(): v_(1) { *(uint8_t*)v_.Data() = 0; } | 
|  | 37 | + | 
|  | 38 | +    explicit CompositeIndexValue(const Value& v): v_(v) {} | 
|  | 39 | + | 
|  | 40 | +    explicit CompositeIndexValue(Value&& v): v_(std::move(v)) {} | 
|  | 41 | + | 
|  | 42 | +    int GetVidCount() const { return static_cast<int>(*(uint8_t*)v_.Data()); } | 
|  | 43 | + | 
|  | 44 | +    template <typename IT> | 
|  | 45 | +    CompositeIndexValue(const IT& beg, const IT& end) { | 
|  | 46 | +        size_t n = end - beg; | 
|  | 47 | +        FMA_DBG_ASSERT(n < std::numeric_limits<uint8_t>::max()); | 
|  | 48 | +        v_.Resize(1 + _detail::VID_SIZE * n); | 
|  | 49 | +        char* p = v_.Data(); | 
|  | 50 | +        *(uint8_t*)p = (uint8_t)n; | 
|  | 51 | +        p++; | 
|  | 52 | +        for (auto it = beg; it < end; it++) { | 
|  | 53 | +            _detail::WriteVid(p, *it); | 
|  | 54 | +            p += _detail::VID_SIZE; | 
|  | 55 | +        } | 
|  | 56 | +    } | 
|  | 57 | + | 
|  | 58 | +    /** | 
|  | 59 | +     * Patch the key with the last vid in this VertexCompositeIndexValue. | 
|  | 60 | +     */ | 
|  | 61 | +    Value CreateKey(const Value& key) const; | 
|  | 62 | + | 
|  | 63 | +    const Value& GetBuf() const { return v_; } | 
|  | 64 | + | 
|  | 65 | +    Value& GetBuf() { return v_; } | 
|  | 66 | +}; | 
|  | 67 | + | 
|  | 68 | +/** | 
|  | 69 | + * The indices | 
|  | 70 | + */ | 
|  | 71 | +class CompositeIndex { | 
|  | 72 | +    friend class LightningGraph; | 
|  | 73 | + | 
|  | 74 | +    std::shared_ptr<KvTable> table_; | 
|  | 75 | +    std::vector<FieldType> key_types; | 
|  | 76 | +    std::atomic<bool> ready_; | 
|  | 77 | +    std::atomic<bool> disabled_; | 
|  | 78 | +    CompositeIndexType type_; | 
|  | 79 | + | 
|  | 80 | + public: | 
|  | 81 | +    CompositeIndex(std::shared_ptr<KvTable> table, std::vector<FieldType> key_types, | 
|  | 82 | +                   CompositeIndexType type); | 
|  | 83 | + | 
|  | 84 | +    CompositeIndex(const CompositeIndex& rhs); | 
|  | 85 | + | 
|  | 86 | +    CompositeIndex(CompositeIndex&& rhs) = delete; | 
|  | 87 | + | 
|  | 88 | +    CompositeIndex& operator=(const CompositeIndex& rhs) = delete; | 
|  | 89 | + | 
|  | 90 | +    CompositeIndex& operator=(CompositeIndex&& rhs) = delete; | 
|  | 91 | + | 
|  | 92 | +    static std::unique_ptr<KvTable> OpenTable(KvTransaction& txn, KvStore& store, | 
|  | 93 | +                                              const std::string& name, | 
|  | 94 | +                                              const std::vector<FieldType> &dt, | 
|  | 95 | +                                              CompositeIndexType type); | 
|  | 96 | + | 
|  | 97 | +    void _AppendCompositeIndexEntry(KvTransaction& txn, const Value& k, VertexId vid); | 
|  | 98 | + | 
|  | 99 | +    bool Add(KvTransaction& txn, const Value& k, int64_t vid); | 
|  | 100 | + | 
|  | 101 | + private: | 
|  | 102 | +    void Clear(KvTransaction& txn) { table_->Drop(txn); } | 
|  | 103 | + | 
|  | 104 | +    void SetReady() { ready_.store(true, std::memory_order_release); } | 
|  | 105 | + | 
|  | 106 | +    void Disable() { disabled_.store(true, std::memory_order_release); } | 
|  | 107 | + | 
|  | 108 | +    void Enable() { disabled_.store(false, std::memory_order_release); } | 
|  | 109 | + | 
|  | 110 | +    [[nodiscard]] bool IsDisabled() const { return disabled_.load(std::memory_order_acquire); } | 
|  | 111 | +}; | 
|  | 112 | +}  // namespace lgraph | 
0 commit comments