io.provenance.eventstream.stream.Checkpoint.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of es-core Show documentation
Show all versions of es-core Show documentation
A collection of libraries to connect and stream blocks from a node
The newest version!
package io.provenance.eventstream.stream
import java.io.File
import java.util.concurrent.atomic.AtomicLong
abstract class Checkpoint(open val checkEvery: Long = 20) {
abstract fun checkpoint(at: Long)
abstract fun lastCheckpoint(): Long?
}
class InMemoryCheckpoint(override val checkEvery: Long = 20) : Checkpoint(checkEvery) {
private val currentBlock = AtomicLong(0)
override fun checkpoint(at: Long) = currentBlock.set(at)
override fun lastCheckpoint(): Long? = currentBlock.get().let {
if (it == 0L) { null } else { it }
}
}
class FileCheckpoint(override val checkEvery: Long = 20) : Checkpoint(checkEvery) {
private val filename = "./checkpoint.txt"
override fun checkpoint(at: Long) {
File(filename).writeText(at.toString())
}
override fun lastCheckpoint(): Long? = with(File(filename)) {
if (exists()) {
readLines().first().toLong()
} else {
null
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy