blob: 9b74f76aa6ed76b7b9fbb40e5bb7c26c3c1b2d67 (
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
|
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "ranking_expressions.h"
#include <vespa/vespalib/io/mapped_file_input.h>
#include <vespa/vespalib/data/lz4_input_decoder.h>
#include <vespa/vespalib/util/size_literals.h>
#include <vespa/log/log.h>
LOG_SETUP(".proton.matching.ranking_expressions");
namespace proton::matching {
namespace {
vespalib::string extract_data(vespalib::Input &input) {
vespalib::string result;
for (auto chunk = input.obtain(); chunk.size > 0; chunk = input.obtain()) {
result.append(vespalib::stringref(chunk.data, chunk.size));
input.evict(chunk.size);
}
return result;
}
} // unnamed
RankingExpressions::RankingExpressions() = default;
RankingExpressions::RankingExpressions(RankingExpressions &&rhs) = default;
RankingExpressions::RankingExpressions(const RankingExpressions &rhs) = default;
RankingExpressions::~RankingExpressions() = default;
RankingExpressions &
RankingExpressions::add(const vespalib::string &name, const vespalib::string &path)
{
_expressions.insert_or_assign(name, path);
return *this;
}
vespalib::string
RankingExpressions::loadExpression(const vespalib::string &name) const
{
auto pos = _expressions.find(name);
if (pos == _expressions.end()) {
// not warning about missing expression here since what we
// think is a name might be an expression itself.
return {};
}
auto path = pos->second;
vespalib::MappedFileInput file(path);
if (!file.valid()) {
LOG(warning, "could not read file: %s", path.c_str());
return {};
}
if (ends_with(path, ".lz4")) {
size_t buffer_size = 64_Ki;
vespalib::Lz4InputDecoder lz4_decoder(file, buffer_size);
auto result = extract_data(lz4_decoder);
if (lz4_decoder.failed()) {
LOG(warning, "file contains lz4 errors (%s): %s",
lz4_decoder.reason().c_str(), path.c_str());
return {};
}
return result;
}
return extract_data(file);
}
}
|