From e96dbb0942382a4a8caadce9fc5632a064d9fbef Mon Sep 17 00:00:00 2001 From: Tor Egge Date: Tue, 1 Nov 2022 15:06:25 +0100 Subject: Use member functions in TensorAttribute. --- .../searchlib/tensor/dense_tensor_attribute.cpp | 111 --------------------- .../searchlib/tensor/dense_tensor_attribute.h | 14 --- .../searchlib/tensor/direct_tensor_attribute.cpp | 17 ---- .../searchlib/tensor/direct_tensor_attribute.h | 1 - .../tensor/serialized_fast_value_attribute.cpp | 22 +--- .../tensor/serialized_fast_value_attribute.h | 3 - .../vespa/searchlib/tensor/tensor_attribute.cpp | 68 +++++++++++++ .../src/vespa/searchlib/tensor/tensor_attribute.h | 7 +- 8 files changed, 74 insertions(+), 169 deletions(-) (limited to 'searchlib') diff --git a/searchlib/src/vespa/searchlib/tensor/dense_tensor_attribute.cpp b/searchlib/src/vespa/searchlib/tensor/dense_tensor_attribute.cpp index 67215a99b71..08a511c65bf 100644 --- a/searchlib/src/vespa/searchlib/tensor/dense_tensor_attribute.cpp +++ b/searchlib/src/vespa/searchlib/tensor/dense_tensor_attribute.cpp @@ -20,51 +20,6 @@ DenseTensorAttribute::tensor_is_unchanged(DocId docid, const Value& new_tensor) return _comp.equals(old_tensor, new_tensor.cells()); } -void -DenseTensorAttribute::internal_set_tensor(DocId docid, const Value& tensor) -{ - consider_remove_from_index(docid); - EntryRef ref = _denseTensorStore.store_tensor(tensor); - setTensorRef(docid, ref); -} - -void -DenseTensorAttribute::consider_remove_from_index(DocId docid) -{ - if (_index && _refVector[docid].load_relaxed().valid()) { - _index->remove_document(docid); - } -} - -vespalib::MemoryUsage -DenseTensorAttribute::update_stat() -{ - vespalib::MemoryUsage result = TensorAttribute::update_stat(); - if (_index) { - result.merge(_index->update_stat(getConfig().getCompactionStrategy())); - } - return result; -} - -vespalib::MemoryUsage -DenseTensorAttribute::memory_usage() const -{ - vespalib::MemoryUsage result = TensorAttribute::memory_usage(); - if (_index) { - result.merge(_index->memory_usage()); - } - return result; -} - -void -DenseTensorAttribute::populate_address_space_usage(AddressSpaceUsage& usage) const -{ - TensorAttribute::populate_address_space_usage(usage); - if (_index) { - _index->populate_address_space_usage(usage); - } -} - DenseTensorAttribute::DenseTensorAttribute(vespalib::stringref baseFileName, const Config& cfg, const NearestNeighborIndexFactory& index_factory) : TensorAttribute(baseFileName, cfg, _denseTensorStore), @@ -87,23 +42,6 @@ DenseTensorAttribute::~DenseTensorAttribute() _tensorStore.reclaim_all_memory(); } -uint32_t -DenseTensorAttribute::clearDoc(DocId docId) -{ - consider_remove_from_index(docId); - return TensorAttribute::clearDoc(docId); -} - -void -DenseTensorAttribute::setTensor(DocId docId, const Value& tensor) -{ - checkTensorType(tensor); - internal_set_tensor(docId, tensor); - if (_index) { - _index->add_document(docId); - } -} - std::unique_ptr DenseTensorAttribute::prepare_set_tensor(DocId docid, const Value& tensor) const { @@ -133,16 +71,6 @@ DenseTensorAttribute::complete_set_tensor(DocId docid, const Value& tensor, } } -std::unique_ptr -DenseTensorAttribute::getTensor(DocId docId) const -{ - EntryRef ref; - if (docId < getCommittedDocIdLimit()) { - ref = acquire_entry_ref(docId); - } - return _denseTensorStore.get_tensor(ref); -} - vespalib::eval::TypedCells DenseTensorAttribute::extract_cells_ref(DocId docId) const { @@ -153,36 +81,6 @@ DenseTensorAttribute::extract_cells_ref(DocId docId) const return _denseTensorStore.get_typed_cells(ref); } -void -DenseTensorAttribute::onCommit() -{ - TensorAttribute::onCommit(); - if (_index) { - if (_index->consider_compact(getConfig().getCompactionStrategy())) { - incGeneration(); - updateStat(true); - } - } -} - -void -DenseTensorAttribute::before_inc_generation(generation_t current_gen) -{ - TensorAttribute::before_inc_generation(current_gen); - if (_index) { - _index->assign_generation(current_gen); - } -} - -void -DenseTensorAttribute::reclaim_memory(generation_t oldest_used_gen) -{ - TensorAttribute::reclaim_memory(oldest_used_gen); - if (_index) { - _index->reclaim_memory(oldest_used_gen); - } -} - void DenseTensorAttribute::get_state(const vespalib::slime::Inserter& inserter) const { @@ -194,15 +92,6 @@ DenseTensorAttribute::get_state(const vespalib::slime::Inserter& inserter) const } } -void -DenseTensorAttribute::onShrinkLidSpace() -{ - TensorAttribute::onShrinkLidSpace(); - if (_index) { - _index->shrink_lid_space(getCommittedDocIdLimit()); - } -} - vespalib::eval::TypedCells DenseTensorAttribute::get_vector(uint32_t docid) const { diff --git a/searchlib/src/vespa/searchlib/tensor/dense_tensor_attribute.h b/searchlib/src/vespa/searchlib/tensor/dense_tensor_attribute.h index fb9ece45182..58c68c71f7b 100644 --- a/searchlib/src/vespa/searchlib/tensor/dense_tensor_attribute.h +++ b/searchlib/src/vespa/searchlib/tensor/dense_tensor_attribute.h @@ -23,33 +23,19 @@ private: TypedCellsComparator _comp; bool tensor_is_unchanged(DocId docid, const vespalib::eval::Value& new_tensor) const; - void internal_set_tensor(DocId docid, const vespalib::eval::Value& tensor); - void consider_remove_from_index(DocId docid); - vespalib::MemoryUsage update_stat() override; - vespalib::MemoryUsage memory_usage() const override; - void populate_address_space_usage(AddressSpaceUsage& usage) const override; public: DenseTensorAttribute(vespalib::stringref baseFileName, const Config& cfg, const NearestNeighborIndexFactory& index_factory = DefaultNearestNeighborIndexFactory()); ~DenseTensorAttribute() override; // Implements AttributeVector and ITensorAttribute - uint32_t clearDoc(DocId docId) override; - void setTensor(DocId docId, const vespalib::eval::Value &tensor) override; std::unique_ptr prepare_set_tensor(DocId docid, const vespalib::eval::Value& tensor) const override; void complete_set_tensor(DocId docid, const vespalib::eval::Value& tensor, std::unique_ptr prepare_result) override; - std::unique_ptr getTensor(DocId docId) const override; vespalib::eval::TypedCells extract_cells_ref(DocId docId) const override; bool supports_extract_cells_ref() const override { return true; } - void onCommit() override; - void before_inc_generation(generation_t current_gen) override; - void reclaim_memory(generation_t oldest_used_gen) override; void get_state(const vespalib::slime::Inserter& inserter) const override; - void onShrinkLidSpace() override; // Implements DocVectorAccess vespalib::eval::TypedCells get_vector(uint32_t docid) const override; - - const NearestNeighborIndex* nearest_neighbor_index() const override { return _index.get(); } }; } diff --git a/searchlib/src/vespa/searchlib/tensor/direct_tensor_attribute.cpp b/searchlib/src/vespa/searchlib/tensor/direct_tensor_attribute.cpp index 22db5dc5b47..c30b48f8b40 100644 --- a/searchlib/src/vespa/searchlib/tensor/direct_tensor_attribute.cpp +++ b/searchlib/src/vespa/searchlib/tensor/direct_tensor_attribute.cpp @@ -60,23 +60,6 @@ DirectTensorAttribute::update_tensor(DocId docId, } } -std::unique_ptr -DirectTensorAttribute::getTensor(DocId docId) const -{ - EntryRef ref; - if (docId < getCommittedDocIdLimit()) { - ref = acquire_entry_ref(docId); - } - if (ref.valid()) { - auto ptr = _direct_store.get_tensor_ptr(ref); - if (ptr) { - return FastValueBuilderFactory::get().copy(*ptr); - } - } - std::unique_ptr empty; - return empty; -} - const vespalib::eval::Value & DirectTensorAttribute::get_tensor_ref(DocId docId) const { diff --git a/searchlib/src/vespa/searchlib/tensor/direct_tensor_attribute.h b/searchlib/src/vespa/searchlib/tensor/direct_tensor_attribute.h index 6466c6f7537..1e929ad6933 100644 --- a/searchlib/src/vespa/searchlib/tensor/direct_tensor_attribute.h +++ b/searchlib/src/vespa/searchlib/tensor/direct_tensor_attribute.h @@ -20,7 +20,6 @@ public: void update_tensor(DocId docId, const document::TensorUpdate &update, bool create_empty_if_non_existing) override; - std::unique_ptr getTensor(DocId docId) const override; void set_tensor(DocId docId, std::unique_ptr tensor); const vespalib::eval::Value &get_tensor_ref(DocId docId) const override; bool supports_get_tensor_ref() const override { return true; } diff --git a/searchlib/src/vespa/searchlib/tensor/serialized_fast_value_attribute.cpp b/searchlib/src/vespa/searchlib/tensor/serialized_fast_value_attribute.cpp index bb1c1a3d880..9cb50c43983 100644 --- a/searchlib/src/vespa/searchlib/tensor/serialized_fast_value_attribute.cpp +++ b/searchlib/src/vespa/searchlib/tensor/serialized_fast_value_attribute.cpp @@ -15,8 +15,7 @@ namespace search::tensor { SerializedFastValueAttribute::SerializedFastValueAttribute(stringref name, const Config &cfg) : TensorAttribute(name, cfg, _tensorBufferStore), - _tensor_type(cfg.tensorType()), - _tensorBufferStore(_tensor_type, get_memory_allocator(), 1000u) + _tensorBufferStore(cfg.tensorType(), get_memory_allocator(), 1000u) { } @@ -27,23 +26,4 @@ SerializedFastValueAttribute::~SerializedFastValueAttribute() _tensorStore.reclaim_all_memory(); } -void -SerializedFastValueAttribute::setTensor(DocId docId, const vespalib::eval::Value &tensor) -{ - checkTensorType(tensor); - EntryRef ref = _tensorBufferStore.store_tensor(tensor); - assert(ref.valid()); - setTensorRef(docId, ref); -} - -std::unique_ptr -SerializedFastValueAttribute::getTensor(DocId docId) const -{ - EntryRef ref; - if (docId < getCommittedDocIdLimit()) { - ref = acquire_entry_ref(docId); - } - return _tensorBufferStore.get_tensor(ref); -} - } diff --git a/searchlib/src/vespa/searchlib/tensor/serialized_fast_value_attribute.h b/searchlib/src/vespa/searchlib/tensor/serialized_fast_value_attribute.h index 2124ddeb70a..ab2f2dc43bf 100644 --- a/searchlib/src/vespa/searchlib/tensor/serialized_fast_value_attribute.h +++ b/searchlib/src/vespa/searchlib/tensor/serialized_fast_value_attribute.h @@ -17,13 +17,10 @@ namespace search::tensor { * */ class SerializedFastValueAttribute : public TensorAttribute { - vespalib::eval::ValueType _tensor_type; TensorBufferStore _tensorBufferStore; // data store for serialized tensors public: SerializedFastValueAttribute(vespalib::stringref baseFileName, const Config &cfg); ~SerializedFastValueAttribute() override; - void setTensor(DocId docId, const vespalib::eval::Value &tensor) override; - std::unique_ptr getTensor(DocId docId) const override; }; } diff --git a/searchlib/src/vespa/searchlib/tensor/tensor_attribute.cpp b/searchlib/src/vespa/searchlib/tensor/tensor_attribute.cpp index ddaf0780cab..43fc602ff4c 100644 --- a/searchlib/src/vespa/searchlib/tensor/tensor_attribute.cpp +++ b/searchlib/src/vespa/searchlib/tensor/tensor_attribute.cpp @@ -73,6 +73,7 @@ TensorAttribute::asTensorAttribute() const uint32_t TensorAttribute::clearDoc(DocId docId) { + consider_remove_from_index(docId); EntryRef oldRef(_refVector[docId].load_relaxed()); updateUncommittedDocIdLimit(docId); _refVector[docId] = AtomicEntryRef(); @@ -97,6 +98,12 @@ TensorAttribute::onCommit() incGeneration(); updateStat(true); } + if (_index) { + if (_index->consider_compact(getConfig().getCompactionStrategy())) { + incGeneration(); + updateStat(true); + } + } } void @@ -116,6 +123,9 @@ TensorAttribute::reclaim_memory(generation_t oldest_used_gen) { _tensorStore.reclaim_memory(oldest_used_gen); getGenerationHolder().reclaim(oldest_used_gen); + if (_index) { + _index->reclaim_memory(oldest_used_gen); + } } void @@ -123,6 +133,9 @@ TensorAttribute::before_inc_generation(generation_t current_gen) { getGenerationHolder().assign_generation(current_gen); _tensorStore.assign_generation(current_gen); + if (_index) { + _index->assign_generation(current_gen); + } } bool @@ -166,12 +179,32 @@ TensorAttribute::setTensorRef(DocId docId, EntryRef ref) } } +void +TensorAttribute::internal_set_tensor(DocId docid, const Value& tensor) +{ + consider_remove_from_index(docid); + EntryRef ref = _tensorStore.store_tensor(tensor); + assert(ref.valid()); + setTensorRef(docid, ref); +} + +void +TensorAttribute::consider_remove_from_index(DocId docid) +{ + if (_index && _refVector[docid].load_relaxed().valid()) { + _index->remove_document(docid); + } +} + vespalib::MemoryUsage TensorAttribute::update_stat() { vespalib::MemoryUsage result = _refVector.getMemoryUsage(); result.merge(_tensorStore.update_stat(getConfig().getCompactionStrategy())); result.mergeGenerationHeldBytes(getGenerationHolder().get_held_bytes()); + if (_index) { + result.merge(_index->update_stat(getConfig().getCompactionStrategy())); + } return result; } @@ -181,6 +214,9 @@ TensorAttribute::memory_usage() const vespalib::MemoryUsage result = _refVector.getMemoryUsage(); result.merge(_tensorStore.getMemoryUsage()); result.mergeGenerationHeldBytes(getGenerationHolder().get_held_bytes()); + if (_index) { + result.merge(_index->memory_usage()); + } return result; } @@ -203,6 +239,9 @@ TensorAttribute::populate_address_space_usage(AddressSpaceUsage& usage) const usage.set(AddressSpaceComponents::shared_string_repo, vespalib::AddressSpace(stats.max_part_usage, 0, stats.part_limit())); } + if (_index) { + _index->populate_address_space_usage(usage); + } } vespalib::eval::Value::UP @@ -229,6 +268,22 @@ TensorAttribute::getTensorType() const return getConfig().tensorType(); } +const NearestNeighborIndex* +TensorAttribute::nearest_neighbor_index() const +{ + return _index.get(); +} + +std::unique_ptr +TensorAttribute::getTensor(DocId docId) const +{ + EntryRef ref; + if (docId < getCommittedDocIdLimit()) { + ref = acquire_entry_ref(docId); + } + return _tensorStore.get_tensor(ref); +} + void TensorAttribute::get_state(const vespalib::slime::Inserter& inserter) const { @@ -259,6 +314,9 @@ TensorAttribute::onShrinkLidSpace() assert(_refVector.size() >= committedDocIdLimit); _refVector.shrink(committedDocIdLimit); setNumDocs(committedDocIdLimit); + if (_index) { + _index->shrink_lid_space(committedDocIdLimit); + } } uint32_t @@ -302,6 +360,16 @@ TensorAttribute::onInitSave(vespalib::stringref fileName) std::move(index_saver)); } +void +TensorAttribute::setTensor(DocId docId, const Value& tensor) +{ + checkTensorType(tensor); + internal_set_tensor(docId, tensor); + if (_index) { + _index->add_document(docId); + } +} + void TensorAttribute::update_tensor(DocId docId, const document::TensorUpdate &update, diff --git a/searchlib/src/vespa/searchlib/tensor/tensor_attribute.h b/searchlib/src/vespa/searchlib/tensor/tensor_attribute.h index 76bce7076ac..f87c97acaad 100644 --- a/searchlib/src/vespa/searchlib/tensor/tensor_attribute.h +++ b/searchlib/src/vespa/searchlib/tensor/tensor_attribute.h @@ -32,6 +32,8 @@ protected: void checkTensorType(const vespalib::eval::Value &tensor) const; void setTensorRef(DocId docId, EntryRef ref); + void internal_set_tensor(DocId docid, const vespalib::eval::Value& tensor); + void consider_remove_from_index(DocId docid); virtual vespalib::MemoryUsage update_stat(); virtual vespalib::MemoryUsage memory_usage() const; void populate_state(vespalib::slime::Cursor& object) const; @@ -52,18 +54,20 @@ public: void reclaim_memory(generation_t oldest_used_gen) override; void before_inc_generation(generation_t current_gen) override; bool addDoc(DocId &docId) override; + std::unique_ptr getTensor(DocId docId) const override; std::unique_ptr getEmptyTensor() const override; vespalib::eval::TypedCells extract_cells_ref(uint32_t docid) const override; const vespalib::eval::Value& get_tensor_ref(uint32_t docid) const override; bool supports_extract_cells_ref() const override { return false; } bool supports_get_tensor_ref() const override { return false; } const vespalib::eval::ValueType & getTensorType() const override; + const NearestNeighborIndex* nearest_neighbor_index() const override; void get_state(const vespalib::slime::Inserter& inserter) const override; void clearDocs(DocId lidLow, DocId lidLimit, bool in_shrink_lid_space) override; void onShrinkLidSpace() override; uint32_t getVersion() const override; RefCopyVector getRefCopy() const; - virtual void setTensor(DocId docId, const vespalib::eval::Value &tensor) = 0; + virtual void setTensor(DocId docId, const vespalib::eval::Value &tensor); virtual void update_tensor(DocId docId, const document::TensorUpdate &update, bool create_empty_if_non_existing); @@ -85,7 +89,6 @@ public: * It uses the result from the prepare step to do the modifying changes. */ virtual void complete_set_tensor(DocId docid, const vespalib::eval::Value& tensor, std::unique_ptr prepare_result); - }; } -- cgit v1.2.3