Пример #1
0
/**
* 随机构造总大小为dataSize的记录插入到table中,
* 记录ID从在[minidx, minid]之间随机生成
* @param table 数据表
* @param dataSize 记录总大小
* @param maxid 最大id 
* @param minid 最小id
* @return 记录数
*/
uint TNTCountTable::populate(Session *session, TNTTable *table, TNTOpInfo *opInfo, u64 *dataSize, u64 maxId, u64 minId) {
	assert(session->getTrans() != NULL);
	u64 volumnCnt = *dataSize;
	uint recCnt = 0;
	uint dupIdx;
	int startId = 0;
	Record *rec;
	RowId rid;
	while (true) {
		// 创建记录
		u64 id = (u64)RandomGen::nextInt((u32)minId, (u32)maxId);
		rec = createRecord((u64)recCnt, (int)startId++);
		if (volumnCnt < getRecordSize()) {
			freeRecord(rec);
			break;
		}
		// 插入记录
		rid = table->insert(session, rec->m_data, &dupIdx, opInfo);
		if (rid != INVALID_ROW_ID) {
			recCnt++;
			volumnCnt -= getRecordSize();
		}
		freeRecord(rec);
	}

	*dataSize -= volumnCnt;
	return recCnt;
}
Пример #2
0
db_int64 CSqlCommand::execute(char *sqlstr)
{
	if (sqlstr == NULL || sqlstr == "")
	{
		PRINTFERROR();
		return -1;
	}

	if (m_pMySql == NULL)
	{
		PRINTFERROR();
		m_bIsValid = false;
		return -1;
	}
	
	int err = -1;
	if ((err = mysql_real_query(m_pMySql, sqlstr, (db_uint32)strlen(sqlstr))) != 0)
	{
		MYSQLERROR_SQL(m_pMySql, sqlstr);
		freeRecord();

		if(err == 1) // sql执行失败
		{
			if (mysql_ping(m_pMySql) != 0)
			{
				m_bIsValid = false;				
			}
		}

		// err > 1 需要重新连接
		else
		{
			m_bIsValid = false;	
		}	
		const char *err_str = mysql_error(m_pMySql);
		printf("Sql execute error %s\n",err_str);
		return -1;
	}

	freeRecord();
	m_pRes = mysql_store_result(m_pMySql);
	if (m_pRes)
	{
		m_nRowCount = (db_int32)mysql_num_rows(m_pRes);
		m_nFieldCount = (db_int32)mysql_num_fields(m_pRes);
		m_nAffectRow = (db_int32)mysql_affected_rows(m_pMySql);
	}


	return (db_int64)mysql_affected_rows(m_pMySql);
}
/**
 * Deletes a record from table
 *
 * rel = table handle
 * id = id of the record to be deleted
 *
 */
RC deleteRecord(RM_TableData *rel, RID id) {

	//Sanity Checks
	if (rel == NULL) {
		THROW(RC_INVALID_HANDLE, "Table handle is invalid");
	}

	Record *record;
	//Read the record to be deleted
	createRecord(&record, rel->schema);
	RC rc = readRecord(rel, id, &record);
	if (rc != RC_OK) {
		THROW(RC_REC_MGR_DELETE_REC_FAILED, "Delete record failed");
	}

	//Set tomstone bit
	record->nullMap |= 1 << 15;

	//Write updated record
	rc = writeRecord(rel, record);
	freeRecord(record);

	if (rc == RC_OK)
		((RM_TableMgmtData *) rel->mgmtData)->tupleCount--;
	else {
		THROW(RC_REC_MGR_DELETE_REC_FAILED, "Delete record failed");
	}

	return rc;
}
Пример #4
0
/*
 * allocRecord()
 *
 *   Allocates the requested number of records in a sequential run.
 *   It returns an offset to the start of the first record.
 */
SysStatus
LocalRecordMap::allocRecord(PsoType type, ObjTokenID *offset)
{
    sval rc;

    rc = internal_allocRecord(type, offset);

    //    err_printf("LocalRecordMap::allocrecord(0x%p) for type %d got id %ld\n",
    //	       this, (uval32) type, (uval) offset->id);

    // We have to "prepend" this ID with the RecordMap ID
    offset->id = localIDtoObjTokenID(offset->id);

    if (_FAILURE(rc)) {
	KFS_DPRINTF(DebugMask::RECORD_MAP,
		    "LocalRecordMap::allocRecord() Problem allocating record"
		    " type=%u\n", type);
	// release the entries we've allocated
	freeRecord(offset);
	return rc;
    }

    KFS_DPRINTF(DebugMask::RECORD_MAP, "AllocRecord for type %d got id %ld\n",
		(uval32) type, (uval) offset->id);

    //    err_printf("LocalRecordMap::allocrecord(0x%p) for type %d got id %ld\n",
    //	       this, (uval32) type, (uval) offset->id);

    // write back the ServerObject type for this record
    setRecordType(offset, type);

    // Get location for this ServerObject
    // figure out what the location should be based on the type
    rc = globals->soAlloc->locationAlloc(type, offset, this);
    if (_FAILURE(rc)) {
	//        err_printf("LocalRecordMap::allocRecord() Problem allocating ServerObject"
	//		   " type=%u\n", type);
	passertMsg(0, "here\n");
        // release the entries we've allocated
        freeRecord(offset);
        return rc;
    }

    return rc;
}
Пример #5
0
void CSqlCommand::close()
{
	freeRecord();
	if (m_pMySql)
	{
		mysql_close(m_pMySql);
		m_pMySql = NULL;
		mysql_library_end(); // 使用mysql_real_connect之后,断开连接必须要,否则有可能内存泄露
	}
}
/**
 * Close scan handle and release resources
 *
 * scan = scan handle
 */
