2016-08-22 19:31:03 +02:00
|
|
|
package me.impy.aegis.db;
|
|
|
|
|
2016-11-13 18:21:00 +01:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
2016-08-22 19:31:03 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2017-12-04 21:21:31 +01:00
|
|
|
import java.util.Collections;
|
2016-08-22 19:31:03 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class Database {
|
2017-08-26 21:15:53 +02:00
|
|
|
private static final int VERSION = 1;
|
2017-12-27 21:01:53 +01:00
|
|
|
|
2017-08-26 21:15:53 +02:00
|
|
|
private List<DatabaseEntry> _entries = new ArrayList<>();
|
2017-12-27 21:01:53 +01:00
|
|
|
private long _counter = 0;
|
2016-11-13 18:21:00 +01:00
|
|
|
|
2017-08-26 21:15:53 +02:00
|
|
|
public byte[] serialize() throws Exception {
|
2017-12-27 23:23:05 +01:00
|
|
|
return serialize(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] serialize(boolean pretty) throws Exception {
|
2016-11-13 18:21:00 +01:00
|
|
|
JSONArray array = new JSONArray();
|
2017-08-26 21:15:53 +02:00
|
|
|
for (DatabaseEntry e : _entries) {
|
2016-11-13 18:21:00 +01:00
|
|
|
array.put(e.serialize());
|
|
|
|
}
|
2016-08-22 19:31:03 +02:00
|
|
|
|
2016-11-13 18:21:00 +01:00
|
|
|
JSONObject obj = new JSONObject();
|
2017-08-26 21:15:53 +02:00
|
|
|
obj.put("version", VERSION);
|
2016-11-13 18:21:00 +01:00
|
|
|
obj.put("entries", array);
|
|
|
|
|
2017-12-27 23:23:05 +01:00
|
|
|
String string = pretty ? obj.toString(4) : obj.toString();
|
|
|
|
return string.getBytes("UTF-8");
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 18:21:00 +01:00
|
|
|
public void deserialize(byte[] data) throws Exception {
|
2017-12-27 23:01:23 +01:00
|
|
|
deserialize(data, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deserialize(byte[] data, boolean incCount) throws Exception {
|
2016-11-13 18:21:00 +01:00
|
|
|
JSONObject obj = new JSONObject(new String(data, "UTF-8"));
|
2016-08-22 19:31:03 +02:00
|
|
|
|
2017-08-26 21:15:53 +02:00
|
|
|
// TODO: support different VERSION deserialization providers
|
2016-11-13 18:21:00 +01:00
|
|
|
int ver = obj.getInt("version");
|
2017-08-26 21:15:53 +02:00
|
|
|
if (ver != VERSION) {
|
2016-11-13 18:21:00 +01:00
|
|
|
throw new Exception("Unsupported version");
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 18:21:00 +01:00
|
|
|
JSONArray array = obj.getJSONArray("entries");
|
|
|
|
for (int i = 0; i < array.length(); i++) {
|
2017-12-27 21:08:24 +01:00
|
|
|
DatabaseEntry entry = new DatabaseEntry(null);
|
2017-12-27 22:34:53 +01:00
|
|
|
entry.deserialize(array.getJSONObject(i));
|
2017-12-27 23:01:23 +01:00
|
|
|
|
|
|
|
// if incCount is false, don't increment the counter and don't set an ID
|
|
|
|
// this is used by the database importer to prevent an exception down the line
|
|
|
|
// TODO: find a better solution for this ugly hack
|
|
|
|
if (incCount) {
|
|
|
|
addKey(entry);
|
|
|
|
} else {
|
|
|
|
_entries.add(entry);
|
|
|
|
}
|
2016-11-13 18:21:00 +01:00
|
|
|
}
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
2017-12-27 21:01:53 +01:00
|
|
|
public void addKey(DatabaseEntry entry) throws Exception {
|
|
|
|
entry.setID(++_counter);
|
2017-08-26 21:15:53 +02:00
|
|
|
_entries.add(entry);
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
2017-11-27 21:06:23 +01:00
|
|
|
public void removeKey(DatabaseEntry entry) {
|
2017-08-26 21:15:53 +02:00
|
|
|
_entries.remove(entry);
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
|
|
|
|
2017-12-27 22:04:22 +01:00
|
|
|
public void replaceKey(DatabaseEntry newEntry) {
|
|
|
|
for (DatabaseEntry oldEntry : _entries) {
|
|
|
|
if (oldEntry.getID() == newEntry.getID()) {
|
|
|
|
_entries.set(_entries.indexOf(oldEntry), newEntry);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new AssertionError("no entry found with the same id");
|
|
|
|
}
|
|
|
|
|
2017-12-12 01:50:00 +01:00
|
|
|
public void swapKeys(DatabaseEntry entry1, DatabaseEntry entry2) {
|
|
|
|
Collections.swap(_entries, _entries.indexOf(entry1), _entries.indexOf(entry2));
|
|
|
|
}
|
|
|
|
|
2017-11-27 21:06:23 +01:00
|
|
|
public List<DatabaseEntry> getKeys() {
|
2017-12-04 21:21:31 +01:00
|
|
|
return Collections.unmodifiableList(_entries);
|
2016-08-22 19:31:03 +02:00
|
|
|
}
|
2017-05-03 21:08:38 +02:00
|
|
|
}
|