예제 #1
0
t_rc SSQLM_DDL_Manager::CreateIndex(char * t_Name, char * a_Name) {
	
	t_rc rc;

	AttrMet a_meta;
	rc = this->smm->GetAttributeMetadata(t_Name, a_Name, a_meta);
	if (rc != OK) return rc;
	
	RelMet r_meta;
	rc = this->smm->GetRelationMetadata(t_Name, r_meta);
	if (rc != OK) return rc;

	if (a_meta.indexNo >= 0) return (SSQLM_DDL_INDEXEXISTS);

	int indexNo = 0;
	while (this->im->CreateIndex((path + t_Name).c_str(),indexNo,a_meta.type,a_meta.length) != OK) indexNo++;

	a_meta.indexNo = indexNo;
	r_meta.numOfIndexes++;

	rc = this->smm->UpdateAttributeMetadata(t_Name, a_Name, a_meta.relName, a_meta.attrName, a_meta.offset, a_meta.type, a_meta.length, a_meta.indexNo);
	if (rc != OK) return rc;

	rc = this->smm->UpdateRelationMetadata(t_Name, r_meta.name, r_meta.rs, r_meta.numAttrs, r_meta.numOfIndexes);
	if (rc != OK) return rc;

	REM_RecordFileHandle rfh;
	rc = this->rfm->OpenRecordFile((path + t_Name).c_str(),rfh);
	if (rc != OK) return rc;

	REM_RecordFileScan rfs;
	rc = rfs.OpenRecordScan(rfh,TYPE_INT,0,0,NO_OP,0);
	if (rc != OK) return rc;
	
	INXM_IndexHandle ih;
	rc = this->im->OpenIndex((path + t_Name).c_str(),indexNo,ih);
	if (rc != OK) return rc;

	REM_RecordHandle rh;
	REM_RecordID rid;
	char * rData;

	while ((rc = rfs.GetNextRecord(rh)) == OK) {

		rc = rh.GetData(rData);
		if (rc != OK) return rc;

		rc = rh.GetRecordID(rid);
		if (rc != OK) return rc;

		char * aData = new char[a_meta.length];
		memcpy(aData,&rData[a_meta.offset],a_meta.length);

		rc = ih.InsertEntry(aData, rid);
		if (rc != OK) return rc;
	}

	rc = rfs.CloseRecordScan();
	if (rc != OK) return rc;

	rc = this->rfm->CloseRecordFile(rfh);
	if (rc != OK) return rc;

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

	return (OK);
}
예제 #2
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::Insert(const char *tName,const char *record){
	
	char pathname[50];												//	Gia na eisagoume ena record ston pinaka
	REM_RecordFileHandle *rfh = new REM_RecordFileHandle();			//	anoigoume to arxeio tou pinaka,
	REM_RecordID rid;												//	kanoume thn eisagwgh, kai epeita prepei na
																	//	elegksoume ean yparxoun indexes tous opoious
																	//	prepei na enhmerwsoume.
	// Open the table file
	_snprintf_s(pathname,sizeof(pathname),"%s/%s",dbName,tName);
	t_rc rc = rfm->OpenRecordFile(pathname,*rfh);
	if (rc != OK) { return rc; }

	// Insert the new record							
	rc = rfh->InsertRecord(record,rid);								//	8a xrhsimopoihsoume auto to rid gia na kanoume
	if (rc != OK) { return rc; }									//	thn eisagwgh stous indexers (ean yparxoun)

	// Close the table file
	rc = rfm->CloseRecordFile(*rfh);
	if (rc != OK) { return rc; }

	// CHECK IF THERE ARE INDEXES									//	Gia na elegksoume ean yparxoun indexes,
	bool has_indexes;

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

	// In case the number of indexes is different than 0, we have to insert entry in every index file
	// First we find the ids of the index files by looking into the attr.met file
	if( has_indexes ){

		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;
				_snprintf_s(pathname,sizeof(pathname),"%s/%s",dbName,tName);

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

				int j;
				for( j=0; j<size; j++){
					indexEntry[j] = record[j+offset];
				}
				indexEntry[j] = '\0';
				rc = im->OpenIndex(pathname,indexNo,ih);
				if (rc != OK) { return rc; }
			
				// Insert the entry into index file with id = indexNo
				if(strcmp(type,"TYPE_INT") == 0){
					int coca = atoi(indexEntry);
					int *key = new int(coca);

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

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

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