Пример #1
0
int SM_Manager::ShowTable (const char *readRelName){
	//打开attr文件,查询所有relName属性 = readRelName的记录, 打印该记录
	RM_FileScan rfs = RM_FileScan(rmm.getFileManager(), rmm.getBufPageManager());	
 	RM_Record rec;
	int returnCode;
//	cout << "ShowTable" << endl;
	rmm.OpenFile("attrcat", attrfh);
	returnCode = rfs.OpenScan(attrfh, STRING, strlen(readRelName), 16, EQ_OP, (void*)readRelName);
	int x = 0;
//	cout << "TableName : "<< readRelName << endl;
//	cout << "returnCode: " << returnCode << endl;
	while (returnCode == 1){
//		cout << "ret : 1" << endl;
		x = rfs.GetNextRec(rec);
//		cout << "ret : 2" << endl;
		if (x == -1)
			break;
		cout << x << endl;
		DataAttrInfo * mydata;
		rec.GetData((char*&) mydata);
		mydata->print();
	}
	cout << "Table End" << endl;
	rfs.CloseScan();
	rmm.CloseFile(attrfh);
	return 0;
}
Пример #2
0
int SM_Manager::GetTable (const char *readRelName, int &number, DataAttrInfo* &dataInfo){
	//打开attr文件,查询所有relName属性 = readRelName的记录, 将attr个数传给number, dataAttrInfo传给dataInfo,内存释放需要外面执行
	//属性个数存在上限100
	RM_FileScan rfs = RM_FileScan(rmm.getFileManager(), rmm.getBufPageManager());	
 	RM_Record rec;
	int returnCode;
//	cout << "GetTable" << endl;
	rmm.OpenFile("attrcat", attrfh);
	returnCode = rfs.OpenScan(attrfh, STRING, strlen(readRelName), 16, EQ_OP, (void*)readRelName);
	int x = 0;
	dataInfo = new DataAttrInfo[100];
	number = 0;
//	cout << "TableName : "<< readRelName << endl;
	while (returnCode == 1){
//		cout << "ret : 1" << endl;
		x = rfs.GetNextRec(rec);
//		cout << "ret : 2" << endl;
		if (x == -1)
			break;
//		cout << x << endl;
		DataAttrInfo * mydata;
		rec.GetData((char*&) mydata);
		dataInfo[number].copy(mydata);
		number++;
	}
//	cout << "Table End" << endl;
	rfs.CloseScan();
	rmm.CloseFile(attrfh);
	return 0;
}
Пример #3
0
RC SM_Manager::Print(const char *relName){
  RM_FileScan attribute_scanner(attribute_fh, STRING, RELNAME_LENGTH, 0, EQ_OP, 
                                (void*)relName, strlen(relName));
  RC ret;
  RM_Record record;
  Byte *mem;
  while( (ret=attribute_scanner.GetNextRec(record)) != NOT_FOUND ){
  //  record.GetRid(rid);
  //  attribute_fh.DeleteRec(rid);
    record.GetData(mem);
    cout<<GenString((char *)(mem+RELNAME_LENGTH),ATTRNAME_LENGTH)<<"("<<*(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH+8)<<")"<<*(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH+4)<<endl;
  }
  return OK;
}
Пример #4
0
bool SM_Manager::isChecked(const char*relName,const char *AttrName){
  if (strchr(AttrName,'_')) return true;
  RM_FileScan check_scanner(check_fh, STRING, RELNAME_LENGTH, 0, EQ_OP, 
                                (void*)relName, strlen(relName));
  RC ret;
  RM_Record record;
  Byte *mem;
  while( (ret=check_scanner.GetNextRec(record)) != NOT_FOUND ){
    record.GetData(mem);
    if (strcmp((char*)mem+RELNAME_LENGTH,AttrName)==0){
      return true;
    }
  }
  return false;
}
Пример #5
0
static RC RollBackUpdate(SM_DbHandle &db, Event &event, RID &rid)
{
    RC rc;

    printf("QL: Undoing update.\n");
    RM_FileHandle *pTableHandle;
    if ((rc = db.FindTableHandle(event.tableName.c_str(), pTableHandle)))
        return rc;

    RM_Record record;
    if ((rc = pTableHandle->GetRec(rid, record)))
        return rc;

    char *data;
    if ((rc = record.GetData(data)))
        return rc;

    SM_Attribute *pAttr;
    if ((rc = db.FindAttribute(event.tableName.c_str(), event.attrName.c_str(), pAttr)))
        return rc;

    // If indexed, remove the old entry.
    IX_IndexHandle *pIndexHandle;
    if (pAttr->hasIndex) {
        if ((rc = db.FindIndexHandle(event.tableName.c_str(), event.attrName.c_str(), pIndexHandle))) 
            return rc;
        if ((rc = pIndexHandle->DeleteEntry(data + pAttr->attrOffset, rid))) 
            return rc;
    }

    // Restore the record.
    int recordSize = pTableHandle->header.recordSize;
    memcpy(data, event.record, recordSize);

    // Update the record.
    if ((rc = pTableHandle->UpdateRec(record))) 
        return rc;

    // If indexed, insert the new entry.
    if (pAttr->hasIndex) {
        if ((rc = pIndexHandle->InsertEntry(data + pAttr->attrOffset, rid))) 
            return rc;
    }

    return 0;
}
Пример #6
0
RC SM_Manager::DropTable(const char *relName){
  string rel_name(relName);
  
  //drop in system catalog
  RM_FileScan system_scanner(system_fh, STRING, RELNAME_LENGTH, 0, EQ_OP, 
                             (void*)relName, strlen(relName));
  RC ret;
  RM_Record record;
  ret=system_scanner.GetNextRec(record);
  if (ret==NOT_FOUND){
    return NOT_FOUND;
  }
  RID rid;
  record.GetRid(rid);
  system_fh.DeleteRec(rid);
  //------------------------

  //drop in attribute catalog
  RM_FileScan attribute_scanner(attribute_fh, STRING, RELNAME_LENGTH, 0, EQ_OP, 
                                (void*)relName, strlen(relName));
  while( (ret=attribute_scanner.GetNextRec(record)) != NOT_FOUND ){
    record.GetRid(rid);
    attribute_fh.DeleteRec(rid);
  }
  system((string("mv -f ")+db_dir+string(relName)+" "+db_dir+"dust/"+string(relName)).c_str());
  
  RM_FileScan check_scanner(check_fh, STRING, RELNAME_LENGTH, 0, EQ_OP, 
                                (void*)relName, strlen(relName));
  while( (ret=attribute_scanner.GetNextRec(record)) != NOT_FOUND ){
    Byte *mem;
    record.GetRid(rid);
    record.GetData(mem);
    attribute_fh.DeleteRec(rid);
    string CheckData=string("__checkdata__")+rel_name+(char*)(mem+RELNAME_LENGTH);
    system((string("mv -f ")+db_dir+CheckData+" "+db_dir+"dust/"+CheckData).c_str());
  }

  attribute_fh.ForcePages();
  system_fh.ForcePages();
  check_fh.ForcePages();
  
  //-----------------------
  
  return OK;
}
Пример #7
0
bool SM_Manager::checkNotNull(const char*relName,const char *AttrName){
   RM_FileScan attribute_scanner(attribute_fh, STRING, RELNAME_LENGTH, 0, EQ_OP, 
                                (void*)relName, strlen(relName));
  RC ret;
  RM_Record record;
  Byte *mem;
  int cnt=0;
  while( (ret=attribute_scanner.GetNextRec(record)) != NOT_FOUND ){
  //  record.GetRid(rid);
  //  attribute_fh.DeleteRec(rid);
    record.GetData(mem);
    if (strcmp((char*)mem+RELNAME_LENGTH,AttrName)==0){
     // cout<<"get "<< AttrName<<" "<<*(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH+16)<<endl;
      return *(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH+16);
    }
  }
  return false;
}
Пример #8
0
bool SM_Manager::hasKey(const char *relName,int &offset,int &length,AttrType &attrtype){
  RM_FileScan attribute_scanner(attribute_fh, STRING, RELNAME_LENGTH, 0, EQ_OP, 
                                (void*)relName, strlen(relName));
  RC ret;
  RM_Record record;
  Byte *mem;
  while( (ret=attribute_scanner.GetNextRec(record)) != NOT_FOUND ){
  //  record.GetRid(rid);
  //  attribute_fh.DeleteRec(rid);
    record.GetData(mem);
    if ((*(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH+12)/2)==1){
      offset=*(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH);
      length=*(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH+8);
      attrtype=(AttrType)*(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH+4);
      return true;
    }
  }
  return false;
}
Пример #9
0
RC SM_Manager::ShowTable   (){
  RM_FileScan system_scanner(system_fh, STRING, RELNAME_LENGTH, 0, EQ_OP, 
                                NULL, 0);
  //cout<<"1"<<endl;
  RC ret;
  RM_Record record;
  Byte *mem;
  //cout<<"1.1"<<endl;
  while( (ret=system_scanner.GetNextRec_NoComp(record)) != NOT_FOUND ){
  //  record.GetRid(rid);
  //  attribute_fh.DeleteRec(rid);
   // cout<<"2"<<endl;
    record.GetData(mem);
   // cout<<"3"<<endl;
    cout<<GenString((char *)(mem),RELNAME_LENGTH)<<"("<<*(int*)(mem+RELNAME_LENGTH)<<")"<<endl;
    //cout<<"4"<<endl;
  }
  return OK;
}               // Print table
Пример #10
0
bool SM_Manager::hasIndex(const char *relName,const char *AttrName,int &offset,int &length,AttrType &attrtype){
  RM_FileScan attribute_scanner(attribute_fh, STRING, RELNAME_LENGTH, 0, EQ_OP, 
                                (void*)relName, strlen(relName));
  RC ret;
  RM_Record record;
  Byte *mem;
  while( (ret=attribute_scanner.GetNextRec(record)) != NOT_FOUND ){
  //  record.GetRid(rid);
  //  attribute_fh.DeleteRec(rid);
    record.GetData(mem);
    if (GenString((char *)(mem+RELNAME_LENGTH),ATTRNAME_LENGTH)==GenString((char *)AttrName,strlen(AttrName))){
      offset=*(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH);
      length=*(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH+8);
      attrtype=(AttrType)*(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH+4);
      //cout<<(*(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH+12))<<endl;
      return ((*(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH+12))%2)!=0;
    }
  }
  return OK;
}
Пример #11
0
int SM_Manager::Getoffset(const char *relName,int *offset,int *length){
  RM_FileScan attribute_scanner(attribute_fh, STRING, RELNAME_LENGTH, 0, EQ_OP, 
                                (void*)relName, strlen(relName));
  RC ret;
  RM_Record record;
  Byte *mem;
  int cnt=0;
  while( (ret=attribute_scanner.GetNextRec(record)) != NOT_FOUND ){
  //  record.GetRid(rid);
  //  attribute_fh.DeleteRec(rid);
    record.GetData(mem);
    //if (genString((char *)(mem+RELNAME_LENGTH),ATTRNAME_LENGTH)==genString(AttrName,strlen(AttrName))){
      offset[cnt]=*(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH);
      length[cnt]=*(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH+8);
      cnt++;
    //  attrtype=*(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH+4);
    //}
  }
  return cnt;
}
Пример #12
0
void SM_Manager::GetAttrName(const char *relName,char attrNames[100][100],bool *ischecked,bool *isnotnull,AttrType* attrType){
  RM_FileScan attribute_scanner(attribute_fh, STRING, RELNAME_LENGTH, 0, EQ_OP, 
                                (void*)relName, strlen(relName));
  RC ret;
  RM_Record record;
  Byte *mem;
  int cnt=0;
  while( (ret=attribute_scanner.GetNextRec(record)) != NOT_FOUND ){
  //  record.GetRid(rid);
  //  attribute_fh.DeleteRec(rid);
    record.GetData(mem);
    //if (genString((char *)(mem+RELNAME_LENGTH),ATTRNAME_LENGTH)==genString(AttrName,strlen(AttrName))){
      memcpy(attrNames[cnt],(mem+RELNAME_LENGTH),ATTRNAME_LENGTH-1);
      ischecked[cnt]=isChecked(relName,attrNames[cnt]);
      isnotnull[cnt]=checkNotNull(relName,attrNames[cnt]);
      attrType[cnt]=(AttrType)*(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH+4);
      cnt++;
    //  attrtype=*(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH+4);
    //}
  }
}
Пример #13
0
void SM_Manager::DropIndex(const char *relName, const char* AttrName)
{
  RM_FileScan attribute_scanner(attribute_fh, STRING, RELNAME_LENGTH, 0, EQ_OP, 
                                (void*)relName, strlen(relName));
  RC ret;
  RM_Record record;
  Byte *mem;
  RID rid;
  int mark=0;
  while( (ret=attribute_scanner.GetNextRec(record)) != NOT_FOUND ){
    record.GetRid(rid);
  //  attribute_fh.DeleteRec(rid);
    record.GetData(mem);
    if (GenString((char *)(mem+RELNAME_LENGTH),ATTRNAME_LENGTH)==GenString((char *)AttrName,strlen(AttrName))){
      mark=*(int*)(mem+RELNAME_LENGTH+ATTRNAME_LENGTH+12);
      if(mark&1) mark=mark-1;
      memcpy((mem+RELNAME_LENGTH+ATTRNAME_LENGTH+12), &mark , 4);
      RM_Record rec=RM_Record(mem, attribute_fh.GetRecordSize(), rid);
      attribute_fh.UpdateRec(rec);
    }
  }
  attribute_fh.ForcePages();
}
Пример #14
0
RC RM_FileScan::GetNextRec(RM_Record &rec) {
    if (!bInScan)
        return RM_ERR_NOTINSCAN;
    RC rc;
    RID rid;
    RM_Record tmpRec;
    while (1) {
        //std::cout <<"start next : "<< nowPage <<" "<<nowSlot<<"\n";
        if (bNextPage) {
            //std::cout << "before next page:" << nowPage<<"\n";
            if (PF_ERR_EOF == fileHandle.pfh.GetNextPage(nowPage, nowPH)) {
                bInScan = false;
                return RM_ERR_EOF;
            }
            if (rc = nowPH.GetPageNum(nowPage)) 
                return rc;
            //std::cout << "after next page:" << nowPage<<"\n";
            bNextPage = false;
            nowSlot = -1;
            continue;
        }  

        char *pData;
        if (rc = nowPH.GetData(pData))
            return rc;
        BitMap bitmap;
        if (rc = fileHandle.GetBitMapFromData(pData, bitmap))
            return rc;
        //bitmap.print();
        int nextBit;
        if (RM_ENDOFPAGE == bitmap.GetNextBit(nowSlot, nextBit)) {
            if (rc = fileHandle.pfh.UnpinPage(nowPage))
                return rc;
            bNextPage = true;
            continue;
        }  
        //std::cout << "nextbit: " << nextBit <<"\n";


        nowSlot = nextBit;
        RID rid;
        rid.SetPageNum(nowPage);
        rid.SetSlotNum(nowSlot);

//        std::cout << "find slot: pageNum:"<<nowPage<<"    slotNum:"<<nowSlot<<"\n";

        //std::cout << "recordSize:" << fileHandle.header.recordSize<<"\n";
        //std::cout << "firstFree :" << fileHandle.header.firstFree <<"\n";
        //std::cout << "bitmapOffset :" << fileHandle.header.bitmapOffset <<"\n";
        //std::cout << "bitmapSize : " << fileHandle.header.bitmapSize <<"\n";
        //std::cout << "numRecordsPerPage : " << fileHandle.header.numRecordsPerPage <<"\n";
        //std::cout << "numPages : " << fileHandle.header.numPages <<"\n";
        //std::cout << rid.pageNum <<" "<<rid.slotNum<<"\n";


        if (rc = fileHandle.GetRecordFromData(pData, rid, tmpRec))
            return rc;

        //for (int i = 0; i < fileHandle.header.recordSize; i++) 
        //    std::cout << (int)tmpRec.pData[i] <<" ";
        //std::cout <<"\n";
        
        //std::cout << "recdata: " << (int)tmpRec.pData[3]<<"\n";
        //std::cout << "offset:"<<attrOffset<<" "<<"    len:"<<attrLength<<"  type:"<<attrType<<"\n";
        //std::cout << "compop:"<<this->compOp<<"\n";
        
        char *tmpRecData;
        if (rc = tmpRec.GetData(tmpRecData))
            return rc;
        

        if (this->compOp != NO_OP) {
            bool flag;
            if (this->compOp != IS_OP && this->compOp != IS_NOT_OP && tmpRec.IsNull(attrNumber)) 
                flag = false;
            else
                flag = (* cmp)(tmpRecData + attrOffset, this->value, attrType, attrLength);

            if (flag) { 
                rec = tmpRec;
                break;
            }
        }  else {
            rec = tmpRec; 
            break;
        }
    }
    return 0;
}
Пример #15
0
static RC PrintTable(SM_DbHandle &db, const char *tableName)
{
    RC rc;

    RM_FileHandle *pTable;
    if ((rc = db.FindTableHandle(tableName, pTable)))
        return rc;
    int recordSize = pTable->header.recordSize;

    vector<SM_ActiveAttr> *attributes;
    if ((rc = db.AllAttributes(tableName, attributes)))
        return rc;

    RM_FileScan allScan;
    if ((rc = allScan.OpenScan(*pTable, INT, 0, 0, 0, NO_OP, NULL)))
        return rc;
    RM_Record record;
    int recordCount = 0;

    printf("Table: %s\n", tableName);
    while ((rc = allScan.GetNextRec(record)) == 0) {
        RID rid;
        if ((rc = record.GetRid(rid)))
            return rc;
        PageNum pageNum;
        int slotNum;
        if ((rc = rid.GetPageNum(pageNum)))
            return rc;
        if ((rc = rid.GetSlotNum(slotNum)))
            return rc;

        char *data;
        if ((rc = record.GetData(data)))
            return rc;

        printf("RID: (%d,%d)\n", pageNum, slotNum);
        vector<SM_ActiveAttr>::iterator pAttr;

        int *pNullBitmap = (int *)(data + recordSize - sizeof(int));
        for (pAttr = attributes->begin(); pAttr != attributes->end(); ++pAttr) {
            SM_Attribute *attr = &pAttr->record;
            if (*pNullBitmap & (1 << attr->number))
                printf("  %s: Null\n", attr->attrName);
            else if (attr->attrType == INT)
                printf("  %s: %d\n", attr->attrName, *(int *)(data + attr->attrOffset));
            else if (attr->attrType == FLOAT)
                printf("  %s: %f\n", attr->attrName, *(float *)(data + attr->attrOffset));
            else if (attr->attrType == STRING)
                printf("  %s: %s\n", attr->attrName, data + attr->attrOffset);
        }
        printf("\n");
        recordCount++;
    }
    if (rc != RM_ERR_EOF)
        return rc;
    if ((rc = allScan.CloseScan()))
        return rc;
        printf("\nTotal %d records.\n", recordCount);

    return 0;
}