Repository navigation
Expand file tree
/
Copy pathsendbuffer.h
More file actions
95 lines (83 loc) · 3.06 KB
/
Copy pathsendbuffer.h
File metadata and controls
95 lines (83 loc) · 3.06 KB
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
/*
* Copyright 2017-Present Couchbase, Inc.
*
* Use of this software is governed by the Business Source License included
* in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
* in that file, in accordance with the Business Source License, use of this
* software will be governed by the Apache License, Version 2.0, included in
* the file licenses/APL2.txt.
*/
#pragma once
#include <memcached/engine.h>
#include <string>
class Bucket;
/**
* Abstract class for a generic send buffer to be passed to libevent
* which holds some data allocated elsewhere which needs to be released
* when libevent is done sending the data
*/
class SendBuffer {
public:
/// Using a SendBuffer have an overhead as we need to allocate memory
/// to hold it _AND_ it may result into multiple system calls being used
/// as the underlying system may not fit all of the data into a single
/// system call (for TLS it gets even worse as it'll result in multiple
/// TLS frames which add extra CPU cycles and network overhead)
///.
/// A sendbuffer should not be used unless the payload is >4k
constexpr static std::size_t MinimumDataSize = 4096;
explicit SendBuffer(std::string_view view) : payload(view) {
}
virtual ~SendBuffer() = default;
virtual std::string_view getPayload() {
return payload;
}
protected:
std::string_view payload;
};
/**
* Specialized send buffer which holds an item which needs to be
* released once libevent is done sending any data held by the
* object.
*/
class ItemSendBuffer : public SendBuffer {
public:
/**
* @param itm The item send (the ownership of the item is
* transferred to this object)
* @param view The memory area within the item to transfer
* @param bucket The bucket the item belongs to
*/
ItemSendBuffer(cb::unique_item_ptr itm,
std::string_view view,
Bucket& bucket);
~ItemSendBuffer() override;
protected:
cb::unique_item_ptr item;
Bucket& bucket;
};
/**
* Specialized send buffer which holds a std::string that owns the data
* to be sent. The string is released once libevent is done sending it.
*/
class StringSendBuffer : public SendBuffer {
public:
/**
* @param buf The string holding the data (ownership is transferred to
* this object). The entire string is transferred; the payload
* is anchored to this object's copy so it remains valid
* regardless of how the source string was stored (e.g. SSO).
*/
explicit StringSendBuffer(std::string buf);
/**
* @param buf The string holding the data (ownership is transferred to
* this object)
* @param view The memory area within buf to transfer (the payload is
* re-anchored to this object's copy of buf, so the view
* remains valid regardless of how the source string was
* stored, e.g. SSO)
*/
StringSendBuffer(std::string buf, std::string_view view);
protected:
std::string buf;
};