2007-08-20 16:53:16 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2007-08-14 01:17:31 +00:00
|
|
|
#include <stasis/operations/lsmTree.h>
|
|
|
|
#include <stasis/constants.h>
|
|
|
|
// XXX including fixed.h breaks page api encapsulation; we need a "last slot"
|
|
|
|
// call.
|
|
|
|
#include "../page/fixed.h"
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
static lsm_comparator_t comparators[MAX_LSM_COMPARATORS];
|
2007-11-01 20:09:55 +00:00
|
|
|
static lsm_page_initializer_t initializers[MAX_LSM_PAGE_INITIALIZERS];
|
|
|
|
|
|
|
|
TlsmRegionAllocConf_t LSM_REGION_ALLOC_STATIC_INITIALIZER =
|
2007-11-11 23:22:21 +00:00
|
|
|
{ {0,0,-1}, 0, -1, -1, 1000 };
|
2007-11-01 20:09:55 +00:00
|
|
|
|
|
|
|
pageid_t TlsmRegionAlloc(int xid, void *conf) {
|
2007-11-11 23:22:21 +00:00
|
|
|
TlsmRegionAllocConf_t* a = (TlsmRegionAllocConf_t*)conf;
|
2007-11-01 20:09:55 +00:00
|
|
|
if(a->nextPage == a->endOfRegion) {
|
2007-11-11 23:22:21 +00:00
|
|
|
if(a->regionList.size == -1) {
|
|
|
|
a->regionList = TarrayListAlloc(xid, 1, 4, sizeof(pageid_t));
|
|
|
|
a->regionCount = 0;
|
|
|
|
}
|
|
|
|
TarrayListExtend(xid,a->regionList,1);
|
|
|
|
a->regionList.slot = a->regionCount;
|
|
|
|
DEBUG("region lst slot %lld\n",a->regionList.slot);
|
|
|
|
a->regionCount++;
|
|
|
|
DEBUG("region count %lld\n",a->regionCount);
|
2007-11-01 20:09:55 +00:00
|
|
|
a->nextPage = TregionAlloc(xid, a->regionSize,0);
|
2007-11-11 23:22:21 +00:00
|
|
|
DEBUG("next page %lld\n",a->nextPage);
|
2007-11-01 20:09:55 +00:00
|
|
|
a->endOfRegion = a->nextPage + a->regionSize;
|
2007-11-11 23:22:21 +00:00
|
|
|
Tset(xid,a->regionList,&a->nextPage);
|
|
|
|
DEBUG("next page %lld\n",a->nextPage);
|
2007-11-01 20:09:55 +00:00
|
|
|
}
|
2007-11-11 23:22:21 +00:00
|
|
|
DEBUG("%lld ?= %lld\n", a->nextPage,a->endOfRegion);
|
2007-11-01 20:09:55 +00:00
|
|
|
pageid_t ret = a->nextPage;
|
2007-11-11 23:22:21 +00:00
|
|
|
DEBUG("ret %lld\n",ret);
|
2007-11-01 20:09:55 +00:00
|
|
|
(a->nextPage)++;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-11-12 00:49:00 +00:00
|
|
|
void TlsmRegionForceRid(int xid, void *conf) {
|
|
|
|
recordid rid = *(recordid*)conf;
|
|
|
|
TlsmRegionAllocConf_t a;
|
|
|
|
Tread(xid,rid,&a);
|
|
|
|
// TlsmRegionAllocConf_t* a = (TlsmRegionAllocConf_t*)conf;
|
|
|
|
for(int i = 0; i < a.regionCount; i++) {
|
|
|
|
a.regionList.slot = i;
|
|
|
|
pageid_t pid;
|
|
|
|
Tread(xid,a.regionList,&pid);
|
|
|
|
dirtyPages_flushRange(pid, pid+a.regionSize);
|
|
|
|
// TregionDealloc(xid,pid);
|
|
|
|
}
|
|
|
|
}
|
2007-11-11 23:22:21 +00:00
|
|
|
void TlsmRegionDeallocRid(int xid, void *conf) {
|
|
|
|
recordid rid = *(recordid*)conf;
|
|
|
|
TlsmRegionAllocConf_t a;
|
|
|
|
Tread(xid,rid,&a);
|
|
|
|
// TlsmRegionAllocConf_t* a = (TlsmRegionAllocConf_t*)conf;
|
|
|
|
for(int i = 0; i < a.regionCount; i++) {
|
|
|
|
a.regionList.slot = i;
|
|
|
|
pageid_t pid;
|
|
|
|
Tread(xid,a.regionList,&pid);
|
|
|
|
TregionDealloc(xid,pid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-01 20:09:55 +00:00
|
|
|
pageid_t TlsmRegionAllocRid(int xid, void * ridp) {
|
|
|
|
recordid rid = *(recordid*)ridp;
|
|
|
|
TlsmRegionAllocConf_t conf;
|
|
|
|
Tread(xid,rid,&conf);
|
|
|
|
pageid_t ret = TlsmRegionAlloc(xid,&conf);
|
|
|
|
// XXX get rid of Tset by storing next page in memory, and losing it
|
|
|
|
// on crash.
|
|
|
|
Tset(xid,rid,&conf);
|
|
|
|
return ret;
|
|
|
|
}
|
2007-08-20 16:53:16 +00:00
|
|
|
|
|
|
|
void lsmTreeRegisterComparator(int id, lsm_comparator_t i) {
|
|
|
|
// XXX need to de-init this somewhere... assert(!comparators[id]);
|
|
|
|
comparators[id] = i;
|
|
|
|
}
|
2007-11-01 20:09:55 +00:00
|
|
|
void lsmTreeRegisterPageInitializer(int id, lsm_page_initializer_t i) {
|
|
|
|
initializers[id] = i;
|
|
|
|
}
|
2007-08-20 16:53:16 +00:00
|
|
|
|
|
|
|
#define HEADER_SIZE (2 * sizeof(lsmTreeNodeRecord))
|
|
|
|
|
|
|
|
// These two constants only apply to the root page.
|
|
|
|
#define DEPTH 0
|
|
|
|
#define COMPARATOR 1
|
|
|
|
|
|
|
|
// These two apply to all other pages.
|
|
|
|
#define PREV_LEAF 0
|
|
|
|
#define NEXT_LEAF 1
|
|
|
|
|
|
|
|
// This one applies to all pages.
|
|
|
|
#define FIRST_SLOT 2
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
The implementation strategy used here is a bit of an experiment.
|
|
|
|
|
|
|
|
LSM tree is updated using a FORCE/STEAL strategy. In order to do
|
|
|
|
this efficiently, its root node overrides fixedPage, adding
|
|
|
|
pageLoaded and pageFlushed callbacks. Those callbacks maintain an
|
|
|
|
impl pointer, which tracks dirty pages, a mutex, and other
|
|
|
|
information on behalf of the tree. (Note that the dirtyPage list
|
|
|
|
must be stored somewhere in memory if the root is evicted with
|
|
|
|
outstanding dirty tree pages...)
|
|
|
|
|
|
|
|
Note that this has a particularly nice, general purpose property
|
|
|
|
that may be useful for other data structure implementations; by
|
|
|
|
using a mutex associated with the root of the data structure, we
|
|
|
|
can get rid of the static locks used by existing implementations.
|
|
|
|
|
|
|
|
@todo Need easy way for operations to store things in p->impl, even
|
|
|
|
if the underlying page implementation wants to store something
|
|
|
|
there too (second pointer?)
|
|
|
|
|
|
|
|
Page layout information for lsm trees:
|
|
|
|
|
|
|
|
root page layout
|
|
|
|
----------------
|
|
|
|
|
|
|
|
uses fixedPage (for now)
|
|
|
|
|
|
|
|
slot 0: the integer id of the comparator used by this tree.
|
|
|
|
slot 1: depth of tree.
|
|
|
|
|
|
|
|
the remainder of the slots contain lsmTreeNodeRecords
|
|
|
|
|
|
|
|
internal node page layout
|
|
|
|
-------------------------
|
|
|
|
uses fixedPage (for now)
|
|
|
|
|
|
|
|
slot 0: prev page
|
|
|
|
slot 1: next page
|
|
|
|
the remainder of the slots contain lsmTreeNodeRecords
|
|
|
|
|
|
|
|
leaf page layout
|
|
|
|
----------------
|
|
|
|
|
|
|
|
Defined by client.
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
*/
|
2007-08-14 01:17:31 +00:00
|
|
|
|
|
|
|
typedef struct lsmTreeState {
|
|
|
|
pageid_t lastLeaf;
|
|
|
|
} lsmTreeState;
|
|
|
|
|
|
|
|
/** Initialize a page for use as an internal node of the tree.
|
|
|
|
* lsmTree nodes are based on fixed.h. This function allocates a page
|
|
|
|
* that can hold fixed length records, and then sets up a tree node
|
2007-08-20 16:53:16 +00:00
|
|
|
* header in the first two lsmTreeNodeRecords on the page.
|
2007-08-14 01:17:31 +00:00
|
|
|
*/
|
2007-08-20 16:53:16 +00:00
|
|
|
static void initializeNodePage(int xid, Page *p, size_t keylen) {
|
2007-10-02 00:18:33 +00:00
|
|
|
stasis_fixed_initialize_page(p, sizeof(lsmTreeNodeRecord)+keylen, 0);
|
|
|
|
recordid reserved1 = stasis_record_alloc_begin(xid, p, sizeof(lsmTreeNodeRecord)+keylen);
|
|
|
|
stasis_record_alloc_done(xid, p, reserved1);
|
|
|
|
recordid reserved2 = stasis_record_alloc_begin(xid, p, sizeof(lsmTreeNodeRecord)+keylen);
|
|
|
|
stasis_record_alloc_done(xid, p, reserved2);
|
2007-08-14 01:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-08-20 16:53:16 +00:00
|
|
|
* A macro that hardcodes the page implementation to use fixed.h's
|
|
|
|
* page implementation.
|
2007-08-14 01:17:31 +00:00
|
|
|
*/
|
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
#define readNodeRecord(xid,p,slot,keylen) readNodeRecordFixed(xid,p,slot,keylen)
|
|
|
|
/**
|
|
|
|
* @see readNodeRecord
|
|
|
|
*/
|
|
|
|
#define writeNodeRecord(xid,p,slot,key,keylen,ptr) \
|
|
|
|
writeNodeRecordFixed(xid,p,slot,key,keylen,ptr)
|
2007-08-14 01:17:31 +00:00
|
|
|
/**
|
|
|
|
* @see readNodeRecord
|
|
|
|
*/
|
2007-08-20 16:53:16 +00:00
|
|
|
#define getKeySize(xid,p) getKeySizeFixed(xid,p)
|
|
|
|
|
|
|
|
/*
|
|
|
|
#define getKeySize(xid,p) getKeySizeVirtualMethods(xid,p)
|
|
|
|
#define readNodeRecord(xid,p,slot,keylen) \
|
|
|
|
readNodeRecordVirtualMethods(xid,p,slot,keylen)
|
|
|
|
#define writeNodeRecord(xid,p,slot,key,keylen,ptr) \
|
|
|
|
writeNodeRecordVirtualMethods(xid,p,slot,key,keylen,ptr)
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline size_t getKeySizeFixed(int xid, Page const *p) {
|
2007-10-02 00:18:33 +00:00
|
|
|
return (*recordsize_cptr(p)) - sizeof(lsmTreeNodeRecord);
|
2007-08-20 16:53:16 +00:00
|
|
|
}
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
static inline size_t getKeySizeVirtualMethods(int xid, Page *p) {
|
|
|
|
recordid rid = { p->id, 0, 0 };
|
2007-10-02 00:18:33 +00:00
|
|
|
return stasis_record_length_read(xid, p, rid) - sizeof(lsmTreeNodeRecord);
|
2007-08-20 16:53:16 +00:00
|
|
|
}
|
2007-08-14 01:17:31 +00:00
|
|
|
/**
|
|
|
|
* Read a record from the page node, assuming the nodes are fixed pages.
|
|
|
|
*/
|
2007-08-20 16:53:16 +00:00
|
|
|
static inline
|
|
|
|
const lsmTreeNodeRecord* readNodeRecordFixed(int xid, Page *const p, int slot,
|
|
|
|
int keylen) {
|
|
|
|
return (const lsmTreeNodeRecord*)fixed_record_ptr(p, slot);
|
2007-08-14 01:17:31 +00:00
|
|
|
}
|
|
|
|
/**
|
2007-08-20 16:53:16 +00:00
|
|
|
* Read a record from the page node, using stasis' general-purpose
|
|
|
|
* page access API.
|
2007-08-14 01:17:31 +00:00
|
|
|
*/
|
2007-08-20 16:53:16 +00:00
|
|
|
static inline
|
|
|
|
lsmTreeNodeRecord* readNodeRecordVirtualMethods(int xid, Page * p,
|
|
|
|
int slot, int keylen) {
|
|
|
|
lsmTreeNodeRecord *ret;
|
|
|
|
|
|
|
|
recordid rid = {p->id, slot, sizeof(lsmTreeNodeRecord)};
|
|
|
|
const lsmTreeNodeRecord *nr
|
2007-10-02 00:18:33 +00:00
|
|
|
= (const lsmTreeNodeRecord*)stasis_record_read_begin(xid,p,rid);
|
2007-08-20 16:53:16 +00:00
|
|
|
memcpy(ret, nr, sizeof(lsmTreeNodeRecord) + keylen);
|
2007-10-02 00:18:33 +00:00
|
|
|
stasis_record_read_done(xid,p,rid,(const byte*)nr);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
DEBUG("reading {%lld, %d, %d} = %d, %lld\n",
|
|
|
|
p->id, slot, sizeof(lsmTreeNodeRecord), ret.key, ret.ptr);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@see readNodeFixed
|
|
|
|
*/
|
2007-08-20 16:53:16 +00:00
|
|
|
static inline
|
|
|
|
void writeNodeRecordFixed(int xid, Page *p, int slot,
|
|
|
|
const byte *key, size_t keylen, pageid_t ptr) {
|
|
|
|
lsmTreeNodeRecord *nr = (lsmTreeNodeRecord*)fixed_record_ptr(p,slot);
|
2007-08-14 01:17:31 +00:00
|
|
|
nr->ptr = ptr;
|
2007-08-20 16:53:16 +00:00
|
|
|
memcpy(nr+1, key, keylen);
|
2007-10-02 00:18:33 +00:00
|
|
|
stasis_page_lsn_write(xid, p, 0); // XXX need real LSN?
|
2007-08-14 01:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@see readNodeVirtualMethods
|
|
|
|
*/
|
2007-08-20 16:53:16 +00:00
|
|
|
static inline
|
|
|
|
void writeNodeRecordVirtualMethods(int xid, Page *p, int slot,
|
|
|
|
const byte *key, size_t keylen,
|
|
|
|
pageid_t ptr) {
|
|
|
|
recordid rid = {p->id, slot, sizeof(lsmTreeNodeRecord)};
|
2007-10-02 00:18:33 +00:00
|
|
|
lsmTreeNodeRecord *target = (lsmTreeNodeRecord*)stasis_record_write_begin(xid,p,rid);
|
2007-08-20 16:53:16 +00:00
|
|
|
target->ptr = ptr;
|
|
|
|
memcpy(target+1,key,keylen);
|
|
|
|
|
2007-08-14 01:17:31 +00:00
|
|
|
DEBUG("Writing to record {%d %d %lld}\n", rid.page, rid.slot, rid.size);
|
2007-10-02 00:18:33 +00:00
|
|
|
stasis_record_write_done(xid,p,rid,(byte*)target);
|
|
|
|
stasis_page_lsn_write(xid, p, 0); // XXX need real LSN?
|
2007-08-14 01:17:31 +00:00
|
|
|
}
|
|
|
|
|
2007-11-01 20:09:55 +00:00
|
|
|
recordid TlsmCreate(int xid, int comparator,
|
|
|
|
lsm_page_allocator_t allocator, void *allocator_state,
|
|
|
|
int keySize) {
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
// can the pages hold at least two keys?
|
|
|
|
assert(HEADER_SIZE + 2 * (sizeof(lsmTreeNodeRecord) +keySize) <
|
2007-08-14 01:17:31 +00:00
|
|
|
USABLE_SIZE_OF_PAGE - 2 * sizeof(short));
|
|
|
|
|
2007-11-01 20:09:55 +00:00
|
|
|
pageid_t root = allocator(xid, allocator_state); //pageAllocatorConfig);
|
2007-08-20 16:53:16 +00:00
|
|
|
DEBUG("Root = %lld\n", root);
|
2007-08-14 01:17:31 +00:00
|
|
|
recordid ret = { root, 0, 0 };
|
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
Page *p = loadPage(xid, ret.page);
|
2007-08-14 01:17:31 +00:00
|
|
|
writelock(p->rwlatch,0);
|
2007-10-02 00:18:33 +00:00
|
|
|
stasis_fixed_initialize_page(p, sizeof(lsmTreeNodeRecord) + keySize, 0);
|
|
|
|
*stasis_page_type_ptr(p) = LSM_ROOT_PAGE;
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
lsmTreeState *state = malloc(sizeof(lsmTreeState));
|
|
|
|
state->lastLeaf = -1; /// XXX define something in constants.h?
|
2007-08-14 01:17:31 +00:00
|
|
|
|
|
|
|
p->impl = state;
|
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
recordid tmp
|
2007-10-02 00:18:33 +00:00
|
|
|
= stasis_record_alloc_begin(xid, p, sizeof(lsmTreeNodeRecord) + keySize);
|
|
|
|
stasis_record_alloc_done(xid,p,tmp);
|
2007-08-20 16:53:16 +00:00
|
|
|
|
|
|
|
assert(tmp.page == ret.page
|
|
|
|
&& tmp.slot == DEPTH
|
|
|
|
&& tmp.size == sizeof(lsmTreeNodeRecord) + keySize);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-10-02 00:18:33 +00:00
|
|
|
tmp = stasis_record_alloc_begin(xid, p, sizeof(lsmTreeNodeRecord) + keySize);
|
|
|
|
stasis_record_alloc_done(xid,p,tmp);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
assert(tmp.page == ret.page
|
|
|
|
&& tmp.slot == COMPARATOR
|
|
|
|
&& tmp.size == sizeof(lsmTreeNodeRecord) + keySize);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
byte *dummy = calloc(1,keySize);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
writeNodeRecord(xid, p, DEPTH, dummy, keySize, 0);
|
|
|
|
writeNodeRecord(xid, p, COMPARATOR, dummy, keySize, comparator);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-11-04 01:51:37 +00:00
|
|
|
free(dummy);
|
|
|
|
|
2007-08-14 01:17:31 +00:00
|
|
|
unlock(p->rwlatch);
|
|
|
|
releasePage(p);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
static recordid buildPathToLeaf(int xid, recordid root, Page *root_p,
|
|
|
|
int depth, const byte *key, size_t key_len,
|
2007-11-01 20:09:55 +00:00
|
|
|
pageid_t val_page, pageid_t lastLeaf,
|
|
|
|
lsm_page_allocator_t allocator,
|
|
|
|
void *allocator_state) {
|
2007-08-14 01:17:31 +00:00
|
|
|
// root is the recordid on the root page that should point to the
|
|
|
|
// new subtree.
|
|
|
|
assert(depth);
|
2007-08-20 16:53:16 +00:00
|
|
|
DEBUG("buildPathToLeaf(depth=%d) (lastleaf=%lld) called\n",depth, lastLeaf);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-11-01 20:09:55 +00:00
|
|
|
pageid_t child = allocator(xid,allocator_state);
|
2007-08-20 16:53:16 +00:00
|
|
|
DEBUG("new child = %lld internal? %d\n", child, depth-1);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
Page *child_p = loadPage(xid, child);
|
2007-08-14 01:17:31 +00:00
|
|
|
writelock(child_p->rwlatch,0);
|
2007-08-20 16:53:16 +00:00
|
|
|
initializeNodePage(xid, child_p, key_len);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
|
|
|
recordid ret;
|
|
|
|
|
|
|
|
if(depth-1) {
|
|
|
|
// recurse: the page we just allocated is not a leaf.
|
2007-10-02 00:18:33 +00:00
|
|
|
recordid child_rec = stasis_record_alloc_begin(xid, child_p, sizeof(lsmTreeNodeRecord)+key_len);
|
2007-08-14 01:17:31 +00:00
|
|
|
assert(child_rec.size != INVALID_SLOT);
|
2007-10-02 00:18:33 +00:00
|
|
|
stasis_record_alloc_done(xid, child_p, child_rec);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
|
|
|
ret = buildPathToLeaf(xid, child_rec, child_p, depth-1, key, key_len,
|
2007-11-01 20:09:55 +00:00
|
|
|
val_page,lastLeaf, allocator, allocator_state);
|
2007-08-20 16:53:16 +00:00
|
|
|
|
|
|
|
unlock(child_p->rwlatch);
|
|
|
|
releasePage(child_p);
|
|
|
|
|
2007-08-14 01:17:31 +00:00
|
|
|
} else {
|
|
|
|
// set leaf
|
2007-08-20 16:53:16 +00:00
|
|
|
|
|
|
|
byte *dummy = calloc(1, key_len);
|
|
|
|
|
|
|
|
// backward link.
|
|
|
|
writeNodeRecord(xid,child_p,PREV_LEAF,dummy,key_len,lastLeaf);
|
|
|
|
// forward link (initialize to -1)
|
|
|
|
writeNodeRecord(xid,child_p,NEXT_LEAF,dummy,key_len,-1);
|
|
|
|
|
2007-10-02 00:18:33 +00:00
|
|
|
recordid leaf_rec = stasis_record_alloc_begin(xid, child_p,
|
2007-08-20 16:53:16 +00:00
|
|
|
sizeof(lsmTreeNodeRecord)+key_len);
|
|
|
|
|
|
|
|
assert(leaf_rec.slot == FIRST_SLOT);
|
|
|
|
|
2007-10-02 00:18:33 +00:00
|
|
|
stasis_record_alloc_done(xid, child_p, leaf_rec);
|
2007-08-20 16:53:16 +00:00
|
|
|
writeNodeRecord(xid,child_p,leaf_rec.slot,key,key_len,val_page);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
|
|
|
ret = leaf_rec;
|
2007-08-20 16:53:16 +00:00
|
|
|
|
|
|
|
unlock(child_p->rwlatch);
|
|
|
|
releasePage(child_p);
|
|
|
|
if(lastLeaf != -1) {
|
|
|
|
// install forward link in previous page
|
|
|
|
Page *lastLeafP = loadPage(xid, lastLeaf);
|
|
|
|
writelock(lastLeafP->rwlatch,0);
|
|
|
|
writeNodeRecord(xid,lastLeafP,NEXT_LEAF,dummy,key_len,child);
|
|
|
|
unlock(lastLeafP->rwlatch);
|
|
|
|
releasePage(lastLeafP);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG("%lld <-> %lld\n", lastLeaf, child);
|
|
|
|
free(dummy);
|
2007-08-14 01:17:31 +00:00
|
|
|
}
|
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
writeNodeRecord(xid, root_p, root.slot, key, key_len, child);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
static recordid appendInternalNode(int xid, Page *p,
|
2007-08-14 01:17:31 +00:00
|
|
|
int depth,
|
|
|
|
const byte *key, size_t key_len,
|
2007-11-01 20:09:55 +00:00
|
|
|
pageid_t val_page, pageid_t lastLeaf,
|
|
|
|
lsm_page_allocator_t allocator,
|
|
|
|
void *allocator_state) {
|
2007-10-02 00:18:33 +00:00
|
|
|
assert(*stasis_page_type_ptr(p) == LSM_ROOT_PAGE ||
|
|
|
|
*stasis_page_type_ptr(p) == FIXED_PAGE);
|
2007-08-14 01:17:31 +00:00
|
|
|
if(!depth) {
|
|
|
|
// leaf node.
|
2007-10-02 00:18:33 +00:00
|
|
|
recordid ret = stasis_record_alloc_begin(xid, p, sizeof(lsmTreeNodeRecord)+key_len);
|
2007-08-14 01:17:31 +00:00
|
|
|
if(ret.size != INVALID_SLOT) {
|
2007-10-02 00:18:33 +00:00
|
|
|
stasis_record_alloc_done(xid, p, ret);
|
2007-08-20 16:53:16 +00:00
|
|
|
writeNodeRecord(xid,p,ret.slot,key,key_len,val_page);
|
2007-08-14 01:17:31 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
// recurse
|
|
|
|
int slot = *recordcount_ptr(p)-1;
|
2007-08-20 16:53:16 +00:00
|
|
|
assert(slot >= FIRST_SLOT); // there should be no empty nodes
|
|
|
|
const lsmTreeNodeRecord *nr = readNodeRecord(xid, p, slot, key_len);
|
|
|
|
pageid_t child_id = nr->ptr;
|
2007-08-14 01:17:31 +00:00
|
|
|
recordid ret;
|
|
|
|
{
|
2007-08-20 16:53:16 +00:00
|
|
|
Page *child_page = loadPage(xid, child_id);
|
2007-08-14 01:17:31 +00:00
|
|
|
writelock(child_page->rwlatch,0);
|
2007-08-20 16:53:16 +00:00
|
|
|
ret = appendInternalNode(xid, child_page, depth-1, key, key_len,
|
2007-11-01 20:09:55 +00:00
|
|
|
val_page, lastLeaf, allocator, allocator_state);
|
2007-08-20 16:53:16 +00:00
|
|
|
|
2007-08-14 01:17:31 +00:00
|
|
|
unlock(child_page->rwlatch);
|
|
|
|
releasePage(child_page);
|
|
|
|
}
|
|
|
|
if(ret.size == INVALID_SLOT) { // subtree is full; split
|
2007-10-02 00:18:33 +00:00
|
|
|
ret = stasis_record_alloc_begin(xid, p, sizeof(lsmTreeNodeRecord)+key_len);
|
2007-08-14 01:17:31 +00:00
|
|
|
if(ret.size != INVALID_SLOT) {
|
2007-10-02 00:18:33 +00:00
|
|
|
stasis_record_alloc_done(xid, p, ret);
|
2007-08-20 16:53:16 +00:00
|
|
|
ret = buildPathToLeaf(xid, ret, p, depth, key, key_len, val_page,
|
2007-11-01 20:09:55 +00:00
|
|
|
lastLeaf, allocator, allocator_state);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
DEBUG("split tree rooted at %lld, wrote value to {%d %d %lld}\n",
|
|
|
|
p->id, ret.page, ret.slot, ret.size);
|
2007-08-14 01:17:31 +00:00
|
|
|
} else {
|
2007-08-20 16:53:16 +00:00
|
|
|
// ret is NULLRID; this is the root of a full tree. Return
|
|
|
|
// NULLRID to the caller.
|
2007-08-14 01:17:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we inserted the value in to a subtree rooted here.
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Traverse from the root of the page to the right most leaf (the one
|
|
|
|
* with the higest base key value).
|
|
|
|
*/
|
2007-08-20 16:53:16 +00:00
|
|
|
static pageid_t findLastLeaf(int xid, Page *root, int depth) {
|
2007-08-14 01:17:31 +00:00
|
|
|
if(!depth) {
|
|
|
|
DEBUG("Found last leaf = %lld\n", root->id);
|
|
|
|
return root->id;
|
|
|
|
} else {
|
2007-08-20 16:53:16 +00:00
|
|
|
// passing zero as length is OK, as long as we don't try to access the key.
|
|
|
|
const lsmTreeNodeRecord *nr = readNodeRecord(xid, root,
|
|
|
|
(*recordcount_ptr(root))-1,0);
|
2007-08-14 01:17:31 +00:00
|
|
|
pageid_t ret;
|
2007-08-20 16:53:16 +00:00
|
|
|
|
|
|
|
Page *p = loadPage(xid, nr->ptr);
|
|
|
|
readlock(p->rwlatch,0);
|
|
|
|
ret = findLastLeaf(xid,p,depth-1);
|
|
|
|
unlock(p->rwlatch);
|
|
|
|
releasePage(p);
|
|
|
|
|
2007-08-14 01:17:31 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
/**
|
|
|
|
* Traverse from the root of the tree to the left most (lowest valued
|
|
|
|
* key) leaf.
|
|
|
|
*/
|
|
|
|
static pageid_t findFirstLeaf(int xid, Page *root, int depth) {
|
|
|
|
if(!depth) {
|
|
|
|
return root->id;
|
|
|
|
} else {
|
|
|
|
const lsmTreeNodeRecord *nr = readNodeRecord(xid,root,FIRST_SLOT,0);
|
|
|
|
Page *p = loadPage(xid, nr->ptr);
|
|
|
|
readlock(p->rwlatch,0);
|
|
|
|
pageid_t ret = findFirstLeaf(xid,p,depth-1);
|
|
|
|
unlock(p->rwlatch);
|
|
|
|
releasePage(p);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2007-08-14 01:17:31 +00:00
|
|
|
recordid TlsmAppendPage(int xid, recordid tree,
|
2007-08-20 16:53:16 +00:00
|
|
|
const byte *key,
|
2007-11-01 20:09:55 +00:00
|
|
|
lsm_page_allocator_t allocator, void *allocator_state,
|
2007-08-14 01:17:31 +00:00
|
|
|
long val_page) {
|
2007-08-20 16:53:16 +00:00
|
|
|
Page *p = loadPage(xid, tree.page);
|
2007-08-14 01:17:31 +00:00
|
|
|
writelock(p->rwlatch, 0);
|
2007-08-20 16:53:16 +00:00
|
|
|
lsmTreeState *s = p->impl;
|
|
|
|
|
|
|
|
size_t keySize = getKeySize(xid,p);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
|
|
|
tree.slot = 0;
|
2007-08-20 16:53:16 +00:00
|
|
|
tree.size = sizeof(lsmTreeNodeRecord)+keySize;
|
|
|
|
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
const lsmTreeNodeRecord *nr = readNodeRecord(xid, p, DEPTH, keySize);
|
|
|
|
int depth = nr->ptr;
|
2007-08-14 01:17:31 +00:00
|
|
|
|
|
|
|
if(s->lastLeaf == -1) {
|
|
|
|
s->lastLeaf = findLastLeaf(xid, p, depth);
|
|
|
|
}
|
2007-08-20 16:53:16 +00:00
|
|
|
|
|
|
|
Page *lastLeaf;
|
|
|
|
|
2007-08-14 01:17:31 +00:00
|
|
|
if(s->lastLeaf != tree.page) {
|
|
|
|
lastLeaf= loadPage(xid, s->lastLeaf);
|
2007-08-20 16:53:16 +00:00
|
|
|
writelock(lastLeaf->rwlatch, 0);
|
2007-08-14 01:17:31 +00:00
|
|
|
} else {
|
|
|
|
lastLeaf = p;
|
|
|
|
}
|
|
|
|
|
2007-10-02 00:18:33 +00:00
|
|
|
recordid ret = stasis_record_alloc_begin(xid, lastLeaf,
|
2007-08-20 16:53:16 +00:00
|
|
|
sizeof(lsmTreeNodeRecord)+keySize);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
|
|
|
if(ret.size == INVALID_SLOT) {
|
|
|
|
if(lastLeaf->id != p->id) {
|
2007-08-24 23:01:08 +00:00
|
|
|
assert(s->lastLeaf != tree.page);
|
2007-08-14 01:17:31 +00:00
|
|
|
unlock(lastLeaf->rwlatch);
|
|
|
|
releasePage(lastLeaf); // don't need that page anymore...
|
2007-08-24 23:01:08 +00:00
|
|
|
lastLeaf = 0;
|
2007-08-14 01:17:31 +00:00
|
|
|
}
|
|
|
|
// traverse down the root of the tree.
|
|
|
|
|
|
|
|
tree.slot = 0;
|
|
|
|
|
|
|
|
assert(tree.page == p->id);
|
2007-08-20 16:53:16 +00:00
|
|
|
ret = appendInternalNode(xid, p, depth, key, keySize, val_page,
|
2007-11-01 20:09:55 +00:00
|
|
|
s->lastLeaf == tree.page ? -1 : s->lastLeaf,
|
|
|
|
allocator, allocator_state);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
|
|
|
if(ret.size == INVALID_SLOT) {
|
|
|
|
DEBUG("Need to split root; depth = %d\n", depth);
|
|
|
|
|
2007-11-01 20:09:55 +00:00
|
|
|
pageid_t child = allocator(xid, allocator_state);
|
2007-08-20 16:53:16 +00:00
|
|
|
Page *lc = loadPage(xid, child);
|
2007-08-14 01:17:31 +00:00
|
|
|
writelock(lc->rwlatch,0);
|
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
initializeNodePage(xid, lc,keySize);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
for(int i = FIRST_SLOT; i < *recordcount_ptr(p); i++) {
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-10-02 00:18:33 +00:00
|
|
|
recordid cnext = stasis_record_alloc_begin(xid, lc,
|
2007-08-20 16:53:16 +00:00
|
|
|
sizeof(lsmTreeNodeRecord)+keySize);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
assert(i == cnext.slot);
|
2007-08-14 01:17:31 +00:00
|
|
|
assert(cnext.size != INVALID_SLOT);
|
|
|
|
|
2007-10-02 00:18:33 +00:00
|
|
|
stasis_record_alloc_done(xid, lc, cnext);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
const lsmTreeNodeRecord *nr = readNodeRecord(xid,p,i,keySize);
|
|
|
|
writeNodeRecord(xid,lc,i,(byte*)(nr+1),keySize,nr->ptr);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// deallocate old entries, and update pointer on parent node.
|
2007-08-20 16:53:16 +00:00
|
|
|
recordid pFirstSlot = { p->id, FIRST_SLOT,
|
|
|
|
sizeof(lsmTreeNodeRecord)+keySize };
|
|
|
|
|
|
|
|
// @todo should fixed.h support bulk deallocation directly?
|
|
|
|
*recordcount_ptr(p) = FIRST_SLOT+1;
|
|
|
|
|
|
|
|
lsmTreeNodeRecord *nr
|
2007-10-02 00:18:33 +00:00
|
|
|
= (lsmTreeNodeRecord*)stasis_record_write_begin(xid, p, pFirstSlot);
|
2007-08-20 16:53:16 +00:00
|
|
|
|
2007-08-14 01:17:31 +00:00
|
|
|
// don't overwrite key...
|
|
|
|
nr->ptr = child;
|
2007-10-02 00:18:33 +00:00
|
|
|
stasis_record_write_done(xid,p,pFirstSlot,(byte*)nr);
|
|
|
|
stasis_page_lsn_write(xid, p, 0); // XXX need real LSN?
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
byte *dummy = calloc(1,keySize);
|
|
|
|
if(!depth) {
|
|
|
|
s->lastLeaf = lc->id;
|
|
|
|
writeNodeRecord(xid,lc,PREV_LEAF,dummy,keySize,-1);
|
|
|
|
writeNodeRecord(xid,lc,NEXT_LEAF,dummy,keySize,-1);
|
|
|
|
}
|
|
|
|
|
2007-08-14 01:17:31 +00:00
|
|
|
unlock(lc->rwlatch);
|
|
|
|
releasePage(lc);
|
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
|
2007-08-14 01:17:31 +00:00
|
|
|
depth ++;
|
2007-08-20 16:53:16 +00:00
|
|
|
writeNodeRecord(xid,p,DEPTH,dummy,keySize,depth);
|
|
|
|
free(dummy);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
|
|
|
assert(tree.page == p->id);
|
2007-08-20 16:53:16 +00:00
|
|
|
ret = appendInternalNode(xid, p, depth, key, keySize, val_page,
|
2007-11-01 20:09:55 +00:00
|
|
|
s->lastLeaf == tree.page ? -1 : s->lastLeaf,
|
|
|
|
allocator, allocator_state);
|
2007-08-20 16:53:16 +00:00
|
|
|
|
2007-08-14 01:17:31 +00:00
|
|
|
assert(ret.size != INVALID_SLOT);
|
|
|
|
|
|
|
|
} else {
|
2007-08-20 16:53:16 +00:00
|
|
|
DEBUG("Appended new internal node tree depth = %d key = %d\n",
|
|
|
|
depth, *(int*)key);
|
2007-08-14 01:17:31 +00:00
|
|
|
}
|
|
|
|
s->lastLeaf = ret.page;
|
|
|
|
DEBUG("lastleaf is %lld\n", s->lastLeaf);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// write the new value to an existing page
|
|
|
|
DEBUG("Writing %d to existing page# %lld\n", *(int*)key, lastLeaf->id);
|
|
|
|
|
2007-10-02 00:18:33 +00:00
|
|
|
stasis_record_alloc_done(xid, lastLeaf, ret);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
writeNodeRecord(xid, lastLeaf, ret.slot, key, keySize, val_page);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
|
|
|
if(lastLeaf->id != p->id) {
|
2007-08-24 23:01:08 +00:00
|
|
|
assert(s->lastLeaf != tree.page);
|
2007-08-14 01:17:31 +00:00
|
|
|
unlock(lastLeaf->rwlatch);
|
|
|
|
releasePage(lastLeaf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock(p->rwlatch);
|
|
|
|
releasePage(p);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2007-11-12 00:49:00 +00:00
|
|
|
void TlsmForce(int xid, recordid tree, lsm_page_forcer_t force,
|
|
|
|
void *allocator_state) {
|
|
|
|
force(xid, allocator_state);
|
|
|
|
}
|
|
|
|
|
2007-11-11 23:22:21 +00:00
|
|
|
void TlsmFree(int xid, recordid tree, lsm_page_deallocator_t dealloc,
|
|
|
|
void *allocator_state) {
|
|
|
|
// Tdealloc(xid,tree);
|
|
|
|
dealloc(xid,allocator_state);
|
|
|
|
// XXX fishy shouldn't caller do this?
|
|
|
|
Tdealloc(xid, *(recordid*)allocator_state);
|
|
|
|
}
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
static pageid_t lsmLookup(int xid, Page *node, int depth,
|
|
|
|
const byte *key, size_t keySize, lsm_comparator_t cmp) {
|
|
|
|
|
|
|
|
if(*recordcount_ptr(node) == FIRST_SLOT) { return -1; }
|
|
|
|
assert(*recordcount_ptr(node) > FIRST_SLOT);
|
|
|
|
|
|
|
|
const lsmTreeNodeRecord *prev = readNodeRecord(xid,node,FIRST_SLOT,keySize);
|
|
|
|
int prev_cmp_key = cmp(prev+1,key);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
// @todo binary search within each page
|
|
|
|
for(int i = FIRST_SLOT+1; i < *recordcount_ptr(node); i++) {
|
|
|
|
const lsmTreeNodeRecord *rec = readNodeRecord(xid,node,i,keySize);
|
|
|
|
|
|
|
|
int rec_cmp_key = cmp(rec+1,key);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
|
|
|
if(depth) {
|
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
if(prev_cmp_key <= 0 && rec_cmp_key > 0) {
|
|
|
|
pageid_t child_id = prev->ptr;
|
|
|
|
Page *child_page = loadPage(xid, child_id);
|
2007-08-14 01:17:31 +00:00
|
|
|
readlock(child_page->rwlatch,0);
|
2007-08-20 16:53:16 +00:00
|
|
|
long ret = lsmLookup(xid,child_page,depth-1,key,keySize,cmp);
|
2007-08-14 01:17:31 +00:00
|
|
|
unlock(child_page->rwlatch);
|
|
|
|
releasePage(child_page);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2007-11-06 02:27:04 +00:00
|
|
|
// XXX Doesn't handle runs of duplicates.
|
|
|
|
if(prev_cmp_key <= 0 && rec_cmp_key > 0) {
|
2007-08-20 16:53:16 +00:00
|
|
|
return prev->ptr;
|
2007-08-14 01:17:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
prev = rec;
|
2007-08-20 16:53:16 +00:00
|
|
|
prev_cmp_key = rec_cmp_key;
|
|
|
|
if(rec_cmp_key > 0) { break; }
|
2007-08-14 01:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(depth) {
|
2007-11-06 02:27:04 +00:00
|
|
|
// this handles the rhs of the tree.
|
2007-08-20 16:53:16 +00:00
|
|
|
if(prev_cmp_key <= 0) {
|
|
|
|
pageid_t child_id = prev->ptr;
|
|
|
|
Page *child_page = loadPage(xid, child_id);
|
2007-08-14 01:17:31 +00:00
|
|
|
readlock(child_page->rwlatch,0);
|
2007-08-20 16:53:16 +00:00
|
|
|
long ret = lsmLookup(xid,child_page,depth-1,key,keySize,cmp);
|
2007-08-14 01:17:31 +00:00
|
|
|
unlock(child_page->rwlatch);
|
|
|
|
releasePage(child_page);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else {
|
2007-11-06 02:27:04 +00:00
|
|
|
if(prev_cmp_key <= 0) {
|
2007-08-20 16:53:16 +00:00
|
|
|
return prev->ptr;
|
2007-08-14 01:17:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
/**
|
|
|
|
Look up the value associated with key.
|
|
|
|
|
|
|
|
@return -1 if key isn't in the tree.
|
|
|
|
*/
|
|
|
|
pageid_t TlsmFindPage(int xid, recordid tree, const byte *key) {
|
|
|
|
Page *p = loadPage(xid, tree.page);
|
2007-08-14 01:17:31 +00:00
|
|
|
readlock(p->rwlatch,0);
|
|
|
|
|
|
|
|
tree.slot = 0;
|
|
|
|
tree.size = *recordsize_ptr(p);
|
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
size_t keySize = getKeySize(xid,p);
|
|
|
|
|
2007-11-01 20:09:55 +00:00
|
|
|
const lsmTreeNodeRecord *depth_nr = readNodeRecord(xid, p , DEPTH, keySize);
|
|
|
|
const lsmTreeNodeRecord *cmp_nr = readNodeRecord(xid, p , COMPARATOR, keySize);
|
2007-08-20 16:53:16 +00:00
|
|
|
|
|
|
|
int depth = depth_nr->ptr;
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
lsm_comparator_t cmp = comparators[cmp_nr->ptr];
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
pageid_t ret = lsmLookup(xid, p, depth, key, keySize, cmp);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
|
|
|
unlock(p->rwlatch);
|
|
|
|
releasePage(p);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-08-24 23:01:08 +00:00
|
|
|
pageid_t TlsmLastPage(int xid, recordid tree) {
|
|
|
|
Page * root = loadPage(xid, tree.page);
|
|
|
|
readlock(root->rwlatch,0);
|
|
|
|
lsmTreeState *state = root->impl;
|
|
|
|
int keySize = getKeySize(xid,root);
|
|
|
|
if(state->lastLeaf == -1) {
|
|
|
|
const lsmTreeNodeRecord *nr = readNodeRecord(xid,root,DEPTH,
|
|
|
|
keySize);
|
|
|
|
int depth = nr->ptr;
|
|
|
|
state->lastLeaf = findLastLeaf(xid,root,depth);
|
|
|
|
}
|
|
|
|
pageid_t ret = state->lastLeaf;
|
|
|
|
unlock(root->rwlatch);
|
|
|
|
|
|
|
|
// ret points to the last internal node at this point.
|
|
|
|
releasePage(root);
|
|
|
|
|
|
|
|
Page * p = loadPage(xid, ret);
|
|
|
|
readlock(p->rwlatch,0);
|
|
|
|
if(*recordcount_ptr(p) == 2) {
|
|
|
|
ret = -1;
|
|
|
|
} else {
|
|
|
|
const lsmTreeNodeRecord *nr = readNodeRecord(xid,p,(*recordcount_ptr(p))-1,keySize);
|
|
|
|
ret = nr->ptr;
|
|
|
|
}
|
|
|
|
unlock(p->rwlatch);
|
|
|
|
releasePage(p);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-08-14 01:17:31 +00:00
|
|
|
/**
|
|
|
|
The buffer manager calls this when the lsmTree's root page is
|
|
|
|
loaded. This function allocates some storage for cached values
|
|
|
|
associated with the tree.
|
|
|
|
*/
|
|
|
|
static void lsmPageLoaded(Page *p) {
|
2007-08-20 21:58:20 +00:00
|
|
|
/// XXX should call fixedLoaded, or something...
|
2007-08-20 16:53:16 +00:00
|
|
|
lsmTreeState *state = malloc(sizeof(lsmTreeState));
|
2007-08-14 01:17:31 +00:00
|
|
|
state->lastLeaf = -1;
|
|
|
|
p->impl = state;
|
|
|
|
}
|
2007-08-20 21:58:20 +00:00
|
|
|
static void lsmPageFlushed(Page *p) { }
|
2007-08-14 01:17:31 +00:00
|
|
|
/**
|
|
|
|
Free any soft state associated with the tree rooted at page p.
|
|
|
|
This is called by the buffer manager.
|
|
|
|
*/
|
2007-08-20 21:58:20 +00:00
|
|
|
static void lsmPageCleanup(Page *p) {
|
2007-08-20 16:53:16 +00:00
|
|
|
lsmTreeState *state = p->impl;
|
2007-08-14 01:17:31 +00:00
|
|
|
free(state);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
A page_impl for the root of an lsmTree.
|
|
|
|
*/
|
|
|
|
page_impl lsmRootImpl() {
|
|
|
|
page_impl pi = fixedImpl();
|
|
|
|
pi.pageLoaded = lsmPageLoaded;
|
|
|
|
pi.pageFlushed = lsmPageFlushed;
|
2007-08-20 21:58:20 +00:00
|
|
|
pi.pageCleanup = lsmPageCleanup;
|
2007-08-14 01:17:31 +00:00
|
|
|
pi.page_type = LSM_ROOT_PAGE;
|
|
|
|
return pi;
|
|
|
|
}
|
2007-08-20 16:53:16 +00:00
|
|
|
///--------------------- Iterator implementation
|
|
|
|
|
|
|
|
lladdIterator_t *lsmTreeIterator_open(int xid, recordid root) {
|
|
|
|
Page *p = loadPage(xid,root.page);
|
|
|
|
readlock(p->rwlatch,0);
|
|
|
|
size_t keySize = getKeySize(xid,p);
|
|
|
|
const lsmTreeNodeRecord *nr = readNodeRecord(xid,p,DEPTH,keySize);
|
|
|
|
int depth = nr->ptr;
|
|
|
|
pageid_t leafid = findFirstLeaf(xid, p, depth);
|
|
|
|
if(leafid != root.page) {
|
|
|
|
unlock(p->rwlatch);
|
|
|
|
releasePage(p);
|
|
|
|
p = loadPage(xid,leafid);
|
|
|
|
readlock(p->rwlatch,0);
|
2007-08-24 23:01:08 +00:00
|
|
|
assert(depth != 0);
|
|
|
|
} else {
|
|
|
|
assert(depth == 0);
|
2007-08-20 16:53:16 +00:00
|
|
|
}
|
|
|
|
lsmIteratorImpl *impl = malloc(sizeof(lsmIteratorImpl));
|
|
|
|
impl->p = p;
|
|
|
|
{
|
|
|
|
recordid rid = { p->id, 1, keySize };
|
|
|
|
impl->current = rid;
|
|
|
|
}
|
|
|
|
DEBUG("keysize = %d, slot = %d\n", keySize, impl->current.slot);
|
|
|
|
impl->t = 0;
|
|
|
|
impl->justOnePage = (depth == 0);
|
|
|
|
|
|
|
|
lladdIterator_t *it = malloc(sizeof(lladdIterator_t));
|
|
|
|
it->type = -1; // XXX LSM_TREE_ITERATOR;
|
|
|
|
it->impl = impl;
|
|
|
|
/* itdef = { <-- @todo register lsmTree iterators with stasis someday...
|
|
|
|
lsmTreeIterator_close;
|
|
|
|
lsmTreeIterator_next;
|
|
|
|
lsmTreeIterator_next;
|
|
|
|
lsmTreeIterator_key;
|
|
|
|
lsmTreeIterator_value;
|
|
|
|
lsmTreeIterator_tupleDone;
|
|
|
|
lsmTreeIterator_releaseLock;
|
|
|
|
} */
|
|
|
|
return it;
|
|
|
|
}
|
2007-08-24 23:01:08 +00:00
|
|
|
lladdIterator_t *lsmTreeIterator_copy(int xid, lladdIterator_t* i) {
|
|
|
|
lsmIteratorImpl *it = i->impl;
|
|
|
|
lsmIteratorImpl *mine = malloc(sizeof(lsmIteratorImpl));
|
|
|
|
|
|
|
|
if(it->p) {
|
|
|
|
mine->p = loadPage(xid, it->p->id);
|
|
|
|
readlock(mine->p->rwlatch,0);
|
|
|
|
} else {
|
|
|
|
mine->p = 0;
|
|
|
|
}
|
|
|
|
memcpy(&mine->current, &it->current,sizeof(recordid));
|
|
|
|
mine->t = it->t;
|
|
|
|
mine->justOnePage = it->justOnePage;
|
|
|
|
lladdIterator_t * ret = malloc(sizeof(lladdIterator_t));
|
|
|
|
ret->type = -1; // XXX LSM_TREE_ITERATOR
|
|
|
|
ret->impl = mine;
|
|
|
|
return ret;
|
|
|
|
}
|
2007-08-20 16:53:16 +00:00
|
|
|
void lsmTreeIterator_close(int xid, lladdIterator_t *it) {
|
|
|
|
lsmIteratorImpl *impl = it->impl;
|
|
|
|
if(impl->p) {
|
|
|
|
unlock(impl->p->rwlatch);
|
|
|
|
releasePage(impl->p);
|
|
|
|
}
|
|
|
|
free(impl);
|
|
|
|
free(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
int lsmTreeIterator_next(int xid, lladdIterator_t *it) {
|
|
|
|
lsmIteratorImpl *impl = it->impl;
|
|
|
|
size_t keySize = impl->current.size;
|
|
|
|
impl->current = fixedNext(xid, impl->p, impl->current);
|
|
|
|
if(impl->current.size == INVALID_SLOT) {
|
|
|
|
const lsmTreeNodeRecord *next_rec = readNodeRecord(xid,impl->p,NEXT_LEAF,
|
2007-08-20 21:58:20 +00:00
|
|
|
keySize);
|
2007-08-20 16:53:16 +00:00
|
|
|
unlock(impl->p->rwlatch);
|
|
|
|
releasePage(impl->p);
|
|
|
|
|
|
|
|
DEBUG("done with page %lld next = %lld\n", impl->p->id, next_rec->ptr);
|
|
|
|
|
|
|
|
if(next_rec->ptr != -1 && ! impl->justOnePage) {
|
|
|
|
impl->p = loadPage(xid, next_rec->ptr);
|
|
|
|
readlock(impl->p->rwlatch,0);
|
|
|
|
impl->current.page = next_rec->ptr;
|
|
|
|
impl->current.slot = 2;
|
|
|
|
impl->current.size = keySize;
|
|
|
|
} else {
|
|
|
|
impl->p = 0;
|
2007-08-24 23:01:08 +00:00
|
|
|
impl->current.size = INVALID_SLOT;
|
2007-08-20 16:53:16 +00:00
|
|
|
}
|
2007-08-20 21:58:20 +00:00
|
|
|
} else {
|
|
|
|
assert(impl->current.size == keySize + sizeof(lsmTreeNodeRecord));
|
|
|
|
impl->current.size = keySize;
|
2007-08-20 16:53:16 +00:00
|
|
|
}
|
|
|
|
if(impl->current.size != INVALID_SLOT) {
|
|
|
|
impl->t = readNodeRecord(xid,impl->p,impl->current.slot,impl->current.size);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
impl->t = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|