2 * Sixth - System for data storage, computation, exploration and interaction.
3 * Copyright ©2012-2016, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 3 of the GNU Lesser General Public License
7 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.sixth.data.store.file;
12 import java.io.IOException;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
17 public class EntryAllocationTable {
19 private final FileDataStore fileDataStore;
21 public EntryAllocationTable(final FileDataStore fileDataStore) {
22 this.fileDataStore = fileDataStore;
25 public void enlarge(final int newSize) throws IOException {
26 final int oldSize = fileDataStore.metaData.getEntriesTableSize();
28 for (int i = oldSize; i < newSize; i++) {
29 final EntryRecord entryRecord = new EntryRecord(i, 0, 0);
30 entryRecord.save(fileDataStore);
33 fileDataStore.metaData.setEntriesTableSize(newSize);
36 public long getEntryRecordLocation(final int entryId) {
37 return (entryId * EntryRecord.ENTRY_RECORD_LENGTH)
38 + MetaData.FILE_LOCATION_ENTRY_ALLOCATION_TABLE_START;
41 public int getNewUnusedEntryId() throws IOException {
44 final int newEntryId = fileDataStore.metaData.getNewEntryId();
46 final EntryRecord entryRecord = new EntryRecord(fileDataStore,
49 if (!entryRecord.isUsed())
54 public void initializeNewFile() throws IOException {
55 for (int i = 0; i < fileDataStore.metaData.getEntriesTableSize(); i++) {
56 final EntryRecord entryRecord = new EntryRecord(i, 0, 0);
57 entryRecord.save(fileDataStore);
62 * Sorted list of @link {@link EntryRecord}'s.
64 public List<EntryRecord> loadAllEntryRecords() throws IOException {
65 final List<EntryRecord> entryRecords = new ArrayList<>();
67 for (int i = 0; i < fileDataStore.metaData.getEntriesTableSize(); i++) {
68 final EntryRecord entryRecord = new EntryRecord(fileDataStore, i);
69 if (entryRecord.isUsed())
70 entryRecords.add(entryRecord);
73 Collections.sort(entryRecords);