INDEX_FILE *open_index(int i, HFILE *hf){ INDEX_FILE *indexfile; FILE *fp1, *fp2; indexfile=(INDEX_FILE*)malloc(sizeof(INDEX_FILE)); int length = strlen(hf->path)+10; indexfile->main_path = calloc(length,sizeof(char)); indexfile->overflow_path = calloc(length,sizeof(char)); get_index_file_name(hf->path, i+1, indexfile->main_path, indexfile->overflow_path); indexfile->type = hf->schema[i]; indexfile->column_number=i; fp1 = fopen(indexfile->main_path,"r"); if (fp1==NULL){ printf("fail to open file."); exit(1); } fp2 = fopen(indexfile->overflow_path,"r"); if (fp2==NULL){ printf("fail to open file."); exit(1); } fread(&indexfile->next, sizeof(long long), 1, fp2); fread(&indexfile->s, sizeof(long long), 1, fp2); fread(&indexfile->n, sizeof(long long), 1, fp2); fclose(fp1); fclose(fp2); return indexfile; }
int cam_get_new_file_name(void* handle, char* filename, char* ext, int idx) { folderInfo_t* fi = (folderInfo_t*)handle; int ret; char folder[256]; if (fi->name_format == 1) { ret = get_current_folder2(fi, folder); if (0 > ret) return -1; get_time_file_name(filename, folder, fi->postfix, ext); return 0; } else { ret = get_current_folder(fi, folder); if (0 > ret) return -1; if (ret) idx = ret; get_index_file_name(filename, idx, folder, ext); return idx; } }
void init_index(int i, HFILE *hf){ int j; INDEX_FILE indexfile; long long bigzero = 0; long long bignull = -1; FILE *fp1, *fp2; void* bucket; indexfile.next=0; indexfile.s=4; indexfile.n=4; int length = strlen(hf->path)+10; indexfile.main_path = calloc(length,sizeof(char)); indexfile.overflow_path = calloc(length,sizeof(char)); get_index_file_name(hf->path, i+1, indexfile.main_path, indexfile.overflow_path); indexfile.type = hf->schema[i]; indexfile.column_number=i; fp1 = fopen(indexfile.main_path,"w"); if (fp1==NULL){ printf("fail to create file."); exit(1); } fp2 = fopen(indexfile.overflow_path,"w"); if (fp2==NULL){ printf("fail to create file."); exit(1); } bucket = calloc(BSIZE,sizeof(char)); if (bucket==NULL){ printf("Malloc failed"); exit(1); } init_bucket(bucket); fclose(fp1); for(j=0;j<4;j++){ write_bucket(bucket, j, indexfile.main_path); } fwrite(&indexfile.next, sizeof(long long), 1, fp2); fwrite(&indexfile.s, sizeof(long long), 1, fp2); fwrite(&indexfile.n, sizeof(long long), 1, fp2); fwrite(&bignull, sizeof(long long), 1, fp2); fwrite(&bigzero, sizeof(long long), 1, fp2); free(bucket); fclose(fp2); return; }
/* open and return a heapfile given the path */ HFILE *hf_open(const char *path){ int n_records; int n_fields; char tmp[2]; int i; HFILE *hf = (HFILE*)malloc(sizeof(HFILE)); char *main_index_file; char *overflow_index_file; if (hf==NULL){ printf("Not enough memory.\n"); exit(1); } FILE *fp=fopen(path,"r"); if (fp==NULL){ printf("Fail to open heap file.\n"); return NULL; } // read the number of records fread(&n_records, sizeof(long long),1,fp); hf->n_records=n_records; // read the number of fields fread(&n_fields, sizeof(int),1,fp); hf->n_fields=n_fields; // read the schema // field processing // map schema to "schema_array" based on different data types // "schema_array" determines accesses to different read/write/compare functions hf->schema=(char**)malloc(n_fields*sizeof(char*)); hf->schema_array=(int*)malloc(n_fields*sizeof(int)); hf->index_array=(int*)malloc(n_fields*sizeof(int)); for(i=0;i<n_fields;i++){ fread(tmp, sizeof(char),2,fp); hf->schema[i]=(char*)calloc(5, sizeof(char)); if (tmp[0] !='c') { hf->schema[i][0]=tmp[0]; hf->schema[i][1]=tmp[1]; hf->schema[i][2]=0; if (tmp[0]=='i' && tmp[1]=='1') hf->schema_array[i]=0; else if (tmp[0]=='i' && tmp[1]=='2') hf->schema_array[i]=1; else if (tmp[0]=='i' && tmp[1]=='4') hf->schema_array[i]=2; else if (tmp[0]=='i' && tmp[1]=='8') hf->schema_array[i]=3; else if (tmp[0]=='r' && tmp[1]=='4') hf->schema_array[i]=4; else if (tmp[0]=='r' && tmp[1]=='8') hf->schema_array[i]=5; } else { sprintf(hf->schema[i], "c%d", tmp[1]); hf->schema_array[i]=6; } } hf->path=path; int length = strlen(hf->path)+10; main_index_file = calloc(length,sizeof(char)); overflow_index_file = calloc(length,sizeof(char)); for(i=0;i<n_fields;i++){ FILE *fp1 , *fp2; get_index_file_name(hf->path, i+1, main_index_file, overflow_index_file); fp1 = fopen(main_index_file,"r"); fp2 = fopen(overflow_index_file,"r"); if (fp1 != NULL && fp2 != NULL){ hf->index_array[i]=1; fclose(fp1); fclose(fp2); } else { hf->index_array[i]=0; } //free(main_index_file); //free(overflow_index_file); } fclose(fp); //free(main_index_file); //free(overflow_index_file); return hf; }