Skip to content
Snippets Groups Projects
Commit 5c07a8c5 authored by Koen's avatar Koen
Browse files

Added ability to query by content type. Added unixmilliseconds support.

parent 04e612d3
No related branches found
No related tags found
No related merge requests found
......@@ -63,14 +63,17 @@ class MemoryStore : Store() {
return SignedEvent.fromProto(e)
}
override fun enumerateSignedEvents(system: PublicKey, handler: (SignedEvent) -> Unit) {
override fun enumerateSignedEvents(system: PublicKey, contentType: ContentType?, handler: (SignedEvent) -> Unit) {
for (pair in _signedEvents) {
if (pair.key.system != system) {
continue;
}
val e = pair.value.event ?: continue
handler(SignedEvent.fromProto(e))
val se = SignedEvent.fromProto(e)
if (contentType == null || se.event.contentType == contentType.value) {
handler(se)
}
}
}
......
......@@ -171,10 +171,7 @@ data class Opinion(val data: ByteArray) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as Opinion
if (other !is Opinion) return false
if (!data.contentEquals(other.data)) return false
return true
......@@ -338,7 +335,7 @@ class Event(
val unixMilliseconds: Long?
) {
fun toProto(): Protocol.Event {
return Protocol.Event.newBuilder()
val builder = Protocol.Event.newBuilder()
.setSystem(system.toProto())
.setProcess(process.toProto())
.setLogicalClock(logicalClock)
......@@ -355,8 +352,13 @@ class Event(
if (this@Event.lwwElement != null) {
lwwElement = this@Event.lwwElement.toProto()
}
}
.build()
};
if (unixMilliseconds != null) {
builder.unixMilliseconds = unixMilliseconds;
}
return builder.build();
}
companion object {
......
......@@ -16,13 +16,18 @@ class SqlLiteDbHelper : SQLiteOpenHelper {
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
Log.w(TAG, "Upgrade from version $oldVersion to $newVersion")
Log.w(TAG, "This is version 1, no DB to update")
if (oldVersion == 1 && newVersion == 2) {
deleteTables(db);
createTables(db);
}
}
private fun createTables(db: SQLiteDatabase) {
db.execSQL("CREATE TABLE system_states (public_key VARCHAR PRIMARY KEY, value BLOB)")
db.execSQL("CREATE TABLE process_states (public_key_process VARCHAR PRIMARY KEY, value BLOB)")
db.execSQL("CREATE TABLE process_secrets (public_key VARCHAR PRIMARY KEY, value BLOB)")
db.execSQL("CREATE TABLE signed_events (id INTEGER PRIMARY KEY, public_key VARCHAR, process VARCHAR, logical_clock INTEGER, value BLOB)")
db.execSQL("CREATE TABLE signed_events (id INTEGER PRIMARY KEY, public_key VARCHAR, process VARCHAR, logical_clock INTEGER, content_type INTEGER, value BLOB)")
db.execSQL("CREATE INDEX idx_event_pointer ON signed_events (public_key, process, logical_clock)")
}
......@@ -45,6 +50,6 @@ class SqlLiteDbHelper : SQLiteOpenHelper {
companion object {
private const val TAG = "SqlLiteDbHelper"
private const val DATABASE_NAME = "polycentric_core.db"
private const val DATABASE_VERSION = 1
private const val DATABASE_VERSION = 2
}
}
\ No newline at end of file
......@@ -57,7 +57,7 @@ class SqlLiteStore(private val _db: SqlLiteDbHelper) : Store() {
.setMutationPointer(mutationPointer.toProto())
.build()
_db.writableDatabase.execSQL("REPLACE INTO signed_events(public_key, process, logical_clock, value) VALUES(?, ?, ?, ?)",
_db.writableDatabase.execSQL("REPLACE INTO signed_events(public_key, process, logical_clock, content_type, value) VALUES(?, ?, ?, 0, ?)",
arrayOf(system.key.toBase64(), process.toBase64(), logicalClock.toString(), storageTypeEvent.toByteArray()))
}
......@@ -84,9 +84,14 @@ class SqlLiteStore(private val _db: SqlLiteDbHelper) : Store() {
return null
}
override fun enumerateSignedEvents(system: PublicKey, handler: (SignedEvent) -> Unit) {
val cursor = _db.readableDatabase.rawQuery("SELECT value FROM signed_events WHERE public_key = ?",
arrayOf(system.key.toBase64()));
override fun enumerateSignedEvents(system: PublicKey, contentType: ContentType?, handler: (SignedEvent) -> Unit) {
val cursor = if (contentType == null) {
_db.readableDatabase.rawQuery("SELECT value FROM signed_events WHERE public_key = ?",
arrayOf(system.key.toBase64()))
} else {
_db.readableDatabase.rawQuery("SELECT value FROM signed_events WHERE public_key = ? AND content_type = ?",
arrayOf(system.key.toBase64(), contentType.value.toString()))
}
cursor.use {
if (!it.moveToFirst()) {
......@@ -110,8 +115,8 @@ class SqlLiteStore(private val _db: SqlLiteDbHelper) : Store() {
.setEvent(event.toProto())
.build()
_db.writableDatabase.execSQL("REPLACE INTO signed_events(public_key, process, logical_clock, value) VALUES(?, ?, ?, ?)",
arrayOf(event.event.system.key.toBase64(), event.event.process.process.toBase64(), event.event.logicalClock.toString(), storageTypeEvent.toByteArray()))
_db.writableDatabase.execSQL("REPLACE INTO signed_events(public_key, process, logical_clock, content_type, value) VALUES(?, ?, ?, ?, ?)",
arrayOf(event.event.system.key.toBase64(), event.event.process.process.toBase64(), event.event.logicalClock.toString(), event.event.contentType.toString(), storageTypeEvent.toByteArray()))
}
override fun getProcessSecret(system: PublicKey): ProcessSecret? {
......
......@@ -14,7 +14,7 @@ abstract class Store {
abstract fun removeProcessSecret(system: PublicKey)
abstract fun addProcessSecret(processSecret: ProcessSecret)
abstract fun getSignedEvent(system: PublicKey, process: Process, logicalClock: Long): SignedEvent?
abstract fun enumerateSignedEvents(system: PublicKey, handler: (SignedEvent) -> Unit)
abstract fun enumerateSignedEvents(system: PublicKey, contentType: ContentType? = null, handler: (SignedEvent) -> Unit)
open fun cleanup() {}
fun getSignedEvent(pointer: Pointer): SignedEvent? {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment