2005-03-02 05:47:38 +00:00
|
|
|
|
2004-10-06 06:08:09 +00:00
|
|
|
#include <config.h>
|
2007-06-11 21:36:57 +00:00
|
|
|
#include <stasis/common.h>
|
2004-10-06 06:08:09 +00:00
|
|
|
|
2008-04-13 04:02:57 +00:00
|
|
|
#include <stasis/page.h>
|
2007-06-11 21:36:57 +00:00
|
|
|
#include <stasis/operations/pageOperations.h>
|
|
|
|
#include <stasis/operations/arrayList.h>
|
|
|
|
#include <stasis/transactional.h>
|
|
|
|
#include <stasis/bufferManager.h>
|
2004-10-06 06:08:09 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
2005-03-02 05:47:38 +00:00
|
|
|
#define _XOPEN_SOURCE 600
|
2004-10-06 06:08:09 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int firstPage;
|
|
|
|
int initialSize;
|
|
|
|
int multiplier;
|
|
|
|
int size;
|
|
|
|
int maxOffset;
|
|
|
|
} TarrayListParameters;
|
|
|
|
|
|
|
|
|
2007-06-07 21:53:09 +00:00
|
|
|
static TarrayListParameters pageToTLP(int xid, Page * p);
|
2004-10-06 06:08:09 +00:00
|
|
|
static int getBlockContainingOffset(TarrayListParameters tlp, int offset, int * firstSlotInBlock);
|
|
|
|
|
2007-06-07 21:53:09 +00:00
|
|
|
#define MAX_OFFSET_POSITION 3
|
|
|
|
#define FIRST_DATA_PAGE_OFFSET 4
|
|
|
|
|
2004-10-06 06:08:09 +00:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2005-02-22 03:10:54 +00:00
|
|
|
compensated_function recordid TarrayListAlloc(int xid, int count, int multiplier, int size) {
|
|
|
|
|
|
|
|
int firstPage;
|
|
|
|
try_ret(NULLRID) {
|
|
|
|
firstPage = TpageAllocMany(xid, count+1);
|
|
|
|
} end_ret(NULLRID);
|
2004-10-06 06:08:09 +00:00
|
|
|
TarrayListParameters tlp;
|
|
|
|
|
|
|
|
tlp.firstPage = firstPage;
|
|
|
|
tlp.initialSize = count;
|
|
|
|
tlp.multiplier = multiplier;
|
|
|
|
tlp.size = size;
|
|
|
|
tlp.maxOffset = 0;
|
|
|
|
|
|
|
|
recordid rid;
|
|
|
|
|
|
|
|
rid.page = firstPage;
|
2004-10-12 02:44:47 +00:00
|
|
|
rid.size = size;
|
2004-10-06 06:08:09 +00:00
|
|
|
rid.slot = 0;
|
2005-02-22 03:10:54 +00:00
|
|
|
try_ret(NULLRID) {
|
|
|
|
Tupdate(xid, rid, &tlp, OPERATION_ARRAY_LIST_ALLOC);
|
|
|
|
} end_ret(NULLRID);
|
2004-10-06 06:08:09 +00:00
|
|
|
|
|
|
|
return rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int operateAlloc(int xid, Page * p, lsn_t lsn, recordid rid, const void * dat) {
|
|
|
|
|
|
|
|
const TarrayListParameters * tlp = dat;
|
|
|
|
|
|
|
|
int firstPage = tlp->firstPage;
|
|
|
|
int count = tlp->initialSize;
|
|
|
|
int multiplier = tlp->multiplier;
|
|
|
|
int size = tlp->size;
|
|
|
|
|
2007-06-01 21:32:33 +00:00
|
|
|
/* Allocing this page -> implicit lock, but latch to conformt to
|
|
|
|
fixedPage's interface. */
|
|
|
|
writelock(p->rwlatch, 0);
|
2007-10-02 00:18:33 +00:00
|
|
|
stasis_fixed_initialize_page(p, sizeof(int), stasis_fixed_records_per_page(sizeof(int)));
|
2004-10-06 06:08:09 +00:00
|
|
|
|
|
|
|
recordid countRid, multiplierRid, slotSizeRid, maxOffset, firstDataPageRid;
|
|
|
|
countRid.page = multiplierRid.page = slotSizeRid.page = maxOffset.page = firstDataPageRid.page = p->id;
|
|
|
|
countRid.size = multiplierRid.size = slotSizeRid.size = maxOffset.size = firstDataPageRid.size = sizeof(int);
|
|
|
|
|
2007-06-07 21:53:09 +00:00
|
|
|
countRid.slot = 0;
|
2004-10-06 06:08:09 +00:00
|
|
|
multiplierRid.slot = 1;
|
|
|
|
slotSizeRid.slot = 2;
|
|
|
|
maxOffset.slot = 3;
|
|
|
|
firstDataPageRid.slot = 4;
|
|
|
|
|
2007-06-07 21:53:09 +00:00
|
|
|
int firstDataPage = firstPage + 1;
|
2007-10-02 00:18:33 +00:00
|
|
|
(*(int*)stasis_record_write_begin(xid, p, countRid))= count;
|
|
|
|
(*(int*)stasis_record_write_begin(xid, p, multiplierRid))= multiplier;
|
|
|
|
(*(int*)stasis_record_write_begin(xid, p, firstDataPageRid))= firstDataPage;
|
|
|
|
(*(int*)stasis_record_write_begin(xid, p, slotSizeRid))= size;
|
|
|
|
(*(int*)stasis_record_write_begin(xid, p, maxOffset))= -1;
|
2004-10-06 06:08:09 +00:00
|
|
|
|
2007-10-02 00:18:33 +00:00
|
|
|
*stasis_page_type_ptr(p) = ARRAY_LIST_PAGE;
|
2004-10-06 06:08:09 +00:00
|
|
|
|
2007-10-02 00:18:33 +00:00
|
|
|
stasis_page_lsn_write(xid, p, lsn);
|
2004-10-06 06:08:09 +00:00
|
|
|
|
|
|
|
recordid ret;
|
|
|
|
ret.page = firstPage;
|
|
|
|
ret.slot = 0; /* slot = # of slots in array... */
|
|
|
|
ret.size = size;
|
2007-06-01 21:32:33 +00:00
|
|
|
unlock(p->rwlatch);
|
|
|
|
|
2004-10-06 06:08:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Operation getArrayListAlloc() {
|
|
|
|
Operation o = {
|
|
|
|
OPERATION_ARRAY_LIST_ALLOC, /* ID */
|
|
|
|
sizeof(TarrayListParameters),
|
|
|
|
OPERATION_NOOP, /* Since TpageAllocMany will be undone, the page we touch will be nuked anyway, so set this to NO-OP. */
|
|
|
|
&operateAlloc
|
|
|
|
};
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2005-02-24 21:12:36 +00:00
|
|
|
/** @todo locking for arrayList... this isn't pressing since currently
|
|
|
|
the only thing that calls arraylist (the hashtable
|
2005-03-02 05:47:38 +00:00
|
|
|
implementations) serialize bucket list operations anyway...
|
|
|
|
|
|
|
|
@todo this function calls pow(), which is horribly inefficient.
|
|
|
|
*/
|
2005-02-24 21:12:36 +00:00
|
|
|
|
|
|
|
static compensated_function int TarrayListExtendInternal(int xid, recordid rid, int slots, int op) {
|
2005-02-22 03:10:54 +00:00
|
|
|
Page * p;
|
|
|
|
try_ret(compensation_error()) {
|
|
|
|
p = loadPage(xid, rid.page);
|
|
|
|
} end_ret(compensation_error());
|
2007-06-07 21:53:09 +00:00
|
|
|
readlock(p->rwlatch, 0);
|
|
|
|
TarrayListParameters tlp = pageToTLP(xid, p);
|
|
|
|
unlock(p->rwlatch);;
|
2005-02-24 21:12:36 +00:00
|
|
|
releasePage(p);
|
|
|
|
p = NULL;
|
2004-10-06 06:08:09 +00:00
|
|
|
|
|
|
|
int lastCurrentBlock;
|
|
|
|
if(tlp.maxOffset == -1) {
|
|
|
|
lastCurrentBlock = -1;
|
|
|
|
} else{
|
|
|
|
lastCurrentBlock = getBlockContainingOffset(tlp, tlp.maxOffset, NULL);
|
|
|
|
}
|
|
|
|
int lastNewBlock = getBlockContainingOffset(tlp, tlp.maxOffset+slots, NULL);
|
|
|
|
|
|
|
|
DEBUG("lastCurrentBlock = %d, lastNewBlock = %d\n", lastCurrentBlock, lastNewBlock);
|
|
|
|
|
|
|
|
recordid tmp; /* recordid of slot in base page that holds new block. */
|
|
|
|
tmp.page = rid.page;
|
|
|
|
tmp.size = sizeof(int);
|
|
|
|
|
|
|
|
recordid tmp2; /* recordid of newly created pages. */
|
|
|
|
tmp2.slot = 0;
|
|
|
|
tmp2.size = tlp.size;
|
2004-10-18 18:24:54 +00:00
|
|
|
/* Iterate over the (small number) of indirection blocks that need to be updated */
|
2005-02-24 21:12:36 +00:00
|
|
|
try_ret(compensation_error()) {
|
|
|
|
for(int i = lastCurrentBlock+1; i <= lastNewBlock; i++) {
|
|
|
|
/* Alloc block i */
|
2005-03-02 05:47:38 +00:00
|
|
|
int blockSize = tlp.initialSize * pow(tlp.multiplier, i);
|
2005-02-24 21:12:36 +00:00
|
|
|
int newFirstPage = TpageAllocMany(xid, blockSize);
|
|
|
|
DEBUG("block %d\n", i);
|
|
|
|
tmp.slot = i + FIRST_DATA_PAGE_OFFSET;
|
2007-11-11 17:18:57 +00:00
|
|
|
/* Iterate over the (large number) of new blocks, clearing their contents */
|
|
|
|
/* @todo XXX arraylist generates N log entries initing pages.
|
|
|
|
It should generate 1 entry. (Need better LSN handling first.)*/
|
|
|
|
{
|
|
|
|
recordid newpage;
|
|
|
|
newpage.slot = 0;
|
|
|
|
newpage.size = tlp.size;
|
|
|
|
for(int i = newFirstPage; i < newFirstPage + blockSize; i++) {
|
|
|
|
newpage.page = i;
|
|
|
|
TupdateRaw(xid, newpage, 0, OPERATION_FIXED_PAGE_ALLOC);
|
|
|
|
}
|
|
|
|
}
|
2007-03-28 09:21:07 +00:00
|
|
|
TupdateRaw(xid, tmp, &newFirstPage, op);
|
2005-02-24 21:12:36 +00:00
|
|
|
DEBUG("Tset: {%d, %d, %d} = %d\n", tmp.page, tmp.slot, tmp.size, newFirstPage);
|
|
|
|
}
|
2004-10-06 06:08:09 +00:00
|
|
|
|
2005-02-24 21:12:36 +00:00
|
|
|
tmp.slot = MAX_OFFSET_POSITION;
|
2004-10-18 18:24:54 +00:00
|
|
|
|
2005-02-24 21:12:36 +00:00
|
|
|
int newMaxOffset = tlp.maxOffset+slots;
|
2007-03-28 09:21:07 +00:00
|
|
|
TupdateRaw(xid, tmp, &newMaxOffset, op);
|
2005-02-24 21:12:36 +00:00
|
|
|
} end_ret(compensation_error());
|
2004-10-06 06:08:09 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-02-24 21:12:36 +00:00
|
|
|
compensated_function int TarrayListInstantExtend(int xid, recordid rid, int slots) {
|
2007-03-28 09:21:07 +00:00
|
|
|
return TarrayListExtendInternal(xid, rid, slots, OPERATION_INSTANT_SET);
|
2005-02-24 21:12:36 +00:00
|
|
|
}
|
|
|
|
compensated_function int TarrayListExtend(int xid, recordid rid, int slots) {
|
2007-03-28 09:21:07 +00:00
|
|
|
return TarrayListExtendInternal(xid, rid, slots, OPERATION_SET);
|
2005-02-24 21:12:36 +00:00
|
|
|
}
|
2006-07-28 00:00:32 +00:00
|
|
|
compensated_function int TarrayListLength(int xid, recordid rid) {
|
|
|
|
Page * p = loadPage(xid, rid.page);
|
2007-06-07 21:53:09 +00:00
|
|
|
readlock(p->rwlatch, 0);
|
|
|
|
TarrayListParameters tlp = pageToTLP(xid, p);
|
|
|
|
unlock(p->rwlatch);
|
2006-07-28 00:00:32 +00:00
|
|
|
releasePage(p);
|
|
|
|
return tlp.maxOffset+1;
|
|
|
|
}
|
2004-10-06 06:08:09 +00:00
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2007-06-07 21:53:09 +00:00
|
|
|
/**
|
|
|
|
@todo XXX latching for dereference arraylist rid (and other dereference functions...)
|
|
|
|
*/
|
|
|
|
recordid dereferenceArrayListRid(int xid, Page * p, int offset) {
|
2007-06-01 21:32:33 +00:00
|
|
|
readlock(p->rwlatch,0);
|
2007-06-07 21:53:09 +00:00
|
|
|
TarrayListParameters tlp = pageToTLP(xid, p);
|
2004-10-06 06:08:09 +00:00
|
|
|
|
2007-10-02 00:18:33 +00:00
|
|
|
int rec_per_page = stasis_fixed_records_per_page((size_t)tlp.size);
|
2004-10-06 06:08:09 +00:00
|
|
|
int lastHigh = 0;
|
|
|
|
int pageRidSlot = 0; /* The slot on the root arrayList page that contains the first page of the block of interest */
|
|
|
|
|
|
|
|
assert(tlp.maxOffset >= offset);
|
|
|
|
|
|
|
|
pageRidSlot = getBlockContainingOffset(tlp, offset, &lastHigh);
|
|
|
|
|
|
|
|
int dataSlot = offset - lastHigh; /* The offset in the block of interest of the slot we want. */
|
|
|
|
int blockPage = dataSlot / rec_per_page; /* The page in the block of interest that contains the slot we want */
|
|
|
|
int blockSlot = dataSlot - blockPage * rec_per_page;
|
|
|
|
|
|
|
|
int thePage;
|
|
|
|
|
2007-06-07 21:53:09 +00:00
|
|
|
recordid rid = { p->id, pageRidSlot + FIRST_DATA_PAGE_OFFSET, sizeof(int) };
|
2007-10-02 00:18:33 +00:00
|
|
|
thePage = *(int*)stasis_record_read_begin(xid,p,rid);
|
2007-06-01 21:32:33 +00:00
|
|
|
unlock(p->rwlatch);
|
2004-10-06 06:08:09 +00:00
|
|
|
|
|
|
|
rid.page = thePage + blockPage;
|
|
|
|
rid.slot = blockSlot;
|
|
|
|
rid.size = tlp.size;
|
|
|
|
|
|
|
|
return rid;
|
|
|
|
|
|
|
|
}
|
|
|
|
static int getBlockContainingOffset(TarrayListParameters tlp, int offset, int * firstSlotInBlock) {
|
2007-10-02 00:18:33 +00:00
|
|
|
int rec_per_page = stasis_fixed_records_per_page((size_t)tlp.size);
|
2004-10-06 06:08:09 +00:00
|
|
|
long thisHigh = rec_per_page * tlp.initialSize;
|
|
|
|
int lastHigh = 0;
|
|
|
|
int pageRidSlot = 0;
|
|
|
|
int currentPageLength = tlp.initialSize;
|
|
|
|
|
|
|
|
while(((long)offset) >= thisHigh) {
|
|
|
|
pageRidSlot ++;
|
|
|
|
lastHigh = thisHigh;
|
|
|
|
currentPageLength *= tlp.multiplier;
|
|
|
|
thisHigh += rec_per_page * currentPageLength;
|
|
|
|
}
|
|
|
|
if(firstSlotInBlock) {
|
|
|
|
*firstSlotInBlock = lastHigh;
|
|
|
|
}
|
|
|
|
return pageRidSlot;
|
|
|
|
}
|
|
|
|
|
2007-06-07 21:53:09 +00:00
|
|
|
static TarrayListParameters pageToTLP(int xid, Page * p) {
|
2004-10-06 06:08:09 +00:00
|
|
|
|
|
|
|
TarrayListParameters tlp;
|
|
|
|
tlp.firstPage = p->id;
|
2007-06-07 21:53:09 +00:00
|
|
|
/* tlp.maxOffset = *(int*)fixed_record_ptr(p, 3); */
|
|
|
|
recordid rid = { p->id, 0, sizeof(int) };
|
2007-10-02 00:18:33 +00:00
|
|
|
tlp.initialSize = *(int*)stasis_record_read_begin(xid, p, rid);
|
2007-06-07 21:53:09 +00:00
|
|
|
rid.slot = 1;
|
2007-10-02 00:18:33 +00:00
|
|
|
tlp.multiplier = *(int*)stasis_record_read_begin(xid, p, rid);
|
2007-06-07 21:53:09 +00:00
|
|
|
rid.slot = 2;
|
2007-10-02 00:18:33 +00:00
|
|
|
tlp.size = *(int*)stasis_record_read_begin(xid, p, rid);
|
2007-06-07 21:53:09 +00:00
|
|
|
rid.slot = 3;
|
2007-10-02 00:18:33 +00:00
|
|
|
tlp.maxOffset = *(int*)stasis_record_read_begin(xid, p, rid);
|
2004-10-06 06:08:09 +00:00
|
|
|
|
|
|
|
return tlp;
|
|
|
|
}
|