TreeFrogFramework  2.8
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 
8 struct hash_header_t;
9 class TSharedMemoryKvsDriver;
10 
11 
13 public:
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:
57  WriteLockingIterator(TSharedMemoryKvs *hash, uint it);
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 
89 protected:
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 
98 private:
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
friend QDataStream & operator>>(QDataStream &ds, Bucket &bucket)
Definition: tsharedmemorykvs.h:38
bool isExpired() const
Definition: tsharedmemorykvs.h:20
friend QDataStream & operator<<(QDataStream &ds, const Bucket &bucket)
Definition: tsharedmemorykvs.h:32
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
uint index(const QByteArray &key) const
Definition: tsharedmemorykvs.cpp:399
void gc()
Executes garbage collection.
Definition: tsharedmemorykvs.cpp:331
float loadFactor() const
Definition: tsharedmemorykvs.cpp:302
void cleanup()
Cleanup the KVS.
Definition: tsharedmemorykvs.cpp:105
uint next(uint index) const
Definition: tsharedmemorykvs.cpp:405
static bool initialize(const QString &name, const QString &options)
Initializes in-memory KVS with the given name and options.
Definition: tsharedmemorykvs.cpp:84
WriteLockingIterator end()
Returns an STL-style iterator pointing just after the last item in the KVS.
Definition: tsharedmemorykvs.cpp:490
int searchIndex(int first)
uint find(const QByteArray &key, Bucket &bucket) const
Definition: tsharedmemorykvs.cpp:185
~TSharedMemoryKvs()
Destructor.
Definition: tsharedmemorykvs.cpp:78
QByteArray get(const QByteArray &key)
Returns the value associated with the key; otherwise returns an empty byte array.
Definition: tsharedmemorykvs.cpp:250
bool remove(const QByteArray &key)
Removes the specified key.
Definition: tsharedmemorykvs.cpp:263
WriteLockingIterator begin()
Locks the attached shared-memory segment for writing by this process and returns an STL-style iterato...
Definition: tsharedmemorykvs.cpp:479
TSharedMemoryKvs()
Constructs a TSharedMemoryKvs object.
Definition: tsharedmemorykvs.cpp:62
bool set(const QByteArray &key, const QByteArray &value, int seconds)
Sets the key to hold the value.
Definition: tsharedmemorykvs.cpp:114
bool lockForWrite()
Locks the shared memory segment for writing by this process.
Definition: tsharedmemorykvs.cpp:421
void rehash()
Internal use.
Definition: tsharedmemorykvs.cpp:347
bool lockForRead()
Locks the shared memory segment for reading by this process.
Definition: tsharedmemorykvs.cpp:413
void clear()
Removes all items from the KVS.
Definition: tsharedmemorykvs.cpp:310
uint tableSize() const
Definition: tsharedmemorykvs.cpp:296
bool unlock()
Releases the lock on the shared memory segment.
Definition: tsharedmemorykvs.cpp:429
uint count() const
Returns the number of items set.
Definition: tsharedmemorykvs.cpp:290
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