Faster expiration calculation and tag array serialization

This commit is contained in:
Vitor Pamplona
2025-07-28 16:31:08 -04:00
parent 0116bc30e4
commit 88f9beafd3
4 changed files with 54 additions and 4 deletions

View File

@@ -24,6 +24,7 @@ import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.ser.std.StdSerializer
import com.vitorpamplona.quartz.nip01Core.core.Event
import kotlin.collections.indices
class EventSerializer : StdSerializer<Event>(Event::class.java) {
override fun serialize(
@@ -37,7 +38,13 @@ class EventSerializer : StdSerializer<Event>(Event::class.java) {
gen.writeNumberField("created_at", event.createdAt)
gen.writeNumberField("kind", event.kind)
gen.writeArrayFieldStart("tags")
event.tags.forEach { tag -> gen.writeArray(tag, 0, tag.size) }
for (i in event.tags.indices) {
gen.writeStartArray()
for (j in event.tags[i].indices) {
gen.writeString(event.tags[i][j])
}
gen.writeEndArray()
}
gen.writeEndArray()
gen.writeStringField("content", event.content)
gen.writeStringField("sig", event.sig)

View File

@@ -71,7 +71,8 @@ class JsonMapper {
.addSerializer(Permission::class.java, PermissionSerializer())
.addDeserializer(IntentResult::class.java, IntentResultJsonDeserializer())
.addSerializer(IntentResult::class.java, IntentResultJsonSerializer())
.addDeserializer(Array<Array<String>>::class.java, TagArrayDeserializer()),
.addDeserializer(Array<Array<String>>::class.java, TagArrayDeserializer())
.addSerializer(Array<Array<String>>::class.java, TagArraySerializer()),
)
fun fromJson(json: String): Event = mapper.readValue(json, Event::class.java)

View File

@@ -0,0 +1,43 @@
/**
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.quartz.nip01Core.jackson
import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.ser.std.StdSerializer
class TagArraySerializer : StdSerializer<Array<Array<String>>>(Array<Array<String>>::class.java) {
override fun serialize(
tags: Array<Array<String>>,
gen: JsonGenerator,
provider: SerializerProvider,
) {
gen.writeStartArray()
for (i in tags.indices) {
gen.writeStartArray()
for (j in tags[i].indices) {
gen.writeString(tags[i][j])
}
gen.writeEndArray()
}
gen.writeEndArray()
}
}

View File

@@ -33,8 +33,7 @@ class ExpirationTag {
@JvmStatic
fun parse(tag: Array<String>): Long? {
ensure(tag.has(1)) { return null }
ensure(tag[0] == TAG_NAME) { return null }
ensure(tag.has(1) && tag[0] == TAG_NAME) { return null }
ensure(tag[1].isNotEmpty()) { return null }
return tag[1].toLongOrNull()
}