java.lang.Object
com.renomad.minum.database.DbData<T>
- Direct Known Subclasses:
Inmate
An abstract data type meant to be used with
Db
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected abstract T
deserialize
(String serializedText) deserializes the text back into an object.protected abstract long
getIndex()
We need an index so that each piece of data is distinct, even if it has the same data.protected abstract String
Serializes this object into a string representation.protected abstract void
setIndex
(long index) It is necessary for this method to exist because it is used by theDb
code to add the new index into this data.
-
Constructor Details
-
DbData
protected DbData()
-
-
Method Details
-
serialize
Serializes this object into a string representation. It will be the values of this object as strings, encoded with URL encoding, separated by pipe symbols.An example:
import static com.renomad.minum.utils.SerializationUtils.serializeHelper; public String serialize() { return serializeHelper(index, a, b); }
- Returns:
- this type serialized to a string - use
SerializationUtils.serializeHelper(Object[])
- See Also:
-
deserialize
deserializes the text back into an object. See helper methodSerializationUtils.deserializeHelper(String)
to split a serialized string into tokens for rebuilding the object. See alsoserialize()
An example:
import static com.renomad.minum.utils.SerializationUtils.deserializeHelper; public Foo deserialize(String serializedText) { final var tokens = deserializeHelper(serializedText); return new Foo( Integer.parseInt(tokens.get(0)), Integer.parseInt(tokens.get(1)), tokens.get(2) ); }
- Parameters:
serializedText
- the serialized string- Returns:
- this type deserialized from a string
- See Also:
-
getIndex
protected abstract long getIndex()We need an index so that each piece of data is distinct, even if it has the same data.- Returns:
- an index for this data, used to name a file for this particular instance of data
-
setIndex
protected abstract void setIndex(long index) It is necessary for this method to exist because it is used by theDb
code to add the new index into this data.Let me unpack that a bit.
The way our database works, it's expected that when you are creating a new instance of data, you won't know its index yet, because that is something the database manages for you.
The index is an
AtomicLong
value that allows us to create new data without worrying about race conditions between threads (that is, we don't have to worry about two threads accidentally adding the same index value to two different datas).This value is also used as the name for the file of this data stored on disk.
-