mirror of
https://github.com/beemdevelopment/Aegis.git
synced 2025-06-08 15:47:47 +00:00
Introduce UUIDMap for storing objects that are keyed by a UUID
This patch introduces the new ``UUIDMap`` type, reducing code duplication and making UUID lookups faster. We currently already use UUIDs as the identifier for the ``DatabaseEntry`` and ``Slot`` types, but the way lookups by UUID work are kind of ugly, as we simply iterate over the list until we find a match. As we're probably going to have more types like this soon (groups and icons, for example), I figured it'd be good to abstract this away into a separate type and make it a map instead of a list. The only thing that has gotten slower is the ``swap`` method. The internal ``LinkedHashMap`` retains insertion order with a linked list, but does not know about the position of the values, so we basically have to copy the entire map to simply swap two values. I don't think it's too big of a deal, because swap operations still take less than a millisecond even with large vaults, but suggestions for improving this are welcome. I had to update gradle and JUnit to be able to use the new ``assertThrows`` assertion method, so this patch includes that as well.
This commit is contained in:
parent
6769fefd00
commit
2323d89938
21 changed files with 375 additions and 243 deletions
|
@ -2,17 +2,15 @@ package com.beemdevelopment.aegis.db;
|
|||
|
||||
import com.beemdevelopment.aegis.encoding.Base64Exception;
|
||||
import com.beemdevelopment.aegis.otp.OtpInfoException;
|
||||
import com.beemdevelopment.aegis.util.UUIDMap;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Database {
|
||||
private static final int VERSION = 1;
|
||||
private DatabaseEntryList _entries = new DatabaseEntryList();
|
||||
private UUIDMap<DatabaseEntry> _entries = new UUIDMap<>();
|
||||
|
||||
public JSONObject toJson() {
|
||||
try {
|
||||
|
@ -32,6 +30,7 @@ public class Database {
|
|||
|
||||
public static Database fromJson(JSONObject obj) throws DatabaseException {
|
||||
Database db = new Database();
|
||||
UUIDMap<DatabaseEntry> entries = db.getEntries();
|
||||
|
||||
try {
|
||||
int ver = obj.getInt("version");
|
||||
|
@ -42,7 +41,7 @@ public class Database {
|
|||
JSONArray array = obj.getJSONArray("entries");
|
||||
for (int i = 0; i < array.length(); i++) {
|
||||
DatabaseEntry entry = DatabaseEntry.fromJson(array.getJSONObject(i));
|
||||
db.addEntry(entry);
|
||||
entries.add(entry);
|
||||
}
|
||||
} catch (Base64Exception | OtpInfoException | JSONException e) {
|
||||
throw new DatabaseException(e);
|
||||
|
@ -51,27 +50,7 @@ public class Database {
|
|||
return db;
|
||||
}
|
||||
|
||||
public void addEntry(DatabaseEntry entry) {
|
||||
_entries.add(entry);
|
||||
}
|
||||
|
||||
public void removeEntry(DatabaseEntry entry) {
|
||||
_entries.remove(entry);
|
||||
}
|
||||
|
||||
public void replaceEntry(DatabaseEntry newEntry) {
|
||||
_entries.replace(newEntry);
|
||||
}
|
||||
|
||||
public void swapEntries(DatabaseEntry entry1, DatabaseEntry entry2) {
|
||||
_entries.swap(entry1, entry2);
|
||||
}
|
||||
|
||||
public List<DatabaseEntry> getEntries() {
|
||||
return _entries.getList();
|
||||
}
|
||||
|
||||
public DatabaseEntry getEntryByUUID(UUID uuid) {
|
||||
return _entries.getByUUID(uuid);
|
||||
public UUIDMap<DatabaseEntry> getEntries() {
|
||||
return _entries;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue