Class HumanFaceMemoryManagerImpl

java.lang.Object
cz.fidentis.analyst.data.face.impl.HumanFaceMemoryManagerImpl
All Implemented Interfaces:
HumanFaceMemoryManager

public class HumanFaceMemoryManagerImpl extends Object implements HumanFaceMemoryManager
A flyweight factory that creates and caches human faces. Faces are stored in the memory until there is enough space in the Java heap. Then they are cached on disk automatically. The dumping strategy can be switched at run-time.

Currently, listeners registered to HumanFace are neither dumped nor recovered!

  • Constructor Details

    • HumanFaceMemoryManagerImpl

      public HumanFaceMemoryManagerImpl()
      Constructor.
  • Method Details

    • setStrategy

      public void setStrategy(HumanFaceMemoryManager.Strategy strategy)
      Description copied from interface: HumanFaceMemoryManager
      Changes the dumping strategy
      Specified by:
      setStrategy in interface HumanFaceMemoryManager
      Parameters:
      strategy - Dumping strategy. Must not be null
    • getStrategy

      public HumanFaceMemoryManager.Strategy getStrategy()
      Description copied from interface: HumanFaceMemoryManager
      Returns current dumping strategy.
      Specified by:
      getStrategy in interface HumanFaceMemoryManager
      Returns:
      current dumping strategy
    • setReuseDumpFile

      public void setReuseDumpFile(boolean use)
      Description copied from interface: HumanFaceMemoryManager
      If set to true, then the dump file of a face is created only once and then reused for every recovery (it is never overwritten). It accelerates the dumping face, but can be used only if the state of faces never changes between the first dump a consequent recoveries. If set to false, then every dump of the face to the disk overwrites the file.
      Specified by:
      setReuseDumpFile in interface HumanFaceMemoryManager
      Parameters:
      use - If true, then this optimization is turned on.
    • loadFace

      public String loadFace(File file)
      Description copied from interface: HumanFaceMemoryManager
      Loads new face. If the face is already loaded, then the ID of existing instance is returned. To access the human face instance, use HumanFaceMemoryManager.getFace(String).
      Specified by:
      loadFace in interface HumanFaceMemoryManager
      Parameters:
      file - OBJ file with human face.
      Returns:
      ID of the human face or null
    • isLoaded

      public boolean isLoaded(File faceFile)
      Description copied from interface: HumanFaceMemoryManager
      Checks whether the given face is loaded in the factory
      Specified by:
      isLoaded in interface HumanFaceMemoryManager
      Parameters:
      faceFile - OBJ file with human face.
      Returns:
      true if the face exists in the factory, false otherwise.
    • addFace

      public String addFace(HumanFace face)
      Description copied from interface: HumanFaceMemoryManager
      Directly adds an existing face instance into the factory. If a face with the same ID already exists, then it is replaced.
      Specified by:
      addFace in interface HumanFaceMemoryManager
      Parameters:
      face - Human face
      Returns:
      ID of the human face or null
    • getFace

      public HumanFace getFace(String faceId)
      Description copied from interface: HumanFaceMemoryManager
      Returns a human face. Recovers the face from the disk if necessary.
      Specified by:
      getFace in interface HumanFaceMemoryManager
      Parameters:
      faceId - ID of the face
      Returns:
      Human face or null
    • removeFace

      public boolean removeFace(String faceId)
      Description copied from interface: HumanFaceMemoryManager
      Removes the face from either memory or swap space. To re-initiate the face, use HumanFaceMemoryManager.loadFace(java.io.File) again.
      Specified by:
      removeFace in interface HumanFaceMemoryManager
      Parameters:
      faceId - Face ID
      Returns:
      true if the face existed and was removed.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • updateAccessTime

      protected boolean updateAccessTime(String faceId)
      Updates last access time of the face allocated in the memory. Returns false if the face is not in the memory.
      Parameters:
      faceId - Face to be updated
      Returns:
      false if the face is not in the memory.
    • checkMemAndDump

      protected int checkMemAndDump() throws IOException
      Checks and releases the heap, if necessary.
      Returns:
      true if some existing face has been dumped to free the memory.
      Throws:
      IOException - on I/O error
    • formatSize

      protected static String formatSize(long v)
    • presumableFreeMemory

      protected long presumableFreeMemory()
      https://stackoverflow.com/questions/12807797/java-get-available-memory
      Returns:
    • dumpToFile

      protected File dumpToFile(HumanFace face) throws IOException
      Throws:
      IOException
    • restoreFromFile

      protected static HumanFace restoreFromFile(File dumpFile) throws IOException
      Throws:
      IOException