-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kengo TODA <skypencil@gmail.com>
- Loading branch information
Showing
23 changed files
with
228 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Kosmo | ||
|
||
[![Continuous Integration](https://github.com/KengoTODA/kosmo/actions/workflows/ci.yaml/badge.svg)](https://github.com/KengoTODA/kosmo/actions/workflows/ci.yaml) | ||
|
||
Kosmo is a simple Relational Database implemented in Kotlin. | ||
This product is not developed for production use, just for personal learning. | ||
|
||
## Architecture | ||
|
||
```mermaid | ||
graph TD | ||
subgraph frontend | ||
main-process --> cpf[connection pool] | ||
subgraph worker | ||
worker-process --> | ||
optimizer --> | ||
rewriter --> | ||
planner --> worker-process | ||
end | ||
main-process --> worker-process | ||
worker-process --> al[(audit-log)] | ||
end | ||
subgraph backend | ||
coordinator-process --> cpb[connection pool] | ||
subgraph database | ||
database-process | ||
wal[(write ahead log)] | ||
subgraph storage-engine | ||
table --> df[(data file)] | ||
index --> df | ||
vacuumer --> df | ||
end | ||
database-process --> wal | ||
database-process --> table | ||
database-process --> index | ||
database-process --> vacuumer | ||
end | ||
end | ||
subgraph replica | ||
wal --> replica-process | ||
end | ||
client --> main-process | ||
worker-process --> coordinator-process --> database-process | ||
``` |
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
10 changes: 9 additions & 1 deletion
10
backend/src/main/kotlin/jp/skypencil/kosmo/backend/Coordinator.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
package jp.skypencil.kosmo.backend | ||
|
||
import jp.skypencil.kosmo.backend.storage.onmemory.OnMemoryDatabase | ||
import jp.skypencil.kosmo.backend.wal.LogWriter | ||
|
||
/** | ||
* フロントエンドからの要求を受け取り、それぞれのクラスを組み合わせて実現する。 | ||
*/ | ||
class Coordinator { | ||
} | ||
private val database = OnMemoryDatabase() | ||
private val logWriter = LogWriter() | ||
} |
17 changes: 17 additions & 0 deletions
17
backend/src/main/kotlin/jp/skypencil/kosmo/backend/storage/onmemory/OnMemoryDatabase.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package jp.skypencil.kosmo.backend.storage.onmemory | ||
|
||
import jp.skypencil.kosmo.backend.storage.shared.Table | ||
import jp.skypencil.kosmo.backend.storage.shared.Database | ||
|
||
class OnMemoryDatabase : Database { | ||
private val tables = mutableMapOf<String, OnMemoryTable>() | ||
override suspend fun findTable(name: String): Table = | ||
checkNotNull(tables[name]) | ||
|
||
override suspend fun createTable(name: String): Table { | ||
require(!tables.containsKey(name)) | ||
return OnMemoryTable(name).also { | ||
tables[name] = it | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
backend/src/main/kotlin/jp/skypencil/kosmo/backend/storage/onmemory/OnMemoryTable.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package jp.skypencil.kosmo.backend.storage.onmemory | ||
|
||
import jp.skypencil.kosmo.backend.storage.shared.Table | ||
import jp.skypencil.kosmo.backend.value.Row | ||
import jp.skypencil.kosmo.backend.value.RowId | ||
import jp.skypencil.kosmo.backend.value.TransactionId | ||
import kotlin.streams.asSequence | ||
|
||
// TODO トランザクション | ||
// TODO スレッドセーフティ | ||
class OnMemoryTable(private val name: String) : Table { | ||
private val map = mutableMapOf<RowId, Row>() | ||
|
||
override fun getName(): String = name | ||
|
||
override suspend fun find(transactionId: TransactionId, id: RowId): Row = checkNotNull(map[id]) | ||
|
||
override suspend fun tableScan(transactionId: TransactionId): Sequence<Row> = map.values.stream().asSequence() | ||
|
||
override suspend fun insert(transactionId: TransactionId, row: Row) { | ||
check(map[row.id] == null) | ||
map[row.id] = row | ||
} | ||
|
||
override suspend fun delete(transactionId: TransactionId, id: RowId): Boolean = map.remove(id) != null | ||
|
||
override suspend fun update(transactionId: TransactionId, row: Row) { | ||
checkNotNull(map[row.id]) | ||
map[row.id] = row | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
backend/src/main/kotlin/jp/skypencil/kosmo/backend/storage/shared/Database.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package jp.skypencil.kosmo.backend.storage.shared | ||
|
||
interface Database { | ||
suspend fun findTable(name: String): Table | ||
suspend fun createTable(name: String): Table | ||
} |
13 changes: 13 additions & 0 deletions
13
backend/src/main/kotlin/jp/skypencil/kosmo/backend/storage/shared/StorageEngine.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package jp.skypencil.kosmo.backend.storage.shared | ||
|
||
import jp.skypencil.kosmo.backend.value.Row | ||
import jp.skypencil.kosmo.backend.value.RowId | ||
|
||
/** | ||
* データや索引の管理を抽象化する。 | ||
*/ | ||
interface StorageEngine { | ||
suspend fun find(id: RowId): Row | ||
suspend fun tableScan(): Sequence<Row> | ||
suspend fun indexScan(): Sequence<Row> | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/src/main/kotlin/jp/skypencil/kosmo/backend/storage/shared/Table.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package jp.skypencil.kosmo.backend.storage.shared | ||
|
||
import jp.skypencil.kosmo.backend.value.Row | ||
import jp.skypencil.kosmo.backend.value.RowId | ||
import jp.skypencil.kosmo.backend.value.TransactionId | ||
|
||
interface Table { | ||
fun getName(): String | ||
|
||
suspend fun find(tx: TransactionId, id: RowId): Row | ||
suspend fun tableScan(tx: TransactionId): Sequence<Row> | ||
suspend fun insert(tx: TransactionId, row: Row) | ||
suspend fun delete(tx: TransactionId, id: RowId): Boolean | ||
suspend fun update(tx: TransactionId, row: Row) | ||
} |
7 changes: 7 additions & 0 deletions
7
backend/src/main/kotlin/jp/skypencil/kosmo/backend/value/LogEntry.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package jp.skypencil.kosmo.backend.value | ||
|
||
/** | ||
* データベースの変更を表す。 | ||
*/ | ||
interface LogEntry { | ||
} |
4 changes: 4 additions & 0 deletions
4
backend/src/main/kotlin/jp/skypencil/kosmo/backend/value/Row.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package jp.skypencil.kosmo.backend.value | ||
|
||
data class Row(val id: RowId) { | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/src/main/kotlin/jp/skypencil/kosmo/backend/value/RowId.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package jp.skypencil.kosmo.backend.value | ||
|
||
import java.util.UUID | ||
|
||
data class RowId(private val uuid: UUID): Comparable<RowId> { | ||
init { | ||
check(uuid.version() == 6) { | ||
"RowIdはTime-basedなUUIDである必要があります" | ||
} | ||
} | ||
|
||
override fun compareTo(other: RowId): Int = | ||
this.uuid.compareTo(other.uuid) | ||
|
||
} |
12 changes: 10 additions & 2 deletions
12
backend/src/main/kotlin/jp/skypencil/kosmo/backend/value/TransactionId.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
package jp.skypencil.kosmo.backend.value | ||
|
||
class TransactionId { | ||
} | ||
import com.github.f4b6a3.uuid.UuidCreator | ||
import java.util.UUID | ||
|
||
class TransactionId(private val uuid: UUID): Comparable<TransactionId> { | ||
companion object { | ||
fun create() = TransactionId(UuidCreator.getTimeOrdered()) | ||
} | ||
|
||
override fun compareTo(other: TransactionId): Int = uuid.compareTo(other.uuid) | ||
} |
12 changes: 12 additions & 0 deletions
12
backend/src/main/kotlin/jp/skypencil/kosmo/backend/wal/LogWriter.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package jp.skypencil.kosmo.backend.wal | ||
|
||
import jp.skypencil.kosmo.backend.value.LogEntry | ||
|
||
/** | ||
* Write Ahead Log(WAL)を記録する責務を負う。 | ||
*/ | ||
class LogWriter { | ||
suspend fun write(logEntry: LogEntry) { | ||
|
||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
/* | ||
* This file was generated by the Gradle 'init' task. | ||
* | ||
* This settings file is used to specify which projects to include in your build-logic build. | ||
*/ | ||
|
||
rootProject.name = "buildSrc" | ||
|
||
dependencyResolutionManagement { | ||
versionCatalogs { | ||
create("libs") { | ||
from(files("../gradle/libs.versions.toml")) | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
org.gradle.parallel=true | ||
org.gradle.caching=true |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[versions] | ||
kotest = "5.8.0" | ||
log4j = "2.20.0" | ||
slf4j = "2.0.9" | ||
|
||
[libraries] | ||
kotest-runner-juni5 = { module = "io.kotest:kotest-runner-junit5", version.ref = "kotest" } | ||
log4j-slf4j2-impl = { module = "org.apache.logging.log4j:log4j-slf4j2-impl", version.ref = "log4j" } | ||
slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" } | ||
uuid-creator = { module = "com.github.f4b6a3:uuid-creator", version = "5.3.3" } |
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
File renamed without changes.