java.lang.Object
com.renomad.minum.database.Db<T>
- Type Parameters:
T
- the type of data we'll be persisting (must extend fromDbData
a memory-based disk-persisted database class.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Delete datavoid
stop()
This function will stop the minum.database persistence cleanly.void
stop
(int count, int sleepTime) Similar tostop()
but gives more control over how long we'll wait before crashing it closed.values()
This method provides read capability for the values of a database.Write data to the database.
-
Constructor Details
-
Db
Constructs an in-memory disk-persisted database.- Parameters:
dbDirectory
- this uniquely names your database, and also sets the directory name for this data. The expected use case is to name this after the data in question. For example, "users", or "accounts".context
- used to provide important state data to several componentsinstance
- an instance of theDbData
object relevant for use in this database. Note that each database (that is, each instance of this class), focuses on just one data, which must be an implementation ofDbData
.
-
-
Method Details
-
stop
public void stop()This function will stop the minum.database persistence cleanly.In order to do this, we need to wait for our threads to finish their work. In particular, we have offloaded our file writes to [actionQueue], which has an internal thread for serializing all actions on our minum.database
-
stop
public void stop(int count, int sleepTime) Similar tostop()
but gives more control over how long we'll wait before crashing it closed. SeeActionQueue.stop(int, int)
-
write
Write data to the database. Use an index of 0 to store new data, and a positive non-zero value to update data.Example of adding new data to the database:
final var newSalt = StringUtils.generateSecureRandomString(10); final var hashedPassword = CryptoUtils.createPasswordHash(newPassword, newSalt); final var newUser = new User(0L, newUsername, hashedPassword, newSalt); userDb.write(newUser);
Example of updating data:
// write the updated salted password to the database final var updatedUser = new User( user().getIndex(), user().getUsername(), hashedPassword, newSalt); userDb.write(updatedUser);
- Parameters:
newData
- the data we are writing
-
delete
Delete dataExample:
userDb.delete(user);
- Parameters:
dataToDelete
- the data we are serializing and writing
-
values
This method provides read capability for the values of a database.
The returned collection is a read-only view over the data, throughCollections.unmodifiableCollection(Collection)
Example:
boolean doesUserAlreadyExist(String username) { return userDb.values().stream().anyMatch(x -> x.getUsername().equals(username)); }
-