Package com.github.dagnelies.filemap
Class AbstractFileMap<K,V>
- java.lang.Object
-
- com.github.dagnelies.filemap.AbstractFileMap<K,V>
-
- Type Parameters:
K-V-
- Direct Known Subclasses:
CachedFileMap,IndexedFileMap
public abstract class AbstractFileMap<K,V> extends Object implements FileMap<K,V>
This abstract class is a utility to easily manipulate lines in a random access file and converting text lines into key/value format and vice-versa.- Author:
- dagnelies
-
-
Field Summary
Fields Modifier and Type Field Description protected Filefileprotected BufferedRandomAccessFilefileio
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidclearLines()voidclose()longdiskSize()protected longgetEntriesWritten()FilegetFile()doublegetFragmentation()Returns an estimate of the file's content fragmentation.protected abstract voidinit()protected abstract voidloadEntry(long offset, String line)protected KparseKey(String line)protected Map.Entry<K,V>parseLine(String line)protected VparseValue(String line)voidputAll(Map<? extends K,? extends V> m)protected StringreadLine(long offset)protected longwriteLine(K key, V value)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Map
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, put, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
-
-
-
-
Field Detail
-
file
protected File file
-
fileio
protected BufferedRandomAccessFile fileio
-
-
Constructor Detail
-
AbstractFileMap
public AbstractFileMap(File file, Class<K> keyType, Class<V> valueType) throws IOException
- Throws:
IOException
-
-
Method Detail
-
getEntriesWritten
protected long getEntriesWritten()
-
init
protected abstract void init() throws IOException- Throws:
IOException
-
loadEntry
protected abstract void loadEntry(long offset, String line) throws IOException- Throws:
IOException
-
parseLine
protected Map.Entry<K,V> parseLine(String line) throws IOException
- Throws:
IOException
-
parseKey
protected K parseKey(String line) throws IOException
- Throws:
IOException
-
parseValue
protected V parseValue(String line) throws IOException
- Throws:
IOException
-
readLine
protected String readLine(long offset) throws IOException
- Throws:
IOException
-
clearLines
protected void clearLines()
-
getFragmentation
public double getFragmentation()
Returns an estimate of the file's content fragmentation. It is the ratio of obsolete data in the file. When entries are frequently updated and removed, the old entries are still stored in the file. For example, a fragmentation of 2/3 would mean that roughly 2/3 of the file is filled with obsolete content.- Returns:
-
close
public void close() throws IOException- Specified by:
closein interfaceFileMap<K,V>- Throws:
IOException
-
-