BadgerDB
|
00001 00008 #pragma once 00009 00010 #include "file.h" 00011 #include "buffer.h" 00012 #include <string> 00013 00014 #define MAXNAMESIZE 32 // length of relName, attrName 00015 00016 namespace badgerdb 00017 { 00018 00019 //TODO::this calculations seems wrong 00020 const int DIRSIZE = (Page::SIZE - MAXNAMESIZE - 4*sizeof(int) - sizeof(Datatype)) / sizeof(PageId); 00021 const int UNIQUE = 1; 00022 const int NONUNIQUE = 0; 00023 00024 00025 struct iHeaderPage 00026 { 00027 char fileName[MAXNAMESIZE]; // name of file 00028 int offset; // byte offset of the indexed attribute 00029 int length; // length of the attribute 00030 int depth; // depth of the directory 00031 int unique; // enforce uniqueness on inserts 00032 Datatype type; // datatype of the attribute 00033 PageId dir[DIRSIZE]; 00034 }; 00035 00036 struct Bucket { 00037 short depth; 00038 short slotCnt; 00039 char data[Page::SIZE - 2*sizeof(short)]; 00040 }; 00041 00045 class Index { 00046 BufMgr *bufMgr; 00047 File *file; 00048 iHeaderPage* headerPage; 00049 PageId headerPageNo; 00050 int dirSize; // size of the directory 00051 int numSlots; // maximum number of slots in a page 00052 int recSize; 00053 int curOffset; // offset of the next entry to be scanned 00054 Bucket* curBuc; // pointer to pinned page in buffer pool 00055 PageId curPageNo; // page number of pinned page 00056 const void* curValue; // comparison value of filter 00057 00058 void matchRec(const Bucket* bucket, const void *value, int offset); 00059 00060 void hashIndex(const void *value, int &hashvalue); 00061 00062 public: 00063 /* 00064 * Construct an Index object over an attribute of a relation. If index alreayd present as a file on disk, the same index is read, else new index file is created. 00065 * @param name Name of the relation being indexed 00066 * @param offset Offset of the attribute being indexed inside records 00067 * @param length Length of the attribute being indexed 00068 * @param type Type of the attribute being indexed 00069 * @param unique =1 if the index should only allow unique entries. Create UNIQUE indices only. 00070 * @param bufMgr Instance of global buffer manager. 00071 */ 00072 Index(const std::string & name, 00073 const int offset, 00074 const int length, 00075 const Datatype type, 00076 const int unique, 00077 BufMgr *bufMgr); 00078 00079 /* 00080 * Destructor. 00081 */ 00082 ~Index(); 00083 00084 /* 00085 * Insert an entry into the index. value should point to the index key (attribute) 00086 */ 00087 void insertEntry(const void* value, RecordId rid); 00088 00089 /* 00090 * Delete an entry from the index. value should point to the index key (attribute) 00091 */ 00092 void deleteEntry(const void* value, const RecordId & rid); 00093 00094 /* 00095 * Initiate a indexed scan 00096 */ 00097 void startScan(const void* value); 00098 00099 /* 00100 * Return RecordId of next scanned entry. 00101 */ 00102 void scanNext(RecordId& outRid); 00103 00104 /* 00105 * End the scan. 00106 */ 00107 void endScan(); 00108 00109 /* 00110 * Print the directory. 00111 */ 00112 void printDir(); 00113 00114 /* 00115 * Print all the buckets. 00116 */ 00117 void printBucs(); 00118 }; 00119 00120 }