|
|
@@ -18,9 +18,9 @@ public class FileHandler implements Closeable { |
|
|
|
String description) throws IOException { |
|
|
|
//position pointer at the end of file |
|
|
|
|
|
|
|
long currentInsertPosition = dbFile.length(); |
|
|
|
Index.getInstance().add(currentInsertPosition); |
|
|
|
dbFile.seek(currentInsertPosition); |
|
|
|
long bytePosition = dbFile.length(); |
|
|
|
Index.getInstance().add(bytePosition); |
|
|
|
dbFile.seek(bytePosition); |
|
|
|
/* |
|
|
|
calculate the total length of the record |
|
|
|
to put a pointer at the beginning of the record how long the record is |
|
|
@@ -96,6 +96,28 @@ public class FileHandler implements Closeable { |
|
|
|
} |
|
|
|
return rawData; |
|
|
|
} |
|
|
|
/* |
|
|
|
loadIndex method creates the index contents at the startup of program |
|
|
|
if file doesnt exist, does nothing |
|
|
|
if file already exists, reads every recordlength and moves byteposition to end |
|
|
|
*/ |
|
|
|
public void loadIndex() throws IOException { |
|
|
|
if (dbFile.length()==0){ |
|
|
|
return; |
|
|
|
} |
|
|
|
long bytePos = 0; |
|
|
|
while (bytePos < dbFile.length()){ |
|
|
|
dbFile.seek(bytePos); |
|
|
|
boolean isDeleted = dbFile.readBoolean(); |
|
|
|
if (!isDeleted){ |
|
|
|
Index.getInstance().add(bytePos); |
|
|
|
bytePos += BYTE_LENGTH; |
|
|
|
int recordLength = dbFile.readInt(); |
|
|
|
bytePos += INT_LENGTH + recordLength; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void close() throws IOException { |
|
|
|
dbFile.close(); |