summaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/tensor/direct_tensor_attribute.cpp
blob: d9fe025b4e5c3327c132bddb683a992fc1b1a0c7 (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
82
83
84
85
86
87
88
89
90
91
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "direct_tensor_attribute.h"
#include <vespa/eval/eval/fast_value.h>
#include <vespa/eval/eval/value.h>

using vespalib::eval::FastValueBuilderFactory;

namespace search::tensor {

DirectTensorAttribute::DirectTensorAttribute(stringref name, const Config &cfg)
    : TensorAttribute(name, cfg, _direct_store)
{
}

DirectTensorAttribute::~DirectTensorAttribute()
{
    getGenerationHolder().clearHoldLists();
    _tensorStore.clearHoldLists();
}

void
DirectTensorAttribute::set_tensor(DocId lid, std::unique_ptr<vespalib::eval::Value> tensor)
{
    checkTensorType(*tensor);
    EntryRef ref = _direct_store.store_tensor(std::move(tensor));
    setTensorRef(lid, ref);
}

void
DirectTensorAttribute::setTensor(DocId lid, const vespalib::eval::Value &tensor)
{
    set_tensor(lid, FastValueBuilderFactory::get().copy(tensor));
}

void
DirectTensorAttribute::update_tensor(DocId docId,
                                     const document::TensorUpdate &update,
                                     bool create_if_non_existing)
{
    EntryRef ref;
    if (docId < getCommittedDocIdLimit()) {
        ref = _refVector[docId].load_relaxed();
    }
    if (ref.valid()) {
        auto ptr = _direct_store.get_tensor_ptr(ref);
        if (ptr) {
            auto new_value = update.apply_to(*ptr, FastValueBuilderFactory::get());
            if (new_value) {
                set_tensor(docId, std::move(new_value));
            }
            return;
        }
    }
    if (create_if_non_existing) {
        auto new_value = update.apply_to(*_emptyTensor, FastValueBuilderFactory::get());
        if (new_value) {
            set_tensor(docId, std::move(new_value));
        }
    }
}

std::unique_ptr<vespalib::eval::Value>
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<vespalib::eval::Value> empty;
    return empty;
}

const vespalib::eval::Value &
DirectTensorAttribute::get_tensor_ref(DocId docId) const
{
    if (docId >= getCommittedDocIdLimit()) { return *_emptyTensor; }

    auto ptr = _direct_store.get_tensor_ptr(acquire_entry_ref(docId));
    if ( ptr == nullptr) { return *_emptyTensor; }

    return *ptr;
}

} // namespace