Created
November 5, 2020 05:11
-
-
Save almibe/0b4b8604b95b68e6e0f587519d398fc1 to your computer and use it in GitHub Desktop.
Sample of using Xodus in memory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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