Package org.rapidcontext.core.storage
Class Storage
java.lang.Object
org.rapidcontext.core.storage.StorableObject
org.rapidcontext.core.storage.Storage
- All Implemented Interfaces:
Comparable<Storage>
- Direct Known Subclasses:
DirStorage
,MemoryStorage
,ZipStorage
The persistent data storage and retrieval class. This base class
is extended by storage services to provide actual data lookup and
storage.
- Version:
- 1.0
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String[]
The list of file extensions supported for data.static final String
The file extension for JSON data.static final String
The file extension for properties data.static final String
The file extension for XML data.static final String
The file extension for YAML data.static final String
The dictionary key for the mount overlay path.static final String
The dictionary key for the mount overlay priority.static final String
The dictionary key for the mount path.static final String
The dictionary key for the mount timestamp.static final String
The dictionary key for the read-write flag.static final Path
The storage information path.Fields inherited from class org.rapidcontext.core.storage.StorableObject
dict, KEY_ACTIVATED_TIME, KEY_CLASSNAME, KEY_ID, KEY_TYPE, PREFIX_COMPUTED, PREFIX_HIDDEN
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionint
Compares this storage with another.boolean
Returns the read-write flag.abstract Object
Loads an object from the specified location.<T> T
Loads an object from the specified location.protected Path
Returns a local storage path by removing an optional base storage path.abstract Metadata
Searches for an object at the specified location and returns metadata about the object if found.Returns the mount overlay path.int
Returns the mount overlay priority.Returns the storage mount time.static String
objectName
(String name) Returns a normalized object name by removing any supported data format file extension (if found).static Path
objectPath
(Path path) Returns a normalized path by removing any supported data format file extension (if found).path()
Returns the storage mount path.Returns a new storage query for this storage.abstract void
Removes an object or an index at the specified location.protected static void
serialize
(String filename, Object obj, OutputStream os) Serializes a data object into a data stream.static Path
serializedPath
(Path path, String mimeType) Returns a serialized path by adding a supported data format file extension.void
setMountInfo
(Path path, boolean readWrite, Path overlay, int prio) Updates the mount information for this storage.abstract void
Stores an object at the specified location.protected static Object
unserialize
(String filename, InputStream is) Unserializes a data stream into a data object.Methods inherited from class org.rapidcontext.core.storage.StorableObject
activate, activatedTime, destroy, id, init, isActive, isModified, passivate, serialize, sterilize, toString, type
-
Field Details
-
PATH_STORAGEINFO
The storage information path. Each storage implementation should provide introspection abilities by returning it's own dictionary when queried for this path. -
KEY_READWRITE
The dictionary key for the read-write flag.- See Also:
-
KEY_MOUNT_PATH
The dictionary key for the mount path. The value stored is a path object. If the storage is not mounted, this key is not set or has a null value.- See Also:
-
KEY_MOUNT_TIME
The dictionary key for the mount timestamp. The value stored is a Date object, using the current system time as default.- See Also:
-
KEY_MOUNT_OVERLAY_PATH
The dictionary key for the mount overlay path. The value stored is a path object indicating if the storage is also mounted as a global overlay. If the storage has not been mounted at all, this key is not set.- See Also:
-
KEY_MOUNT_OVERLAY_PRIO
The dictionary key for the mount overlay priority. The value stored is an integer indicating the mount overlay priority, higher numbers corresponding to higher priority. If the storage has not been mounted at all, this key is not set.- See Also:
-
EXT_PROPERTIES
The file extension for properties data.- See Also:
-
EXT_JSON
The file extension for JSON data.- See Also:
-
EXT_XML
The file extension for XML data.- See Also:
-
EXT_YAML
The file extension for YAML data.- See Also:
-
EXT_ALL
The list of file extensions supported for data.
-
-
Constructor Details
-
Storage
Creates a new storage.- Parameters:
id
- the storage identifierstorageType
- the storage type namereadWrite
- the read write flag
-
-
Method Details
-
objectName
Returns a normalized object name by removing any supported data format file extension (if found).- Parameters:
name
- the filename or file path to check- Returns:
- the filename without extension, or the input filename if not recognized
-
objectPath
Returns a normalized path by removing any supported data format file extension (if found).- Parameters:
path
- the original path- Returns:
- the path without trailing extension, or the input path if not recognized
-
serializedPath
Returns a serialized path by adding a supported data format file extension.- Parameters:
path
- the original pathmimeType
- the optional desired MIME type- Returns:
- the path with a supported file extension, or the input path if an extension was already set
-
serialize
Serializes a data object into a data stream.- Parameters:
filename
- the filename (to choose format)obj
- the object to writeos
- the output data stream- Throws:
IOException
- if the serialization failed
-
unserialize
Unserializes a data stream into a data object.- Parameters:
filename
- the filename (to choose format)is
- the input data stream- Returns:
- the object read, or null if not supported
- Throws:
IOException
- if the unserialization failed
-
compareTo
Compares this storage with another.- Specified by:
compareTo
in interfaceComparable<Storage>
- Parameters:
other
- the object to compare with- Returns:
- a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified one
- Throws:
ClassCastException
- if the object wasn't comparable
-
path
Returns the storage mount path.- Overrides:
path
in classStorableObject
- Returns:
- the storage mount path, or the root path if the storage isn't mounted
-
localPath
Returns a local storage path by removing an optional base storage path. If the specified path does not have the base path prefix, it is returned unmodified.- Parameters:
path
- the path to adjust- Returns:
- the local storage path
-
isReadWrite
public boolean isReadWrite()Returns the read-write flag.- Returns:
- true if the storage is writable, or false otherwise
-
mountTime
Returns the storage mount time.- Returns:
- the storage mount time, or the current system time if not mounted
-
mountOverlayPath
Returns the mount overlay path. This value is set if the storage has been mounted as a global overlay (to the path returned). If the storage has not been mounted at all, null will be returned.- Returns:
- the storage mount overlay path, or null if no mount overlay is used
-
mountOverlayPrio
public int mountOverlayPrio()Returns the mount overlay priority. Higher numbers correspond to higher priority.- Returns:
- the mount overlay priority, or -1 if not mounted
-
setMountInfo
Updates the mount information for this storage. This method will update the mount time to the current timestamp.- Parameters:
path
- the mount path (or storage root path)readWrite
- the storage read-write flagoverlay
- the mount overlay pathprio
- the mount overlay priority
-
query
Returns a new storage query for this storage. Supported data file extensions will automatically be hidden by this query.- Parameters:
base
- the base path (must be an index)- Returns:
- the storage query
-
lookup
Searches for an object at the specified location and returns metadata about the object if found. The path may locate either an index or a specific object.- Parameters:
path
- the storage location- Returns:
- the metadata for the object, or null if not found
-
load
Loads an object from the specified location. The path may locate either an index or a specific object. In case of an index, the data returned is an index dictionary listing of all objects in it.- Parameters:
path
- the storage location- Returns:
- the data read, or null if not found
-
load
Loads an object from the specified location. The result must be an instance of a specific class (or subclass).- Type Parameters:
T
- the type to cast objects to- Parameters:
path
- the storage locationclazz
- the class required- Returns:
- the data read, or null if not found or unmatched type
-
store
Stores an object at the specified location. The path must locate a particular object or file, since direct manipulation of indices is not supported. Any previous data at the specified path will be overwritten or removed.- Parameters:
path
- the storage locationdata
- the data to store- Throws:
StorageException
- if the data couldn't be written
-
remove
Removes an object or an index at the specified location. If the path refers to an index, all contained objects and indices will be removed recursively.- Parameters:
path
- the storage location- Throws:
StorageException
- if the data couldn't be removed
-