summaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/common/packets.h
blob: fbdae8b51ec23fedce424f6e2bfa8a3107b7ee46 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/vespalib/stllike/string.h>
#include <vespa/vespalib/util/compressionconfig.h>
#include <vespa/vespalib/util/memory.h>
#include <vector>
#include <atomic>

class FNET_DataBuffer;

namespace search::fs4transport {

using vespalib::string;

class FS4PersistentPacketStreamer {
    using CompressionConfig = vespalib::compression::CompressionConfig;

    std::atomic<unsigned int> _compressionLimit;
    std::atomic<unsigned int> _compressionLevel;
    std::atomic<CompressionConfig::Type> _compressionType;

public:
    static FS4PersistentPacketStreamer Instance;

    FS4PersistentPacketStreamer();

    void SetCompressionLimit(unsigned int limit) { _compressionLimit.store(limit, std::memory_order_relaxed); }
    void SetCompressionLevel(unsigned int level) { _compressionLevel.store(level, std::memory_order_relaxed); }
    void SetCompressionType(CompressionConfig::Type compressionType) { _compressionType.store(compressionType, std::memory_order_relaxed); }
    CompressionConfig::Type getCompressionType() const { return _compressionType.load(std::memory_order_relaxed); }
    uint32_t getCompressionLimit() const { return _compressionLimit.load(std::memory_order_relaxed); }
    uint32_t getCompressionLevel() const { return _compressionLevel.load(std::memory_order_relaxed); }
};

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

class FS4Properties
{
private:
    using StringRef = std::pair<uint32_t, uint32_t>;
    using Entry = std::pair<StringRef, StringRef>;
    using KeyValueVector = std::vector<Entry>;

    KeyValueVector   _entries;
    vespalib::string _name;
    vespalib::string _backing;
    const char * c_str(size_t sz) const { return _backing.c_str() + sz; }
    void set(StringRef & e, vespalib::stringref s);
    void allocEntries(uint32_t cnt);
public:
    FS4Properties(FS4Properties &&) noexcept;
    FS4Properties &operator=(FS4Properties &&) noexcept;
    FS4Properties(const FS4Properties &) = delete;
    FS4Properties &operator=(const FS4Properties &) = delete;

    FS4Properties();
    ~FS4Properties();
    void setName(const char *name, uint32_t nameSize) { _name.assign(name, nameSize); }
    void setName(vespalib::stringref val) {
        setName(val.data(), val.size());
    }
    void setKey(uint32_t entry, const char *key, uint32_t keySize);
    void setKey(uint32_t entry, vespalib::stringref val) {
        setKey(entry, val.data(), val.size());
    }
    void setValue(uint32_t entry, const char *value, uint32_t valueSize);
    void setValue(uint32_t entry, vespalib::stringref val) {
        setValue(entry, val.data(), val.size());
    }
    uint32_t size() const noexcept { return _entries.size(); }
    const vespalib::string & name() const noexcept { return _name; }
    vespalib::stringref key(uint32_t entry) const noexcept;
    vespalib::stringref value(uint32_t entry) const noexcept;

    // sub-packet methods below
    uint32_t getLength() const noexcept;

    void encode(FNET_DataBuffer &dst);
    bool decode(FNET_DataBuffer &src, uint32_t &len);
    vespalib::string toString(uint32_t indent = 0) const;
};

}