2 * Sixth Data. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
7 package eu.svjatoslav.sixth.data.store.file;
9 import java.io.IOException;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
14 public class EntryAllocationTable {
16 private final FileDataStore fileDataStore;
18 public EntryAllocationTable(final FileDataStore fileDataStore) {
19 this.fileDataStore = fileDataStore;
22 public void enlarge(final int newSize) throws IOException {
23 final int oldSize = fileDataStore.metaData.getEntriesTableSize();
25 for (int i = oldSize; i < newSize; i++) {
26 final EntryRecord entryRecord = new EntryRecord(i, 0, 0);
27 entryRecord.save(fileDataStore);
30 fileDataStore.metaData.setEntriesTableSize(newSize);
33 public long getEntryRecordLocation(final int entryId) {
34 return (entryId * EntryRecord.ENTRY_RECORD_LENGTH)
35 + MetaData.FILE_LOCATION_ENTRY_ALLOCATION_TABLE_START;
38 public int getNewUnusedEntryId() throws IOException {
41 final int newEntryId = fileDataStore.metaData.getNewEntryId();
43 final EntryRecord entryRecord = new EntryRecord(fileDataStore,
46 if (!entryRecord.isUsed())
51 public void initializeNewFile() throws IOException {
52 for (int i = 0; i < fileDataStore.metaData.getEntriesTableSize(); i++) {
53 final EntryRecord entryRecord = new EntryRecord(i, 0, 0);
54 entryRecord.save(fileDataStore);
59 * Sorted list of @link {@link EntryRecord}'s.
61 public List<EntryRecord> loadAllEntryRecords() throws IOException {
62 final List<EntryRecord> entryRecords = new ArrayList<>();
64 for (int i = 0; i < fileDataStore.metaData.getEntriesTableSize(); i++) {
65 final EntryRecord entryRecord = new EntryRecord(fileDataStore, i);
66 if (entryRecord.isUsed())
67 entryRecords.add(entryRecord);
70 Collections.sort(entryRecords);