Skip to content

Commit

Permalink
Db location to system home
Browse files Browse the repository at this point in the history
  • Loading branch information
amankgo committed Jan 11, 2022
1 parent eeac013 commit 904b35f
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 32 deletions.
14 changes: 7 additions & 7 deletions detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,20 @@ complexity:
active: true
excludes:
[
"**/test/**",
"**/test/**",
"**/androidTest/**",
"**/commonTest/**",
"**/jvmTest/**",
"**/jsTest/**",
"**/iosTest/**",
"**/commonTest/**",
"**/jvmTest/**",
"**/jsTest/**",
"**/iosTest/**",
]
thresholdInFiles: 11
thresholdInClasses: 11
thresholdInInterfaces: 11
thresholdInObjects: 11
thresholdInEnums: 11
ignoreDeprecated: false
ignorePrivate: false
ignoreDeprecated: true
ignorePrivate: true
ignoreOverridden: false

coroutines:
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/app/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ package app
import androidx.compose.ui.window.application

fun main() = application(false) {
val environment = System.getenv("SKIKO_RENDER_API")
val property = System.getProperty("skiko.renderApi")
println("env: $environment render: $property")
appWindow()
}
44 changes: 26 additions & 18 deletions src/main/kotlin/storage/Db.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,33 @@ import org.mapdb.DBMaker
import org.mapdb.HTreeMap
import org.mapdb.Serializer
import storage.serializer.ObjectSerializer
import java.io.File

object Db {

private const val PREFIX = "Session-"

private val db by lazy {
DBMaker.fileDB("sessions.db").fileMmapEnableIfSupported().checksumHeaderBypass().make()
private val diskDb by lazy {
val homeDir = System.getProperty("user.home")
val slash = File.pathSeparator
val dbName = "sessions.db"
val dbFile = File("$homeDir${slash}logvue${slash}$dbName")
if (!(dbFile.canRead() && dbFile.canWrite())) {
throw SecurityException("Can not read at path ${dbFile.canonicalPath}")
}
DBMaker.fileDB(dbFile).fileMmapEnableIfSupported().checksumHeaderBypass().make()
}

private var session: HTreeMap<String, LogItem>? = null
private var sessionId: String? = null
private val LOCK = Any()

val configs by lazy {
db.hashMap("configs", Serializer.STRING, Serializer.STRING).createOrOpen()
diskDb.hashMap("configs", Serializer.STRING, Serializer.STRING).createOrOpen()
}

private val sessionInfoMap by lazy {
db.hashMap("sessionInfo", Serializer.STRING, ObjectSerializer<SessionInfo>()).createOrOpen()
diskDb.hashMap("sessionInfo", Serializer.STRING, ObjectSerializer<SessionInfo>()).createOrOpen()
}

init {
Expand All @@ -37,14 +45,14 @@ object Db {
return sessionInfoMap[sessionId]
}

fun getAllSessions() = db.getAllNames().filter { it.startsWith(PREFIX) }.sortedBy { getSessionNumber(it) }
fun getAllSessions() = diskDb.getAllNames().filter { it.startsWith(PREFIX) }.sortedBy { getSessionNumber(it) }

fun getLastSessionNumber(): Int {
private fun getLastSessionNumber(): Int {
val lastSessionId = getAllSessions().lastOrNull() ?: sessionIdFromNumber(0)
return getSessionNumber(lastSessionId)
}

fun getPreviousSessionNumber(): Int {
private fun getPreviousSessionNumber(): Int {
val lastDbSessionId = configs["lastSessionId"]
val lastSessionId = if (lastDbSessionId.isNullOrBlank() || !lastDbSessionId.startsWith(PREFIX)) {
getAllSessions().lastOrNull() ?: sessionIdFromNumber(0)
Expand All @@ -54,11 +62,11 @@ object Db {
return getSessionNumber(lastSessionId)
}

fun getSessionNumber(sessionId: String) = sessionId.split("-").lastOrNull()?.toIntOrNull() ?: 0
private fun getSessionNumber(sessionId: String) = sessionId.split("-").lastOrNull()?.toIntOrNull() ?: 0

fun areNoSessionsCreated() = getAllSessions().isEmpty()
private fun areNoSessionsCreated() = getAllSessions().isEmpty()

fun isThisTheOnlySession(sessionId: String): Boolean {
private fun isThisTheOnlySession(sessionId: String): Boolean {
val sessions = getAllSessions()
if (sessions.size != 1) return false
return sessions.first() == sessionId
Expand All @@ -78,12 +86,12 @@ object Db {
} else if (sessionId == sessionId()) {
changeSession(null)
}
val oldSession = db.hashMap(sessionId, Serializer.STRING, ObjectSerializer<LogItem>())
val oldSession = diskDb.hashMap(sessionId, Serializer.STRING, ObjectSerializer<LogItem>())
.open()
oldSession.clear()
sessionInfoMap.remove(sessionId)
val recIds = arrayListOf<Long>()
db.nameCatalogParamsFor(sessionId).forEach { (t, u) ->
diskDb.nameCatalogParamsFor(sessionId).forEach { (t, u) ->
if (t.endsWith("rootRecids")) {
u.split(",").forEach { value ->
val recId = value.trim().toLongOrNull()
Expand All @@ -93,15 +101,15 @@ object Db {
}
}
recIds.forEach {
db.getStore().delete(it, Serializer.STRING)
diskDb.getStore().delete(it, Serializer.STRING)
}
val newCatalog = db.nameCatalogLoad()
val newCatalog = diskDb.nameCatalogLoad()
val keys = newCatalog.keys.filter { it.startsWith(sessionId) }
keys.forEach {
newCatalog.remove(it)
}
db.nameCatalogSave(newCatalog)
db.commit()
diskDb.nameCatalogSave(newCatalog)
diskDb.commit()
}

fun changeSession(sessionId: String?) {
Expand All @@ -123,7 +131,7 @@ object Db {
}
if (sessionNumber < 1) throw Exception("Session number must be greater than 1")
val sessionId = sessionIdFromNumber(sessionNumber)
val session = db
val session = diskDb
.hashMap(sessionId, Serializer.STRING, ObjectSerializer<LogItem>())
.createOrOpen()
this.sessionId = sessionId
Expand All @@ -143,6 +151,6 @@ object Db {
}

fun close() {
db.close()
diskDb.close()
}
}
8 changes: 1 addition & 7 deletions src/main/kotlin/utils/Helpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,10 @@ object Helpers {
return takeValue
}

fun isWindows(): Boolean {
val os = System.getProperty("os.name").lowercase(Locale.getDefault())
// windows
return os.indexOf("win") >= 0
}

fun openFileExplorer(path: Path) {
try {
val pathString = path.absolutePathString()
val command = if (isWindows()) {
val command = if (SystemTools.getOS() == OsWindows) {
"Explorer.exe $pathString"
} else {
"open $pathString"
Expand Down
48 changes: 48 additions & 0 deletions src/main/kotlin/utils/Renderer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package utils

import org.jetbrains.skiko.GraphicsApi
import org.jetbrains.skiko.OS
import org.jetbrains.skiko.hostOs

object Renderer {

// private const val RENDER_API_KEY = "skiko.renderApi"

// fun setRender() {
// when(SystemTools.getOS()) {
// OsWindows, OsLinux -> {
// System.setProperty(RENDER_API_KEY, "OPENGL")
// }
// OsMac -> {
//
// }
// }
// }

internal fun parseRenderApi(text: String?): GraphicsApi {
return when (text) {
"SOFTWARE_COMPAT" -> GraphicsApi.SOFTWARE_COMPAT
"SOFTWARE_FAST", "DIRECT_SOFTWARE", "SOFTWARE" -> GraphicsApi.SOFTWARE_FAST
"OPENGL" -> GraphicsApi.OPENGL
"DIRECT3D" -> {
if (hostOs == OS.Windows) GraphicsApi.DIRECT3D
else throw UnsupportedOperationException("$hostOs does not support DirectX rendering API.")
}
"METAL" -> {
if (hostOs == OS.MacOS) GraphicsApi.METAL
else throw UnsupportedOperationException("$hostOs does not support Metal rendering API.")
}
else -> bestRenderApiForCurrentOS()
}
}

private fun bestRenderApiForCurrentOS(): GraphicsApi {
return when (hostOs) {
OS.MacOS -> GraphicsApi.METAL
OS.Linux -> GraphicsApi.OPENGL
OS.Windows -> GraphicsApi.DIRECT3D
OS.JS, OS.Ios -> TODO("commonize me")
}
}

}
22 changes: 22 additions & 0 deletions src/main/kotlin/utils/SystemTools.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package utils

import java.util.*

object SystemTools {

fun getOS(): OS {
val os = System.getProperty("os.name").lowercase(Locale.getDefault())
return when {
os.contains("window") -> OsWindows
os.contains("nix") || os.contains("nux") || os.contains("aix") -> OsLinux
os.contains("os x") || os.contains("mac") -> OsMac
else -> throw UnsupportedOperationException("Operating system $os is not supported")
}
}

}

sealed interface OS
object OsWindows : OS
object OsLinux : OS
object OsMac : OS

0 comments on commit 904b35f

Please sign in to comment.