Class PersistentMap<V>

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.util.Map<java.lang.String,​V>

    public class PersistentMap<V>
    extends java.util.AbstractMap<java.lang.String,​V>
    implements java.io.Closeable
    Implements a low performance but easy to use map that is backed on a directory. All objects are stored as JSON objects and therefore should be DTOs. Each key is a file name and the contents is the value encoded in JSON. The PersistentMap will attempt to lock the directory. This is a non-concurrent implementation so you must ensure it is only used in a single thread. It cannot of course also not share the data directory.
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.util.AbstractMap

        java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,​V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,​V extends java.lang.Object>
      • 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
      (package private) java.util.Map<java.lang.String,​java.lang.ref.SoftReference<V>> cache  
      (package private) boolean closed  
      (package private) static JSONCodec codec  
      (package private) java.io.File data  
      (package private) java.io.File dir  
      private static char ESCAPE  
      (package private) boolean inited  
      (package private) java.io.RandomAccessFile lockFile  
      (package private) java.lang.reflect.Type type  
    • Constructor Summary

      Constructors 
      Constructor Description
      PersistentMap​(java.io.File dir, java.lang.Class<V> type)  
      PersistentMap​(java.io.File dir, java.lang.Class<V> type, java.util.Map<java.lang.String,​V> map)  
      PersistentMap​(java.io.File dir, java.lang.reflect.Type type)  
      PersistentMap​(java.io.File dir, java.lang.reflect.Type type, java.util.Map<java.lang.String,​V> map)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      void clear​(long whenOlder)  
      void close()  
      java.util.Set<java.util.Map.Entry<java.lang.String,​V>> entrySet()  
      (package private) void init()  
      java.util.Set<java.lang.String> keySet()  
      private java.io.File keyToFile​(java.lang.String key)  
      private java.nio.channels.FileLock lock()  
      V put​(java.lang.String key, V value)  
      V remove​(java.lang.String key)  
      java.lang.String toString()  
      private void unlock​(java.nio.channels.FileLock lock)  
      • Methods inherited from class java.util.AbstractMap

        clone, containsKey, containsValue, equals, get, hashCode, isEmpty, putAll, remove, size, values
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

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

      • dir

        final java.io.File dir
      • data

        final java.io.File data
      • lockFile

        final java.io.RandomAccessFile lockFile
      • cache

        final java.util.Map<java.lang.String,​java.lang.ref.SoftReference<V>> cache
      • inited

        boolean inited
      • closed

        boolean closed
      • type

        java.lang.reflect.Type type
    • Constructor Detail

      • PersistentMap

        public PersistentMap​(java.io.File dir,
                             java.lang.reflect.Type type)
                      throws java.lang.Exception
        Throws:
        java.lang.Exception
      • PersistentMap

        public PersistentMap​(java.io.File dir,
                             java.lang.Class<V> type)
                      throws java.lang.Exception
        Throws:
        java.lang.Exception
      • PersistentMap

        public PersistentMap​(java.io.File dir,
                             java.lang.Class<V> type,
                             java.util.Map<java.lang.String,​V> map)
                      throws java.lang.Exception
        Throws:
        java.lang.Exception
      • PersistentMap

        public PersistentMap​(java.io.File dir,
                             java.lang.reflect.Type type,
                             java.util.Map<java.lang.String,​V> map)
                      throws java.lang.Exception
        Throws:
        java.lang.Exception
    • Method Detail

      • init

        void init()
      • entrySet

        public java.util.Set<java.util.Map.Entry<java.lang.String,​V>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<java.lang.String,​V>
        Specified by:
        entrySet in class java.util.AbstractMap<java.lang.String,​V>
      • put

        public V put​(java.lang.String key,
                     V value)
        Specified by:
        put in interface java.util.Map<java.lang.String,​V>
        Overrides:
        put in class java.util.AbstractMap<java.lang.String,​V>
      • lock

        private java.nio.channels.FileLock lock()
                                         throws java.io.IOException,
                                                java.lang.InterruptedException
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • unlock

        private void unlock​(java.nio.channels.FileLock lock)
                     throws java.io.IOException
        Throws:
        java.io.IOException
      • remove

        public V remove​(java.lang.String key)
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map<java.lang.String,​V>
        Overrides:
        clear in class java.util.AbstractMap<java.lang.String,​V>
      • keySet

        public java.util.Set<java.lang.String> keySet()
        Specified by:
        keySet in interface java.util.Map<java.lang.String,​V>
        Overrides:
        keySet in class java.util.AbstractMap<java.lang.String,​V>
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.util.AbstractMap<java.lang.String,​V>
      • clear

        public void clear​(long whenOlder)
      • keyToFile

        private java.io.File keyToFile​(java.lang.String key)