aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/string_sort_blob_writer.cpp
blob: 10386020afece505e18bcea2e451c3abed4b2341 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "string_sort_blob_writer.h"
#include <vespa/searchcommon/common/iblobconverter.h>
#include <vespa/vespalib/util/arrayref.h>
#include <algorithm>
#include <cstring>

namespace search::attribute {

StringSortBlobWriter::StringSortBlobWriter(void* serialize_to, size_t available, const BlobConverter* bc, bool asc) noexcept
    : _best_size(),
      _serialize_to(static_cast<unsigned char*>(serialize_to)),
      _available(available),
      _bc(bc),
      _asc(asc)
{
}

StringSortBlobWriter::~StringSortBlobWriter() noexcept = default;

bool
StringSortBlobWriter::candidate(const char* val)
{
    size_t size = std::strlen(val) + 1;
    vespalib::ConstBufferRef buf(val, size);
    if (_bc != nullptr) {
        buf = _bc->convert(buf);
    }
    if (_best_size.has_value()) {
        auto common_size = std::min(_best_size.value(), buf.size());
        auto cmpres = std::memcmp(_serialize_to + 1, buf.data(), common_size);
        if (_asc) {
            if (cmpres < 0 || (cmpres == 0 && _best_size.value() < buf.size())) {
                return true;
            }
        } else {
            if (cmpres > 0 || (cmpres == 0 && _best_size.value() > buf.size())) {
                return true;
            }
        }
    }
    if (_available < buf.size() + 1) {
        return false;
    }
    _serialize_to[0] = has_value;
    memcpy(_serialize_to + 1, buf.data(), buf.size());
    _best_size = buf.size();
    return true;
}

long
StringSortBlobWriter::write()
{
    if (_best_size.has_value()) {
        if (!_asc) {
            vespalib::ArrayRef<unsigned char> buf(_serialize_to + 1, _best_size.value());
            for (auto& c : buf) {
                c = 0xff - c;
            }
        }
        return 1 + _best_size.value();
    } else {
        if (_available < 1) {
            return -1;
        }
        _serialize_to[0] = missing_value;
        return 1;
    }
}

}