aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/tensor/tensor_store.h
blob: 627b4e53c5e89251540e1294f6268c85160b9f4a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/vespalib/datastore/compaction_spec.h>
#include <vespa/vespalib/datastore/datastorebase.h>
#include <vespa/vespalib/datastore/entryref.h>
#include <vespa/vespalib/datastore/i_compactable.h>
#include <vespa/vespalib/util/generationhandler.h>

namespace vespalib { class nbostream; }
namespace vespalib::datastore { struct ICompactionContext; }
namespace vespalib::eval { struct Value; }

namespace search::tensor {

class DenseTensorStore;

/**
 * Class for storing serialized tensors in memory, used by TensorAttribute.
 *
 * Serialization format is subject to change.  Changes to serialization format
 * might also require corresponding changes to implemented optimized tensor
 * operations that use the serialized tensor as argument.
 */
class TensorStore : public vespalib::datastore::ICompactable
{
public:
    using EntryRef = vespalib::datastore::EntryRef;
    using generation_t = vespalib::GenerationHandler::generation_t;

protected:
    vespalib::datastore::DataStoreBase& _store;
    vespalib::datastore::CompactionSpec _compaction_spec;

public:
    TensorStore(vespalib::datastore::DataStoreBase &store);

    virtual ~TensorStore();

    virtual void holdTensor(EntryRef ref) = 0;

    virtual vespalib::MemoryUsage update_stat(const vespalib::datastore::CompactionStrategy& compaction_strategy) = 0;

    virtual std::unique_ptr<vespalib::datastore::ICompactionContext> start_compact(const vespalib::datastore::CompactionStrategy& compaction_strategy) = 0;

    virtual EntryRef store_tensor(const vespalib::eval::Value& tensor) = 0;
    virtual EntryRef store_encoded_tensor(vespalib::nbostream& encoded) = 0;
    virtual std::unique_ptr<vespalib::eval::Value> get_tensor(EntryRef ref) const = 0;
    virtual bool encode_stored_tensor(EntryRef ref, vespalib::nbostream& target) const = 0;
    virtual const DenseTensorStore* as_dense() const;
    virtual DenseTensorStore* as_dense();

    // Inherit doc from DataStoreBase
    void reclaim_memory(generation_t oldest_used_gen) {
        _store.reclaim_memory(oldest_used_gen);
    }

    // Inherit doc from DataStoreBase
    void assign_generation(generation_t current_gen) {
        _store.assign_generation(current_gen);
    }

    void reclaim_all_memory() {
        _store.reclaim_all_memory();
    }

    vespalib::MemoryUsage getMemoryUsage() const {
        return _store.getMemoryUsage();
    }

    vespalib::AddressSpace get_address_space_usage() const {
        return _store.getAddressSpaceUsage();
    }

    bool consider_compact() const noexcept {
        return _compaction_spec.compact() && !_store.has_held_buffers();
    }
};

}