#include "Directory.h" #include "Bucket.h" #include "hash.h" #include "Bucketfile.h" #include "Directoryfile.h" #include #include #include #include // for setw, setiosflags, etc... using namespace std; Directory::Directory (int maxBucketKeys) { Depth=0;//depth of directory NumCells =1;//number of entries, = 2**Depth BucketAddr=new int[NumCells]; //array of bucket addresses Restributing=0; LogCounter=0; //log counter Depth_length=4; // depth length BucketAddress_length = 4; // BucketAddress length MaxBucketKeys= maxBucketKeys; CurrentBucket = new Bucket (*this, MaxBucketKeys); } Directory::~Directory() { } int Directory::Create(char *dirFileName, char *bucketFileName,char *logFileName) { //create the two files,clear the directory,create a single //bucket and add it to the directory and the bucket file DirectoryFile.open(dirFileName,ios::out); BucketFile.open(bucketFileName,ios::out); LogFile.open(logFileName,ios::out); //store the empty CurrentBucket in the BucketFile //and add to directory BucketAddr[0]=0; Bucketfile theBucket; theBucket.writeBucketfile(BucketFile); DirectoryFile.close(); BucketFile.close(); LogFile.close(); return 1; } int Directory::Open(char *dirFileName,char *bucketFileName,char *logFileName) { DirectoryFile.open(dirFileName,ios::in|ios::out); // Check to make sure that we could open the files properly! if (DirectoryFile.fail()) { cerr << "Could not open file "; cerr << dirFileName; cerr << ".\n"; return 1; } BucketFile.open(bucketFileName,ios::in|ios::out); // Check to make sure that we could open the files properly! if (BucketFile.fail()) { cerr << "Could not open file "; cerr << bucketFileName; cerr << ".\n"; return 1; } LogFile.open(logFileName,ios::in|ios::out); // Check to make sure that we could open the files properly! if (LogFile.fail()) { cerr << "Could not open file "; cerr << logFileName; cerr << ".\n"; return 1; } return 1; } int Directory::Close() {//write the directory and close. error occurs on buffer overflow DirectoryFile <Insert(key,recAddr); } int Directory::Search (long key) { int bucketAddr=Find(key); LoadBucket(CurrentBucket,bucketAddr); return bucketAddr; } int Directory::Find(long key) //return BucketAddr for key { return BucketAddr[hash(key, Depth)]; } int Directory::StoreBucket(Bucket * bucket,long key, int recAddr,int position) {//update or append the bucket to the bucket file Bucketfile theBucket; int addr = bucket->BucketAddr; theBucket.ModifyBucketFileNumKeys(addr,BucketFile,position+1); theBucket.ModifyBucketFileDepthBucket(addr,BucketFile,bucket->Depth); theBucket.ModifyBucketFileKey(addr,BucketFile,key,position); theBucket.ModifyBucketFileRecAddr(addr,BucketFile,recAddr,position); return 0; } int Directory::LoadBucket(Bucket * bucket, int bucketAddr) {//read bucket fron file, and set BucketAddr field int result; Bucketfile theBucket; theBucket.readBucketfile(bucketAddr,BucketFile); bucket->NumKeys=theBucket.getNumKeys(); result=theBucket.getNumKeys(); if (result==-1) return 0; bucket->BucketAddr=bucketAddr; if (Restributing==0) { bucket->Depth=theBucket.getDepthBucket(); bucket->BucketContent[0]=theBucket.getKeys0(); bucket->BucketContent[1]=theBucket.getRecAddr0(); bucket->BucketContent[2]=theBucket.getKeys1(); bucket->BucketContent[3]=theBucket.getRecAddr1(); bucket->BucketContent[4]=theBucket.getKeys2(); bucket->BucketContent[5]=theBucket.getRecAddr2(); bucket->BucketContent[6]=theBucket.getKeys3(); bucket->BucketContent[7]=theBucket.getRecAddr3(); bucket->BucketContent[8]=theBucket.getKeys4(); bucket->BucketContent[9]=theBucket.getRecAddr4(); } return 1; } int Directory::DoubleSize() //double the size of the directory { int newSize =2 * NumCells; int * newBucketAddr = new int[newSize]; for(int i=0;iBucketAddr; theBucket.ModifyBucketFileToVoid(addr,BucketFile); theBucket.ModifyBucketFileDepthBucket(addr,BucketFile,bucket->Depth); BucketFileAdd.open("EHbuckets.txt",ios::out|ios::app); theBucket.writeBucketfile(BucketFileAdd); BucketFileAdd.close(); theBucket.ModifyBucketFileDepthBucket(bucketAddr,BucketFile,bucket->Depth); BucketAddr[Index] = bucketAddr; return 1; }