Procházet zdrojové kódy

create index from file at startup

master
Jordi Claes před 3 roky
rodič
revize
dbe8f17c2a

+ 25
- 3
src/main/java/cc/javastudio/raw/FileHandler.java Zobrazit soubor

@@ -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();

+ 3
- 2
src/main/java/cc/javastudio/testapp/TestAPP.java Zobrazit soubor

@@ -9,12 +9,13 @@ import java.io.IOException;
public class TestAPP {
public static void main(String[] args) {
try (FileHandler fh = new FileHandler("DBServer.db")){
fh.loadIndex();
fh.add("Johñ Doe", 44, "Berlin", "CCC-404", "Blue VW Beetle");
fh.add("Jane Doe", 35, "USA", "CCC-404", "Seat Mottig");
Person person = fh.readRow(1);
System.out.println(person);
Index index = Index.getInstance();
System.out.println(index);
//Index index = Index.getInstance();
//System.out.println(index);
} catch (IOException e) {
e.printStackTrace();
}

Načítá se…
Zrušit
Uložit