aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/tensor/tensor_store.h
blob: e2426d2e89959962888ef485bf43b444117c8683 (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
// Copyright Yahoo. 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 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;
    typedef vespalib::GenerationHandler::generation_t 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;

    // Inherit doc from DataStoreBase
    void trimHoldLists(generation_t usedGen) {
        _store.trimHoldLists(usedGen);
    }

    // Inherit doc from DataStoreBase
    void transferHoldLists(generation_t generation) {
        _store.transferHoldLists(generation);
    }

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

    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();
    }
};

}