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 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (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 
38    Licensed under the Apache License, Version 2.0 (the "License");
39    you may not use this file except in compliance with the License.
40    You may obtain a copy of the License at
41 
42        http://www.apache.org/licenses/LICENSE-2.0
43 
44    Unless required by applicable law or agreed to in writing, software
45    distributed under the License is distributed on an "AS IS" BASIS,
46    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
47    See the License for the specific language governing permissions and
48    limitations under the License.
49 ======= */
50 
51 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
52 
53 #include <db.h>
54 #include "txnid_set.h"
55 
56 namespace toku {
57 
58 int find_by_txnid(const TXNID &txnid_a, const TXNID &txnid_b);
find_by_txnid(const TXNID & txnid_a,const TXNID & txnid_b)59 int find_by_txnid(const TXNID &txnid_a, const TXNID &txnid_b) {
60     if (txnid_a < txnid_b) {
61         return -1;
62     } else if (txnid_a == txnid_b) {
63         return 0;
64     } else {
65         return 1;
66     }
67 }
68 
create(void)69 void txnid_set::create(void) {
70     // lazily allocate the underlying omt, since it is common
71     // to create a txnid set and never put anything in it.
72     m_txnids.create_no_array();
73 }
74 
destroy(void)75 void txnid_set::destroy(void) {
76     m_txnids.destroy();
77 }
78 
79 // Return true if the given transaction id is a member of the set.
80 // Otherwise, return false.
contains(TXNID txnid) const81 bool txnid_set::contains(TXNID txnid) const {
82     TXNID find_txnid;
83     int r = m_txnids.find_zero<TXNID, find_by_txnid>(txnid, &find_txnid, nullptr);
84     return r == 0 ? true : false;
85 }
86 
87 // Add a given txnid to the set
add(TXNID txnid)88 void txnid_set::add(TXNID txnid) {
89     int r = m_txnids.insert<TXNID, find_by_txnid>(txnid, txnid, nullptr);
90     invariant(r == 0 || r == DB_KEYEXIST);
91 }
92 
93 // Delete a given txnid from the set.
remove(TXNID txnid)94 void txnid_set::remove(TXNID txnid) {
95     uint32_t idx;
96     int r = m_txnids.find_zero<TXNID, find_by_txnid>(txnid, nullptr, &idx);
97     if (r == 0) {
98         r = m_txnids.delete_at(idx);
99         invariant_zero(r);
100     }
101 }
102 
103 // Return the size of the set
size(void) const104 size_t txnid_set::size(void) const {
105     return m_txnids.size();
106 }
107 
108 // Get the ith id in the set, assuming that the set is sorted.
get(size_t i) const109 TXNID txnid_set::get(size_t i) const {
110     TXNID txnid;
111     int r = m_txnids.fetch(i, &txnid);
112     invariant_zero(r);
113     return txnid;
114 }
115 
116 } /* namespace toku */
117