2007-08-14 01:17:31 +00:00
|
|
|
#ifndef _LSMTREE_H__
|
|
|
|
#define _LSMTREE_H__
|
|
|
|
/**
|
|
|
|
@file
|
|
|
|
|
|
|
|
A log structured merge tree implementation. This implementation
|
|
|
|
performs atomic bulk append operations to reduce logging overheads,
|
|
|
|
and does not support in place updates.
|
|
|
|
|
|
|
|
However, once written, the page format of internal nodes is similar
|
|
|
|
to that of a conventional b-tree, while leaf nodes may be provided
|
|
|
|
by any page type that allows records to be appendend to a page, and
|
|
|
|
read by slot id.
|
|
|
|
|
|
|
|
For now, LSM-trees only support fixed length keys; this restriction
|
|
|
|
will be lifted in the future.
|
|
|
|
*/
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stasis/iterator.h>
|
|
|
|
typedef struct {
|
|
|
|
recordid treeRoot;
|
|
|
|
recordid pos;
|
|
|
|
} lladd_lsm_iterator;
|
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
typedef int(*lsm_comparator_t)(const void* a, const void* b);
|
2007-11-01 20:09:55 +00:00
|
|
|
typedef void*(*lsm_page_initializer_t)(Page *, void *);
|
|
|
|
typedef pageid_t(*lsm_page_allocator_t)(int, void *);
|
2007-11-11 23:22:21 +00:00
|
|
|
typedef void(*lsm_page_deallocator_t)(int, void *);
|
2007-11-12 00:49:00 +00:00
|
|
|
typedef void(*lsm_page_forcer_t)(int, void *);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
void lsmTreeRegisterComparator(int id, lsm_comparator_t i);
|
2007-11-01 20:09:55 +00:00
|
|
|
void lsmTreeRegisterPageInitializer(int id, lsm_page_initializer_t i);
|
|
|
|
|
|
|
|
pageid_t TlsmRegionAlloc(int xid, void *conf);
|
|
|
|
pageid_t TlsmRegionAllocRid(int xid, void *conf);
|
2007-11-12 00:49:00 +00:00
|
|
|
void TlsmRegionForceRid(int xid, void *conf);
|
2007-11-01 20:09:55 +00:00
|
|
|
typedef struct {
|
2007-11-11 23:22:21 +00:00
|
|
|
recordid regionList;
|
|
|
|
pageid_t regionCount;
|
2007-11-01 20:09:55 +00:00
|
|
|
pageid_t nextPage;
|
|
|
|
pageid_t endOfRegion;
|
|
|
|
pageid_t regionSize;
|
|
|
|
} TlsmRegionAllocConf_t;
|
2007-11-11 23:22:21 +00:00
|
|
|
|
|
|
|
void TlsmRegionDeallocRid(int xid, void *conf);
|
|
|
|
|
2007-11-01 20:09:55 +00:00
|
|
|
extern TlsmRegionAllocConf_t LSM_REGION_ALLOC_STATIC_INITIALIZER;
|
2007-08-14 01:17:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Initialize a new LSM tree.
|
|
|
|
|
2007-10-02 00:18:33 +00:00
|
|
|
@param xid The tranasction that is creating the tree.
|
|
|
|
|
|
|
|
@param comparator The id of the comparator this tree should use.
|
2007-08-14 01:17:31 +00:00
|
|
|
(It must have been registered with lsmTreeRegisterComparator
|
|
|
|
before TlsmCreate() is called.
|
2007-10-02 00:18:33 +00:00
|
|
|
|
2008-04-17 06:29:34 +00:00
|
|
|
@param allocator A callback that will return an allocated page when called.
|
|
|
|
This is used as the tree is extended.
|
|
|
|
|
|
|
|
@param allocator_state A pointer that will be paseed into allocator
|
|
|
|
when it is called.
|
|
|
|
|
2007-10-02 00:18:33 +00:00
|
|
|
@param keySize
|
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
|
|
|
/**
|
|
|
|
Free the space associated with an LSM tree.
|
|
|
|
*/
|
2007-11-01 20:09:55 +00:00
|
|
|
recordid TlsmDealloc(int xid,
|
|
|
|
lsm_page_allocator_t allocator, void *allocator_state,
|
|
|
|
recordid tree);
|
2007-08-14 01:17:31 +00:00
|
|
|
/**
|
|
|
|
Append a new leaf page to an LSM tree. Leaves must be appended in
|
|
|
|
ascending order; LSM trees do not support update in place.
|
|
|
|
*/
|
|
|
|
recordid TlsmAppendPage(int xid, recordid tree,
|
2007-11-01 20:09:55 +00:00
|
|
|
const byte *key,
|
|
|
|
lsm_page_allocator_t allocator, void *allocator_state,
|
|
|
|
long pageid);
|
2007-11-12 00:49:00 +00:00
|
|
|
void TlsmForce(int xid, recordid tree, lsm_page_forcer_t force,
|
|
|
|
void *allocator_state);
|
2007-11-11 23:22:21 +00:00
|
|
|
void TlsmFree(int xid, recordid tree, lsm_page_deallocator_t dealloc,
|
|
|
|
void *allocator_state);
|
2007-08-14 01:17:31 +00:00
|
|
|
/**
|
|
|
|
Lookup a leaf page.
|
|
|
|
|
2008-04-23 01:41:57 +00:00
|
|
|
@param xid The transaction that is looking up this page
|
|
|
|
|
|
|
|
@param tree The tree to be queried.
|
|
|
|
|
2007-08-14 01:17:31 +00:00
|
|
|
@param key The value you're looking for. The first page that may
|
|
|
|
contain this value will be returned. (lsmTree supports
|
2008-04-23 01:41:57 +00:00
|
|
|
duplicate keys...) LSM trees currently store fixed
|
|
|
|
length keys, so there is no keySize parameter.
|
2007-08-14 01:17:31 +00:00
|
|
|
*/
|
|
|
|
pageid_t TlsmFindPage(int xid, recordid tree,
|
2007-08-20 16:53:16 +00:00
|
|
|
const byte *key);
|
2007-08-24 23:01:08 +00:00
|
|
|
/**
|
|
|
|
@todo TlsmFirstPage for symmetry?
|
|
|
|
*/
|
|
|
|
pageid_t TlsmLastPage(int xid, recordid tree);
|
2007-08-20 16:53:16 +00:00
|
|
|
/// --------------- Iterator implementation
|
|
|
|
|
|
|
|
typedef struct lsmTreeNodeRecord {
|
|
|
|
pageid_t ptr;
|
|
|
|
} lsmTreeNodeRecord;
|
|
|
|
|
|
|
|
typedef struct lsmIteratorImpl {
|
|
|
|
Page * p;
|
|
|
|
recordid current;
|
|
|
|
const lsmTreeNodeRecord *t;
|
|
|
|
int justOnePage;
|
|
|
|
} lsmIteratorImpl;
|
2007-08-14 01:17:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Return a forward iterator over the tree's leaf pages (*not* their
|
2007-08-20 16:53:16 +00:00
|
|
|
contents). The iterator starts before the first leaf page.
|
|
|
|
|
|
|
|
@see iterator.h for documentation of lsmTree's iterator interface.
|
2007-08-14 01:17:31 +00:00
|
|
|
*/
|
2007-08-20 16:53:16 +00:00
|
|
|
lladdIterator_t * lsmTreeIterator_open(int xid, recordid tree);
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
/*
|
2007-08-14 01:17:31 +00:00
|
|
|
These are the functions that implement lsmTree's iterator.
|
|
|
|
|
|
|
|
They're public so that performance critical code can call them
|
|
|
|
without paying for a virtual method invocation.
|
|
|
|
*/
|
2007-08-20 16:53:16 +00:00
|
|
|
void lsmTreeIterator_close(int xid, lladdIterator_t * it);
|
|
|
|
int lsmTreeIterator_next (int xid, lladdIterator_t * it);
|
2007-08-24 23:01:08 +00:00
|
|
|
lladdIterator_t *lsmTreeIterator_copy(int xid, lladdIterator_t* i);
|
2007-08-20 16:53:16 +00:00
|
|
|
static inline int lsmTreeIterator_key (int xid, lladdIterator_t *it,
|
|
|
|
byte **key) {
|
|
|
|
lsmIteratorImpl * impl = (lsmIteratorImpl*)it->impl;
|
|
|
|
*key = (byte*)(impl->t+1);
|
2007-08-20 21:58:20 +00:00
|
|
|
return impl->current.size;
|
2007-08-14 01:17:31 +00:00
|
|
|
|
2007-08-20 16:53:16 +00:00
|
|
|
}
|
|
|
|
static inline int lsmTreeIterator_value(int xid, lladdIterator_t *it,
|
|
|
|
byte **value) {
|
|
|
|
lsmIteratorImpl * impl = (lsmIteratorImpl*)it->impl;
|
|
|
|
*value = (byte*)&(impl->t->ptr);
|
|
|
|
return sizeof(impl->t->ptr);
|
|
|
|
}
|
|
|
|
static inline void lsmTreeIterator_tupleDone(int xid, void *it) { }
|
|
|
|
static inline void lsmTreeIterator_releaseLock(int xid, void *it) { }
|
2007-08-14 01:17:31 +00:00
|
|
|
#endif // _LSMTREE_H__
|