Class FileStore<V extends Identifiable>

  • All Implemented Interfaces:
    Store<V>, Map<Identifier,​V>
    Direct Known Subclasses:
    FSClientStore, MonitoredFileStore

    public abstract class FileStore<V extends Identifiable>
    extends IndexedStreamStore<V>
    A store backed by the file system. This works with all implementations since it just serializes whatever item (which must be of type Identifiable) it has.

    How's it work?

    There are two directories, one, the storage directory, contains the serialized items themselves. The file names are hashes of the unique identifiers. The other directory, the index directory, contains files whose names are hashes of the other information (e.g. temporary credentials, client identifier, verifier, access token,...). You will have to override to get the right index entries. (See below) Each of these index files contains a single line which is the file name in the storage directory. So a request to get by the temporary credential will hash the credential, grab the index file, read the name of the actual transaction file and load that.

    This does no caching of any sort. If you want caching (strongly suggested) create a transaction cache and set its backing cache to be an instance of this.

    Usage

    To use this, you need to override a couple of methods:

    A store that uses a file system.

    Created by Jeff Gaynor
    on 11/3/11 at 1:54 PM

    • Field Detail

      • indexDirectory

        protected File indexDirectory
      • storageDirectory

        protected File storageDirectory
    • Constructor Detail

      • FileStore

        protected FileStore​(File storeDirectory,
                            File indexDirectory,
                            IdentifiableProvider<V> identifiableProvider,
                            MapConverter<V> converter,
                            boolean removeEmptyFiles,
                            boolean removeFailedFiles)
        For the case where the data and index directories are explicitly given.
        Parameters:
        storeDirectory -
        indexDirectory -
        identifiableProvider -
        converter -
      • FileStore

        public FileStore​(File directory,
                         IdentifiableProvider<V> idp,
                         MapConverter<V> cp,
                         boolean removeEmptyFiles,
                         boolean removeFailedFiles)
        Accepts a directory for both the index and data and creates the subdirectories.
        Parameters:
        directory -
        idp -
        cp -
    • Method Detail

      • checkPermissions

        protected void checkPermissions()
        Since administrators can and have inadvertently changed directory or file permissions while the server is running, here is a method to check if the directory has acceptable permissions. Each call to the store should invoke this as its first action.
      • doSetup

        protected void doSetup​(File storeDirectory,
                               File indexDirectory,
                               IdentifiableProvider<V> identifiableProvider,
                               MapConverter<V> converter,
                               boolean removeEmptyFiles,
                               boolean removeFailedFiles)
      • getIndexDirectory

        public File getIndexDirectory()
      • setIndexDirectory

        public void setIndexDirectory​(File indexDirectory)
      • getStorageDirectory

        public File getStorageDirectory()
      • setStorageDirectory

        public void setStorageDirectory​(File storageDirectory)
      • getItemFile

        protected File getItemFile​(V t)
      • getItemFile

        protected File getItemFile​(String identifier)
      • realSave

        public void realSave​(boolean checkExists,
                             V t)
        Does the actual work of writing everything to the data directory. Override this as needed and invoke createIndexEntry(String, String) to put and entry for the item into the index. When overriding, call this via super first or the item itself will not be saved.
        Parameters:
        checkExists -
        t -
      • createIndexEntry

        protected void createIndexEntry​(String otherKey,
                                        String identifier)
                                 throws IOException
        Add an index entry for an item that is not the unique identifier.
        Parameters:
        otherKey - the other key to index
        identifier - the unique identifier for this item
        Throws:
        IOException
      • loadFromIndex

        protected V loadFromIndex​(String hashedName)
        Finds a file with the given index value. This will look in the index directory for the file with the same name as the lookup, then read the contents of the lookup which is a hashed uri
        Parameters:
        hashedName -
        Returns:
        Throws:
        IOException
      • loadByIdentifier

        protected V loadByIdentifier​(String identifier)
      • loadFile

        protected V loadFile​(File f)
      • update

        public void update​(V t)
        Description copied from interface: Store
        Update an existing object. An UnregisteredObjectException is thrown if the object has not been saved first.
      • register

        public void register​(V t)
        Description copied from interface: Store
        Almost Identical to put(K,V) but since the object should have an identifier, passing along the key is redundant. This persists the object in the store. Note that this returns void since the contract assumes that this is not registered. If the object is registered an exception should be thrown. Generally use save(V).
      • clear

        public void clear()
        Required by the map interface
      • save

        public void save​(V t)
        Description copied from interface: Store
        Saves an object. This bridges the gap between SQL stores update and insert commands. Implementations should check if the object already exists in the store and issue an appropriate call.
      • values

        public Collection<V> values()
        Not an efficient way to get the values, but this will get them all.
        Returns:
      • size

        public int size()
      • size

        public int size​(boolean includeVersions)
      • containsKey

        public boolean containsKey​(Object key)
      • containsValue

        public boolean containsValue​(Object value)
      • get

        public V get​(Object key)
        This updates the last accessed listener
        Parameters:
        key -
        Returns:
      • getAll

        public List<V> getAll()
        Description copied from interface: Store
        Method to get every element in the store. This is useful for command line interfaces. Note that this might be very expensive.
        Returns:
      • remove

        public boolean remove​(List<Identifiable> objects)
        Terribly inefficient. Terribly. However, the assumption is that a file store is small rather than large. For larger numbers of entries, use a database.
        Parameters:
        objects -
        Returns:
      • delete

        public boolean delete​(String identifier)
      • realRemove

        protected V realRemove​(V oldItem)
        Does the actual removal of the item from the store. Be sure to override this to remove any index entries if you need to.
        Parameters:
        oldItem - The item (which is Identifiable) to be removed.
        Returns:
      • remove

        public V remove​(Object key)
        This is required by the map interface. The argument is really the identifier. This returns the transaction if there was one already associated with this identifier
        Parameters:
        key -
        Returns:
      • putAll

        public void putAll​(Map m)
      • removeIndexEntry

        protected boolean removeIndexEntry​(String token)
        Remove an index entry (not the actual item!). To remove the item, use remove(Object).
        Parameters:
        token -
      • getIndexEntry

        protected V getIndexEntry​(String token)
        Get a stored item using a key other than the identifier.
        Parameters:
        token -
        Returns:
      • create

        public V create()
        Description copied from interface: Store
        Create a new object of the given type. This is not in the store until it is registered. Attempts to update the object should throw an exception. Note that this allows for a separation of creation semantics. Some objects require specific initialization before saving
        Specified by:
        create in interface Store<V extends Identifiable>
        Overrides:
        create in class IndexedStreamStore<V extends Identifiable>
        Returns:
      • search

        public List<V> search​(String key,
                              String condition,
                              boolean isRegEx,
                              List<String> attr)
        Description copied from interface: Store
        Return a subset of all the attributes. For non-SQL stores performance may be slow.
        Returns:
      • search

        public List<V> search​(String key,
                              String condition,
                              boolean isRegEx)
        Description copied from interface: Store
        Allows for searching via a reg ex. Note that this may be very expensive for certain stores!
        Returns: