2 * Sixth Data. Copyright ©2012-2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 3 of the GNU Lesser General Public License
6 * or later as published by the Free Software Foundation.
9 package eu.svjatoslav.sixth.data.store.file;
11 import java.io.IOException;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
16 public class EntryAllocationTable {
18 private final FileDataStore fileDataStore;
20 public EntryAllocationTable(final FileDataStore fileDataStore) {
21 this.fileDataStore = fileDataStore;
24 public void enlarge(final int newSize) throws IOException {
25 final int oldSize = fileDataStore.metaData.getEntriesTableSize();
27 for (int i = oldSize; i < newSize; i++) {
28 final EntryRecord entryRecord = new EntryRecord(i, 0, 0);
29 entryRecord.save(fileDataStore);
32 fileDataStore.metaData.setEntriesTableSize(newSize);
35 public long getEntryRecordLocation(final int entryId) {
36 return (entryId * EntryRecord.ENTRY_RECORD_LENGTH)
37 + MetaData.FILE_LOCATION_ENTRY_ALLOCATION_TABLE_START;
40 public int getNewUnusedEntryId() throws IOException {
43 final int newEntryId = fileDataStore.metaData.getNewEntryId();
45 final EntryRecord entryRecord = new EntryRecord(fileDataStore,
48 if (!entryRecord.isUsed())
53 public void initializeNewFile() throws IOException {
54 for (int i = 0; i < fileDataStore.metaData.getEntriesTableSize(); i++) {
55 final EntryRecord entryRecord = new EntryRecord(i, 0, 0);
56 entryRecord.save(fileDataStore);
61 * Sorted list of @link {@link EntryRecord}'s.
63 public List<EntryRecord> loadAllEntryRecords() throws IOException {
64 final List<EntryRecord> entryRecords = new ArrayList<>();
66 for (int i = 0; i < fileDataStore.metaData.getEntriesTableSize(); i++) {
67 final EntryRecord entryRecord = new EntryRecord(fileDataStore, i);
68 if (entryRecord.isUsed())
69 entryRecords.add(entryRecord);
72 Collections.sort(entryRecords);