2004-07-31 00:27:55 +00:00
|
|
|
#include "indirect.h"
|
2007-06-07 21:53:09 +00:00
|
|
|
#include "slotted.h"
|
2004-07-31 00:27:55 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include "../blobManager.h"
|
|
|
|
#include "../page.h"
|
2004-08-03 02:04:56 +00:00
|
|
|
|
2007-06-11 21:36:57 +00:00
|
|
|
#include <stasis/operations.h>
|
2004-08-03 02:04:56 +00:00
|
|
|
|
2004-07-31 00:27:55 +00:00
|
|
|
void indirectInitialize(Page * p, int height) {
|
2007-11-12 07:27:49 +00:00
|
|
|
stasis_page_cleanup(p);
|
2007-11-12 16:32:00 +00:00
|
|
|
*level_ptr(p) = height;
|
2007-10-02 00:18:33 +00:00
|
|
|
*stasis_page_type_ptr(p) = INDIRECT_PAGE;
|
2006-03-20 23:11:46 +00:00
|
|
|
memset(p->memAddr, INVALID_SLOT, ((size_t)level_ptr(p)) - ((size_t)p->memAddr));
|
2004-07-31 00:27:55 +00:00
|
|
|
}
|
2007-06-01 21:32:33 +00:00
|
|
|
/** @todo Is locking for dereferenceRID really necessary? */
|
2007-10-02 00:18:33 +00:00
|
|
|
compensated_function recordid dereferenceIndirectRID(int xid, recordid rid) {
|
2007-06-01 21:32:33 +00:00
|
|
|
Page * page;
|
2005-02-16 04:11:14 +00:00
|
|
|
try_ret(NULLRID) {
|
2007-06-01 21:32:33 +00:00
|
|
|
page = loadPage(xid, rid.page);
|
|
|
|
readlock(page->rwlatch, 0);
|
2005-02-16 04:11:14 +00:00
|
|
|
} end_ret(NULLRID);
|
2005-03-10 03:19:04 +00:00
|
|
|
// printf("a"); fflush(stdout);
|
2004-07-31 00:27:55 +00:00
|
|
|
int offset = 0;
|
|
|
|
int max_slot;
|
2007-10-02 00:18:33 +00:00
|
|
|
while(*stasis_page_type_ptr(page) == INDIRECT_PAGE) {
|
2004-07-31 00:27:55 +00:00
|
|
|
int i = 0;
|
2007-06-01 21:32:33 +00:00
|
|
|
for(max_slot = *maxslot_ptr(page, i); ( max_slot + offset ) <= rid.slot; max_slot = *maxslot_ptr(page, i)) {
|
2004-07-31 00:27:55 +00:00
|
|
|
i++;
|
|
|
|
assert(max_slot != INVALID_SLOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(i) {
|
2007-06-01 21:32:33 +00:00
|
|
|
offset += *maxslot_ptr(page, i - 1);
|
2004-07-31 00:27:55 +00:00
|
|
|
} /** else, the adjustment to the offset is zero */
|
|
|
|
|
2007-06-01 21:32:33 +00:00
|
|
|
int nextPage = *page_ptr(page, i);
|
2004-07-31 00:27:55 +00:00
|
|
|
|
2007-06-01 21:32:33 +00:00
|
|
|
unlock(page->rwlatch);
|
|
|
|
releasePage(page);
|
2005-02-16 04:11:14 +00:00
|
|
|
try_ret(NULLRID) {
|
2007-06-01 21:32:33 +00:00
|
|
|
page = loadPage(xid, nextPage);
|
|
|
|
readlock(page->rwlatch, 0);
|
2005-02-16 04:11:14 +00:00
|
|
|
} end_ret(NULLRID);
|
2004-07-31 00:27:55 +00:00
|
|
|
}
|
2005-03-10 03:19:04 +00:00
|
|
|
// printf("b"); fflush(stdout);
|
2004-07-31 00:27:55 +00:00
|
|
|
|
2007-06-01 21:32:33 +00:00
|
|
|
rid.page = page->id;
|
2004-07-31 00:27:55 +00:00
|
|
|
rid.slot -= offset;
|
|
|
|
|
2007-06-01 21:32:33 +00:00
|
|
|
unlock(page->rwlatch);
|
|
|
|
releasePage(page);
|
2005-03-10 03:19:04 +00:00
|
|
|
// printf("c"); fflush(stdout);
|
|
|
|
|
2004-07-31 00:27:55 +00:00
|
|
|
return rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define min(x, y) ((x) < (y) ? (x) : (y))
|
|
|
|
/** Would be static, but there is a unit test for this function */
|
|
|
|
unsigned int calculate_level (unsigned int number_of_pages) {
|
|
|
|
long long tmp = INDIRECT_POINTERS_PER_PAGE;
|
|
|
|
unsigned int level = 1;
|
|
|
|
while(tmp < number_of_pages) {
|
|
|
|
tmp *= INDIRECT_POINTERS_PER_PAGE;
|
|
|
|
level++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return level;
|
|
|
|
}
|
|
|
|
|
2005-02-22 03:10:54 +00:00
|
|
|
compensated_function recordid __rallocMany(int xid, int parentPage, int recordSize, int recordCount);
|
2004-08-03 02:04:56 +00:00
|
|
|
/**
|
|
|
|
@todo is there a way to implement rallocMany so that it doesn't
|
|
|
|
have to physically log pre- and post-images of the allocated space?
|
|
|
|
*/
|
2005-02-22 03:10:54 +00:00
|
|
|
compensated_function recordid rallocMany(int xid, int recordSize, int recordCount) {
|
|
|
|
recordid ret;
|
|
|
|
int page;
|
|
|
|
try_ret(NULLRID) {
|
|
|
|
page = TpageAlloc(xid/*, SLOTTED_PAGE*/);
|
|
|
|
}end_ret(NULLRID);
|
|
|
|
try_ret(NULLRID) {
|
|
|
|
ret = __rallocMany(xid, page, recordSize, recordCount);
|
|
|
|
}end_ret(NULLRID);
|
|
|
|
|
|
|
|
return ret;
|
2004-08-03 02:04:56 +00:00
|
|
|
}
|
|
|
|
|
2005-02-22 03:10:54 +00:00
|
|
|
compensated_function recordid __rallocMany(int xid, int parentPage, int recordSize, int recordCount) {
|
2004-07-31 00:27:55 +00:00
|
|
|
|
|
|
|
/* How many levels of pages do we need? */
|
|
|
|
|
|
|
|
int physical_size;
|
|
|
|
recordid rid;
|
|
|
|
|
|
|
|
if(recordSize > BLOB_THRESHOLD_SIZE) {
|
|
|
|
physical_size = sizeof(blob_record_t);
|
|
|
|
} else {
|
|
|
|
physical_size = recordSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
int records_per_page = (USABLE_SIZE_OF_PAGE - SLOTTED_PAGE_HEADER_OVERHEAD)
|
|
|
|
/ (physical_size + SLOTTED_PAGE_OVERHEAD_PER_RECORD); /* we need to take the floor */
|
|
|
|
|
|
|
|
int number_of_pages = (int)ceil( (double)recordCount / (double)records_per_page); /* need to take ceiling here */
|
2004-08-03 02:04:56 +00:00
|
|
|
|
|
|
|
Page p;
|
|
|
|
byte buffer[PAGE_SIZE];
|
|
|
|
p.memAddr = buffer;
|
|
|
|
p.rwlatch = initlock();
|
|
|
|
p.loadlatch = initlock();
|
2007-11-12 16:32:00 +00:00
|
|
|
*stasis_page_type_ptr(&p) = UNINITIALIZED_PAGE;
|
2004-08-03 02:04:56 +00:00
|
|
|
|
2004-07-31 00:27:55 +00:00
|
|
|
if(number_of_pages > 1) {
|
|
|
|
|
|
|
|
int level = calculate_level(number_of_pages);
|
|
|
|
DEBUG("recordsize = %d, physicalsize = %d, recordCount = %d, level = %d\n",
|
|
|
|
recordSize, physical_size, recordCount, level);
|
|
|
|
|
|
|
|
/* OK, now allocate the pages. */
|
|
|
|
|
|
|
|
int next_level_records_per_page = records_per_page;
|
|
|
|
|
|
|
|
for(int i = 0; i < (level - 1); i++) {
|
|
|
|
next_level_records_per_page *= INDIRECT_POINTERS_PER_PAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int newPageCount = (int)ceil((double)recordCount / (double)next_level_records_per_page);
|
2005-02-22 03:10:54 +00:00
|
|
|
int firstChildPage;
|
|
|
|
|
|
|
|
try_ret(NULLRID) {
|
|
|
|
firstChildPage = TpageAllocMany(xid, newPageCount/*, SLOTTED_PAGE*/);/*pageAllocMultiple(newPageCount); */
|
|
|
|
} end_ret(NULLRID);
|
2004-08-03 02:04:56 +00:00
|
|
|
|
2004-07-31 00:27:55 +00:00
|
|
|
int tmpRecordCount = recordCount;
|
|
|
|
int thisChildPage = firstChildPage;
|
|
|
|
|
|
|
|
while(tmpRecordCount > 0) {
|
2005-02-22 03:10:54 +00:00
|
|
|
try_ret(NULLRID) {
|
|
|
|
__rallocMany(xid, thisChildPage, recordSize, min(tmpRecordCount, next_level_records_per_page));
|
|
|
|
} end_ret(NULLRID);
|
2004-07-31 00:27:55 +00:00
|
|
|
tmpRecordCount -= next_level_records_per_page;
|
|
|
|
thisChildPage ++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
assert((thisChildPage-firstChildPage)== newPageCount);
|
|
|
|
|
|
|
|
tmpRecordCount = recordCount;
|
|
|
|
|
2004-08-03 02:04:56 +00:00
|
|
|
indirectInitialize(&p, level);
|
2004-07-31 00:27:55 +00:00
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for(tmpRecordCount = recordCount; tmpRecordCount > 0; tmpRecordCount -= next_level_records_per_page) {
|
|
|
|
|
2004-08-03 02:04:56 +00:00
|
|
|
*page_ptr(&p, i) = firstChildPage + i;
|
2004-07-31 00:27:55 +00:00
|
|
|
if(i) {
|
2004-08-03 02:04:56 +00:00
|
|
|
*maxslot_ptr(&p, i) = *maxslot_ptr(&p, i-1) + min(tmpRecordCount+1, next_level_records_per_page);
|
2004-07-31 00:27:55 +00:00
|
|
|
} else {
|
2004-08-03 02:04:56 +00:00
|
|
|
*maxslot_ptr(&p, i) = min(tmpRecordCount+1, next_level_records_per_page);
|
2004-07-31 00:27:55 +00:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(i == newPageCount);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
DEBUG("recordsize = %d, recordCount = %d, level = 0 (don't need indirect pages)\n", recordSize, recordCount);
|
2004-08-17 01:46:17 +00:00
|
|
|
|
|
|
|
/* Initialize leaves. (As SLOTTED_PAGE's) */
|
2004-07-31 00:27:55 +00:00
|
|
|
|
2007-06-07 21:53:09 +00:00
|
|
|
writelock(p.rwlatch,0);
|
2007-10-02 00:18:33 +00:00
|
|
|
stasis_slotted_initialize_page(&p);
|
2004-08-03 02:04:56 +00:00
|
|
|
p.id = parentPage;
|
|
|
|
for(int i = 0; i < recordCount; i++) {
|
|
|
|
/* Normally, we would worry that the page id isn't set, but
|
|
|
|
we're discarding the recordid returned by page ralloc
|
|
|
|
anyway. */
|
2007-10-02 00:18:33 +00:00
|
|
|
recordid rid = stasis_record_alloc_begin(xid, &p, recordSize);
|
|
|
|
stasis_record_alloc_done(xid, &p, rid);
|
2004-08-03 02:04:56 +00:00
|
|
|
}
|
2007-06-07 21:53:09 +00:00
|
|
|
unlock(p.rwlatch);
|
2004-08-03 02:04:56 +00:00
|
|
|
}
|
2005-02-22 03:10:54 +00:00
|
|
|
try_ret(NULLRID) {
|
|
|
|
TpageSet(xid, parentPage, p.memAddr);
|
|
|
|
} end_ret(NULLRID);
|
2004-07-31 00:27:55 +00:00
|
|
|
|
2004-08-03 02:04:56 +00:00
|
|
|
rid.page = parentPage;
|
|
|
|
rid.slot = RECORD_ARRAY;
|
|
|
|
rid.size = recordSize;
|
2004-07-31 00:27:55 +00:00
|
|
|
|
2004-08-03 02:04:56 +00:00
|
|
|
deletelock(p.rwlatch);
|
|
|
|
deletelock(p.loadlatch);
|
2004-07-31 00:27:55 +00:00
|
|
|
|
2004-08-03 02:04:56 +00:00
|
|
|
return rid;
|
|
|
|
}
|
2007-06-07 21:53:09 +00:00
|
|
|
|
2005-02-16 04:11:14 +00:00
|
|
|
compensated_function int indirectPageRecordCount(int xid, recordid rid) {
|
|
|
|
Page * p;
|
|
|
|
try_ret(-1){
|
|
|
|
p = loadPage(xid, rid.page);
|
|
|
|
}end_ret(-1);
|
2007-06-01 21:32:33 +00:00
|
|
|
readlock(p->rwlatch, 0);
|
2004-08-03 02:04:56 +00:00
|
|
|
int i = 0;
|
|
|
|
unsigned int ret;
|
2007-10-02 00:18:33 +00:00
|
|
|
if(*stasis_page_type_ptr(p) == INDIRECT_PAGE) {
|
2004-08-03 02:04:56 +00:00
|
|
|
|
|
|
|
while(*maxslot_ptr(p, i) > 0) {
|
|
|
|
i++;
|
2004-07-31 00:27:55 +00:00
|
|
|
}
|
2004-08-03 02:04:56 +00:00
|
|
|
if(!i) {
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
ret = (*maxslot_ptr(p, i-1)) - 1;
|
|
|
|
}
|
2007-10-02 00:18:33 +00:00
|
|
|
} else if (*stasis_page_type_ptr(p) == SLOTTED_PAGE) {
|
2004-07-31 00:27:55 +00:00
|
|
|
|
2004-08-03 02:04:56 +00:00
|
|
|
int numslots = *numslots_ptr(p);
|
|
|
|
ret = 0;
|
|
|
|
for(int i = 0; i < numslots; i++) {
|
2007-08-20 16:04:44 +00:00
|
|
|
if(*slot_ptr(p,i) != INVALID_SLOT) {
|
2004-08-03 02:04:56 +00:00
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
2004-07-31 00:27:55 +00:00
|
|
|
|
2004-08-03 02:04:56 +00:00
|
|
|
} else {
|
2005-02-16 04:11:14 +00:00
|
|
|
printf("Unknown page type in indirectPageRecordCount\n");
|
2004-08-03 02:04:56 +00:00
|
|
|
abort();
|
2004-07-31 00:27:55 +00:00
|
|
|
}
|
2007-06-01 21:32:33 +00:00
|
|
|
|
|
|
|
unlock(p->rwlatch);
|
2004-08-03 02:04:56 +00:00
|
|
|
releasePage(p);
|
|
|
|
return ret;
|
2004-07-31 00:27:55 +00:00
|
|
|
}
|
2007-07-18 20:09:14 +00:00
|
|
|
|
|
|
|
static int notSupported(int xid, Page * p) { return 0; }
|
|
|
|
|
|
|
|
void indirectLoaded(Page *p) {
|
2007-10-02 00:18:33 +00:00
|
|
|
p->LSN = *stasis_page_lsn_ptr(p);
|
2007-07-18 20:09:14 +00:00
|
|
|
}
|
|
|
|
void indirectFlushed(Page *p) {
|
2007-10-02 00:18:33 +00:00
|
|
|
*stasis_page_lsn_ptr(p) = p->LSN;
|
2007-07-18 20:09:14 +00:00
|
|
|
}
|
2007-08-20 21:58:20 +00:00
|
|
|
void indirectCleanup(Page *p) { }
|
2007-07-18 20:09:14 +00:00
|
|
|
static page_impl pi = {
|
|
|
|
INDIRECT_PAGE,
|
|
|
|
0, //read,
|
|
|
|
0, //write,
|
|
|
|
0, //readDone
|
|
|
|
0, //writeDone
|
|
|
|
0, //getType,
|
|
|
|
0, //setType,
|
|
|
|
0, //getLength,
|
|
|
|
0, //recordFirst,
|
|
|
|
0, //recordNext,
|
|
|
|
notSupported, // is block supported
|
|
|
|
0, //pageGenericBlockFirst,
|
|
|
|
0, //pageGenericBlockNext,
|
|
|
|
0, //pageGenericBlockDone,
|
|
|
|
0, //freespace,
|
|
|
|
0, //compact,
|
|
|
|
0, //preRalloc,
|
|
|
|
0, //postRalloc,
|
|
|
|
0, //Free,
|
|
|
|
0, //XXX page_impl_dereference_identity,
|
|
|
|
indirectLoaded,
|
|
|
|
indirectFlushed,
|
2007-08-20 21:58:20 +00:00
|
|
|
indirectCleanup
|
2007-07-18 20:09:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
@todo Flesh out INDIRECT_PAGE's implementation of new PAGE_API, or
|
|
|
|
remove INDIRECT_PAGE from Stasis.
|
|
|
|
*/
|
|
|
|
page_impl indirectImpl() {
|
|
|
|
return pi;
|
|
|
|
}
|