diff options
author | George Hazan <ghazan@miranda.im> | 2020-05-13 16:09:27 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2020-05-13 16:09:27 +0300 |
commit | 2b30685427500c9eedac4c0c4862b32af144a90c (patch) | |
tree | a34df3dd8446a4ec9cb466ddd0632b2fcbcde448 /plugins/Dbx_mdbx/src | |
parent | e27e90cf6167d5d23a4458ae99479df6135a16b2 (diff) |
kinda first working version of database event cursors
Diffstat (limited to 'plugins/Dbx_mdbx/src')
-rw-r--r-- | plugins/Dbx_mdbx/src/dbevents.cpp | 105 | ||||
-rw-r--r-- | plugins/Dbx_mdbx/src/dbintf.h | 21 |
2 files changed, 59 insertions, 67 deletions
diff --git a/plugins/Dbx_mdbx/src/dbevents.cpp b/plugins/Dbx_mdbx/src/dbevents.cpp index e435e4b975..f065989221 100644 --- a/plugins/Dbx_mdbx/src/dbevents.cpp +++ b/plugins/Dbx_mdbx/src/dbevents.cpp @@ -623,41 +623,66 @@ MEVENT CDbxMDBX::FindPrevEvent(MCONTACT contactID, MEVENT hDbEvent) /////////////////////////////////////////////////////////////////////////////////////////
// Event cursors
-CMdbxEventCursor::CMdbxEventCursor(CDbxMDBX *pDb, MCONTACT hContact, DBEVENTINFO &dbei, bool bForward) :
- EventCursor(hContact, dbei),
- m_bForward(bForward)
+class CMdbxEventCursor : public DB::EventCursor
{
- mdbx_txn_begin(pDb->m_env, nullptr, MDBX_RDONLY, &m_txn_ro);
- mdbx_cursor_open(m_txn_ro, pDb->m_dbEventsSort, &m_cursor);
-
- m_key.hContact = hContact;
- if (bForward) {
- m_key.hEvent = 0;
- m_key.ts = 0;
- }
- else {
- m_key.hEvent = 0xFFFFFFFF;
- m_key.ts = 0xFFFFFFFFFFFFFFFF;
+ friend class CDbxMDBX;
+ CDbxMDBX *m_pOwner;
+
+ bool m_bForward, m_bFirst = true;
+ DBCachedContact *m_cc;
+ DBEventSortingKey m_key;
+
+public:
+ CMdbxEventCursor(class CDbxMDBX *pDb, DBCachedContact *cc, bool bForward) :
+ EventCursor(cc->contactID),
+ m_pOwner(pDb),
+ m_bForward(bForward),
+ m_cc(cc)
+ {
+ m_key.hContact = hContact;
+ if (bForward) {
+ m_key.hEvent = 0;
+ m_key.ts = 0;
+ }
+ else {
+ m_key.hEvent = 0xFFFFFFFF;
+ m_key.ts = 0xFFFFFFFFFFFFFFFF;
+ }
}
-}
-CMdbxEventCursor::~CMdbxEventCursor()
-{
- if (m_cursor)
- mdbx_cursor_close(m_cursor);
-}
+ MEVENT FetchNext() override
+ {
+ MDBX_cursor_op op;
+ if (m_bFirst) {
+ op = MDBX_SET_RANGE;
+ m_bFirst = false;
+ }
+ else {
+ op = (m_bForward) ? MDBX_NEXT : MDBX_PREV;
+ if (m_key.hEvent != m_cc->t_evLast)
+ return 0;
+ }
-MEVENT CMdbxEventCursor::FetchNext()
-{
- MDBX_val key = { &m_key, sizeof(m_key) }, data;
- if (mdbx_cursor_get(*this, &key, &data, m_bForward ? MDBX_NEXT : MDBX_PREV) != MDBX_SUCCESS)
- return 0;
+ txn_ptr_ro txn(m_pOwner->m_txn_ro);
- const DBEventSortingKey *pKey = (const DBEventSortingKey *)key.iov_base;
- return (pKey->hContact == hContact) ? pKey->hEvent : 0;
-}
+ MDBX_val key = { &m_key, sizeof(m_key) }, data;
+ if (mdbx_cursor_get(m_pOwner->m_curEventsSort, &key, &data, op) != MDBX_SUCCESS)
+ return 0;
+
+ const DBEventSortingKey *pKey = (const DBEventSortingKey *)key.iov_base;
+ if (pKey->hContact != hContact) {
+ m_cc->t_tsLast = 0;
+ return m_cc->t_evLast = 0;
+ }
+
+ m_key = *pKey;
+ m_cc->t_evLast = pKey->hEvent;
+ m_cc->t_tsLast = pKey->ts;
+ return pKey->hEvent;
+ }
+};
-DB::EventCursor* CDbxMDBX::EventCursor(MCONTACT hContact, DBEVENTINFO &dbei)
+DB::EventCursor* CDbxMDBX::EventCursor(MCONTACT hContact, MEVENT)
{
DBCachedContact *cc;
if (hContact != 0) {
@@ -667,18 +692,10 @@ DB::EventCursor* CDbxMDBX::EventCursor(MCONTACT hContact, DBEVENTINFO &dbei) }
else cc = &m_ccDummy;
- auto *pCursor = new CMdbxEventCursor(this, hContact, dbei, true);
-
- MDBX_val key = { &pCursor->m_key, sizeof(pCursor->m_key) }, data;
- if (mdbx_cursor_get(*pCursor, &key, &data, MDBX_SET_RANGE) != MDBX_SUCCESS) {
- delete pCursor;
- return nullptr;
- }
-
- return pCursor;
+ return new CMdbxEventCursor(this, cc, true);
}
-DB::EventCursor* CDbxMDBX::EventCursorRev(MCONTACT hContact, DBEVENTINFO &dbei)
+DB::EventCursor* CDbxMDBX::EventCursorRev(MCONTACT hContact, MEVENT)
{
DBCachedContact *cc;
if (hContact != 0) {
@@ -688,13 +705,5 @@ DB::EventCursor* CDbxMDBX::EventCursorRev(MCONTACT hContact, DBEVENTINFO &dbei) }
else cc = &m_ccDummy;
- auto *pCursor = new CMdbxEventCursor(this, hContact, dbei, false);
-
- MDBX_val key = { &pCursor->m_key, sizeof(pCursor->m_key) }, data;
- if (mdbx_cursor_get(*pCursor, &key, &data, MDBX_SET_RANGE) != MDBX_SUCCESS) {
- delete pCursor;
- return nullptr;
- }
-
- return pCursor;
+ return new CMdbxEventCursor(this, cc, false);
}
diff --git a/plugins/Dbx_mdbx/src/dbintf.h b/plugins/Dbx_mdbx/src/dbintf.h index 05687bbfd2..06602c8632 100644 --- a/plugins/Dbx_mdbx/src/dbintf.h +++ b/plugins/Dbx_mdbx/src/dbintf.h @@ -136,23 +136,6 @@ struct EventItem MEVENT eventId;
};
-class CMdbxEventCursor : public DB::EventCursor
-{
- CMDBX_txn_ro m_txn_ro;
- MDBX_cursor* m_cursor = nullptr;
- bool m_bForward;
-
-public:
- CMdbxEventCursor(class CDbxMDBX *pOdb, MCONTACT hContact, DBEVENTINFO &dbei, bool bForward);
- ~CMdbxEventCursor() override;
-
- MEVENT FetchNext() override;
-
- DBEventSortingKey m_key;
-
- __forceinline operator MDBX_cursor*() const { return m_cursor; }
-};
-
class CDbxMDBX : public MDatabaseCommon, public MZeroedObject
{
friend class CMdbxEventCursor;
@@ -315,8 +298,8 @@ public: STDMETHODIMP_(MEVENT) GetEventById(LPCSTR szModule, LPCSTR szId) override;
STDMETHODIMP_(BOOL) SetEventId(LPCSTR szModule, MEVENT, LPCSTR szId) override;
- STDMETHODIMP_(DB::EventCursor *) EventCursor(MCONTACT hContact, DBEVENTINFO &dbei) override;
- STDMETHODIMP_(DB::EventCursor *) EventCursorRev(MCONTACT hContact, DBEVENTINFO &dbei) override;
+ STDMETHODIMP_(DB::EventCursor *) EventCursor(MCONTACT hContact, MEVENT hDbEvent) override;
+ STDMETHODIMP_(DB::EventCursor *) EventCursorRev(MCONTACT hContact, MEVENT hDbEvent) override;
public:
MICryptoEngine *m_crypto;
|