Skip to content

Commit

Permalink
Working data persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
xpdota committed Aug 28, 2024
1 parent aabc5dd commit e832f1b
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 112 deletions.
12 changes: 2 additions & 10 deletions src/main/groovy/gg/xp/xivgear/dataapi/Main.groovy
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package gg.xp.xivgear.dataapi

import io.micronaut.context.annotation.Bean
import io.micronaut.context.annotation.Context
import io.micronaut.jackson.JacksonConfiguration

import io.micronaut.runtime.Micronaut
import io.swagger.v3.oas.annotations.OpenAPIDefinition
import io.swagger.v3.oas.annotations.info.Contact
import io.swagger.v3.oas.annotations.info.Info
import io.swagger.v3.oas.annotations.info.License
import jakarta.inject.Singleton

@OpenAPIDefinition(
info = @Info(
Expand All @@ -20,14 +17,9 @@ import jakarta.inject.Singleton
)
)
class Main {

static void main(String[] args) {
Micronaut.run(Main, args)
}

@Bean
@Singleton
@Context
JacksonConfiguration jacksonConfiguration() {
return new JacksonConfiguration();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import gg.xp.xivgear.dataapi.persistence.DataPersistence
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import io.micronaut.context.annotation.Context
import io.micronaut.json.JsonConfiguration
import jakarta.inject.Singleton

import java.time.Duration
Expand Down Expand Up @@ -45,19 +46,21 @@ class DataManager implements AutoCloseable {
private static final int maxIlvlFood = 999


DataManager(DataPersistence pers) {
DataManager(DataPersistence pers, JsonConfiguration js) {
this.pers = pers
client = new XivApiClient()
xivApiUpdater = Thread.startVirtualThread this.&xivApiUpdateLoop
persistenceUpdater = Thread.startVirtualThread this.&persistenceUpdateLoop
}

private void offerNewData(FullData possibleNewData) {
private void offerNewData(FullData possibleNewData, boolean tryPersist) {
if (dataFuture.state() == Future.State.SUCCESS) {
FullData existing = dataFuture.get()
if (existing.timestamp.isBefore(possibleNewData.timestamp)) {
dataFuture = CompletableFuture.completedFuture possibleNewData
persistData possibleNewData
if (tryPersist) {
persistData possibleNewData
}
log.info "New data timestamp: ${possibleNewData.timestamp}"
}
else {
Expand All @@ -67,7 +70,9 @@ class DataManager implements AutoCloseable {
else if (dataFuture.state() == Future.State.RUNNING) {
log.info "Initial data at ${possibleNewData.timestamp}"
dataFuture.complete possibleNewData
persistData possibleNewData
if (tryPersist) {
persistData possibleNewData
}
}
else {
log.error "Invalid state ${dataFuture.state()}"
Expand Down Expand Up @@ -113,14 +118,14 @@ class DataManager implements AutoCloseable {
else {
log.info "Update triggered (both changed), going to reload data"
}
offerNewData makeData()
offerNewData makeData(), true
log.info "Reloaded data"
}
}
else {
// If no existing data, then unconditionally update
log.info "No existing data"
offerNewData makeData()
offerNewData makeData(), true
}
}
catch (Throwable t) {
Expand All @@ -144,7 +149,7 @@ class DataManager implements AutoCloseable {
}
else {
log.info "Persistence: Has Data"
offerNewData fd
offerNewData fd, false
}
}
catch (Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package gg.xp.xivgear.dataapi.endpoints

import gg.xp.xivgear.dataapi.datamanager.DataManager
import gg.xp.xivgear.dataapi.datamanager.FullData
import groovy.transform.CompileStatic
import groovy.transform.TupleConstructor
import io.micronaut.core.annotation.NonNull
import io.micronaut.http.HttpHeaders
import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpResponse
import io.micronaut.jackson.codec.JsonMediaTypeCodec
import jakarta.inject.Inject

import java.time.ZoneOffset
import java.time.ZonedDateTime
Expand All @@ -28,7 +31,9 @@ import java.time.temporal.ChronoUnit
* @param <Out> The response object type.
*/
@TupleConstructor(includeFields = true, defaults = false)
@CompileStatic
abstract class BaseDataEndpoint<In, Out> {

protected final DataManager dm

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import io.micronaut.objectstorage.ObjectStorageOperations
import io.micronaut.objectstorage.request.UploadRequest
import jakarta.inject.Singleton

import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream

@Slf4j
@Singleton
class DataPersistence {
Expand All @@ -25,14 +28,17 @@ class DataPersistence {
if (raw.present) {
log.info "Stored data present"
ObjectStorageEntry<?> entry = raw.get()
ObjectInputStream stream = new ObjectInputStream(entry.inputStream)
def object = stream.readObject()
if (object instanceof FullData) {
log.info "Stored data successfully deserialized"
return object
}
else {
log.info "Deserialization failed! Wrong type: ${object}"
try (
GZIPInputStream gzipIn = new GZIPInputStream(entry.inputStream)
ObjectInputStream stream = new ObjectInputStream(gzipIn)) {
def object = stream.readObject()
if (object instanceof FullData) {
log.info "Stored data successfully deserialized"
return object
}
else {
log.info "Deserialization failed! Wrong type: ${object}"
}
}
}
else {
Expand All @@ -48,8 +54,11 @@ class DataPersistence {
else {
byte[] bytes
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()
ObjectOutputStream oos = new ObjectOutputStream(baos)) {
GZIPOutputStream gzipOut = new GZIPOutputStream(baos)
ObjectOutputStream oos = new ObjectOutputStream(gzipOut)) {
oos.writeObject(data)
oos.flush()
gzipOut.finish()
bytes = baos.toByteArray()
}
storage.upload UploadRequest.fromBytes(bytes, key)
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gg.xp.xivgear.dataapi.serialization

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module
import io.micronaut.context.annotation.Context
import io.micronaut.context.annotation.Factory
import io.micronaut.context.annotation.Primary
import io.micronaut.context.annotation.Replaces
import io.micronaut.jackson.databind.JacksonDatabindMapper
import io.micronaut.json.JsonMapper
import jakarta.inject.Inject
import jakarta.inject.Singleton

@Factory
@Context
class JacksonExtras {

@Inject
void customize(ObjectMapper mapper) {
// mapper.setPropertyNamingStrategy(new CapitalizedPropertyNamingStrategy())
mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS)
mapper.registerModule(new Jdk8Module())
}

@Singleton
@Primary
@Replaces(JsonMapper)
JsonMapper jsonMapper(JacksonDatabindMapper mapper) {
return mapper
}
}

This file was deleted.

0 comments on commit e832f1b

Please sign in to comment.