summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/btree/qt/qbtreecursor.cpp
blob: 21bb99347ed3df33011daf6e2da14af7271fc5e6 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "btree.h"
#include "qbtree.h"
#include "qbtreetxn.h"
#include "qbtreecursor.h"


QBtreeCursor::QBtreeCursor()
    : mCursor(0)
{
}

QBtreeCursor::QBtreeCursor(QBtreeTxn *txn)
    : mCursor(0)
{
    if (txn)
        mCursor = btree_txn_cursor_open(txn->btree()->handle(), txn->handle());
}

QBtreeCursor::QBtreeCursor(QBtree *btree, bool commitedOnly)
    : mCursor(0)
{
    // Hack: Old AoDb only starts cursors on write transactions
    // TODO: This constructor should not be available.
    Q_ASSERT(btree);
    struct btree_txn *txn = 0;
    if (!commitedOnly)
        txn = btree_get_txn(btree->handle());

    mCursor = btree_txn_cursor_open(btree->handle(), txn);
}

QBtreeCursor::~QBtreeCursor()
{
    if (mCursor)
        btree_cursor_close(mCursor);
}

QBtreeCursor::QBtreeCursor(const QBtreeCursor &other)
    : mCursor(0)
{
    if (other.mCursor) {
        mCursor = btree_txn_cursor_open(btree_cursor_bt(other.mCursor), btree_cursor_txn(other.mCursor));
        if (!other.mKey.isNull())
            seek(other.mKey);
    }
}

QBtreeCursor &QBtreeCursor::operator =(const QBtreeCursor &other)
{
    if (this == &other)
        return *this;
    if (mCursor)
        btree_cursor_close(mCursor);
    if (other.mCursor) {
        mCursor = btree_txn_cursor_open(btree_cursor_bt(other.mCursor), btree_cursor_txn(other.mCursor));
        mKey = other.mKey;
        mValue = other.mValue;
        if (!mKey.isNull())
            seek(other.mKey);
    }
    return *this;
}

bool QBtreeCursor::current(QBtreeData *key, QBtreeData *value) const
{
    if (key)
        *key = mKey;
    if (value)
        *value = mValue;
    return true;
}

bool QBtreeCursor::current(QByteArray *key, QByteArray *value) const
{
    if (key)
        *key = mKey.toByteArray();
    if (value)
        *value = mValue.toByteArray();
    return true;
}

bool QBtreeCursor::first()
{
    return moveHelper(0, 0, BT_FIRST);
}

bool QBtreeCursor::last()
{
    return moveHelper(0, 0, BT_LAST);
}

bool QBtreeCursor::next()
{
    return moveHelper(0, 0, BT_NEXT);
}

bool QBtreeCursor::previous()
{
    return moveHelper(0, 0, BT_PREV);
}

bool QBtreeCursor::seek(const QByteArray &baKey)
{
    return moveHelper(baKey.constData(), baKey.size(), BT_CURSOR_EXACT);
}

bool QBtreeCursor::seek(const QBtreeData &key)
{
    return moveHelper(key.constData(), key.size(), BT_CURSOR_EXACT);
}

bool QBtreeCursor::seekRange(const QByteArray &baKey)
{
    return moveHelper(baKey.constData(), baKey.size(), BT_CURSOR);
}

bool QBtreeCursor::seekRange(const QBtreeData &key)
{
    return moveHelper(key.constData(), key.size(), BT_CURSOR);
}

bool QBtreeCursor::moveHelper(const char *key, int size, int cursorOp)
{
    Q_ASSERT(mCursor);
    Q_ASSERT(key || (cursorOp == BT_PREV || cursorOp == BT_NEXT || cursorOp == BT_LAST || cursorOp == BT_FIRST));
    Q_ASSERT(!key || (cursorOp == BT_CURSOR || cursorOp == BT_CURSOR_EXACT));

    struct btval btkey;

    btkey.data = (void *)key;
    btkey.size = size;
    btkey.free_data = 0;
    btkey.mp = 0;

    struct btval btvalue;
    btvalue.data = 0;
    btvalue.size = 0;
    btvalue.free_data = 0;
    btvalue.mp = 0;

    if (btree_cursor_get(mCursor, &btkey, &btvalue, static_cast<cursor_op>(cursorOp)) != BT_SUCCESS) {
        btval_reset(&btkey);
        btval_reset(&btvalue);
        mKey = mValue = QBtreeData();
        return false;
    }

    mKey = QBtreeData(&btkey);
    mValue = QBtreeData(&btvalue);
    return true;
}