Class WrappingStorageUtility
java.lang.Object
org.javarosa.core.services.storage.WrappingStorageUtility
- All Implemented Interfaces:
IStorageUtility,IStorageUtilityIndexed
public class WrappingStorageUtility extends Object implements IStorageUtilityIndexed
A wrapper implementation of IStorageUtility that lets you serialize an object with a serialization
scheme other than the default scheme provided by the object's readExternal/writeExternal methods.
For example, FormInstance contains lots of redundant information about the structure of the instance
which doesn't change among saved instances. The extra space used for this redundant info can seriously
limit the number of saved forms we can store on a device. We can use this utility to serialize
FormInstances in a different way that excludes this redundant info (meaning we have to take the more
complicated step of restoring it from elsewhere during deserialization), with the benefit of much
smaller record sizes.
The alternate scheme is provided via a wrapper object, which accepts the base object and whose
readExternal/writeExternal methods implement the new scheme.
All methods pass through to an underlying StorageUtility; you may get warnings about type mismatches
- Author:
- Drew Roos
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceWrappingStorageUtility.SerializationWrapperDefines an alternate serialization scheme. -
Constructor Summary
Constructors Constructor Description WrappingStorageUtility(String name, WrappingStorageUtility.SerializationWrapper wrapper, IStorageFactory storageFactory)Create a new wrapping StorageUtility -
Method Summary
Modifier and Type Method Description intadd(Externalizable e)Add a new record to the store.voidclose()Close all resources associated with this StorageUtility.voiddestroy()Delete the storage utility itself, along with all stored records and meta-databooleanexists(int id)Return whether a record exists in the storeObjectgetAccessLock()Fetch the object that acts as the synchronization lock for this StorageUtilityList<Integer>getIDsForValue(String fieldName, Object value)Retrieves a List of IDs of Externalizable objects in storage for which the field specified contains the value specified.intgetNumRecords()Return the number of records in the storeExternalizablegetRecordForValue(String fieldName, Object value)Retrieves a Externalizable object from the storage which is reference by the unique index fieldName.intgetRecordSize(int id)Get the size of a recordintgetTotalSize()Return total size of device storage consumed by this StorageUtilitybooleanisEmpty()Return whether the store is emptyIStorageIteratoriterate()Return an iterator to iterate through all records in this storeExternalizableread(int id)Read and return the record corresponding to 'id'.byte[]readBytes(int id)Read and return the raw bytes for the record corresponding to 'id'.voidregisterIndex(String filterIndex)Optional.voidremove(int id)Remove record with the given ID from the store.voidremove(Persistable p)Remove object from the storevoidremoveAll()List<Integer>removeAll(EntityFilter ef)voidrepack()Perform any clean-up/consolidation of the StorageUtility's underlying datastructures that is too expensive to do during normal usage (e.g., if all the records are scattered among 10 half-empty RMSes, repack them into 5 full RMSes)voidrepair()If the StorageUtility has been left in a corrupt/inconsistent state, restore it to a non-corrupt state, even if it results in data loss.voidsetReadOnly()voidupdate(int id, Externalizable e)Update a record in the store.voidwrite(Persistable p)Write an object to the store.
-
Constructor Details
-
WrappingStorageUtility
public WrappingStorageUtility(String name, WrappingStorageUtility.SerializationWrapper wrapper, IStorageFactory storageFactory)Create a new wrapping StorageUtility- Parameters:
name- unique name for underlying StorageUtilitywrapper- serialization wrapperstorageFactory- factory to create underlying StorageUtility
-
-
Method Details
-
read
Description copied from interface:IStorageUtilityRead and return the record corresponding to 'id'.- Specified by:
readin interfaceIStorageUtility- Parameters:
id- id of the object- Returns:
- object for 'id'. null if no object is stored under that ID
-
write
Description copied from interface:IStorageUtilityWrite an object to the store. Will either add a new record, or update the existing record (if one exists) for the object's ID. This function should never be used in conjunction with add() and update() within the same StorageUtility- Specified by:
writein interfaceIStorageUtility- Parameters:
p- object to store- Throws:
StorageFullException- if there is not enough room to store the object
-
add
Description copied from interface:IStorageUtilityAdd a new record to the store. This function always adds a new record; it never updates an existing record. The record ID under which this record is added is allocated by the StorageUtility. If this StorageUtility stores Persistables, you should almost certainly use write() instead.- Specified by:
addin interfaceIStorageUtility- Parameters:
e- object to add- Returns:
- record ID for newly added object
- Throws:
StorageFullException- if not enough space available
-
update
Description copied from interface:IStorageUtilityUpdate a record in the store. The record must have previously been added to the store using add(). If this StorageUtility stores Persistables, you should almost certainly use write() instead.- Specified by:
updatein interfaceIStorageUtility- Parameters:
id- ID of record to updatee- updated object- Throws:
StorageFullException- if not enough space available to update
-
iterate
Description copied from interface:IStorageUtilityReturn an iterator to iterate through all records in this store- Specified by:
iteratein interfaceIStorageUtility- Returns:
- record iterator
-
readBytes
public byte[] readBytes(int id)Description copied from interface:IStorageUtilityRead and return the raw bytes for the record corresponding to 'id'.- Specified by:
readBytesin interfaceIStorageUtility- Parameters:
id- id of the object- Returns:
- raw bytes for the record. null if no record is stored under that ID
-
remove
public void remove(int id)Description copied from interface:IStorageUtilityRemove record with the given ID from the store.- Specified by:
removein interfaceIStorageUtility- Parameters:
id- ID of record to remove
-
remove
Description copied from interface:IStorageUtilityRemove object from the store- Specified by:
removein interfaceIStorageUtility- Parameters:
p- object to remove
-
removeAll
public void removeAll()- Specified by:
removeAllin interfaceIStorageUtility
-
removeAll
- Specified by:
removeAllin interfaceIStorageUtility
-
exists
public boolean exists(int id)Description copied from interface:IStorageUtilityReturn whether a record exists in the store- Specified by:
existsin interfaceIStorageUtility- Parameters:
id- record ID- Returns:
- true if a record exists for that ID in the store
-
isEmpty
public boolean isEmpty()Description copied from interface:IStorageUtilityReturn whether the store is empty- Specified by:
isEmptyin interfaceIStorageUtility- Returns:
- true if there are no records in the store
-
getNumRecords
public int getNumRecords()Description copied from interface:IStorageUtilityReturn the number of records in the store- Specified by:
getNumRecordsin interfaceIStorageUtility- Returns:
- number of records
-
getRecordSize
public int getRecordSize(int id)Description copied from interface:IStorageUtilityGet the size of a record- Specified by:
getRecordSizein interfaceIStorageUtility- Parameters:
id- record ID- Returns:
- size of that record, in bytes
-
getTotalSize
public int getTotalSize()Description copied from interface:IStorageUtilityReturn total size of device storage consumed by this StorageUtility- Specified by:
getTotalSizein interfaceIStorageUtility- Returns:
- total size (bytes)
-
close
public void close()Description copied from interface:IStorageUtilityClose all resources associated with this StorageUtility. Any attempt to use this StorageUtility after this call will result in error. Though not strictly necessary, it is a good idea to call this when you are done with the StorageUtility, as closing may trigger clean-up in the underlying device storage (reclaiming unused space, etc.).- Specified by:
closein interfaceIStorageUtility
-
destroy
public void destroy()Description copied from interface:IStorageUtilityDelete the storage utility itself, along with all stored records and meta-data- Specified by:
destroyin interfaceIStorageUtility
-
repack
public void repack()Description copied from interface:IStorageUtilityPerform any clean-up/consolidation of the StorageUtility's underlying datastructures that is too expensive to do during normal usage (e.g., if all the records are scattered among 10 half-empty RMSes, repack them into 5 full RMSes)- Specified by:
repackin interfaceIStorageUtility
-
repair
public void repair()Description copied from interface:IStorageUtilityIf the StorageUtility has been left in a corrupt/inconsistent state, restore it to a non-corrupt state, even if it results in data loss. If the integrity is intact, do nothing- Specified by:
repairin interfaceIStorageUtility
-
getAccessLock
Description copied from interface:IStorageUtilityFetch the object that acts as the synchronization lock for this StorageUtility- Specified by:
getAccessLockin interfaceIStorageUtility- Returns:
- lock object
-
getIDsForValue
Description copied from interface:IStorageUtilityIndexedRetrieves a List of IDs of Externalizable objects in storage for which the field specified contains the value specified.- Specified by:
getIDsForValuein interfaceIStorageUtilityIndexed- Parameters:
fieldName- The name of a field which should be evaluatedvalue- The value which should be contained by the field specified- Returns:
- A List of Integers such that retrieving the Externalizable object with any of those integer IDs will result in an object for which the field specified is equal to the value provided.
-
getRecordForValue
public Externalizable getRecordForValue(String fieldName, Object value) throws NoSuchElementException, InvalidIndexExceptionDescription copied from interface:IStorageUtilityIndexedRetrieves a Externalizable object from the storage which is reference by the unique index fieldName.- Specified by:
getRecordForValuein interfaceIStorageUtilityIndexed- Parameters:
fieldName- The name of the index field which will be evaluatedvalue- The value which should be set in the index specified by fieldName for the returned object.- Returns:
- An Externalizable object e, such that e.getMetaData(fieldName).equals(value);
- Throws:
NoSuchElementException- If no objects reside in storage for which the return condition can be successful.InvalidIndexException- If the field used is an invalid index, because more than one field in the Storage contains the value of the index requested.
-
setReadOnly
public void setReadOnly()- Specified by:
setReadOnlyin interfaceIStorageUtility
-
registerIndex
Description copied from interface:IStorageUtilityIndexedOptional. Register a new index for this storage which may optionally be able for indexed operations going forward. This will likely take a substantial amount of time for larger storage utilities.- Specified by:
registerIndexin interfaceIStorageUtilityIndexed
-