2012-01-19 16:49:54 +00:00
|
|
|
/*
|
2012-02-23 00:18:02 +00:00
|
|
|
* blsm.h
|
2012-01-19 16:49:54 +00:00
|
|
|
*
|
|
|
|
* Copyright 2009-2012 Yahoo! Inc.
|
|
|
|
*
|
|
|
|
* Licensed 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.
|
|
|
|
*
|
|
|
|
*/
|
2010-01-23 02:13:59 +00:00
|
|
|
#ifndef _LOGSTORE_H_
|
|
|
|
#define _LOGSTORE_H_
|
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
#include <stasis/common.h>
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2010-02-19 00:52:21 +00:00
|
|
|
#include "diskTreeComponent.h"
|
2010-03-09 01:42:23 +00:00
|
|
|
#include "memTreeComponent.h"
|
2012-02-23 01:25:24 +00:00
|
|
|
#include "tupleMerger.h"
|
2010-05-19 23:42:06 +00:00
|
|
|
#include "mergeManager.h"
|
2010-05-12 22:16:41 +00:00
|
|
|
#include "mergeStats.h"
|
|
|
|
|
2012-02-23 01:11:55 +00:00
|
|
|
class bLSM {
|
2010-01-23 02:13:59 +00:00
|
|
|
public:
|
2010-03-17 21:51:26 +00:00
|
|
|
|
|
|
|
class iterator;
|
2010-03-24 20:30:35 +00:00
|
|
|
|
|
|
|
// We want datapages to be as small as possible, assuming they don't force an extra seek to traverse the bottom level of internal nodes.
|
|
|
|
// Internal b-tree mem requirements:
|
|
|
|
// - Assume keys are small (compared to stasis pages) so we can ignore all but the bottom level of the tree.
|
|
|
|
//
|
|
|
|
// |internal nodes| ~= (|key| * |tree|) / (datapage_size * |stasis PAGE_SIZE|)
|
|
|
|
//
|
|
|
|
// Plugging in the numbers today:
|
|
|
|
//
|
|
|
|
// 6GB ~= 100B * 500 GB / (datapage_size * 4KB)
|
|
|
|
// (100B * 500GB) / (6GB * 4KB) = 2.035
|
2010-10-05 16:23:31 +00:00
|
|
|
// RCS: Set this to 1 so that we do (on average) one seek per b-tree read.
|
2012-03-20 21:55:44 +00:00
|
|
|
bLSM(int log_mode = 0, pageid_t max_c0_size = 100 * 1024 * 1024, pageid_t internal_region_size = 16384, pageid_t datapage_region_size = 256000, pageid_t datapage_size = 1);
|
2010-03-24 20:30:35 +00:00
|
|
|
|
2012-02-23 01:11:55 +00:00
|
|
|
~bLSM();
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2010-05-21 23:43:17 +00:00
|
|
|
double * R() { return &r_val; }
|
|
|
|
|
2010-01-23 02:13:59 +00:00
|
|
|
//user access functions
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple * findTuple(int xid, const dataTuple::key_t key, size_t keySize);
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple * findTuple_first(int xid, dataTuple::key_t key, size_t keySize);
|
2010-08-30 22:22:25 +00:00
|
|
|
|
|
|
|
private:
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple * insertTupleHelper(dataTuple *tuple);
|
2010-08-30 22:22:25 +00:00
|
|
|
public:
|
2012-02-23 01:11:55 +00:00
|
|
|
void insertManyTuples(struct dataTuple **tuples, int tuple_count);
|
|
|
|
void insertTuple(struct dataTuple *tuple);
|
2010-09-16 22:36:48 +00:00
|
|
|
/** This test and set has strange semantics on two fronts:
|
|
|
|
*
|
|
|
|
* 1) It is not atomic with respect to non-testAndSet operations (which is fine in theory, since they have no barrier semantics, and we don't have a use case to support the extra overhead)
|
|
|
|
* 2) If tuple2 is not null, it looks at tuple2's key instead of tuple's key. This means you can atomically set the value of one key based on the value of another (if you want to...)
|
|
|
|
*/
|
2012-02-23 01:11:55 +00:00
|
|
|
bool testAndSetTuple(struct dataTuple *tuple, struct dataTuple *tuple2);
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
//other class functions
|
|
|
|
recordid allocTable(int xid);
|
2010-03-01 21:26:07 +00:00
|
|
|
void openTable(int xid, recordid rid);
|
2010-01-23 02:13:59 +00:00
|
|
|
void flushTable();
|
|
|
|
|
2011-04-20 20:17:26 +00:00
|
|
|
void replayLog();
|
2012-02-23 01:11:55 +00:00
|
|
|
void logUpdate(dataTuple * tup);
|
2011-04-20 20:17:26 +00:00
|
|
|
|
2010-04-28 19:21:25 +00:00
|
|
|
static void init_stasis();
|
|
|
|
static void deinit_stasis();
|
|
|
|
|
2010-02-20 01:18:39 +00:00
|
|
|
inline uint64_t get_epoch() { return epoch; }
|
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
void registerIterator(iterator * it);
|
|
|
|
void forgetIterator(iterator * it);
|
2010-02-25 01:29:32 +00:00
|
|
|
void bump_epoch() ;
|
2010-02-20 01:18:39 +00:00
|
|
|
|
2010-03-13 00:05:06 +00:00
|
|
|
inline diskTreeComponent * get_tree_c2(){return tree_c2;}
|
|
|
|
inline diskTreeComponent * get_tree_c1(){return tree_c1;}
|
|
|
|
inline diskTreeComponent * get_tree_c1_mergeable(){return tree_c1_mergeable;}
|
2010-08-05 17:43:46 +00:00
|
|
|
inline diskTreeComponent * get_tree_c1_prime(){return tree_c1_prime;}
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2010-03-13 00:05:06 +00:00
|
|
|
inline void set_tree_c1(diskTreeComponent *t){tree_c1=t; bump_epoch(); }
|
|
|
|
inline void set_tree_c1_mergeable(diskTreeComponent *t){tree_c1_mergeable=t; bump_epoch(); }
|
2010-08-05 17:43:46 +00:00
|
|
|
inline void set_tree_c1_prime(diskTreeComponent *t){tree_c1_prime=t; bump_epoch(); }
|
2010-03-13 00:05:06 +00:00
|
|
|
inline void set_tree_c2(diskTreeComponent *t){tree_c2=t; bump_epoch(); }
|
2010-05-19 23:42:06 +00:00
|
|
|
pthread_cond_t c0_needed;
|
|
|
|
pthread_cond_t c0_ready;
|
|
|
|
pthread_cond_t c1_needed;
|
|
|
|
pthread_cond_t c1_ready;
|
|
|
|
|
2011-06-09 00:27:57 +00:00
|
|
|
inline memTreeComponent::rbtree_ptr_t get_tree_c0(){return tree_c0;}
|
|
|
|
inline memTreeComponent::rbtree_ptr_t get_tree_c0_mergeable(){return tree_c0_mergeable;}
|
|
|
|
void set_tree_c0(memTreeComponent::rbtree_ptr_t newtree){tree_c0 = newtree; bump_epoch(); }
|
2010-12-14 00:06:32 +00:00
|
|
|
|
2010-08-05 17:43:46 +00:00
|
|
|
bool get_c0_is_merging() { return c0_is_merging; }
|
|
|
|
void set_c0_is_merging(bool is_merging) { c0_is_merging = is_merging; }
|
2011-06-09 00:27:57 +00:00
|
|
|
void set_tree_c0_mergeable(memTreeComponent::rbtree_ptr_t newtree){tree_c0_mergeable = newtree; bump_epoch(); }
|
2011-04-20 20:17:26 +00:00
|
|
|
lsn_t get_log_offset();
|
|
|
|
void truncate_log();
|
|
|
|
|
|
|
|
void update_persistent_header(int xid, lsn_t log_trunc = INVALID_LSN);
|
2010-02-27 00:35:13 +00:00
|
|
|
|
2012-02-23 01:11:55 +00:00
|
|
|
inline tupleMerger * gettuplemerger(){return tmerger;}
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
struct table_header {
|
|
|
|
recordid c2_root; //tree root record --> points to the root of the b-tree
|
|
|
|
recordid c2_state; //tree state --> describes the regions used by the index tree
|
|
|
|
recordid c2_dp_state; //data pages state --> regions used by the data pages
|
|
|
|
recordid c1_root;
|
|
|
|
recordid c1_state;
|
|
|
|
recordid c1_dp_state;
|
2010-12-14 00:06:32 +00:00
|
|
|
recordid merge_manager;
|
2011-04-20 20:17:26 +00:00
|
|
|
lsn_t log_trunc;
|
2010-01-23 02:13:59 +00:00
|
|
|
};
|
2010-05-27 01:49:27 +00:00
|
|
|
rwlc * header_mut;
|
2010-06-21 22:59:05 +00:00
|
|
|
pthread_mutex_t tick_mut;
|
2010-05-27 01:49:27 +00:00
|
|
|
pthread_mutex_t rb_mut;
|
2010-02-10 21:49:50 +00:00
|
|
|
int64_t max_c0_size;
|
2010-08-21 03:09:18 +00:00
|
|
|
// these track the effectiveness of snowshoveling
|
2010-12-14 01:49:23 +00:00
|
|
|
int64_t mean_c0_run_length;
|
2010-08-21 03:09:18 +00:00
|
|
|
int64_t num_c0_mergers;
|
|
|
|
|
2010-04-29 23:13:04 +00:00
|
|
|
mergeManager * merge_mgr;
|
|
|
|
|
2011-04-20 20:17:26 +00:00
|
|
|
stasis_log_t * log_file;
|
2011-04-20 21:51:04 +00:00
|
|
|
int log_mode;
|
|
|
|
int batch_size;
|
2011-04-20 20:17:26 +00:00
|
|
|
bool recovering;
|
|
|
|
|
2010-08-17 21:23:39 +00:00
|
|
|
bool accepting_new_requests;
|
2010-12-08 19:49:13 +00:00
|
|
|
inline bool is_still_running() { return !shutting_down_; }
|
2010-02-17 23:38:31 +00:00
|
|
|
inline void stop() {
|
2010-05-27 23:15:24 +00:00
|
|
|
rwlc_writelock(header_mut);
|
2010-12-08 19:49:13 +00:00
|
|
|
if(!shutting_down_) {
|
|
|
|
shutting_down_ = true;
|
2010-05-27 23:15:24 +00:00
|
|
|
flushTable();
|
2011-03-25 20:05:49 +00:00
|
|
|
c0_flushing = true;
|
|
|
|
c1_flushing = true;
|
2010-05-27 23:15:24 +00:00
|
|
|
}
|
|
|
|
rwlc_unlock(header_mut);
|
|
|
|
// XXX must need to do other things! (join the threads?)
|
2010-02-17 23:38:31 +00:00
|
|
|
}
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
private:
|
2010-05-21 23:43:17 +00:00
|
|
|
double r_val;
|
2010-01-23 02:13:59 +00:00
|
|
|
recordid table_rec;
|
|
|
|
struct table_header tbl_header;
|
2010-02-20 01:18:39 +00:00
|
|
|
uint64_t epoch;
|
2010-03-13 00:05:06 +00:00
|
|
|
diskTreeComponent *tree_c2; //big tree
|
|
|
|
diskTreeComponent *tree_c1; //small tree
|
|
|
|
diskTreeComponent *tree_c1_mergeable; //small tree: ready to be merged with c2
|
2010-08-05 17:43:46 +00:00
|
|
|
diskTreeComponent *tree_c1_prime; //small tree: ready to be merged with c2
|
2011-06-09 00:27:57 +00:00
|
|
|
memTreeComponent::rbtree_ptr_t tree_c0; // in-mem red black tree
|
|
|
|
memTreeComponent::rbtree_ptr_t tree_c0_mergeable; // in-mem red black tree: ready to be merged with c1.
|
2010-08-05 17:43:46 +00:00
|
|
|
bool c0_is_merging;
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2010-08-05 17:43:46 +00:00
|
|
|
public:
|
2011-03-25 20:05:49 +00:00
|
|
|
bool c0_flushing;
|
|
|
|
bool c1_flushing; // this needs to be set to true at shutdown, or when the c0-c1 merger is waiting for c1-c2 to finish its merge
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2011-04-22 22:54:49 +00:00
|
|
|
lsn_t current_timestamp;
|
|
|
|
lsn_t expiry;
|
|
|
|
|
2010-01-23 02:13:59 +00:00
|
|
|
//DATA PAGE SETTINGS
|
2010-03-13 00:05:06 +00:00
|
|
|
pageid_t internal_region_size; // in number of pages
|
|
|
|
pageid_t datapage_region_size; // "
|
|
|
|
pageid_t datapage_size; // "
|
2010-05-19 23:42:06 +00:00
|
|
|
private:
|
2012-02-23 01:11:55 +00:00
|
|
|
tupleMerger *tmerger;
|
2010-02-17 23:38:31 +00:00
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
std::vector<iterator *> its;
|
2010-02-25 01:29:32 +00:00
|
|
|
|
2010-02-20 01:18:39 +00:00
|
|
|
public:
|
2010-12-08 19:49:13 +00:00
|
|
|
bool shutting_down_;
|
2010-03-17 21:51:26 +00:00
|
|
|
|
2012-02-23 01:11:55 +00:00
|
|
|
bool mightBeOnDisk(dataTuple * t) {
|
2011-04-22 22:54:49 +00:00
|
|
|
if(tree_c1) {
|
2011-05-02 22:09:59 +00:00
|
|
|
if(!tree_c1->bloom_filter) { DEBUG("no c1 bloom filter\n"); return true; }
|
2012-03-20 22:46:29 +00:00
|
|
|
if(stasis_bloom_filter_lookup(tree_c1->bloom_filter, (const char*)t->strippedkey(), t->strippedkeylen())) { DEBUG("in c1\n"); return true; }
|
2011-04-22 22:54:49 +00:00
|
|
|
}
|
|
|
|
if(tree_c1_prime) {
|
2011-05-02 22:09:59 +00:00
|
|
|
if(!tree_c1_prime->bloom_filter) { DEBUG("no c1' bloom filter\n"); return true; }
|
2012-03-20 22:46:29 +00:00
|
|
|
if(stasis_bloom_filter_lookup(tree_c1_prime->bloom_filter, (const char*)t->strippedkey(), t->strippedkeylen())) { DEBUG("in c1'\n"); return true; }
|
2011-04-22 22:54:49 +00:00
|
|
|
}
|
|
|
|
return mightBeAfterMemMerge(t);
|
|
|
|
}
|
|
|
|
|
2012-02-23 01:11:55 +00:00
|
|
|
bool mightBeAfterMemMerge(dataTuple * t) {
|
2011-04-22 22:54:49 +00:00
|
|
|
|
|
|
|
if(tree_c1_mergeable) {
|
2011-05-02 22:09:59 +00:00
|
|
|
if(!tree_c1_mergeable->bloom_filter) { DEBUG("no c1m bloom filter\n"); return true; }
|
2012-03-20 22:46:29 +00:00
|
|
|
if(stasis_bloom_filter_lookup(tree_c1_mergeable->bloom_filter, (const char*)t->strippedkey(), t->strippedkeylen())) { DEBUG("in c1m'\n");return true; }
|
2011-04-22 22:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(tree_c2) {
|
2011-05-02 22:09:59 +00:00
|
|
|
if(!tree_c2->bloom_filter) { DEBUG("no c2 bloom filter\n"); return true; }
|
2012-03-20 22:46:29 +00:00
|
|
|
if(stasis_bloom_filter_lookup(tree_c2->bloom_filter, (const char*)t->strippedkey(), t->strippedkeylen())) { DEBUG("in c2\n");return true; }
|
2011-04-22 22:54:49 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
template<class ITRA, class ITRN>
|
|
|
|
class mergeManyIterator {
|
|
|
|
public:
|
2012-02-23 01:11:55 +00:00
|
|
|
explicit mergeManyIterator(ITRA* a, ITRN** iters, int num_iters, dataTuple*(*merge)(const dataTuple*,const dataTuple*), int (*cmp)(const dataTuple*,const dataTuple*)) :
|
2010-03-17 21:51:26 +00:00
|
|
|
num_iters_(num_iters+1),
|
|
|
|
first_iter_(a),
|
|
|
|
iters_((ITRN**)malloc(sizeof(*iters_) * num_iters)), // exactly the number passed in
|
2012-02-23 01:11:55 +00:00
|
|
|
current_((dataTuple**)malloc(sizeof(*current_) * (num_iters_))), // one more than was passed in
|
2010-03-17 21:51:26 +00:00
|
|
|
last_iter_(-1),
|
|
|
|
cmp_(cmp),
|
|
|
|
merge_(merge),
|
|
|
|
dups((int*)malloc(sizeof(*dups)*num_iters_))
|
|
|
|
{
|
2010-05-28 01:29:10 +00:00
|
|
|
current_[0] = first_iter_->next_callerFrees();
|
2010-03-17 21:51:26 +00:00
|
|
|
for(int i = 1; i < num_iters_; i++) {
|
|
|
|
iters_[i-1] = iters[i-1];
|
2010-03-17 23:45:41 +00:00
|
|
|
current_[i] = iters_[i-1] ? iters_[i-1]->next_callerFrees() : NULL;
|
2010-03-17 21:51:26 +00:00
|
|
|
}
|
2010-02-20 01:18:39 +00:00
|
|
|
}
|
2010-03-17 21:51:26 +00:00
|
|
|
~mergeManyIterator() {
|
|
|
|
delete(first_iter_);
|
|
|
|
for(int i = 0; i < num_iters_; i++) {
|
|
|
|
if(i != last_iter_) {
|
2012-02-23 01:11:55 +00:00
|
|
|
if(current_[i]) dataTuple::freetuple(current_[i]);
|
2010-03-17 21:51:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for(int i = 1; i < num_iters_; i++) {
|
|
|
|
delete iters_[i-1];
|
|
|
|
}
|
|
|
|
free(current_);
|
|
|
|
free(iters_);
|
|
|
|
free(dups);
|
2010-02-20 01:18:39 +00:00
|
|
|
}
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple * peek() {
|
|
|
|
dataTuple * ret = next_callerFrees();
|
2010-03-17 21:51:26 +00:00
|
|
|
last_iter_ = -1; // don't advance iterator on next peek() or getnext() call.
|
|
|
|
return ret;
|
|
|
|
}
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple * next_callerFrees() {
|
2010-03-17 21:51:26 +00:00
|
|
|
int num_dups = 0;
|
|
|
|
if(last_iter_ != -1) {
|
|
|
|
// get the value after the one we just returned to the user
|
2011-06-09 01:16:55 +00:00
|
|
|
//datatuple::freetuple(current_[last_iter_]); // should never be null
|
2010-03-17 21:51:26 +00:00
|
|
|
if(last_iter_ == 0) {
|
2010-05-28 01:29:10 +00:00
|
|
|
current_[last_iter_] = first_iter_->next_callerFrees();
|
2010-03-17 21:51:26 +00:00
|
|
|
} else if(last_iter_ != -1){
|
|
|
|
current_[last_iter_] = iters_[last_iter_-1]->next_callerFrees();
|
|
|
|
} else {
|
|
|
|
// last call was 'peek'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// find the first non-empty iterator. (Don't need to special-case ITRA since we're looking at current.)
|
|
|
|
int min = 0;
|
|
|
|
while(min < num_iters_ && !current_[min]) {
|
|
|
|
min++;
|
|
|
|
}
|
|
|
|
if(min == num_iters_) { return NULL; }
|
|
|
|
// examine current to decide which tuple to return.
|
|
|
|
for(int i = min+1; i < num_iters_; i++) {
|
|
|
|
if(current_[i]) {
|
|
|
|
int res = cmp_(current_[min], current_[i]);
|
|
|
|
if(res > 0) { // min > i
|
|
|
|
min = i;
|
|
|
|
num_dups = 0;
|
|
|
|
} else if(res == 0) { // min == i
|
|
|
|
dups[num_dups] = i;
|
|
|
|
num_dups++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple * ret;
|
2010-03-17 21:51:26 +00:00
|
|
|
if(!merge_) {
|
|
|
|
ret = current_[min];
|
|
|
|
} else {
|
|
|
|
// XXX use merge function to build a new ret.
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
// advance the iterators that match the tuple we're returning.
|
|
|
|
for(int i = 0; i < num_dups; i++) {
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple::freetuple(current_[dups[i]]); // should never be null
|
2010-03-17 21:51:26 +00:00
|
|
|
current_[dups[i]] = iters_[dups[i]-1]->next_callerFrees();
|
|
|
|
}
|
|
|
|
last_iter_ = min; // mark the min iter to be advance at the next invocation of next(). This saves us a copy in the non-merging case.
|
|
|
|
return ret;
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
int num_iters_;
|
|
|
|
ITRA * first_iter_;
|
|
|
|
ITRN ** iters_;
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple ** current_;
|
2010-03-17 21:51:26 +00:00
|
|
|
int last_iter_;
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
|
2012-02-23 01:11:55 +00:00
|
|
|
int (*cmp_)(const dataTuple*,const dataTuple*);
|
|
|
|
dataTuple*(*merge_)(const dataTuple*,const dataTuple*);
|
2010-02-20 01:18:39 +00:00
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
// temporary variables initiaized once for effiency
|
|
|
|
int * dups;
|
2010-02-20 01:18:39 +00:00
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
};
|
2010-02-20 01:18:39 +00:00
|
|
|
|
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
class iterator {
|
|
|
|
public:
|
2012-02-23 01:11:55 +00:00
|
|
|
explicit iterator(bLSM* ltable)
|
2010-03-17 21:51:26 +00:00
|
|
|
: ltable(ltable),
|
|
|
|
epoch(ltable->get_epoch()),
|
|
|
|
merge_it_(NULL),
|
|
|
|
last_returned(NULL),
|
|
|
|
key(NULL),
|
2010-09-02 23:02:24 +00:00
|
|
|
valid(false),
|
|
|
|
reval_count(0) {
|
2010-05-27 01:49:27 +00:00
|
|
|
rwlc_readlock(ltable->header_mut);
|
|
|
|
pthread_mutex_lock(<able->rb_mut);
|
2010-03-17 21:51:26 +00:00
|
|
|
ltable->registerIterator(this);
|
2010-05-27 01:49:27 +00:00
|
|
|
pthread_mutex_unlock(<able->rb_mut);
|
2010-03-17 21:51:26 +00:00
|
|
|
validate();
|
2010-09-02 23:02:24 +00:00
|
|
|
// rwlc_unlock(ltable->header_mut);
|
2010-03-17 21:51:26 +00:00
|
|
|
}
|
2010-02-25 01:29:32 +00:00
|
|
|
|
2012-02-23 01:11:55 +00:00
|
|
|
explicit iterator(bLSM* ltable,dataTuple *key)
|
2010-03-17 21:51:26 +00:00
|
|
|
: ltable(ltable),
|
|
|
|
epoch(ltable->get_epoch()),
|
|
|
|
merge_it_(NULL),
|
|
|
|
last_returned(NULL),
|
|
|
|
key(key),
|
2010-09-02 23:02:24 +00:00
|
|
|
valid(false),
|
|
|
|
reval_count(0)
|
2010-03-17 21:51:26 +00:00
|
|
|
{
|
2010-05-27 01:49:27 +00:00
|
|
|
rwlc_readlock(ltable->header_mut);
|
|
|
|
pthread_mutex_lock(<able->rb_mut);
|
2010-03-17 21:51:26 +00:00
|
|
|
ltable->registerIterator(this);
|
2010-05-27 01:49:27 +00:00
|
|
|
pthread_mutex_unlock(<able->rb_mut);
|
2010-03-17 21:51:26 +00:00
|
|
|
validate();
|
2010-09-02 23:02:24 +00:00
|
|
|
// rwlc_unlock(ltable->header_mut);
|
2010-03-17 21:51:26 +00:00
|
|
|
}
|
2010-01-23 02:13:59 +00:00
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
~iterator() {
|
2010-09-02 23:02:24 +00:00
|
|
|
// rwlc_readlock(ltable->header_mut);
|
2010-05-27 01:49:27 +00:00
|
|
|
pthread_mutex_lock(<able->rb_mut);
|
2010-03-24 20:30:35 +00:00
|
|
|
ltable->forgetIterator(this);
|
|
|
|
invalidate();
|
2010-05-27 01:49:27 +00:00
|
|
|
pthread_mutex_unlock(<able->rb_mut);
|
2012-02-23 01:11:55 +00:00
|
|
|
if(last_returned) dataTuple::freetuple(last_returned);
|
2010-05-27 01:49:27 +00:00
|
|
|
rwlc_unlock(ltable->header_mut);
|
2010-03-17 21:51:26 +00:00
|
|
|
}
|
|
|
|
private:
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple * getnextHelper() {
|
2010-09-02 23:02:24 +00:00
|
|
|
// rwlc_readlock(ltable->header_mut);
|
|
|
|
revalidate();
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple * tmp = merge_it_->next_callerFrees();
|
2010-03-17 21:51:26 +00:00
|
|
|
if(last_returned && tmp) {
|
2012-02-23 01:11:55 +00:00
|
|
|
int res = dataTuple::compare(last_returned->strippedkey(), last_returned->strippedkeylen(), tmp->strippedkey(), tmp->strippedkeylen());
|
2011-05-02 22:09:59 +00:00
|
|
|
if(res >= 0) {
|
|
|
|
int al = last_returned->strippedkeylen();
|
|
|
|
char * a =(char*)malloc(al + 1);
|
|
|
|
memcpy(a, last_returned->strippedkey(), al);
|
|
|
|
a[al] = 0;
|
|
|
|
int bl = tmp->strippedkeylen();
|
|
|
|
char * b =(char*)malloc(bl + 1);
|
|
|
|
memcpy(b, tmp->strippedkey(), bl);
|
|
|
|
b[bl] = 0;
|
2012-02-23 00:18:02 +00:00
|
|
|
printf("blsm.h assert fail: out of order tuples %d should be < 0. %s <=> %s\n", res, a, b);
|
2011-05-02 22:09:59 +00:00
|
|
|
free(a);
|
|
|
|
free(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if(last_returned) {
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple::freetuple(last_returned);
|
2010-03-17 21:51:26 +00:00
|
|
|
}
|
|
|
|
last_returned = tmp;
|
2010-09-02 23:02:24 +00:00
|
|
|
// rwlc_unlock(ltable->header_mut);
|
2010-03-17 21:51:26 +00:00
|
|
|
return last_returned;
|
|
|
|
}
|
|
|
|
public:
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple * getnextIncludingTombstones() {
|
|
|
|
dataTuple * ret = getnextHelper();
|
2010-05-28 01:29:10 +00:00
|
|
|
ret = ret ? ret->create_copy() : NULL;
|
|
|
|
return ret;
|
2010-02-25 01:29:32 +00:00
|
|
|
}
|
2010-02-20 01:18:39 +00:00
|
|
|
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple * getnext() {
|
|
|
|
dataTuple * ret;
|
2010-03-17 21:51:26 +00:00
|
|
|
while((ret = getnextHelper()) && ret->isDelete()) { } // getNextHelper handles its own memory.
|
2010-05-28 01:29:10 +00:00
|
|
|
ret = ret ? ret->create_copy() : NULL; // XXX hate making copy! Caller should not manage our memory.
|
|
|
|
return ret;
|
2010-03-17 21:51:26 +00:00
|
|
|
}
|
2010-02-20 01:18:39 +00:00
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
void invalidate() {
|
2010-05-19 23:42:06 +00:00
|
|
|
// assert(!trywritelock(ltable->header_lock,0));
|
2010-03-17 21:51:26 +00:00
|
|
|
if(valid) {
|
|
|
|
delete merge_it_;
|
|
|
|
merge_it_ = NULL;
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
}
|
2010-02-20 01:18:39 +00:00
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
private:
|
|
|
|
inline void init_helper();
|
|
|
|
|
|
|
|
explicit iterator() { abort(); }
|
|
|
|
void operator=(iterator & t) { abort(); }
|
|
|
|
int operator-(iterator & t) { abort(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const int C1 = 0;
|
|
|
|
static const int C1_MERGEABLE = 1;
|
|
|
|
static const int C2 = 2;
|
2012-02-23 01:11:55 +00:00
|
|
|
bLSM * ltable;
|
2010-03-17 21:51:26 +00:00
|
|
|
uint64_t epoch;
|
|
|
|
typedef mergeManyIterator<
|
2011-06-13 18:47:21 +00:00
|
|
|
memTreeComponent::batchedRevalidatingIterator,
|
|
|
|
memTreeComponent::iterator> inner_merge_it_t;
|
2010-03-17 21:51:26 +00:00
|
|
|
typedef mergeManyIterator<
|
|
|
|
inner_merge_it_t,
|
|
|
|
diskTreeComponent::iterator> merge_it_t;
|
|
|
|
|
|
|
|
merge_it_t* merge_it_;
|
|
|
|
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple * last_returned;
|
|
|
|
dataTuple * key;
|
2010-03-17 21:51:26 +00:00
|
|
|
bool valid;
|
2010-09-02 23:02:24 +00:00
|
|
|
int reval_count;
|
|
|
|
static const int reval_period = 100;
|
2010-03-17 21:51:26 +00:00
|
|
|
void revalidate() {
|
2010-09-02 23:02:24 +00:00
|
|
|
if(reval_count == reval_period) {
|
|
|
|
rwlc_unlock(ltable->header_mut);
|
|
|
|
reval_count = 0;
|
|
|
|
rwlc_readlock(ltable->header_mut);
|
|
|
|
} else {
|
|
|
|
reval_count++;
|
|
|
|
}
|
2010-03-17 21:51:26 +00:00
|
|
|
if(!valid) {
|
|
|
|
validate();
|
|
|
|
} else {
|
|
|
|
assert(epoch == ltable->get_epoch());
|
|
|
|
}
|
2010-02-25 01:29:32 +00:00
|
|
|
}
|
2010-01-23 02:13:59 +00:00
|
|
|
|
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
void validate() {
|
2011-06-13 18:47:21 +00:00
|
|
|
memTreeComponent::batchedRevalidatingIterator * c0_it;
|
|
|
|
memTreeComponent::iterator *c0_mergeable_it[1];
|
2010-08-05 17:43:46 +00:00
|
|
|
diskTreeComponent::iterator * disk_it[4];
|
2010-03-17 21:51:26 +00:00
|
|
|
epoch = ltable->get_epoch();
|
2010-03-17 23:45:41 +00:00
|
|
|
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple *t;
|
2010-03-17 21:51:26 +00:00
|
|
|
if(last_returned) {
|
2010-03-17 23:45:41 +00:00
|
|
|
t = last_returned;
|
2010-03-17 21:51:26 +00:00
|
|
|
} else if(key) {
|
2010-03-17 23:45:41 +00:00
|
|
|
t = key;
|
|
|
|
} else {
|
|
|
|
t = NULL;
|
|
|
|
}
|
|
|
|
|
2011-06-13 18:47:21 +00:00
|
|
|
c0_it = new memTreeComponent::batchedRevalidatingIterator(ltable->get_tree_c0(), 100, <able->rb_mut, t);
|
|
|
|
c0_mergeable_it[0] = new memTreeComponent::iterator (ltable->get_tree_c0_mergeable(), t);
|
2010-08-05 17:43:46 +00:00
|
|
|
if(ltable->get_tree_c1_prime()) {
|
|
|
|
disk_it[0] = ltable->get_tree_c1_prime()->open_iterator(t);
|
|
|
|
} else {
|
|
|
|
disk_it[0] = NULL;
|
|
|
|
}
|
|
|
|
disk_it[1] = ltable->get_tree_c1()->open_iterator(t);
|
2010-03-17 23:45:41 +00:00
|
|
|
if(ltable->get_tree_c1_mergeable()) {
|
2010-08-05 17:43:46 +00:00
|
|
|
disk_it[2] = ltable->get_tree_c1_mergeable()->open_iterator(t);
|
2010-03-17 21:51:26 +00:00
|
|
|
} else {
|
2010-08-05 17:43:46 +00:00
|
|
|
disk_it[2] = NULL;
|
2010-03-17 21:51:26 +00:00
|
|
|
}
|
2010-08-05 17:43:46 +00:00
|
|
|
disk_it[3] = ltable->get_tree_c2()->open_iterator(t);
|
2010-02-20 01:18:39 +00:00
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
inner_merge_it_t * inner_merge_it =
|
2012-02-23 01:11:55 +00:00
|
|
|
new inner_merge_it_t(c0_it, c0_mergeable_it, 1, NULL, dataTuple::compare_obj);
|
|
|
|
merge_it_ = new merge_it_t(inner_merge_it, disk_it, 4, NULL, dataTuple::compare_obj); // XXX Hardcodes comparator, and does not handle merges
|
2010-03-17 21:51:26 +00:00
|
|
|
if(last_returned) {
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple * junk = merge_it_->peek();
|
|
|
|
if(junk && !dataTuple::compare(junk->strippedkey(), junk->strippedkeylen(), last_returned->strippedkey(), last_returned->strippedkeylen())) {
|
2010-03-17 21:51:26 +00:00
|
|
|
// we already returned junk
|
2012-02-23 01:11:55 +00:00
|
|
|
dataTuple::freetuple(merge_it_->next_callerFrees());
|
2010-03-17 21:51:26 +00:00
|
|
|
}
|
2010-03-09 19:02:54 +00:00
|
|
|
}
|
2010-03-17 21:51:26 +00:00
|
|
|
valid = true;
|
2010-03-09 19:02:54 +00:00
|
|
|
}
|
2010-03-17 21:51:26 +00:00
|
|
|
};
|
2010-02-20 01:18:39 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2010-01-23 02:13:59 +00:00
|
|
|
#endif
|