summaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/common/packets.cpp
blob: af5dc5fc110a7ae5d6d86760b22a37727989ce36 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "packets.h"
#include "mapnames.h"
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/fnet/databuffer.h>

#include <vespa/log/log.h>
LOG_SETUP(".searchlib.common.fs4packets");

using vespalib::make_string;
using vespalib::stringref;

namespace search::fs4transport {

/**
 * Persistent packet streamer.
 **/
FS4PersistentPacketStreamer FS4PersistentPacketStreamer::Instance;

//============================================================

FS4PersistentPacketStreamer::
FS4PersistentPacketStreamer()
    : _compressionLimit(0),
      _compressionLevel(9),
      _compressionType(CompressionConfig::LZ4)
{ }

//============================================================

FS4Properties::FS4Properties()
    : _entries(),
      _name(),
      _backing()
{ }

FS4Properties::FS4Properties(FS4Properties && rhs) noexcept
    : _entries(std::move(rhs._entries)),
      _name(std::move(rhs._name)),
      _backing(std::move(rhs._backing))
{ }

FS4Properties &
FS4Properties::operator=(FS4Properties && rhs) noexcept
{
    _entries = std::move(rhs._entries);
    _name = std::move(rhs._name);
    _backing = std::move(rhs._backing);
    return *this;
}

FS4Properties::~FS4Properties() = default;

void FS4Properties::set(StringRef & e, vespalib::stringref s)
{
    e.first = _backing.size();
    e.second = s.size();
    _backing.append(s.data(), s.size());
}

void
FS4Properties::setKey(uint32_t entry, const char *key, uint32_t keySize)
{
    set(_entries[entry].first, vespalib::stringref(key, keySize));
}

void
FS4Properties::setValue(uint32_t entry, const char *value, uint32_t valueSize)
{
    set(_entries[entry].second, vespalib::stringref(value, valueSize));
}

uint32_t
FS4Properties::getLength() const noexcept
{
    uint32_t len = sizeof(uint32_t) * 2 + name().size();
    len += _backing.size();
    len += _entries.size() * sizeof(uint32_t) * 2;
    return len;
}

vespalib::stringref
FS4Properties::key(uint32_t entry) const noexcept {
    auto pair = _entries[entry].first;
    return {c_str(pair.first), pair.second};
}
vespalib::stringref
FS4Properties::value(uint32_t entry) const noexcept {
    auto pair = _entries[entry].second;
    return {c_str(pair.first), pair.second};
}

vespalib::string
FS4Properties::toString(uint32_t indent) const
{
    vespalib::string s;
    s += make_string("%*sProperties {\n", indent, "");
    s += make_string("%*s  name: ", indent, "");
    s += _name;
    s += "\n";
    for (uint32_t i = 0; i < size(); ++i) {
        s += make_string("%*s  Entry[%d] {\n", indent, "", i);
        s += make_string("%*s    key  : %s\n", indent, "", string(key(i)).c_str());
        s += make_string("%*s    value: %s\n", indent, "", string(value(i)).c_str());
        s += make_string("%*s  }\n", indent, "");
    }
    s += make_string("%*s}\n", indent, "");
    return s;
}

bool
FS4Properties::decode(FNET_DataBuffer &src, uint32_t &len)
{
    if (len < sizeof(uint32_t)) return false;
    uint32_t strLen = src.ReadInt32();
    len -= sizeof(uint32_t);
    if (len < strLen) return false;
    setName(src.GetData(), strLen);
    src.DataToDead(strLen);
    len -= strLen;
    if (len < sizeof(uint32_t)) return false;
    uint32_t cnt = src.ReadInt32();
    len -= sizeof(uint32_t);
    allocEntries(cnt);
    for (uint32_t i = 0; i < cnt; ++i) {
        if (len < sizeof(uint32_t)) return false;
        strLen = src.ReadInt32();
        len -= sizeof(uint32_t);
        if (len < strLen) return false;
        setKey(i, src.GetData(), strLen);
        src.DataToDead(strLen);
        len -= strLen;
        if (len < sizeof(uint32_t)) return false;
        strLen = src.ReadInt32();
        len -= sizeof(uint32_t);
        if (len < strLen) return false;
        setValue(i, src.GetData(), strLen);
        src.DataToDead(strLen);
        len -= strLen;
    }
    return true;
}

void
FS4Properties::allocEntries(uint32_t cnt)
{
    _entries.resize(cnt);
    _backing.reserve(cnt*2*40); // Assume strings are average 40 bytes
}

}