Пример #1
0
void testINXM() {
	STORM_StorageManager mgr;
	STORM_FileHandle fh;
	t_rc rc;
	STORM_PageHandle ph;
	
	
	
	
	INXM_IndexManager *imgr = new INXM_IndexManager(&mgr);
	
	INXM_IndexHandle ih;
	
	rc = imgr->CreateIndex("test.dat", 1, TYPE_INT, 4);
	if (rc != OK) {DisplayReturnCode(rc);exit(-1);}	
	
	rc = imgr->OpenIndex("test.dat", 1, ih);
	if (rc != OK) {DisplayReturnCode(rc);exit(-1);}	
	
	REM_RecordID rid1, rid2, rid3;
	rid1.SetPageID(1);
	rid1.SetSlot(1);
	rid2.SetPageID(2);
	rid2.SetSlot(2);
	rid3.SetPageID(3);
	rid3.SetSlot(3);
	
	//================================================================================	
	
	int *key1 = new int(1);
	int *key2 = new int(4);
	int *key3 = new int(6);
	int *key4 = new int(10);
	
	rc = ih.InsertEntry(key1, rid1);
	if (rc != OK) {DisplayReturnCode(rc);exit(-1);}	
	
	rc = ih.InsertEntry(key2, rid2);
	if (rc != OK) {DisplayReturnCode(rc);exit(-1);}	
	
	rc = ih.InsertEntry(key3, rid3);
	if (rc != OK) {DisplayReturnCode(rc);exit(-1);}	
	
	rc = ih.InsertEntry(key4, rid1);
	if (rc != OK) {DisplayReturnCode(rc);exit(-1);}	
	
	rc = ih.InsertEntry(key4, rid2);
	if (rc != OK) {DisplayReturnCode(rc);exit(-1);}		
	
	rc = ih.InsertEntry(key4, rid3);
	if (rc != OK) {DisplayReturnCode(rc);exit(-1);}		
	
//	for (int i=0; i<1000; i++) {
//		rc = ih.InsertEntry(key4, rid3);
//		if (rc != OK) {DisplayReturnCode(rc);exit(-1);}	
//	}
	
	ih.debug();
    
    rc = ih.DeleteEntry(key2, rid2);
	if (rc != OK) {DisplayReturnCode(rc);exit(-1);}	
	
    ih.debug();
    
	//================================================================================	
	
	rc = imgr->CloseIndex(ih);
	if (rc != OK) {DisplayReturnCode(rc);exit(-1);}	
	
	rc = imgr->DestroyIndex("test.dat", 1);
	if (rc != OK) {DisplayReturnCode(rc);exit(-1);}	
	
}
t_rc SSQLM_DML_Manager::Delete(const char *tName, REM_RecordID ridsToDelete, char *recordsToDelete){

	STORM_StorageManager *stormgr = new STORM_StorageManager();				
	REM_RecordFileManager *remrfm = new REM_RecordFileManager(stormgr);		
	REM_RecordFileHandle *remrfh = new REM_RecordFileHandle();					
//	char *data;
	char pathname[50];

	_snprintf_s(pathname,sizeof(pathname),"%s/%s",dbName,tName);
	t_rc rc = remrfm->OpenRecordFile(pathname,*remrfh);
	if (rc != OK) { return rc; }

	//for(int i = 0; i < ridsToDelete.size(); i++){

		rc = remrfh->DeleteRecord(ridsToDelete);
		if (rc != OK) { return rc; }
	//}

	rc = remrfh->FlushPages();
	if (rc != OK) { return rc; }

	bool hasIndexes;

	rc = CheckIfTableHasIndexes(tName,hasIndexes);
	if (rc != OK) { return rc; }

	if(hasIndexes){

		REM_RecordFileScan *rfs = new REM_RecordFileScan();
		int tNameLength = strlen(tName);
		REM_RecordHandle rh;

		rc = rfs->OpenRecordScan(*attrmet,TYPE_STRING,tNameLength, 0, EQ_OP, (char*)tName);
		if (rc != OK) {return rc; }

		int offset = 0;
		int size = 0;
		char *type;
		int indexNo = 0;
		while( rfs->GetNextRecord(rh) != REM_FSEOF ){
		
			char *pp;
			rh.GetData(pp);

			//Get the index id, offset, type and size of the attribute
			rc = GetAttrInfo(pp,offset,type,size,indexNo);

			//if there is index for this attribute
			if( indexNo != -1 ){

				// Retrieve the index entry from the attribute data
				INXM_IndexHandle ih;
				REM_RecordID rid;
				_snprintf_s(pathname,sizeof(pathname),"%s/%s",dbName,tName);

				rc = im->OpenIndex(pathname,indexNo,ih);
				if (rc != OK) { return rc; }

				char *indexEntry;
				indexEntry = (char*)malloc(size);

				int j;
				for( j=0; j<size; j++){
					indexEntry[j] = recordsToDelete[j+offset];
				}
				indexEntry[j] = '\0';
			
				// Delete the entry from the index file with id = indexNo
				if(strcmp(type,"TYPE_INT") == 0){
					int coca = atoi(indexEntry);
					int *key = new int(coca);

					rc = ih.DeleteEntry( key, rid );
					if (rc != OK) { return rc; }
				}
				else{
					rc = ih.DeleteEntry( indexEntry, rid);
					if (rc != OK) { return rc; }
				}

				rc = im->CloseIndex(ih);
				if (rc != OK) { return rc; }

			}
		}
		rc = rfs->CloseRecordScan();
	}

	return OK;
}