RC closeScan(RM_ScanHandle *scan) {
	int i;
	RM_ScanIterator* iter = (RM_ScanIterator*) scan->mgmtData;

	for (i = 0; i < iter->totalRecords; ++i) {
		if (iter->records[i]) {
			freeRecord(iter->records[i]);
			iter->records[i] = NULL;
		}
	}

	free(iter->records);
	free(iter);

	//All OK
	return RC_OK;
}
Пример #7
0
void destroy_tree_nodes(node * root, void freeRecord(void *)) {
    int i;
    if (root == NULL) return;
    if (root->is_leaf) {
        for (i = 0; i < root->num_keys; i++) {
            if (freeRecord != NULL) {
                freeRecord(((record*) root->pointers[i])->value);
            }
            free(root->pointers[i]);
        }
    } else {
        for (i = 0; i < root->num_keys + 1; i++) {
            destroy_tree_nodes(root->pointers[i], freeRecord);
        }
    }
    free(root->pointers);
    free(root->keys);
    free(root);
}
Пример #8
0
void
testRecords (void)
{
  TestRecord expected[] = { 
    {1, "aaaa", 3}, 
  };
  Schema *schema;
  Record *r;
  Value *value;
  testName = "test creating records and manipulating attributes";

  // check attributes of created record
  schema = testSchema();
  r = fromTestRecord(schema, expected[0]);

    getAttr(r, schema, 0, &value);
  printf("\n Value is %d",value->v.intV);
   OP_TRUE(stringToValue("i1"), value, valueEquals, "first attr");
  freeVal(value);

  getAttr(r, schema, 1, &value);
  OP_TRUE(stringToValue("saaaa"), value, valueEquals, "second attr");
  freeVal(value);

  getAttr(r, schema, 2, &value);
  OP_TRUE(stringToValue("i3"), value, valueEquals, "third attr");
  freeVal(value);

  //modify attrs
  setAttr(r, schema, 2, stringToValue("i4"));
  getAttr(r, schema, 2, &value);
  OP_TRUE(stringToValue("i4"), value, valueEquals, "third attr after setting");
  freeVal(value);

  freeRecord(r);
  TEST_DONE();
}
void testScansTwo (void)
{
  RM_TableData *table = (RM_TableData *) malloc(sizeof(RM_TableData));
  TestRecord inserts[] = { 
    {1, "aaaa", 3}, 
    {2, "bbbb", 2},
    {3, "cccc", 1},
    {4, "dddd", 3},
    {5, "eeee", 5},
    {6, "ffff", 1},
    {7, "gggg", 3},
    {8, "hhhh", 3},
    {9, "iiii", 2},
    {10, "jjjj", 5},
  };
  bool foundScan[] = {
    FALSE,
    FALSE,
    FALSE,
    FALSE,
    FALSE,
    FALSE,
    FALSE,
    FALSE,
    FALSE,
    FALSE
  };
  int numInserts = 10, i;
  Record *r;
  RID *rids;
  Schema *schema;
  RM_ScanHandle *sc = (RM_ScanHandle *) malloc(sizeof(RM_ScanHandle));
  Expr *sel, *left, *right, *first, *se;
  int rc;

  testName = "test creating a new table and inserting tuples";
  schema = testSchema();
  rids = (RID *) malloc(sizeof(RID) * numInserts);
  
  TEST_CHECK(initRecordManager(NULL));
  TEST_CHECK(createTable("test_table_r",schema));
  TEST_CHECK(openTable(table, "test_table_r"));
  
  // insert rows into table
  for(i = 0; i < numInserts; i++)
  {
    r = fromTestRecord(schema, inserts[i]);
    TEST_CHECK(insertRecord(table,r)); 
    rids[i] = r->id;
  }

  TEST_CHECK(closeTable(table));
  TEST_CHECK(openTable(table, "test_table_r"));

  // Select 1 record with INT in condition a=2.
  MAKE_CONS(left, stringToValue("i2"));
  MAKE_ATTRREF(right, 0);
  MAKE_BINOP_EXPR(sel, left, right, OP_COMP_EQUAL);
  createRecord(&r, schema);
  TEST_CHECK(startScan(table, sc, sel));
  while((rc = next(sc, r)) == RC_OK)
  {
     ASSERT_EQUALS_RECORDS(fromTestRecord(schema, inserts[1]), r, schema, "compare records");
  }
  if (rc != RC_NO_TUPLES)
    TEST_CHECK(rc);
  TEST_CHECK(closeScan(sc));
  
  // Select 1 record with STRING in condition b='ffff'.
  MAKE_CONS(left, stringToValue("sffff"));
  MAKE_ATTRREF(right, 1);
  MAKE_BINOP_EXPR(sel, left, right, OP_COMP_EQUAL);
  createRecord(&r, schema);
  TEST_CHECK(startScan(table, sc, sel));
  while((rc = next(sc, r)) == RC_OK)
  {
     ASSERT_EQUALS_RECORDS(fromTestRecord(schema, inserts[5]), r, schema, "compare records");
     serializeRecord(r, schema);
  }
  if (rc != RC_NO_TUPLES)
    TEST_CHECK(rc);
  TEST_CHECK(closeScan(sc));
  
  // Select all records, with condition being false
  MAKE_CONS(left, stringToValue("i4"));
  MAKE_ATTRREF(right, 2);
  MAKE_BINOP_EXPR(first, right, left, OP_COMP_SMALLER);
  MAKE_UNOP_EXPR(se, first, OP_BOOL_NOT);
  TEST_CHECK(startScan(table, sc, se));
    while((rc = next(sc, r)) == RC_OK)
    {
     serializeRecord(r, schema);
     for(i = 0; i < numInserts; i++)
     {
       if (memcmp(fromTestRecord(schema, inserts[i])->data,r->data,getRecordSize(schema)) == 0)
	     foundScan[i] = TRUE;
     }
    }
  if (rc != RC_NO_TUPLES)
    TEST_CHECK(rc);
  TEST_CHECK(closeScan(sc));
  
  ASSERT_TRUE(!foundScan[0], "not greater than four");
  ASSERT_TRUE(foundScan[4], "greater than four");
  ASSERT_TRUE(foundScan[9], "greater than four");

  // clean up
  TEST_CHECK(closeTable(table));
  TEST_CHECK(deleteTable("test_table_r"));
  TEST_CHECK(shutdownRecordManager());

  freeRecord(r);
  free(table);
  free(sc);
  freeExpr(sel);
  TEST_DONE();
}
void 
testInsertManyRecords(void)
{
  RM_TableData *table = (RM_TableData *) malloc(sizeof(RM_TableData));
  TestRecord inserts[] = { 
    {1, "aaaa", 3}, 
    {2, "bbbb", 2},
    {3, "cccc", 1},
    {4, "dddd", 3},
    {5, "eeee", 5},
    {6, "ffff", 1},
    {7, "gggg", 3},
    {8, "hhhh", 3},
    {9, "iiii", 2},
    {10, "jjjj", 5},
  };
  TestRecord realInserts[10000];
  TestRecord updates[] = {
    {3333, "iiii", 6}
  };
  int numInserts = 10000, i, numcheck=50;
  int randomRec = 3333;
  Record *r;
  RID *rids;
  Schema *schema;
  testName = "test creating a new table and inserting 10000 records then updating record from rids[3333]";
  schema = testSchema();
  rids = (RID *) malloc(sizeof(RID) * numInserts);
  
  TEST_CHECK(initRecordManager(NULL));
  TEST_CHECK(createTable("test_table_t",schema));
  TEST_CHECK(openTable(table, "test_table_t"));
  
  // insert rows into table
  for(i = 0; i < numInserts; i++)
    {
      realInserts[i] = inserts[i%10];
      realInserts[i].a = i;
      r = fromTestRecord(schema, realInserts[i]);
      TEST_CHECK(insertRecord(table,r)); 
      rids[i] = r->id;
    }
  TEST_CHECK(closeTable(table));
  TEST_CHECK(openTable(table, "test_table_t"));

  // retrieve records from the table and compare to expected final stage
  for(i = 0; i < numcheck; i++)
    {
      RID rid = rids[i];
      TEST_CHECK(getRecord(table, rid, r));
      ASSERT_EQUALS_RECORDS(fromTestRecord(schema, realInserts[i]), r, schema, "compare records");
    }
  
  r = fromTestRecord(schema, updates[0]);
  r->id = rids[randomRec];
  TEST_CHECK(updateRecord(table,r));
  TEST_CHECK(getRecord(table, rids[randomRec], r)); 
  ASSERT_EQUALS_RECORDS(fromTestRecord(schema, updates[0]), r, schema, "compare records");
   
  TEST_CHECK(closeTable(table));
  TEST_CHECK(deleteTable("test_table_t"));
  TEST_CHECK(shutdownRecordManager());

  freeRecord(r);
  free(table);
  TEST_DONE();
}
Пример #11
0
void skelJointsHistory::beginRecord(int _size) {
	freeRecord();
	recordSize = _size;
}
Пример #12
0
skelJointsHistory::~skelJointsHistory() {
	curItem = NULL;
	freeItems();
	freeRecord();
}
Пример #13
0
void
testCheckingPrimaryKeyWithSingleAttribute(void)
{
    //just like test_assign3_1.c we insert records in the same way at first.
    RM_TableData *table = (RM_TableData *) malloc(sizeof(RM_TableData));
    TestRecord inserts[] = {
        {1, "aaaa", 3},
        {2, "bbbb", 2},
        {3, "cccc", 1},
        {4, "dddd", 3},
        {5, "eeee", 5},
        {6, "ffff", 1},
        {7, "gggg", 3},
        {8, "hhhh", 3},
        {9, "iiii", 2},
        {10, "jjjj", 5},
    };
    TestRecord realInserts[10000];
    int numInserts = 100, i;
    Record *r;
    RID *rids;
    Schema *schema;
    testName = "test checking primary key with single attribute";

    //this schema is exactly the same as test_assign3_1.c, this primary key only have 1 attibute.
    schema = testSchema();
    rids = (RID *) malloc(sizeof(RID) * numInserts);

    TEST_CHECK(initRecordManager(NULL));
    TEST_CHECK(createTable("test_table_t",schema));
    TEST_CHECK(openTable(table, "test_table_t"));

    for(i = 0; i <numInserts; i++)
    {
        realInserts[i] = inserts[i%10];
        realInserts[i].a = i;
        r = fromTestRecord(schema, realInserts[i]);

        printf("inserting record {%d,\"%s\",%d}",realInserts[i].a,realInserts[i].b,realInserts[i].c);
        //Extra Credit: Check primary key constraints.
        //check whether there exists a tuple that have the same key attribute value with this new record.
        if (CheckPrimaryKey(table, r)==RC_OK) {
            printf("  Success!\n");
        }
        else {
            printf("   Failed! A tuple with same attribute value already exists!\n");
        }
        TEST_CHECK(insertRecord(table,r));
        rids[i] = r->id;
    }

    //here is the code to check distinct,we insert some records that are already exist in the file, if detected, the checking function will return a RC value differen from RC_OK.
    TestRecord newInsert;
    newInsert=inserts[0];
    newInsert.a=0;
    r=fromTestRecord(schema, newInsert);
    printf("inserting record {%d,\"%s\",%d}",newInsert.a,newInsert.b,newInsert.c);
    //Extra Credit: Check primary key constraints.
    //check whether there exists a tuple that have the same key attribute value with this new record.
    if (CheckPrimaryKey(table, r)==RC_OK) {
        printf("  Success!\n");
    }
    else {
        printf("   Failed! A tuple with same attribute value already exists!\n");
    }
    insertRecord(table,r);


    TEST_CHECK(closeTable(table));
    TEST_CHECK(deleteTable("test_table_t"));
    TEST_CHECK(shutdownRecordManager());

    freeRecord(r);
    free(table);
    TEST_DONE();
}
Пример #14
0
void CSqlCommand::restore()
{
	freeRecord();
	clearBinaryParam();
	m_bIsUse = false;
}
/**
 * This API allows conditional updates of the records
 *
 * rel = Table handle
 * cond = Conditional expression to be matched
 * op = Function pointer pointing to update function to be called in case a matching record is found
 */
