Class PersistentMap<K,​V>

  • All Implemented Interfaces:
    java.util.Map<K,​V>

    public class PersistentMap<K,​V>
    extends java.lang.Object
    implements java.util.Map<K,​V>
    A map implementation supporting automatic serialization its data and persisting it on disk.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected static class  PersistentMap.DeletedKey
      Marker for deleted entries.
      protected class  PersistentMap.ValueInfo
      Information about a stored value.
      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected long autocompactionthreshold
      Dirty threshold for auto-compaction.
      protected java.lang.ClassLoader classloader
      Class loader used for serialization.
      protected long dirtybytes
      Bytes of dirty entries.
      protected java.io.File file
      The persistence file.
      protected java.util.Map<K,​PersistentMap.ValueInfo> indexmap
      The index map, key to position and size of value.
      protected java.lang.String mode
      The file access mode.
      protected java.io.RandomAccessFile raf
      Random access to the persistence file
    • Constructor Summary

      Constructors 
      Constructor Description
      PersistentMap​(java.io.File file, boolean synchronous, java.lang.ClassLoader classloader)
      Creates the map.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void buildIndex()
      Builds the index.
      void clear()
      Removes all of the entries from this map.
      void close()
      Closes the persistence file.
      void compact()
      Removes stale entries and compacts the map.
      boolean containsKey​(java.lang.Object key)
      Returns whether this map contains the specified key.
      boolean containsValue​(java.lang.Object value)
      Returns whether the map contains the specified value.
      protected V doPut​(K key, V value)
      Commits a value to the map
      V doRemove​(java.lang.Object key)  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
      Returns the map's entry set.
      V get​(java.lang.Object key)
      Returns the value for a specified key.
      long getDirtyBytes()
      Returns the bytes wasted due to stale entries.
      boolean isEmpty()
      Returns whether this map is empty.
      java.util.Set<K> keySet()
      Returns the keys of the map.
      static void main​(java.lang.String[] args)
      Test main method.
      V put​(K key, V value)
      Puts a new value in the map.
      void putAll​(java.util.Map<? extends K,​? extends V> m)
      Copies entries from the specified map to this map,
      V remove​(java.lang.Object key)
      Removes a map entry.
      void setAutoCompactionThreshold​(long threshold)
      Sets the threshold of stale bytes before auto-compaction is executed.
      int size()
      Returns the size of the map.
      java.util.Collection<V> values()
      Returns the values of the map.
      • 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

        compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Field Detail

      • indexmap

        protected java.util.Map<K,​PersistentMap.ValueInfo> indexmap
        The index map, key to position and size of value.
      • file

        protected java.io.File file
        The persistence file.
      • mode

        protected java.lang.String mode
        The file access mode.
      • raf

        protected java.io.RandomAccessFile raf
        Random access to the persistence file
      • dirtybytes

        protected long dirtybytes
        Bytes of dirty entries.
      • autocompactionthreshold

        protected long autocompactionthreshold
        Dirty threshold for auto-compaction.
      • classloader

        protected java.lang.ClassLoader classloader
        Class loader used for serialization.
    • Constructor Detail

      • PersistentMap

        public PersistentMap​(java.io.File file,
                             boolean synchronous,
                             java.lang.ClassLoader classloader)
        Creates the map.
        Parameters:
        file - File used for persistent data.
    • Method Detail

      • main

        public static void main​(java.lang.String[] args)
        Test main method.
        Parameters:
        args - Arguments.
      • size

        public int size()
        Returns the size of the map.
        Specified by:
        size in interface java.util.Map<K,​V>
      • isEmpty

        public boolean isEmpty()
        Returns whether this map is empty.
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Returns whether this map contains the specified key.
        Specified by:
        containsKey in interface java.util.Map<K,​V>
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Returns whether the map contains the specified value. NOTE: This is very slow, use with caution.
        Specified by:
        containsValue in interface java.util.Map<K,​V>
      • get

        public V get​(java.lang.Object key)
        Returns the value for a specified key.
        Specified by:
        get in interface java.util.Map<K,​V>
      • put

        public V put​(K key,
                     V value)
        Puts a new value in the map.
        Specified by:
        put in interface java.util.Map<K,​V>
      • remove

        public V remove​(java.lang.Object key)
        Removes a map entry.
        Specified by:
        remove in interface java.util.Map<K,​V>
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> m)
        Copies entries from the specified map to this map,
        Specified by:
        putAll in interface java.util.Map<K,​V>
      • clear

        public void clear()
        Removes all of the entries from this map.
        Specified by:
        clear in interface java.util.Map<K,​V>
      • keySet

        public java.util.Set<K> keySet()
        Returns the keys of the map. Note: Unlike other maps, this is the preferred (fastest mode) for iteration.
        Specified by:
        keySet in interface java.util.Map<K,​V>
      • values

        public java.util.Collection<V> values()
        Returns the values of the map. NOTE: This is very slow, use with caution.
        Specified by:
        values in interface java.util.Map<K,​V>
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Returns the map's entry set. Warning: SLOW!
        Specified by:
        entrySet in interface java.util.Map<K,​V>
      • compact

        public void compact()
        Removes stale entries and compacts the map.
      • close

        public void close()
        Closes the persistence file. The map ceases to function after calling this.
      • getDirtyBytes

        public long getDirtyBytes()
        Returns the bytes wasted due to stale entries.
      • setAutoCompactionThreshold

        public void setAutoCompactionThreshold​(long threshold)
        Sets the threshold of stale bytes before auto-compaction is executed. Setting the threshold to Long.MAX_VALUE disables auto-compaction.
      • buildIndex

        protected void buildIndex()
        Builds the index.
      • doPut

        protected V doPut​(K key,
                          V value)
        Commits a value to the map
      • doRemove

        public V doRemove​(java.lang.Object key)