2010-02-19 00:52:21 +00:00
|
|
|
/*
|
|
|
|
* diskTreeComponent.cpp
|
|
|
|
*
|
2012-01-19 16:49:54 +00:00
|
|
|
* Copyright 2010-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-02-19 00:52:21 +00:00
|
|
|
* Created on: Feb 18, 2010
|
|
|
|
* Author: sears
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "merger.h"
|
|
|
|
#include "diskTreeComponent.h"
|
2010-03-17 21:51:26 +00:00
|
|
|
#include "regionAllocator.h"
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-05-19 23:42:06 +00:00
|
|
|
#include "mergeStats.h"
|
2010-02-19 00:52:21 +00:00
|
|
|
#include <stasis/transactional.h>
|
|
|
|
#include <stasis/page.h>
|
|
|
|
#include <stasis/page/slotted.h>
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
// LOGTREE implementation
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
//LSM_ROOT_PAGE
|
|
|
|
|
2010-03-09 19:47:12 +00:00
|
|
|
const int64_t diskTreeComponent::internalNodes::DEPTH = 0; //in root this is the slot num where the DEPTH (of tree) is stored
|
|
|
|
const int64_t diskTreeComponent::internalNodes::COMPARATOR = 1; //in root this is the slot num where the COMPARATOR id is stored
|
|
|
|
const int64_t diskTreeComponent::internalNodes::FIRST_SLOT = 2; //this is the first unused slot in all index pages
|
2010-07-16 21:43:21 +00:00
|
|
|
const ssize_t diskTreeComponent::internalNodes::root_rec_size = sizeof(int64_t);
|
2010-03-09 19:47:12 +00:00
|
|
|
const int64_t diskTreeComponent::internalNodes::PREV_LEAF = 0; //pointer to prev leaf page
|
|
|
|
const int64_t diskTreeComponent::internalNodes::NEXT_LEAF = 1; //pointer to next leaf page
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
recordid diskTreeComponent::get_root_rid() { return ltree->get_root_rec(); }
|
|
|
|
recordid diskTreeComponent::get_datapage_allocator_rid() { return ltree->get_datapage_alloc()->header_rid(); }
|
|
|
|
recordid diskTreeComponent::get_internal_node_allocator_rid() { return ltree->get_internal_node_alloc()->header_rid(); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void diskTreeComponent::force(int xid) {
|
|
|
|
ltree->get_datapage_alloc()->force_regions(xid);
|
|
|
|
ltree->get_internal_node_alloc()->force_regions(xid);
|
|
|
|
}
|
|
|
|
void diskTreeComponent::dealloc(int xid) {
|
|
|
|
ltree->get_datapage_alloc()->dealloc_regions(xid);
|
|
|
|
ltree->get_internal_node_alloc()->dealloc_regions(xid);
|
|
|
|
}
|
|
|
|
void diskTreeComponent::list_regions(int xid, pageid_t *internal_node_region_length, pageid_t *internal_node_region_count, pageid_t **internal_node_regions,
|
|
|
|
pageid_t *datapage_region_length, pageid_t *datapage_region_count, pageid_t **datapage_regions) {
|
|
|
|
*internal_node_regions = ltree->get_internal_node_alloc()->list_regions(xid, internal_node_region_length, internal_node_region_count);
|
|
|
|
*datapage_regions = ltree->get_datapage_alloc() ->list_regions(xid, datapage_region_length, datapage_region_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-13 00:05:06 +00:00
|
|
|
void diskTreeComponent::writes_done() {
|
|
|
|
if(dp) {
|
2010-05-19 23:42:06 +00:00
|
|
|
((mergeStats*)stats)->wrote_datapage(dp);
|
2010-03-13 00:05:06 +00:00
|
|
|
dp->writes_done();
|
|
|
|
delete dp;
|
|
|
|
dp = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-29 00:57:48 +00:00
|
|
|
int diskTreeComponent::insertTuple(int xid, datatuple *t)
|
2010-03-13 00:05:06 +00:00
|
|
|
{
|
2010-10-03 23:05:45 +00:00
|
|
|
if(bloom_filter) {
|
2011-04-22 22:54:49 +00:00
|
|
|
bloom_filter_insert(bloom_filter, (const char*)t->strippedkey(), t->strippedkeylen());
|
2010-10-03 23:05:45 +00:00
|
|
|
}
|
2010-03-13 00:05:06 +00:00
|
|
|
int ret = 0; // no error.
|
|
|
|
if(dp==0) {
|
|
|
|
dp = insertDataPage(xid, t);
|
2010-12-13 22:27:13 +00:00
|
|
|
// stats->stats_num_datapages_out++;
|
2010-03-13 00:05:06 +00:00
|
|
|
} else if(!dp->append(t)) {
|
2010-12-13 22:27:13 +00:00
|
|
|
// stats->stats_bytes_out_with_overhead += (PAGE_SIZE * dp->get_page_count());
|
2010-05-19 23:42:06 +00:00
|
|
|
((mergeStats*)stats)->wrote_datapage(dp);
|
2010-03-13 00:05:06 +00:00
|
|
|
dp->writes_done();
|
|
|
|
delete dp;
|
|
|
|
dp = insertDataPage(xid, t);
|
2010-12-13 22:27:13 +00:00
|
|
|
// stats->stats_num_datapages_out++;
|
2010-03-13 00:05:06 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-06-09 00:07:42 +00:00
|
|
|
DataPage* diskTreeComponent::insertDataPage(int xid, datatuple *tuple) {
|
2010-03-13 00:05:06 +00:00
|
|
|
//create a new data page -- either the last region is full, or the last data page doesn't want our tuple. (or both)
|
|
|
|
|
2011-06-09 00:07:42 +00:00
|
|
|
DataPage * dp = 0;
|
2010-03-13 00:05:06 +00:00
|
|
|
int count = 0;
|
|
|
|
while(dp==0)
|
|
|
|
{
|
2011-06-09 00:07:42 +00:00
|
|
|
dp = new DataPage(xid, datapage_size, ltree->get_datapage_alloc());
|
2010-03-13 00:05:06 +00:00
|
|
|
|
|
|
|
//insert the record into the data page
|
|
|
|
if(!dp->append(tuple))
|
|
|
|
{
|
|
|
|
// the last datapage must have not wanted the tuple, and then this datapage figured out the region is full.
|
2010-05-19 23:42:06 +00:00
|
|
|
((mergeStats*)stats)->wrote_datapage(dp);
|
2010-03-13 00:05:06 +00:00
|
|
|
dp->writes_done();
|
|
|
|
delete dp;
|
|
|
|
dp = 0;
|
|
|
|
assert(count == 0); // only retry once.
|
|
|
|
count ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ltree->appendPage(xid,
|
2011-04-22 22:54:49 +00:00
|
|
|
tuple->strippedkey(),
|
|
|
|
tuple->strippedkeylen(),
|
2010-03-13 00:05:06 +00:00
|
|
|
dp->get_start_pid()
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
//return the datapage
|
|
|
|
return dp;
|
|
|
|
}
|
|
|
|
|
|
|
|
datatuple * diskTreeComponent::findTuple(int xid, datatuple::key_t key, size_t keySize)
|
|
|
|
{
|
|
|
|
datatuple * tup=0;
|
|
|
|
|
2010-10-03 23:05:45 +00:00
|
|
|
if(bloom_filter) {
|
|
|
|
if(!bloom_filter_lookup(bloom_filter, (const char*)key, keySize)) {
|
2010-12-08 19:18:05 +00:00
|
|
|
return NULL;
|
2010-10-03 23:05:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-13 00:05:06 +00:00
|
|
|
//find the datapage
|
|
|
|
pageid_t pid = ltree->findPage(xid, (byte*)key, keySize);
|
|
|
|
|
|
|
|
if(pid!=-1)
|
|
|
|
{
|
2011-06-09 00:07:42 +00:00
|
|
|
DataPage * dp = new DataPage(xid, 0, pid);
|
2010-03-13 00:05:06 +00:00
|
|
|
dp->recordRead(key, keySize, &tup);
|
|
|
|
delete dp;
|
|
|
|
}
|
|
|
|
return tup;
|
|
|
|
}
|
|
|
|
|
2010-03-09 19:47:12 +00:00
|
|
|
recordid diskTreeComponent::internalNodes::create(int xid) {
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-09 23:17:03 +00:00
|
|
|
pageid_t root = internal_node_alloc->alloc_extent(xid, 1);
|
2010-03-05 21:19:52 +00:00
|
|
|
DEBUG("Root = %lld\n", root);
|
|
|
|
recordid ret = { root, 0, 0 };
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
Page *p = loadPage(xid, ret.page);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
//initialize root node
|
|
|
|
stasis_page_slotted_initialize_page(p);
|
|
|
|
recordid tmp = stasis_record_alloc_begin(xid, p, root_rec_size);
|
|
|
|
stasis_record_alloc_done(xid,p,tmp);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
assert(tmp.page == ret.page
|
|
|
|
&& tmp.slot == DEPTH
|
|
|
|
&& tmp.size == root_rec_size);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
int64_t zero = 0;
|
2010-03-08 23:58:13 +00:00
|
|
|
assert(sizeof(zero) == root_rec_size);
|
|
|
|
stasis_record_write(xid, p, tmp, (byte*)&zero);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
tmp = stasis_record_alloc_begin(xid, p, root_rec_size);
|
|
|
|
stasis_record_alloc_done(xid,p,tmp);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
assert(tmp.page == ret.page
|
|
|
|
&& tmp.slot == COMPARATOR
|
|
|
|
&& tmp.size == root_rec_size);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-08 23:58:13 +00:00
|
|
|
stasis_record_write(xid, p, tmp, (byte*)&zero);
|
|
|
|
|
2010-03-09 23:48:42 +00:00
|
|
|
stasis_page_lsn_write(xid, p, internal_node_alloc->get_lsn(xid));
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
releasePage(p);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
root_rec = ret;
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
return ret;
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
|
|
|
|
2010-03-09 19:47:12 +00:00
|
|
|
void diskTreeComponent::internalNodes::writeNodeRecord(int xid, Page * p, recordid & rid,
|
2010-03-05 21:19:52 +00:00
|
|
|
const byte *key, size_t keylen, pageid_t ptr) {
|
|
|
|
DEBUG("writenoderecord:\tp->id\t%lld\tkey:\t%s\tkeylen: %d\tval_page\t%lld\n",
|
|
|
|
p->id, datatuple::key_to_str(key).c_str(), keylen, ptr);
|
|
|
|
indexnode_rec *nr = (indexnode_rec*)stasis_record_write_begin(xid, p, rid);
|
|
|
|
nr->ptr = ptr;
|
|
|
|
memcpy(nr+1, key, keylen);
|
|
|
|
stasis_record_write_done(xid, p, rid, (byte*)nr);
|
2010-03-09 23:48:42 +00:00
|
|
|
stasis_page_lsn_write(xid, p, internal_node_alloc->get_lsn(xid));
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
|
|
|
|
2010-03-09 19:47:12 +00:00
|
|
|
void diskTreeComponent::internalNodes::initializeNodePage(int xid, Page *p) {
|
2010-03-05 21:19:52 +00:00
|
|
|
stasis_page_slotted_initialize_page(p);
|
|
|
|
recordid reserved1 = stasis_record_alloc_begin(xid, p, sizeof(indexnode_rec));
|
|
|
|
stasis_record_alloc_done(xid, p, reserved1);
|
|
|
|
recordid reserved2 = stasis_record_alloc_begin(xid, p, sizeof(indexnode_rec));
|
|
|
|
stasis_record_alloc_done(xid, p, reserved2);
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-09 22:18:55 +00:00
|
|
|
recordid diskTreeComponent::internalNodes::appendPage(int xid,
|
|
|
|
const byte *key, size_t keySize, pageid_t val_page) {
|
|
|
|
recordid tree = root_rec;
|
|
|
|
|
2010-02-19 00:52:21 +00:00
|
|
|
Page *p = loadPage(xid, tree.page);
|
|
|
|
|
2010-03-08 23:58:13 +00:00
|
|
|
tree.slot = DEPTH;
|
|
|
|
tree.size = 0;
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-08-21 03:09:18 +00:00
|
|
|
readlock(p->rwlatch,0);
|
2010-03-08 23:58:13 +00:00
|
|
|
const indexnode_rec *nr = (const indexnode_rec*)stasis_record_read_begin(xid, p, tree);
|
2010-02-19 00:52:21 +00:00
|
|
|
int64_t depth = *((int64_t*)nr);
|
2010-03-08 23:58:13 +00:00
|
|
|
stasis_record_read_done(xid, p, tree, (const byte*)nr);
|
2010-08-21 03:09:18 +00:00
|
|
|
unlock(p->rwlatch);
|
2010-03-09 22:18:55 +00:00
|
|
|
if(lastLeaf == -1) {
|
|
|
|
lastLeaf = findLastLeaf(xid, p, depth);
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
|
|
|
|
2010-03-09 22:18:55 +00:00
|
|
|
Page *lastLeafPage;
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-09 22:18:55 +00:00
|
|
|
if(lastLeaf != tree.page) {
|
|
|
|
lastLeafPage= loadPage(xid, lastLeaf);
|
2010-03-05 21:19:52 +00:00
|
|
|
} else {
|
2010-03-09 22:18:55 +00:00
|
|
|
lastLeafPage = p;
|
2010-03-05 21:19:52 +00:00
|
|
|
}
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-08-21 03:09:18 +00:00
|
|
|
writelock(lastLeafPage->rwlatch, 0);
|
|
|
|
|
2010-03-09 22:18:55 +00:00
|
|
|
recordid ret = stasis_record_alloc_begin(xid, lastLeafPage,
|
2010-02-19 00:52:21 +00:00
|
|
|
sizeof(indexnode_rec)+keySize);
|
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
if(ret.size == INVALID_SLOT) {
|
2010-08-21 03:09:18 +00:00
|
|
|
unlock(lastLeafPage->rwlatch);
|
|
|
|
if(lastLeafPage->id != p->id) { // is the root the last leaf page?
|
2010-03-09 22:18:55 +00:00
|
|
|
assert(lastLeaf != tree.page);
|
|
|
|
releasePage(lastLeafPage); // don't need that page anymore...
|
|
|
|
lastLeafPage = 0;
|
2010-03-05 21:19:52 +00:00
|
|
|
}
|
|
|
|
// traverse down the root of the tree.
|
|
|
|
|
|
|
|
tree.slot = 0;
|
|
|
|
|
|
|
|
assert(tree.page == p->id);
|
|
|
|
|
2010-03-09 23:48:42 +00:00
|
|
|
ret = appendInternalNode(xid, p, depth, key, keySize, val_page);
|
2010-03-05 21:19:52 +00:00
|
|
|
|
|
|
|
if(ret.size == INVALID_SLOT) {
|
|
|
|
DEBUG("Need to split root; depth = %d\n", depth);
|
|
|
|
|
2010-03-09 23:17:03 +00:00
|
|
|
pageid_t child = internal_node_alloc->alloc_extent(xid, 1);
|
2010-08-21 03:09:18 +00:00
|
|
|
slotid_t numslots = stasis_record_last(xid, p).slot+1;
|
|
|
|
{
|
|
|
|
Page *lc = loadPage(xid, child);
|
|
|
|
|
|
|
|
initializeNodePage(xid, lc);
|
|
|
|
|
|
|
|
//creates a copy of the root page records in the
|
|
|
|
//newly allocated child page
|
|
|
|
recordid rid;
|
|
|
|
rid.page = p->id;
|
|
|
|
// XXX writelock lc here? no need, since it's not installed in the tree yet
|
|
|
|
for(rid.slot = FIRST_SLOT; rid.slot < numslots; rid.slot++) {
|
|
|
|
//read the record from the root page
|
|
|
|
rid.size = stasis_record_length_read(xid, p, rid);
|
|
|
|
const indexnode_rec *nr = (const indexnode_rec*)stasis_record_read_begin(xid, p, rid);
|
|
|
|
recordid cnext = stasis_record_alloc_begin(xid, lc,rid.size);
|
|
|
|
|
|
|
|
assert(rid.slot == cnext.slot);
|
|
|
|
assert(cnext.size != INVALID_SLOT);
|
|
|
|
|
|
|
|
stasis_record_alloc_done(xid, lc, cnext);
|
|
|
|
stasis_record_write(xid, lc, cnext, (byte*)nr);
|
|
|
|
stasis_record_read_done(xid, p, rid, (const byte*)nr);
|
|
|
|
}
|
2010-03-05 21:19:52 +00:00
|
|
|
|
2010-08-21 03:09:18 +00:00
|
|
|
if(!depth) {
|
|
|
|
lastLeaf = lc->id;
|
|
|
|
pageid_t tmpid = -1;
|
|
|
|
recordid rid = { lc->id, PREV_LEAF, root_rec_size };
|
|
|
|
stasis_record_write(xid, lc, rid, (byte*)&tmpid);
|
|
|
|
rid.slot = NEXT_LEAF;
|
|
|
|
stasis_record_write(xid, lc, rid, (byte*)&tmpid);
|
|
|
|
}
|
2010-03-05 21:19:52 +00:00
|
|
|
|
2010-08-21 03:09:18 +00:00
|
|
|
stasis_page_lsn_write(xid, lc, internal_node_alloc->get_lsn(xid));
|
|
|
|
releasePage(lc);
|
|
|
|
} // lc is now out of scope.
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
// deallocate old entries, and update pointer on parent node.
|
|
|
|
// NOTE: stasis_record_free call goes to slottedFree in slotted.c
|
|
|
|
// this function only reduces the numslots when you call it
|
|
|
|
// with the last slot. so thats why i go backwards here.
|
2010-03-08 20:16:03 +00:00
|
|
|
DEBUG("slots %d (%d) keysize=%lld\n", (int)last_slot+1, (int)FIRST_SLOT+1, (long long int)keySize);
|
|
|
|
assert(numslots >= FIRST_SLOT+1);
|
2011-04-28 20:18:54 +00:00
|
|
|
|
|
|
|
writelock(p->rwlatch,0);
|
2010-03-08 20:16:03 +00:00
|
|
|
// Note that we leave the first slot in place.
|
|
|
|
for(int i = numslots-1; i>FIRST_SLOT; i--) {
|
2010-03-05 21:19:52 +00:00
|
|
|
recordid tmp_rec= {p->id, i, INVALID_SIZE};
|
|
|
|
stasis_record_free(xid, p, tmp_rec);
|
|
|
|
}
|
2010-03-08 20:16:03 +00:00
|
|
|
recordid pFirstSlot = stasis_record_last(xid, p);
|
|
|
|
assert(pFirstSlot.slot == FIRST_SLOT);
|
2010-03-05 21:19:52 +00:00
|
|
|
//TODO: could change with stasis_slotted_page_initialize(...);
|
|
|
|
// TODO: fsck?
|
|
|
|
|
|
|
|
// reinsert first.
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
indexnode_rec *nr
|
|
|
|
= (indexnode_rec*)stasis_record_write_begin(xid, p, pFirstSlot);
|
|
|
|
|
|
|
|
// don't overwrite key...
|
|
|
|
nr->ptr = child;
|
|
|
|
stasis_record_write_done(xid,p,pFirstSlot,(byte*)nr);
|
|
|
|
|
|
|
|
//update the depth info at the root
|
|
|
|
depth ++;
|
2010-03-08 23:58:13 +00:00
|
|
|
recordid depth_rid = { p->id, DEPTH, root_rec_size };
|
|
|
|
stasis_record_write(xid, p, depth_rid, (byte*)(&depth));
|
2011-04-28 20:18:54 +00:00
|
|
|
unlock(p->rwlatch);
|
2010-03-05 21:19:52 +00:00
|
|
|
assert(tree.page == p->id);
|
2010-03-09 23:48:42 +00:00
|
|
|
ret = appendInternalNode(xid, p, depth, key, keySize, val_page);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
assert(ret.size != INVALID_SLOT);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
} else {
|
|
|
|
DEBUG("Appended new internal node tree depth = %lld key = %s\n",
|
|
|
|
depth, datatuple::key_to_str(key).c_str());
|
|
|
|
}
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-09 22:18:55 +00:00
|
|
|
lastLeaf = ret.page;
|
|
|
|
DEBUG("lastleaf is %lld\n", lastLeaf);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
} else {
|
2010-02-19 00:52:21 +00:00
|
|
|
// write the new value to an existing page
|
2010-03-05 21:19:52 +00:00
|
|
|
DEBUG("Writing %s\t%d to existing page# %lld\n", datatuple::key_to_str(key).c_str(),
|
2010-03-09 22:18:55 +00:00
|
|
|
val_page, lastLeafPage->id);
|
|
|
|
stasis_record_alloc_done(xid, lastLeafPage, ret);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-09 22:18:55 +00:00
|
|
|
writeNodeRecord(xid, lastLeafPage, ret, key, keySize, val_page);
|
2010-08-21 03:09:18 +00:00
|
|
|
unlock(lastLeafPage->rwlatch);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-09 22:18:55 +00:00
|
|
|
if(lastLeafPage->id != p->id) {
|
|
|
|
assert(lastLeaf != tree.page);
|
|
|
|
releasePage(lastLeafPage);
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-09 23:48:42 +00:00
|
|
|
stasis_page_lsn_write(xid, p, internal_node_alloc->get_lsn(xid));
|
2010-03-08 23:58:13 +00:00
|
|
|
|
2010-02-19 00:52:21 +00:00
|
|
|
releasePage(p);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
diskTreeComponent::internalNodes::internalNodes(int xid, pageid_t internal_region_size, pageid_t datapage_region_size, pageid_t datapage_size)
|
|
|
|
: lastLeaf(-1),
|
|
|
|
internal_node_alloc(new RegionAllocator(xid, internal_region_size)),
|
|
|
|
datapage_alloc(new RegionAllocator(xid, datapage_region_size))
|
|
|
|
{ create(xid); }
|
|
|
|
|
|
|
|
diskTreeComponent::internalNodes::internalNodes(int xid, recordid root, recordid internal_node_state, recordid datapage_state)
|
|
|
|
: lastLeaf(-1),
|
|
|
|
root_rec(root),
|
|
|
|
internal_node_alloc(new RegionAllocator(xid, internal_node_state)),
|
|
|
|
datapage_alloc(new RegionAllocator(xid, datapage_state))
|
|
|
|
{ }
|
|
|
|
|
2010-08-05 17:39:31 +00:00
|
|
|
diskTreeComponent::internalNodes::~internalNodes() {
|
|
|
|
delete internal_node_alloc;
|
|
|
|
delete datapage_alloc;
|
|
|
|
}
|
2010-03-17 21:51:26 +00:00
|
|
|
|
2010-02-19 00:52:21 +00:00
|
|
|
/* adding pages:
|
|
|
|
|
|
|
|
1) Try to append value to lsmTreeState->lastLeaf
|
|
|
|
|
|
|
|
2) If that fails, traverses down the root of the tree, split pages while
|
|
|
|
traversing back up.
|
|
|
|
|
|
|
|
3) Split is done by adding new page at end of row (no key
|
|
|
|
redistribution), except at the root, where root contents are
|
|
|
|
pushed into the first page of the next row, and a new path from root to
|
|
|
|
leaf is created starting with the root's immediate second child.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2010-03-09 19:47:12 +00:00
|
|
|
recordid diskTreeComponent::internalNodes::appendInternalNode(int xid, Page *p,
|
2010-02-19 00:52:21 +00:00
|
|
|
int64_t depth,
|
|
|
|
const byte *key, size_t key_len,
|
2010-03-09 23:48:42 +00:00
|
|
|
pageid_t val_page) {
|
2010-03-05 21:19:52 +00:00
|
|
|
|
2010-03-08 20:16:03 +00:00
|
|
|
assert(p->pageType == SLOTTED_PAGE);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
recordid ret;
|
|
|
|
|
|
|
|
if(!depth) {
|
|
|
|
// leaf node.
|
2010-08-21 03:09:18 +00:00
|
|
|
writelock(p->rwlatch, 0);
|
2010-03-05 21:19:52 +00:00
|
|
|
ret = stasis_record_alloc_begin(xid, p, sizeof(indexnode_rec)+key_len);
|
|
|
|
if(ret.size != INVALID_SLOT) {
|
|
|
|
stasis_record_alloc_done(xid, p, ret);
|
|
|
|
writeNodeRecord(xid,p,ret,key,key_len,val_page);
|
2011-03-25 20:05:49 +00:00
|
|
|
stasis_page_lsn_write(xid, p, internal_node_alloc->get_lsn(xid)); // XXX remove this (writeNodeRecord calls it for us)
|
2010-03-05 21:19:52 +00:00
|
|
|
}
|
2010-08-21 03:09:18 +00:00
|
|
|
unlock(p->rwlatch);
|
2010-03-05 21:19:52 +00:00
|
|
|
} else {
|
2010-03-08 20:16:03 +00:00
|
|
|
|
2010-02-19 00:52:21 +00:00
|
|
|
// recurse
|
2010-03-08 23:58:13 +00:00
|
|
|
recordid last_rid = stasis_record_last(xid, p);
|
|
|
|
|
|
|
|
assert(last_rid.slot >= FIRST_SLOT); // there should be no empty nodes
|
|
|
|
const indexnode_rec *nr = (const indexnode_rec*)stasis_record_read_begin(xid, p, last_rid);
|
2010-03-05 21:19:52 +00:00
|
|
|
|
|
|
|
pageid_t child_id = nr->ptr;
|
2010-03-08 23:58:13 +00:00
|
|
|
stasis_record_read_done(xid, p, last_rid, (const byte*)nr);
|
2010-03-05 21:19:52 +00:00
|
|
|
nr = 0;
|
|
|
|
{
|
|
|
|
Page *child_page = loadPage(xid, child_id);
|
|
|
|
ret = appendInternalNode(xid, child_page, depth-1, key, key_len,
|
2010-03-09 23:48:42 +00:00
|
|
|
val_page);
|
2010-03-05 21:19:52 +00:00
|
|
|
releasePage(child_page);
|
|
|
|
}
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
if(ret.size == INVALID_SLOT) { // subtree is full; split
|
|
|
|
ret = stasis_record_alloc_begin(xid, p, sizeof(indexnode_rec)+key_len);
|
|
|
|
DEBUG("keylen %d\tnumslots %d for page id %lld ret.size %lld prv rec len %d\n",
|
|
|
|
key_len,
|
2010-03-08 20:16:03 +00:00
|
|
|
stasis_record_last(xid, p).slot+1,
|
2010-03-05 21:19:52 +00:00
|
|
|
p->id,
|
|
|
|
ret.size,
|
|
|
|
readRecordLength(xid, p, slot));
|
|
|
|
if(ret.size != INVALID_SLOT) {
|
2010-08-21 03:09:18 +00:00
|
|
|
writelock(p->rwlatch, 0); // XXX we hold this longer than necessary. push latching into buildPathToLeaf().
|
2010-03-05 21:19:52 +00:00
|
|
|
stasis_record_alloc_done(xid, p, ret);
|
2010-03-09 23:48:42 +00:00
|
|
|
ret = buildPathToLeaf(xid, ret, p, depth, key, key_len, val_page);
|
2010-08-21 03:09:18 +00:00
|
|
|
unlock(p->rwlatch);
|
2010-03-05 21:19:52 +00:00
|
|
|
DEBUG("split tree rooted at %lld, wrote value to {%d %d %lld}\n",
|
|
|
|
p->id, ret.page, ret.slot, ret.size);
|
2010-02-19 00:52:21 +00:00
|
|
|
} else {
|
2010-03-05 21:19:52 +00:00
|
|
|
// ret is NULLRID; this is the root of a full tree. Return
|
|
|
|
// NULLRID to the caller.
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
2010-03-05 21:19:52 +00:00
|
|
|
} else {
|
|
|
|
// we inserted the value in to a subtree rooted here.
|
|
|
|
}
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
2010-03-05 21:19:52 +00:00
|
|
|
return ret;
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
|
|
|
|
2010-03-09 19:47:12 +00:00
|
|
|
recordid diskTreeComponent::internalNodes::buildPathToLeaf(int xid, recordid root, Page *root_p,
|
2010-02-19 00:52:21 +00:00
|
|
|
int64_t depth, const byte *key, size_t key_len,
|
2010-03-09 23:48:42 +00:00
|
|
|
pageid_t val_page) {
|
2010-02-19 00:52:21 +00:00
|
|
|
|
|
|
|
// root is the recordid on the root page that should point to the
|
|
|
|
// new subtree.
|
|
|
|
assert(depth);
|
|
|
|
DEBUG("buildPathToLeaf(depth=%lld) (lastleaf=%lld) called\n",depth, lastLeaf);
|
|
|
|
|
2010-03-09 23:17:03 +00:00
|
|
|
pageid_t child = internal_node_alloc->alloc_extent(xid, 1);
|
2010-02-19 00:52:21 +00:00
|
|
|
DEBUG("new child = %lld internal? %lld\n", child, depth-1);
|
|
|
|
|
|
|
|
Page *child_p = loadPage(xid, child);
|
|
|
|
initializeNodePage(xid, child_p);
|
|
|
|
|
|
|
|
recordid ret;
|
|
|
|
|
|
|
|
if(depth-1) {
|
|
|
|
// recurse: the page we just allocated is not a leaf.
|
|
|
|
recordid child_rec = stasis_record_alloc_begin(xid, child_p, sizeof(indexnode_rec)+key_len);
|
|
|
|
assert(child_rec.size != INVALID_SLOT);
|
|
|
|
stasis_record_alloc_done(xid, child_p, child_rec);
|
|
|
|
|
|
|
|
ret = buildPathToLeaf(xid, child_rec, child_p, depth-1, key, key_len,
|
2010-03-09 23:48:42 +00:00
|
|
|
val_page);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
|
|
|
releasePage(child_p);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// set leaf
|
|
|
|
|
2010-03-08 23:58:13 +00:00
|
|
|
// backward link. records were alloced by page initialization
|
|
|
|
recordid prev_leaf_rid = { child_p->id, PREV_LEAF, root_rec_size };
|
|
|
|
stasis_record_write(xid, child_p, prev_leaf_rid, (byte*)&lastLeaf);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
|
|
|
// forward link (initialize to -1)
|
2010-03-05 21:19:52 +00:00
|
|
|
pageid_t tmp_pid = -1;
|
2010-03-08 23:58:13 +00:00
|
|
|
recordid next_leaf_rid = { child_p->id, NEXT_LEAF, root_rec_size };
|
|
|
|
stasis_record_write(xid, child_p, next_leaf_rid, (byte*)&tmp_pid);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
recordid leaf_rec = stasis_record_alloc_begin(xid, child_p,
|
|
|
|
sizeof(indexnode_rec)+key_len);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
assert(leaf_rec.slot == FIRST_SLOT);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
stasis_record_alloc_done(xid, child_p, leaf_rec);
|
|
|
|
writeNodeRecord(xid,child_p,leaf_rec,key,key_len,val_page);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
ret = leaf_rec;
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-09 23:48:42 +00:00
|
|
|
stasis_page_lsn_write(xid, child_p, internal_node_alloc->get_lsn(xid));
|
2010-03-05 21:19:52 +00:00
|
|
|
releasePage(child_p);
|
2010-03-09 23:48:42 +00:00
|
|
|
if(lastLeaf != -1 && lastLeaf != root_rec.page) {
|
2010-03-05 21:19:52 +00:00
|
|
|
// install forward link in previous page
|
|
|
|
Page *lastLeafP = loadPage(xid, lastLeaf);
|
|
|
|
writelock(lastLeafP->rwlatch,0);
|
2010-03-08 23:58:13 +00:00
|
|
|
recordid last_next_leaf_rid = {lastLeaf, NEXT_LEAF, root_rec_size };
|
|
|
|
stasis_record_write(xid,lastLeafP,last_next_leaf_rid,(byte*)&child);
|
2010-03-09 23:48:42 +00:00
|
|
|
stasis_page_lsn_write(xid, lastLeafP, internal_node_alloc->get_lsn(xid));
|
2010-03-05 21:19:52 +00:00
|
|
|
unlock(lastLeafP->rwlatch);
|
|
|
|
releasePage(lastLeafP);
|
|
|
|
}
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
DEBUG("%lld <-> %lld\n", lastLeaf, child);
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
|
|
|
|
2010-07-14 22:42:26 +00:00
|
|
|
// Crucially, this happens *after* the recursion. Therefore, we can query the
|
|
|
|
// tree with impunity while the leaf is being built and don't have to worry
|
|
|
|
// about dangling pointers to pages that are in the process of being allocated.
|
2010-08-21 03:09:18 +00:00
|
|
|
|
|
|
|
// XXX set bool on recursive call, and only grab the write latch at the first level of recursion.
|
2010-02-19 00:52:21 +00:00
|
|
|
writeNodeRecord(xid, root_p, root, key, key_len, child);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Traverse from the root of the page to the right most leaf (the one
|
|
|
|
* with the higest base key value).
|
|
|
|
**/
|
2010-03-09 19:47:12 +00:00
|
|
|
pageid_t diskTreeComponent::internalNodes::findLastLeaf(int xid, Page *root, int64_t depth) {
|
2010-03-05 21:19:52 +00:00
|
|
|
if(!depth) {
|
|
|
|
DEBUG("Found last leaf = %lld\n", root->id);
|
|
|
|
return root->id;
|
|
|
|
} else {
|
2010-08-21 03:09:18 +00:00
|
|
|
readlock(root->rwlatch,0);
|
2010-03-08 23:58:13 +00:00
|
|
|
recordid last_rid = stasis_record_last(xid, root);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-08-21 03:09:18 +00:00
|
|
|
const indexnode_rec * nr = (const indexnode_rec*)stasis_record_read_begin(xid, root, last_rid);
|
|
|
|
pageid_t ptr = nr->ptr;
|
2010-03-08 23:58:13 +00:00
|
|
|
stasis_record_read_done(xid, root, last_rid, (const byte*)nr);
|
2010-08-21 03:09:18 +00:00
|
|
|
unlock(root->rwlatch);
|
2010-03-08 23:58:13 +00:00
|
|
|
|
2010-08-21 03:09:18 +00:00
|
|
|
Page *p = loadPage(xid, ptr);
|
2010-03-08 23:58:13 +00:00
|
|
|
pageid_t ret = findLastLeaf(xid,p,depth-1);
|
2010-03-05 21:19:52 +00:00
|
|
|
releasePage(p);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
return ret;
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Traverse from the root of the tree to the left most (lowest valued
|
|
|
|
* key) leaf.
|
|
|
|
*/
|
2010-03-09 19:47:12 +00:00
|
|
|
pageid_t diskTreeComponent::internalNodes::findFirstLeaf(int xid, Page *root, int64_t depth) {
|
2010-03-05 21:19:52 +00:00
|
|
|
|
|
|
|
if(!depth) { //if depth is 0, then returns the id of the page
|
|
|
|
return root->id;
|
|
|
|
} else {
|
2010-03-08 23:58:13 +00:00
|
|
|
recordid rid = {root->id, FIRST_SLOT, 0};
|
2010-08-21 03:09:18 +00:00
|
|
|
|
|
|
|
readlock(root->rwlatch,0);
|
2010-03-08 23:58:13 +00:00
|
|
|
const indexnode_rec *nr = (const indexnode_rec*)stasis_record_read_begin(xid, root, rid);
|
2010-08-21 03:09:18 +00:00
|
|
|
pageid_t ptr = nr->ptr;
|
|
|
|
unlock(root->rwlatch);
|
|
|
|
Page *p = loadPage(xid, ptr);
|
2010-03-05 21:19:52 +00:00
|
|
|
pageid_t ret = findFirstLeaf(xid,p,depth-1);
|
|
|
|
releasePage(p);
|
|
|
|
return ret;
|
|
|
|
}
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-09 22:18:55 +00:00
|
|
|
pageid_t diskTreeComponent::internalNodes::findPage(int xid, const byte *key, size_t keySize) {
|
2010-03-05 21:19:52 +00:00
|
|
|
|
2010-03-09 22:18:55 +00:00
|
|
|
Page *p = loadPage(xid, root_rec.page);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-08 23:58:13 +00:00
|
|
|
recordid depth_rid = {p->id, DEPTH, 0};
|
2010-08-21 03:09:18 +00:00
|
|
|
readlock(p->rwlatch,0);
|
|
|
|
const int64_t * depthp = (const int64_t*)stasis_record_read_begin(xid, p, depth_rid);
|
|
|
|
int64_t depth = *depthp;
|
|
|
|
stasis_record_read_done(xid, p, depth_rid, (const byte*)depthp);
|
|
|
|
unlock(p->rwlatch);
|
2010-03-08 23:58:13 +00:00
|
|
|
|
2010-08-21 03:09:18 +00:00
|
|
|
recordid rid = lookup(xid, p, depth, key, keySize);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
pageid_t ret = lookupLeafPageFromRid(xid,rid);
|
2010-02-19 00:52:21 +00:00
|
|
|
releasePage(p);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-03-09 19:47:12 +00:00
|
|
|
pageid_t diskTreeComponent::internalNodes::lookupLeafPageFromRid(int xid, recordid rid) {
|
2010-03-05 21:19:52 +00:00
|
|
|
|
2010-02-19 00:52:21 +00:00
|
|
|
pageid_t pid = -1;
|
2010-03-05 21:19:52 +00:00
|
|
|
if(rid.page != NULLRID.page || rid.slot != NULLRID.slot) {
|
|
|
|
Page * p2 = loadPage(xid, rid.page);
|
|
|
|
readlock(p2->rwlatch,0);
|
2010-03-08 23:58:13 +00:00
|
|
|
const indexnode_rec * nr = (const indexnode_rec*)stasis_record_read_begin(xid, p2, rid);
|
|
|
|
pid = nr->ptr;
|
|
|
|
stasis_record_read_done(xid, p2, rid, (const byte*)nr);
|
2010-03-05 21:19:52 +00:00
|
|
|
unlock(p2->rwlatch);
|
|
|
|
releasePage(p2);
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2010-03-09 19:47:12 +00:00
|
|
|
recordid diskTreeComponent::internalNodes::lookup(int xid,
|
2010-02-19 00:52:21 +00:00
|
|
|
Page *node,
|
|
|
|
int64_t depth,
|
2010-03-05 21:19:52 +00:00
|
|
|
const byte *key, size_t keySize ) {
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
//DEBUG("lookup: pid %lld\t depth %lld\n", node->id, depth);
|
2010-08-21 03:09:18 +00:00
|
|
|
readlock(node->rwlatch,0);
|
2010-03-08 20:16:03 +00:00
|
|
|
slotid_t numslots = stasis_record_last(xid, node).slot + 1;
|
|
|
|
|
|
|
|
if(numslots == FIRST_SLOT) {
|
2010-08-21 03:09:18 +00:00
|
|
|
unlock(node->rwlatch);
|
2010-03-05 21:19:52 +00:00
|
|
|
return NULLRID;
|
|
|
|
}
|
2010-03-08 20:16:03 +00:00
|
|
|
assert(numslots > FIRST_SLOT);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
// don't need to compare w/ first item in tree, since we need to position ourselves at the the max tree value <= key.
|
|
|
|
// positioning at FIRST_SLOT puts us "before" the first value
|
|
|
|
int match = FIRST_SLOT; // (so match is now < key)
|
2010-03-08 23:58:13 +00:00
|
|
|
recordid rid;
|
|
|
|
rid.page = node->id;
|
|
|
|
rid.size = 0;
|
|
|
|
for(rid.slot = FIRST_SLOT+1; rid.slot < numslots; rid.slot++) {
|
|
|
|
rid.size = stasis_record_length_read(xid, node, rid);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-08 23:58:13 +00:00
|
|
|
const indexnode_rec *rec = (const indexnode_rec*)stasis_record_read_begin(xid,node,rid);
|
|
|
|
int cmpval = datatuple::compare((datatuple::key_t) (rec+1), rid.size-sizeof(*rec),
|
2010-03-05 21:19:52 +00:00
|
|
|
(datatuple::key_t) key, keySize);
|
2010-03-08 23:58:13 +00:00
|
|
|
stasis_record_read_done(xid,node,rid,(const byte*)rec);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
// key of current node is too big; there can be no matches under it.
|
|
|
|
if(cmpval>0) break;
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-08 23:58:13 +00:00
|
|
|
match = rid.slot; // only increment match after comparing with the current node.
|
2010-03-05 21:19:52 +00:00
|
|
|
}
|
2010-03-08 23:58:13 +00:00
|
|
|
rid.slot = match;
|
|
|
|
rid.size = 0;
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
if(depth) {
|
2010-03-08 23:58:13 +00:00
|
|
|
const indexnode_rec* nr = (const indexnode_rec*)stasis_record_read_begin(xid, node, rid);
|
|
|
|
pageid_t child_id = nr->ptr;
|
|
|
|
stasis_record_read_done(xid, node, rid, (const byte*)nr);
|
|
|
|
|
2010-08-21 03:09:18 +00:00
|
|
|
unlock(node->rwlatch);
|
2010-03-05 21:19:52 +00:00
|
|
|
Page* child_page = loadPage(xid, child_id);
|
|
|
|
recordid ret = lookup(xid,child_page,depth-1,key,keySize);
|
2010-08-21 03:09:18 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
releasePage(child_page);
|
|
|
|
return ret;
|
|
|
|
} else {
|
2010-08-21 03:09:18 +00:00
|
|
|
unlock(node->rwlatch);
|
2010-03-05 21:19:52 +00:00
|
|
|
recordid ret = {node->id, match, keySize};
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-09 19:47:12 +00:00
|
|
|
void diskTreeComponent::internalNodes::print_tree(int xid) {
|
2010-03-05 21:19:52 +00:00
|
|
|
Page *p = loadPage(xid, root_rec.page);
|
|
|
|
readlock(p->rwlatch,0);
|
2010-03-08 23:58:13 +00:00
|
|
|
recordid depth_rid = {p->id, DEPTH, 0};
|
|
|
|
const indexnode_rec *depth_nr = (const indexnode_rec*)stasis_record_read_begin(xid, p , depth_rid);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-08 23:58:13 +00:00
|
|
|
int64_t depth = depth_nr->ptr;
|
|
|
|
stasis_record_read_done(xid,p,depth_rid,(const byte*)depth_nr);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-08-21 03:09:18 +00:00
|
|
|
print_tree(xid, root_rec.page, depth); // XXX expensive latching!
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
unlock(p->rwlatch);
|
|
|
|
releasePage(p);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-03-09 19:47:12 +00:00
|
|
|
void diskTreeComponent::internalNodes::print_tree(int xid, pageid_t pid, int64_t depth) {
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
Page *node = loadPage(xid, pid);
|
|
|
|
readlock(node->rwlatch,0);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-08 20:16:03 +00:00
|
|
|
slotid_t numslots = stasis_record_last(xid,node).slot + 1;
|
|
|
|
|
|
|
|
printf("page_id:%lld\tnum_slots:%d\t\n", node->id, numslots);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-08 20:16:03 +00:00
|
|
|
if(numslots == FIRST_SLOT) {
|
2010-03-05 21:19:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-08 20:16:03 +00:00
|
|
|
assert(numslots > FIRST_SLOT);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-08 23:58:13 +00:00
|
|
|
recordid rid = { node->id, 0, 0 };
|
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
if(depth) {
|
|
|
|
printf("\tnot_leaf\n");
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-08 20:16:03 +00:00
|
|
|
for(int i = FIRST_SLOT; i < numslots; i++) {
|
2010-03-08 23:58:13 +00:00
|
|
|
rid.slot = i;
|
|
|
|
const indexnode_rec *nr = (const indexnode_rec*)stasis_record_read_begin(xid,node,rid);
|
2010-03-05 21:19:52 +00:00
|
|
|
printf("\tchild_page_id:%lld\tkey:%s\n", nr->ptr,
|
|
|
|
datatuple::key_to_str((byte*)(nr+1)).c_str());
|
2010-03-08 23:58:13 +00:00
|
|
|
stasis_record_read_done(xid, node, rid, (const byte*)nr);
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
|
|
|
|
2010-03-08 20:16:03 +00:00
|
|
|
for(int i = FIRST_SLOT; i < numslots; i++) {
|
2010-03-08 23:58:13 +00:00
|
|
|
rid.slot = i;
|
|
|
|
const indexnode_rec *nr = (const indexnode_rec*)stasis_record_read_begin(xid,node,rid);
|
2010-03-05 21:19:52 +00:00
|
|
|
print_tree(xid, nr->ptr, depth-1);
|
2010-03-08 23:58:13 +00:00
|
|
|
stasis_record_read_done(xid, node, rid, (const byte*)nr);
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
} else {
|
|
|
|
printf("\tis_leaf\t\n");
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-08 23:58:13 +00:00
|
|
|
rid.slot = FIRST_SLOT;
|
|
|
|
const indexnode_rec *nr = (const indexnode_rec*)stasis_record_read_begin(xid,node,rid);
|
2010-03-05 21:19:52 +00:00
|
|
|
printf("\tdata_page_id:%lld\tkey:%s\n", nr->ptr,
|
|
|
|
datatuple::key_to_str((byte*)(nr+1)).c_str());
|
2010-03-08 23:58:13 +00:00
|
|
|
stasis_record_read_done(xid, node, rid, (const byte*)nr);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
printf("\t...\n");
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-08 23:58:13 +00:00
|
|
|
rid.slot= numslots - 1;
|
|
|
|
nr = (const indexnode_rec*)stasis_record_read_begin(xid,node,rid);
|
2010-03-05 21:19:52 +00:00
|
|
|
printf("\tdata_page_id:%lld\tkey:%s\n", nr->ptr,
|
|
|
|
datatuple::key_to_str((byte*)(nr+1)).c_str());
|
2010-03-08 23:58:13 +00:00
|
|
|
stasis_record_read_done(xid, node, rid, (const byte*)nr);
|
|
|
|
}
|
2010-03-05 21:19:52 +00:00
|
|
|
unlock(node->rwlatch);
|
|
|
|
releasePage(node);
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////
|
2010-02-20 01:18:39 +00:00
|
|
|
//diskTreeComponentIterator implementation
|
2010-02-19 00:52:21 +00:00
|
|
|
/////////////////////////////////////////////////
|
|
|
|
|
2010-04-12 20:56:54 +00:00
|
|
|
diskTreeComponent::internalNodes::iterator::iterator(int xid, RegionAllocator* ro_alloc, recordid root) {
|
|
|
|
ro_alloc_ = ro_alloc;
|
2010-03-05 22:21:44 +00:00
|
|
|
if(root.page == 0 && root.slot == 0 && root.size == -1) abort();
|
2010-04-12 20:56:54 +00:00
|
|
|
p = ro_alloc_->load_page(xid,root.page);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-09 19:47:12 +00:00
|
|
|
DEBUG("ROOT_REC_SIZE %d\n", diskTreeComponent::internalNodes::root_rec_size);
|
|
|
|
recordid rid = {p->id, diskTreeComponent::internalNodes::DEPTH, diskTreeComponent::internalNodes::root_rec_size};
|
2010-08-21 03:09:18 +00:00
|
|
|
|
|
|
|
readlock(p->rwlatch, 0);
|
2010-03-08 23:58:13 +00:00
|
|
|
const indexnode_rec* nr = (const indexnode_rec*)stasis_record_read_begin(xid,p, rid);
|
|
|
|
|
|
|
|
int64_t depth = nr->ptr;
|
2011-04-28 20:18:54 +00:00
|
|
|
justOnePage = (depth == 0);
|
2010-03-05 21:19:52 +00:00
|
|
|
DEBUG("DEPTH = %lld\n", depth);
|
2010-03-08 23:58:13 +00:00
|
|
|
stasis_record_read_done(xid,p,rid,(const byte*)nr);
|
2011-04-28 20:18:54 +00:00
|
|
|
// NOTE: The root page is not append only. We need to hold onto
|
|
|
|
// this latch throughout the iteration to protect ourselves from
|
|
|
|
// root tree splits. For multi-level trees, this is not the case,
|
|
|
|
// as everything below the root is append-only, so we can release
|
|
|
|
// and reacquire the latches if need be.
|
|
|
|
if(!justOnePage) unlock(p->rwlatch);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-09 19:47:12 +00:00
|
|
|
pageid_t leafid = diskTreeComponent::internalNodes::findFirstLeaf(xid, p, depth);
|
2010-03-05 21:19:52 +00:00
|
|
|
if(leafid != root.page) {
|
|
|
|
|
|
|
|
releasePage(p);
|
2010-04-12 20:56:54 +00:00
|
|
|
p = ro_alloc_->load_page(xid,leafid);
|
2010-03-05 21:19:52 +00:00
|
|
|
assert(depth != 0);
|
|
|
|
} else {
|
|
|
|
assert(depth == 0);
|
|
|
|
}
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
{
|
2010-03-08 20:16:03 +00:00
|
|
|
// Position just before the first slot.
|
|
|
|
// The first call to next() will increment us to the first slot, or return NULL.
|
2010-03-09 19:47:12 +00:00
|
|
|
recordid rid = { p->id, diskTreeComponent::internalNodes::FIRST_SLOT-1, 0};
|
2010-03-08 20:16:03 +00:00
|
|
|
current = rid;
|
2010-03-05 21:19:52 +00:00
|
|
|
}
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 22:21:44 +00:00
|
|
|
DEBUG("keysize = %d, slot = %d\n", keySize, current.slot);
|
|
|
|
xid_ = xid;
|
|
|
|
done = false;
|
|
|
|
t = 0;
|
2011-04-28 20:18:54 +00:00
|
|
|
if(!justOnePage) readlock(p->rwlatch,0);
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
|
|
|
|
2010-04-12 20:56:54 +00:00
|
|
|
diskTreeComponent::internalNodes::iterator::iterator(int xid, RegionAllocator* ro_alloc, recordid root, const byte* key, len_t keylen) {
|
2010-03-05 22:21:44 +00:00
|
|
|
if(root.page == NULLRID.page && root.slot == NULLRID.slot) abort();
|
2010-04-12 20:56:54 +00:00
|
|
|
ro_alloc_ = ro_alloc;
|
|
|
|
p = ro_alloc_->load_page(xid,root.page);
|
2010-08-21 03:09:18 +00:00
|
|
|
|
2010-03-09 19:47:12 +00:00
|
|
|
recordid rid = {p->id, diskTreeComponent::internalNodes::DEPTH, diskTreeComponent::internalNodes::root_rec_size};
|
2010-03-05 20:34:42 +00:00
|
|
|
|
2010-08-21 03:09:18 +00:00
|
|
|
|
|
|
|
readlock(p->rwlatch,0);
|
|
|
|
const indexnode_rec* nr = (const indexnode_rec*)stasis_record_read_begin(xid,p,rid);
|
2010-03-08 23:58:13 +00:00
|
|
|
int64_t depth = nr->ptr;
|
2011-04-28 20:18:54 +00:00
|
|
|
justOnePage = (depth==0);
|
2010-03-08 23:58:13 +00:00
|
|
|
stasis_record_read_done(xid,p,rid,(const byte*)nr);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-09 19:47:12 +00:00
|
|
|
recordid lsm_entry_rid = diskTreeComponent::internalNodes::lookup(xid,p,depth,key,keylen);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
|
|
|
if(lsm_entry_rid.page == NULLRID.page && lsm_entry_rid.slot == NULLRID.slot) {
|
2011-04-28 20:18:54 +00:00
|
|
|
unlock(p->rwlatch);
|
2010-03-09 17:45:51 +00:00
|
|
|
releasePage(p);
|
|
|
|
p = NULL;
|
2010-03-05 22:21:44 +00:00
|
|
|
done = true;
|
|
|
|
} else {
|
2011-04-28 20:18:54 +00:00
|
|
|
|
|
|
|
if(!justOnePage) unlock(p->rwlatch);
|
|
|
|
|
2010-03-05 22:21:44 +00:00
|
|
|
assert(lsm_entry_rid.size != INVALID_SLOT);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 22:21:44 +00:00
|
|
|
if(root.page != lsm_entry_rid.page)
|
|
|
|
{
|
|
|
|
releasePage(p);
|
2010-04-12 20:56:54 +00:00
|
|
|
p = ro_alloc->load_page(xid,lsm_entry_rid.page);
|
2011-04-28 20:18:54 +00:00
|
|
|
assert(!justOnePage);
|
|
|
|
} else {
|
|
|
|
assert(justOnePage);
|
2010-03-05 22:21:44 +00:00
|
|
|
}
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 22:21:44 +00:00
|
|
|
done = false;
|
|
|
|
current.page = lsm_entry_rid.page;
|
|
|
|
current.slot = lsm_entry_rid.slot-1; // this is current rid, which is one less than the first thing next will return (so subtract 1)
|
|
|
|
current.size = lsm_entry_rid.size;
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 22:21:44 +00:00
|
|
|
xid_ = xid;
|
2010-02-25 01:29:32 +00:00
|
|
|
|
2010-03-05 22:21:44 +00:00
|
|
|
DEBUG("diskTreeComponentIterator: index root %lld index page %lld data page %lld key %s\n", root.page, current.page, rec->ptr, key);
|
|
|
|
DEBUG("entry = %s key = %s\n", (char*)(rec+1), (char*)key);
|
2010-08-21 03:09:18 +00:00
|
|
|
|
2011-04-28 20:18:54 +00:00
|
|
|
if(!justOnePage) readlock(p->rwlatch,0);
|
2010-03-05 22:21:44 +00:00
|
|
|
}
|
2010-03-24 20:30:35 +00:00
|
|
|
t = 0; // must be zero so free() doesn't croak.
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* move to the next page
|
|
|
|
**/
|
2010-03-09 19:47:12 +00:00
|
|
|
int diskTreeComponent::internalNodes::iterator::next()
|
2010-02-19 00:52:21 +00:00
|
|
|
{
|
2010-03-05 22:21:44 +00:00
|
|
|
if(done) return 0;
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 22:21:44 +00:00
|
|
|
current = stasis_record_next(xid_, p, current);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 22:21:44 +00:00
|
|
|
if(current.size == INVALID_SLOT) {
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-09 19:47:12 +00:00
|
|
|
recordid next_leaf_rid = {p->id, diskTreeComponent::internalNodes::NEXT_LEAF,0};
|
2010-03-08 23:58:13 +00:00
|
|
|
const indexnode_rec *nr = (const indexnode_rec*)stasis_record_read_begin(xid_, p, next_leaf_rid);
|
|
|
|
pageid_t next_rec = nr->ptr;
|
|
|
|
stasis_record_read_done(xid_,p,next_leaf_rid,(const byte*)nr);
|
|
|
|
|
2010-03-05 22:21:44 +00:00
|
|
|
unlock(p->rwlatch);
|
|
|
|
releasePage(p);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 22:21:44 +00:00
|
|
|
DEBUG("done with page %lld next = %lld\n", p->id, next_rec.ptr);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-08 23:58:13 +00:00
|
|
|
if(next_rec != -1 && ! justOnePage) {
|
2010-04-12 20:56:54 +00:00
|
|
|
p = ro_alloc_->load_page(xid_, next_rec);
|
2010-03-05 22:21:44 +00:00
|
|
|
readlock(p->rwlatch,0);
|
2010-03-08 23:58:13 +00:00
|
|
|
current.page = next_rec;
|
2010-03-05 22:21:44 +00:00
|
|
|
current.slot = 2;
|
|
|
|
current.size = stasis_record_length_read(xid_, p, current);
|
2010-03-05 21:19:52 +00:00
|
|
|
} else {
|
2010-03-05 22:21:44 +00:00
|
|
|
p = 0;
|
|
|
|
current.size = INVALID_SLOT;
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
}
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 22:21:44 +00:00
|
|
|
if(current.size != INVALID_SLOT) {
|
|
|
|
if(t != NULL) { free(t); t = NULL; }
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 22:21:44 +00:00
|
|
|
t = (indexnode_rec*)malloc(current.size);
|
2010-03-08 23:58:13 +00:00
|
|
|
const byte * buf = stasis_record_read_begin(xid_, p, current);
|
|
|
|
memcpy(t, buf, current.size);
|
|
|
|
stasis_record_read_done(xid_, p, current, buf);
|
2010-02-19 00:52:21 +00:00
|
|
|
|
2010-03-05 21:19:52 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
2010-03-05 22:21:44 +00:00
|
|
|
assert(!p);
|
|
|
|
if(t != NULL) { free(t); t = NULL; }
|
2010-03-05 21:19:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
|
|
|
|
2010-03-09 19:47:12 +00:00
|
|
|
void diskTreeComponent::internalNodes::iterator::close() {
|
2010-03-05 21:19:52 +00:00
|
|
|
|
2010-03-05 22:21:44 +00:00
|
|
|
if(p) {
|
|
|
|
unlock(p->rwlatch);
|
|
|
|
releasePage(p);
|
2010-03-24 20:30:35 +00:00
|
|
|
p = NULL;
|
|
|
|
}
|
|
|
|
if(t) {
|
|
|
|
free(t);
|
|
|
|
t = NULL;
|
2010-02-19 00:52:21 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-13 00:05:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// tree iterator implementation
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
void diskTreeComponent::iterator::init_iterators(datatuple * key1, datatuple * key2) {
|
2010-03-13 00:05:06 +00:00
|
|
|
assert(!key2); // unimplemented
|
|
|
|
if(tree_.size == INVALID_SIZE) {
|
|
|
|
lsmIterator_ = NULL;
|
|
|
|
} else {
|
|
|
|
if(key1) {
|
2011-04-22 22:54:49 +00:00
|
|
|
lsmIterator_ = new diskTreeComponent::internalNodes::iterator(-1, ro_alloc_, tree_, key1->strippedkey(), key1->strippedkeylen());
|
2010-03-13 00:05:06 +00:00
|
|
|
} else {
|
2010-04-12 20:56:54 +00:00
|
|
|
lsmIterator_ = new diskTreeComponent::internalNodes::iterator(-1, ro_alloc_, tree_);
|
2010-03-13 00:05:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-14 01:49:23 +00:00
|
|
|
diskTreeComponent::iterator::iterator(diskTreeComponent::internalNodes *tree, mergeManager * mgr, double target_progress_delta, bool * flushing) :
|
2010-04-12 20:56:54 +00:00
|
|
|
ro_alloc_(new RegionAllocator()),
|
2010-08-30 22:22:25 +00:00
|
|
|
tree_(tree ? tree->get_root_rec() : NULLRID),
|
2010-12-14 01:49:23 +00:00
|
|
|
mgr_(mgr),
|
2010-08-30 22:22:25 +00:00
|
|
|
target_progress_delta_(target_progress_delta),
|
|
|
|
flushing_(flushing)
|
2010-03-13 00:05:06 +00:00
|
|
|
{
|
|
|
|
init_iterators(NULL, NULL);
|
|
|
|
init_helper(NULL);
|
|
|
|
}
|
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
diskTreeComponent::iterator::iterator(diskTreeComponent::internalNodes *tree, datatuple* key) :
|
2010-04-12 20:56:54 +00:00
|
|
|
ro_alloc_(new RegionAllocator()),
|
2010-08-30 22:22:25 +00:00
|
|
|
tree_(tree ? tree->get_root_rec() : NULLRID),
|
2010-12-14 01:49:23 +00:00
|
|
|
mgr_(NULL),
|
2010-08-30 22:22:25 +00:00
|
|
|
target_progress_delta_(0.0),
|
|
|
|
flushing_(NULL)
|
2010-03-13 00:05:06 +00:00
|
|
|
{
|
|
|
|
init_iterators(key,NULL);
|
|
|
|
init_helper(key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-04-12 20:56:54 +00:00
|
|
|
diskTreeComponent::iterator::~iterator() {
|
|
|
|
if(lsmIterator_) {
|
|
|
|
lsmIterator_->close();
|
|
|
|
delete lsmIterator_;
|
|
|
|
}
|
2010-03-13 00:05:06 +00:00
|
|
|
|
2010-04-12 20:56:54 +00:00
|
|
|
delete curr_page;
|
|
|
|
curr_page = 0;
|
2010-03-13 00:05:06 +00:00
|
|
|
|
2010-04-12 20:56:54 +00:00
|
|
|
delete ro_alloc_;
|
2010-03-13 00:05:06 +00:00
|
|
|
}
|
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
void diskTreeComponent::iterator::init_helper(datatuple* key1)
|
2010-03-13 00:05:06 +00:00
|
|
|
{
|
|
|
|
if(!lsmIterator_)
|
|
|
|
{
|
|
|
|
DEBUG("treeIterator:\t__error__ init_helper():\tnull lsmIterator_");
|
|
|
|
curr_page = 0;
|
|
|
|
dp_itr = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(lsmIterator_->next() == 0)
|
|
|
|
{
|
|
|
|
DEBUG("diskTreeIterator:\t__error__ init_helper():\tlogtreeIteratr::next returned 0." );
|
|
|
|
curr_page = 0;
|
|
|
|
dp_itr = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pageid_t * pid_tmp;
|
|
|
|
pageid_t ** hack = &pid_tmp;
|
|
|
|
lsmIterator_->value((byte**)hack);
|
|
|
|
|
|
|
|
curr_pageid = *pid_tmp;
|
2011-06-09 00:07:42 +00:00
|
|
|
curr_page = new DataPage(-1, ro_alloc_, curr_pageid);
|
2010-03-13 00:05:06 +00:00
|
|
|
|
|
|
|
DEBUG("opening datapage iterator %lld at key %s\n.", curr_pageid, key1 ? (char*)key1->key() : "NULL");
|
|
|
|
dp_itr = new DPITR_T(curr_page, key1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-17 21:51:26 +00:00
|
|
|
datatuple * diskTreeComponent::iterator::next_callerFrees()
|
2010-03-13 00:05:06 +00:00
|
|
|
{
|
|
|
|
if(!this->lsmIterator_) { return NULL; }
|
|
|
|
|
|
|
|
if(dp_itr == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
datatuple* readTuple = dp_itr->getnext();
|
|
|
|
|
|
|
|
|
|
|
|
if(!readTuple)
|
|
|
|
{
|
|
|
|
delete dp_itr;
|
|
|
|
dp_itr = 0;
|
|
|
|
delete curr_page;
|
|
|
|
curr_page = 0;
|
|
|
|
|
|
|
|
if(lsmIterator_->next())
|
|
|
|
{
|
|
|
|
pageid_t *pid_tmp;
|
|
|
|
|
|
|
|
pageid_t **hack = &pid_tmp;
|
|
|
|
size_t ret = lsmIterator_->value((byte**)hack);
|
|
|
|
assert(ret == sizeof(pageid_t));
|
|
|
|
curr_pageid = *pid_tmp;
|
2011-06-09 00:07:42 +00:00
|
|
|
curr_page = new DataPage(-1, ro_alloc_, curr_pageid);
|
2010-03-13 00:05:06 +00:00
|
|
|
DEBUG("opening datapage iterator %lld at beginning\n.", curr_pageid);
|
|
|
|
dp_itr = new DPITR_T(curr_page->begin());
|
|
|
|
|
|
|
|
|
|
|
|
readTuple = dp_itr->getnext();
|
|
|
|
assert(readTuple);
|
|
|
|
}
|
|
|
|
// else readTuple is null. We're done.
|
|
|
|
}
|
2010-08-30 22:22:25 +00:00
|
|
|
|
2010-12-14 01:49:23 +00:00
|
|
|
if(readTuple && mgr_) {
|
|
|
|
// c1_c2_progress_delta() is c1's out progress - c2's in progress. We want to stop processing c2 if we are too far ahead (ie; c2 >> c1; delta << 0).
|
|
|
|
while(mgr_->c1_c2_progress_delta() < -target_progress_delta_ && ((!flushing_) || (! *flushing_))) { // TODO: how to pick this threshold?
|
|
|
|
DEBUG("Input is too far behind. Delta is %f\n", mgr_->c1_c2_progress_delta());
|
2010-08-30 22:22:25 +00:00
|
|
|
struct timespec ts;
|
2010-12-14 01:49:23 +00:00
|
|
|
mergeManager::double_to_ts(&ts, 0.01);
|
2010-08-30 22:22:25 +00:00
|
|
|
nanosleep(&ts, 0);
|
2010-12-14 01:49:23 +00:00
|
|
|
mgr_->update_progress(mgr_->get_merge_stats(1), 0);
|
2010-08-30 22:22:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-13 00:05:06 +00:00
|
|
|
return readTuple;
|
|
|
|
}
|
|
|
|
|