RC updateScan(RM_TableData *rel, Expr *cond, void (*op)(Schema*, Record *)) {

	//Sanity Checks
	if (rel == NULL) {
		THROW(RC_INVALID_HANDLE, "Table handle is invalid");
	}

	// Get total number of pages this table has
	int pages = ((RM_TableMgmtData *) rel->mgmtData)->pageCount;
	RID firstFreeSlot = ((RM_TableMgmtData *) rel->mgmtData)->firstFreeSlot;
	int i, j, tuplesRead, slot;
	RID id;
	Value *result;
	Record* r;
	BM_PageHandle *pageData = (BM_PageHandle *) malloc(sizeof(BM_PageHandle));

	if (pageData == NULL) {
		THROW(RC_NOT_ENOUGH_MEMORY,
				"Not enough memory available for resource allocation");
	}

	j = 0;
	tuplesRead = 0;

	// loop through pages and get the desired records
	for (i = 1; i < pages; i++) {
		slot = 0;
		id.page = i;
		while (slot < ((RM_TableMgmtData *) rel->mgmtData)->slotCapacityPage) {
			createRecord(&r, rel->schema);
			id.slot = slot;
			RC rc = readRecord(rel, id, &r);
			if (rc != RC_OK) {
				THROW(RC_REC_MGR_DELETE_REC_FAILED, "Delete record failed");
			}

			// ignore the deleted records (tombstones) and free slots
			if ((!(r->nullMap & (1 << 15))
					|| ((firstFreeSlot.page == i)
							&& (firstFreeSlot.slot == slot)))) {
				// check if record satisfies the given condition
				evalExpr(r, rel->schema, cond, &result);
				if (result->v.boolV) {
					op(rel->schema, r);
					updateRecord(rel, r);
					j++;
					tuplesRead++;
				}
				freeVal(result);
			}

			freeRecord(r);
			slot++;
		}
	}

	free(pageData);

	//All OK
	return RC_OK;
}
/**
 * Starts scan to fetch records with matching condition
 *
 * rel = table handle
 * scan = scan handle
 * cond = condition to be matched
 */
