2005-05-05 21:26:40 +00:00
|
|
|
/*---
|
|
|
|
This software is copyrighted by the Regents of the University of
|
|
|
|
California, and other parties. The following terms apply to all files
|
|
|
|
associated with the software unless explicitly disclaimed in
|
|
|
|
individual files.
|
|
|
|
|
|
|
|
The authors hereby grant permission to use, copy, modify, distribute,
|
|
|
|
and license this software and its documentation for any purpose,
|
|
|
|
provided that existing copyright notices are retained in all copies
|
|
|
|
and that this notice is included verbatim in any distributions. No
|
|
|
|
written agreement, license, or royalty fee is required for any of the
|
|
|
|
authorized uses. Modifications to this software may be copyrighted by
|
|
|
|
their authors and need not follow the licensing terms described here,
|
|
|
|
provided that the new terms are clearly indicated on the first page of
|
|
|
|
each file where they apply.
|
|
|
|
|
|
|
|
IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
|
|
|
|
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
|
|
|
ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
|
|
|
|
DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
|
|
|
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
|
|
|
|
NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND
|
|
|
|
THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
|
|
|
|
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
|
|
|
|
GOVERNMENT USE: If you are acquiring this software on behalf of the
|
|
|
|
U.S. government, the Government shall have only "Restricted Rights" in
|
|
|
|
the software and related documentation as defined in the Federal
|
|
|
|
Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you are
|
|
|
|
acquiring the software on behalf of the Department of Defense, the
|
|
|
|
software shall be classified as "Commercial Computer Software" and the
|
|
|
|
Government shall have only "Restricted Rights" as defined in Clause
|
|
|
|
252.227-7013 (c) (1) of DFARs. Notwithstanding the foregoing, the
|
|
|
|
authors grant the U.S. Government and others acting in its behalf
|
|
|
|
permission to use and distribute the software in accordance with the
|
|
|
|
terms specified in this license.
|
|
|
|
---*/
|
|
|
|
|
|
|
|
#include "../check_includes.h"
|
|
|
|
|
2007-06-11 21:36:57 +00:00
|
|
|
#include <stasis/transactional.h>
|
2008-04-13 04:02:57 +00:00
|
|
|
#include <stasis/page.h>
|
2005-11-08 20:55:18 +00:00
|
|
|
|
2005-05-05 21:26:40 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#define LOG_NAME "check_bTree.log"
|
|
|
|
#define NUM_ENTRIES 100000
|
|
|
|
/** @test
|
|
|
|
*/
|
2005-10-30 00:39:25 +00:00
|
|
|
|
2006-05-25 00:02:46 +00:00
|
|
|
void testFunctions();
|
|
|
|
int SimpleExample();
|
2005-11-08 20:55:18 +00:00
|
|
|
|
|
|
|
START_TEST(bTreeTest)
|
2005-10-30 00:39:25 +00:00
|
|
|
{
|
2005-11-08 20:55:18 +00:00
|
|
|
Tinit();
|
|
|
|
testFunctions();
|
|
|
|
SimpleExample();
|
|
|
|
// printf("\n end of practice run\n");
|
2005-10-30 00:39:25 +00:00
|
|
|
Tdeinit();
|
|
|
|
} END_TEST
|
|
|
|
|
2005-11-08 20:55:18 +00:00
|
|
|
/* This only takes in a page that is already initialized as a fixed page.
|
2009-04-14 20:21:05 +00:00
|
|
|
and has been initialized as a BTree Node, which involves placing an
|
|
|
|
index to tell how many entries are currently valid.
|
|
|
|
For now it will just return false if you try to add something to it
|
|
|
|
when it is already full.
|
2005-11-08 20:55:18 +00:00
|
|
|
*/
|
2008-10-03 02:42:25 +00:00
|
|
|
int insert(int xid, Page* p, int valueIn){
|
2005-11-08 20:55:18 +00:00
|
|
|
printf ("\nbegin insert\n");
|
|
|
|
int DEBUG = 0;
|
|
|
|
int DEBUGERROR = 1;
|
|
|
|
int DEBUGERROR2 = 0;
|
|
|
|
int DEBUGSTATEMENTS = 1;
|
|
|
|
int DEBUGMALLOC = 1; // 1 makes it malloc the space
|
2005-10-30 00:39:25 +00:00
|
|
|
|
2005-11-08 20:55:18 +00:00
|
|
|
//printf("\npage->id = %d\n", p->id);
|
|
|
|
|
|
|
|
// make a copy of the rid - so we don't effect the caller's copy
|
2008-10-03 02:42:25 +00:00
|
|
|
recordid rid = { p->id, 0, sizeof(int)};
|
2005-11-08 20:55:18 +00:00
|
|
|
|
|
|
|
|
2009-04-14 20:21:05 +00:00
|
|
|
// if DEBUGERROR ==1 this causes a seg fault below!
|
2006-11-07 20:36:59 +00:00
|
|
|
if (DEBUGERROR) {printf("\n***page->id = %lld\n", p->id);}
|
2008-09-28 01:45:18 +00:00
|
|
|
printf("\n***rid.page = %lld\n\n", (long long)rid.page);
|
2005-11-08 20:55:18 +00:00
|
|
|
|
|
|
|
|
2008-09-28 01:45:18 +00:00
|
|
|
if(DEBUG) {printf("\nrid.page = %lld\n", (long long)rid.page);}
|
|
|
|
if(DEBUG) {printf("\nrid.slot = %lld\n", (long long)rid.slot);}
|
2005-10-30 00:39:25 +00:00
|
|
|
|
2009-04-14 20:21:05 +00:00
|
|
|
|
2005-11-08 20:55:18 +00:00
|
|
|
// Figure out how many entries are in the node
|
|
|
|
rid.slot = 0; // need to get the variable in slot 0
|
|
|
|
|
|
|
|
byte * countBuff;/// AHHH - i wasn't mallocing space for this!
|
|
|
|
|
|
|
|
if(DEBUGMALLOC ) {countBuff = (byte *) malloc (sizeof(int));}
|
|
|
|
|
|
|
|
if (DEBUGSTATEMENTS) {printf("\nDebug1\n");}
|
|
|
|
|
2007-10-02 00:18:33 +00:00
|
|
|
stasis_record_read(xid, p, rid, countBuff); // read the value of count from slot 0
|
2005-11-08 20:55:18 +00:00
|
|
|
|
|
|
|
if (DEBUGSTATEMENTS) {printf("\nDebug2\n");}
|
|
|
|
|
|
|
|
int * countInt = (int *) countBuff; // cast it to an int *
|
|
|
|
|
|
|
|
// set rid.slot to be the max slot entry used.
|
|
|
|
rid.slot = *countInt;
|
|
|
|
|
|
|
|
printf("\nrid2slot = %d\n", rid.slot);
|
|
|
|
|
2009-04-14 20:21:05 +00:00
|
|
|
// *recordcount_ptr(p) = last accessible index on the page.
|
|
|
|
int max_index = stasis_fixed_records_per_page(rid.size); // rcs made this change.
|
2007-06-07 21:53:09 +00:00
|
|
|
// int max_index = *recordcount_ptr(p); // recordcount_ptr is the number of slots currently allocated on the page.
|
|
|
|
// but this code seems to do it's own allocation(?)
|
2005-11-08 20:55:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
//THESE LINES WERE CAUSING A SEGFAULT! *******************************************
|
2009-04-14 20:21:05 +00:00
|
|
|
if (DEBUGERROR2) {printf("\nx = %d\n", max_index);} // This causes a segfault after Debug2
|
|
|
|
|
2005-11-08 20:55:18 +00:00
|
|
|
// HACK! TO FIX THE ABOVE PROBLEM!
|
2007-06-07 21:53:09 +00:00
|
|
|
// max_index = 1021;
|
2005-11-08 20:55:18 +00:00
|
|
|
|
|
|
|
// assert that max_index is greater than our record of how many
|
2009-04-14 20:21:05 +00:00
|
|
|
// entries we currently have on the page.
|
2005-11-08 20:55:18 +00:00
|
|
|
assert(max_index>rid.slot);
|
|
|
|
|
|
|
|
|
|
|
|
// check to see if we have room to add the entry.
|
|
|
|
if (rid.slot == max_index){
|
|
|
|
// we can't add any more entries to this node
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// the default location to put the new value is location 1.
|
|
|
|
int insertLocation = 1;
|
|
|
|
|
|
|
|
// Iterating DOWN through the slots. Stop when we reach 0.
|
|
|
|
// Will also break out of the while loop if we've found where
|
|
|
|
// to insert the new value.
|
|
|
|
while ( (rid.slot >= 1) && (insertLocation == 1)){
|
|
|
|
|
2009-04-14 20:21:05 +00:00
|
|
|
// TODO: JuSt haven't filled in the code here yet...
|
2005-11-08 20:55:18 +00:00
|
|
|
insertLocation =2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert the input valueIn into a byte array
|
2006-05-25 00:02:46 +00:00
|
|
|
// byte * valueInBuff = (byte *) & valueIn;
|
2005-11-08 20:55:18 +00:00
|
|
|
|
|
|
|
// get the rid ready to write to the insertLocation (determined above)
|
|
|
|
rid.slot = insertLocation;
|
|
|
|
|
|
|
|
// fixedWrite(p, rid, valueInBuff); // page/fixed.c:58: checkRid: Assertion `page->id == rid.page' failed.
|
2006-11-07 20:36:59 +00:00
|
|
|
printf("\n***page->id = %lld\n", p->id);
|
2008-09-28 01:45:18 +00:00
|
|
|
printf("\n***rid.page = %lld\n", (long long)rid.page);
|
2005-11-08 20:55:18 +00:00
|
|
|
|
2009-04-14 20:21:05 +00:00
|
|
|
|
2006-05-25 00:02:46 +00:00
|
|
|
return 0;
|
2005-11-08 20:55:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This takes a page that is already initialized and a
|
2009-04-14 20:21:05 +00:00
|
|
|
corresponding rid and initalizes the count value for
|
|
|
|
it to be a BTreeNode. Just puts the value 0 in the
|
|
|
|
first index of the page.
|
2005-11-08 20:55:18 +00:00
|
|
|
*/
|
2008-10-03 02:42:25 +00:00
|
|
|
void initializeNewBTreeNode(int xid, Page* p){
|
2009-04-14 20:21:05 +00:00
|
|
|
|
|
|
|
// need access to the first slot
|
2008-10-03 02:42:25 +00:00
|
|
|
recordid rid = { p->id, 0, sizeof(int)};
|
2005-11-08 20:55:18 +00:00
|
|
|
|
|
|
|
// prepare the value to go into the first slot
|
2009-04-14 20:21:05 +00:00
|
|
|
int countInt = 0;
|
2005-11-08 20:55:18 +00:00
|
|
|
byte * countBuff = (byte *) & countInt;
|
2009-04-14 20:21:05 +00:00
|
|
|
|
2005-11-08 20:55:18 +00:00
|
|
|
// write the count out
|
2009-04-14 20:21:05 +00:00
|
|
|
stasis_record_write(xid, p, 1, rid, countBuff);
|
2005-11-08 20:55:18 +00:00
|
|
|
|
|
|
|
}
|
2006-05-25 00:02:46 +00:00
|
|
|
void testFunctions(){
|
2005-11-08 20:55:18 +00:00
|
|
|
printf("testing functions");
|
2009-04-14 20:21:05 +00:00
|
|
|
|
2005-11-08 20:55:18 +00:00
|
|
|
// getting things ready
|
|
|
|
int xid = Tbegin();
|
2008-10-03 02:42:25 +00:00
|
|
|
pageid_t pageid1 = TfixedPageAlloc(xid, sizeof(int)); // this does the initialize
|
2005-11-08 20:55:18 +00:00
|
|
|
Page * p1 = loadPage(xid, pageid1);
|
2007-06-01 21:32:33 +00:00
|
|
|
|
2005-11-08 20:55:18 +00:00
|
|
|
// calling functions
|
2008-09-28 05:50:59 +00:00
|
|
|
writelock(p1->rwlatch,0);
|
2008-10-03 02:42:25 +00:00
|
|
|
initializeNewBTreeNode(xid, p1);
|
|
|
|
insert(xid, p1, 3);
|
2008-09-28 05:50:59 +00:00
|
|
|
unlock(p1->rwlatch);
|
2005-11-08 20:55:18 +00:00
|
|
|
|
|
|
|
// cleaning up
|
2007-06-01 21:32:33 +00:00
|
|
|
|
2005-10-30 00:39:25 +00:00
|
|
|
releasePage(p1);
|
|
|
|
Tcommit(xid);
|
|
|
|
}
|
2005-11-08 20:55:18 +00:00
|
|
|
|
2005-05-05 21:26:40 +00:00
|
|
|
|
2005-10-30 00:39:25 +00:00
|
|
|
int SimpleExample(){
|
|
|
|
|
|
|
|
int DEBUGP = 0;
|
2006-05-25 00:02:46 +00:00
|
|
|
// int DEBUGT = 0;
|
|
|
|
// int DEBUGA = 0;
|
2005-10-30 00:39:25 +00:00
|
|
|
int xid = Tbegin();
|
2005-11-08 20:55:18 +00:00
|
|
|
|
2005-10-30 00:39:25 +00:00
|
|
|
/* Where to find stuff
|
|
|
|
* ****************************
|
2007-06-11 21:36:57 +00:00
|
|
|
* TpageAlloc -> stasis/src/stasis/operations/pageOperations.c
|
|
|
|
* TfixedPageAlloc -> stasis/src/stasis/operations/pageOperations.c
|
|
|
|
* fixedPageInitailze -> stasis/src/stasis/page/fixed.c
|
2005-10-30 00:39:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-10-03 02:42:25 +00:00
|
|
|
pageid_t pageid1 = TfixedPageAlloc(xid, sizeof(int)); // this does the initialize
|
2009-04-14 20:21:05 +00:00
|
|
|
|
2005-11-08 20:55:18 +00:00
|
|
|
Page * p1 = loadPage(xid, pageid1);
|
2007-06-01 21:32:33 +00:00
|
|
|
writelock(p1->rwlatch, 0);
|
2006-11-07 20:36:59 +00:00
|
|
|
if(DEBUGP) { printf("\n**** page->id = %lld\n", p1->id);}
|
2005-10-30 00:39:25 +00:00
|
|
|
|
2009-04-14 20:21:05 +00:00
|
|
|
/* check consistency between rid & page's values
|
2005-11-08 20:55:18 +00:00
|
|
|
* for number of slots and record size */
|
2008-10-03 02:42:25 +00:00
|
|
|
assert (p1->id == pageid1);
|
2005-10-30 00:39:25 +00:00
|
|
|
|
|
|
|
|
2005-11-08 20:55:18 +00:00
|
|
|
/* check to make sure page is recorded as a FIXED_PAGE */
|
2007-10-02 00:18:33 +00:00
|
|
|
assert( *stasis_page_type_ptr(p1) == FIXED_PAGE);
|
2009-04-14 20:21:05 +00:00
|
|
|
|
2008-10-03 02:42:25 +00:00
|
|
|
if (DEBUGP) { printf("\n%lld\n", (long long)pageid1); }
|
2005-11-08 20:55:18 +00:00
|
|
|
byte * b1 = (byte *) malloc (sizeof (int));
|
|
|
|
byte * b2 = (byte *) malloc (sizeof (int));
|
|
|
|
byte * b3 = (byte *) malloc (sizeof (int));
|
2009-04-14 20:21:05 +00:00
|
|
|
// int x = *recordcount_ptr(p1);
|
2007-06-07 21:53:09 +00:00
|
|
|
int x = 42; // rcs - recordcount_ptr is no longer exposed here...
|
2008-10-03 02:42:25 +00:00
|
|
|
int y = 0; //rid1.slot;
|
2005-11-08 20:55:18 +00:00
|
|
|
int z = 256;
|
|
|
|
|
|
|
|
b1 = (byte *) & x;
|
|
|
|
b2 = (byte *) & y;
|
|
|
|
b3 = (byte *) & z;
|
2009-04-14 20:21:05 +00:00
|
|
|
|
2005-11-08 20:55:18 +00:00
|
|
|
int * x1 = (int *) b1;
|
2009-04-14 20:21:05 +00:00
|
|
|
|
|
|
|
|
2005-11-08 20:55:18 +00:00
|
|
|
if (DEBUGP) { printf("\nx = %d\n", x);}
|
|
|
|
if (DEBUGP) { printf("\nb1 = %d\n", *b1);}
|
|
|
|
if (DEBUGP) { printf("\nx1 = %d\n", *x1);}
|
|
|
|
if (DEBUGP) { printf("\ny = %d\n", y);}
|
|
|
|
if (DEBUGP) { printf("\nb2 = %d\n", *b2);}
|
|
|
|
if (DEBUGP) { printf("\nz = %d\n", z);}
|
|
|
|
if (DEBUGP) { printf("\nb3 = %d\n", *b3);}
|
2009-04-14 20:21:05 +00:00
|
|
|
|
2008-10-03 02:42:25 +00:00
|
|
|
recordid rid1 = { pageid1, 0,sizeof(int)};
|
2007-06-01 21:32:33 +00:00
|
|
|
|
|
|
|
// @todo This is a messy way to do this...
|
2008-09-28 05:50:59 +00:00
|
|
|
|
2008-10-03 02:42:25 +00:00
|
|
|
stasis_record_write(xid, p1, 1, rid1, b1);
|
|
|
|
stasis_record_read(xid, p1, rid1, b2);
|
2005-11-08 20:55:18 +00:00
|
|
|
if (DEBUGP) { printf("\nb2** = %d\n",*((int *) b2));}
|
2005-10-30 00:39:25 +00:00
|
|
|
|
2009-04-14 20:21:05 +00:00
|
|
|
// initializeNewBTreeNode(p1, rid1);
|
2005-10-30 00:39:25 +00:00
|
|
|
|
2008-09-28 05:50:59 +00:00
|
|
|
unlock(p1->rwlatch);
|
|
|
|
|
2005-10-30 00:39:25 +00:00
|
|
|
releasePage(p1);
|
|
|
|
Tcommit(xid);
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-05-05 21:26:40 +00:00
|
|
|
/** @test
|
|
|
|
*/
|
|
|
|
|
2005-10-30 00:39:25 +00:00
|
|
|
#define NUM_ENTRIES_XACT 10000
|
2005-05-05 21:26:40 +00:00
|
|
|
|
|
|
|
Suite * check_suite(void) {
|
|
|
|
Suite *s = suite_create("bTree");
|
|
|
|
/* Begin a new test */
|
|
|
|
TCase *tc = tcase_create("simple");
|
|
|
|
|
2009-04-14 20:21:05 +00:00
|
|
|
tcase_set_timeout(tc, 0); // disable timeouts if test takes more than 2 sec - it would get killed
|
2005-10-30 00:39:25 +00:00
|
|
|
|
2005-05-05 21:26:40 +00:00
|
|
|
/* Sub tests are added, one per line, here */
|
|
|
|
tcase_add_test(tc, bTreeTest);
|
2005-11-08 20:55:18 +00:00
|
|
|
|
2005-10-30 00:39:25 +00:00
|
|
|
// tcase_add_test(tc, simpleLinearHashTest); // put back in if playing with hashtable
|
|
|
|
|
2009-04-14 20:21:05 +00:00
|
|
|
|
2005-05-05 21:26:40 +00:00
|
|
|
/* --------------------------------------------- */
|
2009-04-14 20:21:05 +00:00
|
|
|
|
|
|
|
tcase_add_checked_fixture(tc, setup, teardown);// leave stuff below here.
|
2005-05-05 21:26:40 +00:00
|
|
|
|
|
|
|
suite_add_tcase(s, tc);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "../check_setup.h"
|