From 88f9beafd349ded60a1bc8ea497fd19cafdf08e4 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Mon, 28 Jul 2025 16:31:08 -0400 Subject: [PATCH] Faster expiration calculation and tag array serialization --- .../nip01Core/jackson/EventSerializer.kt | 9 +++- .../quartz/nip01Core/jackson/JsonMapper.kt | 3 +- .../nip01Core/jackson/TagArraySerializer.kt | 43 +++++++++++++++++++ .../quartz/nip40Expiration/ExpirationTag.kt | 3 +- 4 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/jackson/TagArraySerializer.kt diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/jackson/EventSerializer.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/jackson/EventSerializer.kt index 286a40a0d..0e665defd 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/jackson/EventSerializer.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/jackson/EventSerializer.kt @@ -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::class.java) { override fun serialize( @@ -37,7 +38,13 @@ class EventSerializer : StdSerializer(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) diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/jackson/JsonMapper.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/jackson/JsonMapper.kt index 976a004b1..ea75d82bf 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/jackson/JsonMapper.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/jackson/JsonMapper.kt @@ -71,7 +71,8 @@ class JsonMapper { .addSerializer(Permission::class.java, PermissionSerializer()) .addDeserializer(IntentResult::class.java, IntentResultJsonDeserializer()) .addSerializer(IntentResult::class.java, IntentResultJsonSerializer()) - .addDeserializer(Array>::class.java, TagArrayDeserializer()), + .addDeserializer(Array>::class.java, TagArrayDeserializer()) + .addSerializer(Array>::class.java, TagArraySerializer()), ) fun fromJson(json: String): Event = mapper.readValue(json, Event::class.java) diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/jackson/TagArraySerializer.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/jackson/TagArraySerializer.kt new file mode 100644 index 000000000..6152a1c5e --- /dev/null +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/jackson/TagArraySerializer.kt @@ -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>::class.java) { + override fun serialize( + tags: Array>, + 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() + } +} diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip40Expiration/ExpirationTag.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip40Expiration/ExpirationTag.kt index 7e178f451..92c029f11 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/nip40Expiration/ExpirationTag.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip40Expiration/ExpirationTag.kt @@ -33,8 +33,7 @@ class ExpirationTag { @JvmStatic fun parse(tag: Array): 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() }