RC startScan(RM_TableData *rel, RM_ScanHandle *scan, Expr *cond) {

	//Sanity Checks
	if (rel == NULL) {
		THROW(RC_INVALID_HANDLE, "Table handle is invalid");
	}

	// Get total number of pages this table has
	int pages = ((RM_TableMgmtData *) rel->mgmtData)->pageCount;
	int tupleCount = ((RM_TableMgmtData *) rel->mgmtData)->tupleCount;
	RID firstFreeSlot = ((RM_TableMgmtData *) rel->mgmtData)->firstFreeSlot;
	int i, j, tuplesRead, slot;
	RID id;
	Value* result;
	Record** r;
	BM_PageHandle *pageData = (BM_PageHandle *) malloc(sizeof(BM_PageHandle));
	RM_ScanIterator *iter = (RM_ScanIterator*) malloc(sizeof(RM_ScanIterator));

	if (pageData == NULL) {
		THROW(RC_NOT_ENOUGH_MEMORY,
				"Not enough memory available for resource allocation");
	}

	scan->rel = rel;

	iter->totalRecords = 0;
	iter->lastRecordRead = -1;
	iter->records = (Record**) malloc(sizeof(Record*) * tupleCount);

	scan->mgmtData = (RM_ScanIterator*) iter;

	r = iter->records;
	for (i = 0; i < tupleCount; ++i) {
		r[i] = NULL;
	}

	j = 0;
	tuplesRead = 0;

	//Loop through pages and get the desired records
	for (i = 1; i < pages; i++) {
		slot = 0;
		id.page = i;

		while (slot < ((RM_TableMgmtData *) rel->mgmtData)->slotCapacityPage) {

			createRecord(&r[j], rel->schema);
			id.slot = slot;

			RC rc = readRecord(rel, id, &r[j]);
			if (rc != RC_OK) {
				THROW(RC_REC_MGR_DELETE_REC_FAILED, "Delete record failed");
			}

			// ignore the deleted records (tombstones)
			if (!(r[j]->nullMap & (1 << 15))
					&& !(firstFreeSlot.page == i && firstFreeSlot.slot == slot)) {
				evalExpr(r[j], rel->schema, cond, &result);
				if (result->v.boolV) {
					j++;
					tuplesRead++;
				}
				freeVal(result);

			}
			slot++;
			freeRecord(r[j]);
		}
	}

	iter->totalRecords = j;

	free(pageData);

	//All OK
	return RC_OK;
}