1/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3#ident "$Id$"
4/*======
5This file is part of PerconaFT.
6
7
8Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9
10 PerconaFT is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License, version 2,
12 as published by the Free Software Foundation.
13
14 PerconaFT is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
21
22----------------------------------------
23
24 PerconaFT is free software: you can redistribute it and/or modify
25 it under the terms of the GNU Affero General Public License, version 3,
26 as published by the Free Software Foundation.
27
28 PerconaFT is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU Affero General Public License for more details.
32
33 You should have received a copy of the GNU Affero General Public License
34 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
35======= */
36
37#ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38
39#include <db.h>
40#include "txnid_set.h"
41
42namespace toku {
43
44int find_by_txnid(const TXNID &txnid_a, const TXNID &txnid_b);
45int find_by_txnid(const TXNID &txnid_a, const TXNID &txnid_b) {
46 if (txnid_a < txnid_b) {
47 return -1;
48 } else if (txnid_a == txnid_b) {
49 return 0;
50 } else {
51 return 1;
52 }
53}
54
55void txnid_set::create(void) {
56 // lazily allocate the underlying omt, since it is common
57 // to create a txnid set and never put anything in it.
58 m_txnids.create_no_array();
59}
60
61void txnid_set::destroy(void) {
62 m_txnids.destroy();
63}
64
65// Return true if the given transaction id is a member of the set.
66// Otherwise, return false.
67bool txnid_set::contains(TXNID txnid) const {
68 TXNID find_txnid;
69 int r = m_txnids.find_zero<TXNID, find_by_txnid>(txnid, &find_txnid, nullptr);
70 return r == 0 ? true : false;
71}
72
73// Add a given txnid to the set
74void txnid_set::add(TXNID txnid) {
75 int r = m_txnids.insert<TXNID, find_by_txnid>(txnid, txnid, nullptr);
76 invariant(r == 0 || r == DB_KEYEXIST);
77}
78
79// Delete a given txnid from the set.
80void txnid_set::remove(TXNID txnid) {
81 uint32_t idx;
82 int r = m_txnids.find_zero<TXNID, find_by_txnid>(txnid, nullptr, &idx);
83 if (r == 0) {
84 r = m_txnids.delete_at(idx);
85 invariant_zero(r);
86 }
87}
88
89// Return the size of the set
90size_t txnid_set::size(void) const {
91 return m_txnids.size();
92}
93
94// Get the ith id in the set, assuming that the set is sorted.
95TXNID txnid_set::get(size_t i) const {
96 TXNID txnid;
97 int r = m_txnids.fetch(i, &txnid);
98 invariant_zero(r);
99 return txnid;
100}
101
102} /* namespace toku */
103