Skip to content

Instantly share code, notes, and snippets.

@almibe
Created November 5, 2020 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almibe/0b4b8604b95b68e6e0f587519d398fc1 to your computer and use it in GitHub Desktop.
Save almibe/0b4b8604b95b68e6e0f587519d398fc1 to your computer and use it in GitHub Desktop.
Sample of using Xodus in memory
import jetbrains.exodus.env.Environment
import jetbrains.exodus.env.EnvironmentConfig
import java.nio.file.Path
import jetbrains.exodus.io.inMemory.MemoryDataWriter
import jetbrains.exodus.io.inMemory.MemoryDataReader
import jetbrains.exodus.io.inMemory.Memory
import jetbrains.exodus.log.LogConfig
import jetbrains.exodus.env.Environments
import org.libraryweasel.ligature.LigatureStore
import org.libraryweasel.ligature.ReadTx
import org.libraryweasel.ligature.WriteTx
import java.util.concurrent.locks.ReentrantLock
sealed class StorageType
data class DirectoryStorage(val path: Path): StorageType()
object InMemoryStorage: StorageType()
class XodusLigatureStore private constructor(private val environment: Environment): LigatureStore {
private val lock = ReentrantLock()
companion object {
fun open(storageType: StorageType): LigatureStore {
return when (storageType) {
is DirectoryStorage -> {
XodusLigatureStore(Environments.newInstance(storageType.path.toFile()))
}
is InMemoryStorage -> {
XodusLigatureStore(createInMemoryEnvironment())
}
}
}
private fun createInMemoryEnvironment(): Environment {
val memory = Memory()
return Environments.newInstance(
LogConfig.create(MemoryDataReader(memory), MemoryDataWriter(memory)),
EnvironmentConfig().setGcUtilizationFromScratch(true)
)
}
}
override suspend fun close() {
environment.close()
}
override suspend fun isOpen(): Boolean = environment.isOpen
override suspend fun readTx(): ReadTx = XodusLigatureReadTx(environment)
override suspend fun writeTx(): WriteTx = XodusLigatureWriteTx(environment)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment