summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/legacystore/jrnl/txn_map.cpp
blob: c514670601d4f7cd6c574eb60d51e9a3726e991e (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

/**
 * \file txn_map.cpp
 *
 * Qpid asynchronous store plugin library
 *
 * File containing code for class mrg::journal::txn_map (transaction map). See
 * comments in file txn_map.h for details.
 *
 * \author Kim van der Riet
 */

#include "qpid/legacystore/jrnl/txn_map.h"

#include <iomanip>
#include "qpid/legacystore/jrnl/jerrno.h"
#include "qpid/legacystore/jrnl/jexception.h"
#include "qpid/legacystore/jrnl/slock.h"
#include <sstream>

namespace mrg
{
namespace journal
{

// return/error codes
int16_t txn_map::TMAP_RID_NOT_FOUND = -2;
int16_t txn_map::TMAP_XID_NOT_FOUND = -1;
int16_t txn_map::TMAP_OK = 0;
int16_t txn_map::TMAP_NOT_SYNCED = 0;
int16_t txn_map::TMAP_SYNCED = 1;

txn_data_struct::txn_data_struct(const u_int64_t rid, const u_int64_t drid, const u_int16_t pfid,
		const bool enq_flag, const bool commit_flag):
        _rid(rid),
        _drid(drid),
        _pfid(pfid),
        _enq_flag(enq_flag),
        _commit_flag(commit_flag),
        _aio_compl(false)
{}

txn_map::txn_map():
        _map(),
        _pfid_txn_cnt()
{}

txn_map::~txn_map() {}

void
txn_map::set_num_jfiles(const u_int16_t num_jfiles)
{
    _pfid_txn_cnt.resize(num_jfiles, 0);
}

u_int32_t
txn_map::get_txn_pfid_cnt(const u_int16_t pfid) const
{
    return _pfid_txn_cnt.at(pfid);
}

bool
txn_map::insert_txn_data(const std::string& xid, const txn_data& td)
{
    bool ok = true;
    slock s(_mutex);
    xmap_itr itr = _map.find(xid);
    if (itr == _map.end()) // not found in map
    {
        txn_data_list list;
        list.push_back(td);
        std::pair<xmap_itr, bool> ret = _map.insert(xmap_param(xid, list));
        if (!ret.second) // duplicate
            ok = false;
    }
    else
        itr->second.push_back(td);
    _pfid_txn_cnt.at(td._pfid)++;
    return ok;
}

const txn_data_list
txn_map::get_tdata_list(const std::string& xid)
{
    slock s(_mutex);
    return get_tdata_list_nolock(xid);
}

const txn_data_list
txn_map::get_tdata_list_nolock(const std::string& xid)
{
    xmap_itr itr = _map.find(xid);
    if (itr == _map.end()) // not found in map
        return _empty_data_list;
    return itr->second;
}

const txn_data_list
txn_map::get_remove_tdata_list(const std::string& xid)
{
    slock s(_mutex);
    xmap_itr itr = _map.find(xid);
    if (itr == _map.end()) // not found in map
        return _empty_data_list;
    txn_data_list list = itr->second;
    _map.erase(itr);
    for (tdl_itr i=list.begin(); i!=list.end(); i++)
        _pfid_txn_cnt.at(i->_pfid)--;
    return list;
}

bool
txn_map::in_map(const std::string& xid)
{
    slock s(_mutex);
    xmap_itr itr= _map.find(xid);
    return itr != _map.end();
}

u_int32_t
txn_map::enq_cnt()
{
    return cnt(true);
}

u_int32_t
txn_map::deq_cnt()
{
    return cnt(true);
}

u_int32_t
txn_map::cnt(const bool enq_flag)
{
    slock s(_mutex);
    u_int32_t c = 0;
    for (xmap_itr i = _map.begin(); i != _map.end(); i++)
    {
        for (tdl_itr j = i->second.begin(); j < i->second.end(); j++)
        {
            if (j->_enq_flag == enq_flag)
                c++;
        }
    }
    return c;
}

int16_t
txn_map::is_txn_synced(const std::string& xid)
{
    slock s(_mutex);
    xmap_itr itr = _map.find(xid);
    if (itr == _map.end()) // not found in map
        return TMAP_XID_NOT_FOUND;
    bool is_synced = true;
    for (tdl_itr litr = itr->second.begin(); litr < itr->second.end(); litr++)
    {
        if (!litr->_aio_compl)
        {
            is_synced = false;
            break;
        }
    }
    return is_synced ? TMAP_SYNCED : TMAP_NOT_SYNCED;
}

int16_t
txn_map::set_aio_compl(const std::string& xid, const u_int64_t rid)
{
    slock s(_mutex);
    xmap_itr itr = _map.find(xid);
    if (itr == _map.end()) // xid not found in map
        return TMAP_XID_NOT_FOUND;
    for (tdl_itr litr = itr->second.begin(); litr < itr->second.end(); litr++)
    {
        if (litr->_rid == rid)
        {
            litr->_aio_compl = true;
            return TMAP_OK; // rid found
        }
    }
    // xid present, but rid not found
    return TMAP_RID_NOT_FOUND;
}

bool
txn_map::data_exists(const std::string& xid, const u_int64_t rid)
{
    bool found = false;
    {
        slock s(_mutex);
        txn_data_list tdl = get_tdata_list_nolock(xid);
        tdl_itr itr = tdl.begin();
        while (itr != tdl.end() && !found)
        {
            found = itr->_rid == rid;
            itr++;
        }
    }
    return found;
}

bool
txn_map::is_enq(const u_int64_t rid)
{
    bool found = false;
    {
        slock s(_mutex);
        for (xmap_itr i = _map.begin(); i != _map.end() && !found; i++)
        {
            txn_data_list list = i->second;
            for (tdl_itr j = list.begin(); j < list.end() && !found; j++)
            {
                if (j->_enq_flag)
                    found = j->_rid == rid;
                else
                    found = j->_drid == rid;
            }
        }
    }
    return found;
}

void
txn_map::xid_list(std::vector<std::string>& xv)
{
    xv.clear();
    {
        slock s(_mutex);
        for (xmap_itr itr = _map.begin(); itr != _map.end(); itr++)
            xv.push_back(itr->first);
    }
}

} // namespace journal
} // namespace mrg