TreeFrogFramework 2.10
Loading...
Searching...
No Matches
tsharedmemorykvs.h
Go to the documentation of this file.
1#pragma once
2#include <QByteArray>
3#include <QStringList>
4#include <TGlobal>
5#include <TKvsDatabase>
6#include <TfNamespace>
7
8struct hash_header_t;
9class TSharedMemoryKvsDriver;
10
11
13public:
14 class Bucket {
15 public:
16 QByteArray key;
17 QByteArray value;
18 qint64 expires {0}; // msecs since epoch
19
20 bool isExpired() const
21 {
22 return expires <= Tf::getMSecsSinceEpoch();
23 }
24
25 void clear()
26 {
27 key.resize(0);
28 value.resize(0);
29 expires = 0;
30 }
31
32 friend QDataStream &operator<<(QDataStream &ds, const Bucket &bucket)
33 {
34 ds << bucket.key << bucket.value << bucket.expires;
35 return ds;
36 }
37
38 friend QDataStream &operator>>(QDataStream &ds, Bucket &bucket)
39 {
40 ds >> bucket.key >> bucket.value >> bucket.expires;
41 return ds;
42 }
43 };
44
46 public:
48 const QByteArray &key() const;
49 const QByteArray &value() const;
50 bool isExpired() const;
51 const QByteArray &operator*() const;
52 WriteLockingIterator &operator++();
53 bool operator==(const WriteLockingIterator &other) const { return _hash == other._hash && _it == other._it; }
54 bool operator!=(const WriteLockingIterator &other) const { return _hash != other._hash || _it != other._it; }
55 void remove();
56 private:
58 void search();
59
60 TSharedMemoryKvs *_hash {nullptr};
61 uint _it {0};
62 bool _locked {false};
63 Bucket _tmpbk;
64 friend class TSharedMemoryKvs;
65 };
66
69
70 QByteArray get(const QByteArray &key);
71 bool set(const QByteArray &key, const QByteArray &value, int seconds);
72 bool remove(const QByteArray &key);
73 uint count() const;
74 uint tableSize() const;
75 void clear();
76 void gc();
77 float loadFactor() const;
78 void rehash();
79 bool lockForRead();
80 bool lockForWrite();
81 bool unlock();
82
85
86 static bool initialize(const QString &name, const QString &options); // only first time in system
87 void cleanup();
88
89protected:
90 uint find(const QByteArray &key, Bucket &bucket) const;
91 bool find(uint index, Bucket &bucket) const;
92 int searchIndex(int first);
93 uint index(const QByteArray &key) const;
94 uint next(uint index) const;
95 void remove(uint index);
96
97
98private:
100 TSharedMemoryKvsDriver *driver();
101 const TSharedMemoryKvsDriver *driver() const;
102
103 TKvsDatabase _database;
104 hash_header_t *_h {nullptr};
105
106 friend class TCacheSharedMemoryStore;
109};
The Bucket class represents a data bucket for the in-memory KVS.
Definition tsharedmemorykvs.h:14
QByteArray value
Definition tsharedmemorykvs.h:17
qint64 expires
Definition tsharedmemorykvs.h:18
bool isExpired() const
Definition tsharedmemorykvs.h:20
friend QDataStream & operator<<(QDataStream &ds, const Bucket &bucket)
Definition tsharedmemorykvs.h:32
friend QDataStream & operator>>(QDataStream &ds, Bucket &bucket)
Definition tsharedmemorykvs.h:38
void clear()
Definition tsharedmemorykvs.h:25
QByteArray key
Definition tsharedmemorykvs.h:16
The WriteLockingIterator class provides an STL-style iterator with write-locking for TSharedMemoryKvs...
Definition tsharedmemorykvs.h:45
bool operator==(const WriteLockingIterator &other) const
Definition tsharedmemorykvs.h:53
bool operator!=(const WriteLockingIterator &other) const
Definition tsharedmemorykvs.h:54
The TSharedMemoryKvs class provides a means of operating a in-memory KVS built in the server process.
Definition tsharedmemorykvs.h:12
int searchIndex(int first)
KvsEngine
Definition tfnamespace.h:239
T_CORE_EXPORT int64_t getMSecsSinceEpoch()
Definition tglobal.cpp:295
#define T_CORE_EXPORT
Definition tdeclexport.h:28
#define T_DISABLE_COPY(Class)
Definition tdeclexport.h:37
#define T_DISABLE_MOVE(Class)
Definition tdeclexport.h:41