summaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/dico/parcels2/storage/exposed/ExposedBacking.kt
blob: d9e30716ad143f37db19d40d83ee74f135a6014f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
@file:Suppress("NOTHING_TO_INLINE", "PARAMETER_NAME_CHANGED_ON_OVERRIDE", "LocalVariableName", "UNUSED_EXPRESSION")

package io.dico.parcels2.storage.exposed

import com.zaxxer.hikari.HikariDataSource
import io.dico.parcels2.*
import io.dico.parcels2.PlayerProfile.Star.name
import io.dico.parcels2.storage.*
import io.dico.parcels2.util.math.clampMax
import io.dico.parcels2.util.ext.synchronized
import kotlinx.coroutines.*
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.ArrayChannel
import kotlinx.coroutines.channels.LinkedListChannel
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.SendChannel
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SchemaUtils.create
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.vendors.DatabaseDialect
import org.joda.time.DateTime
import java.util.UUID
import javax.sql.DataSource

class ExposedDatabaseException(message: String? = null) : Exception(message)

class ExposedBacking(private val dataSourceFactory: () -> DataSource, val poolSize: Int) : Backing, CoroutineScope {
    override val name get() = "Exposed"
    override val coroutineContext = Job() + newFixedThreadPoolContext(poolSize, "Parcels StorageThread")
    private var dataSource: DataSource? = null
    private var database: Database? = null
    private var isShutdown: Boolean = false
    override val isConnected get() = database != null

    override fun launchJob(job: Backing.() -> Unit): Job = launch { transaction { job() } }
    override fun <T> launchFuture(future: Backing.() -> T): Deferred<T> = async { transaction { future() } }

    override fun <T> openChannel(future: Backing.(SendChannel<T>) -> Unit): ReceiveChannel<T> {
        val channel = LinkedListChannel<T>()
        launchJob { future(channel) }
        return channel
    }

    override fun <T> openChannelForWriting(action: Backing.(T) -> Unit): SendChannel<T> {
        val channel = ArrayChannel<T>(poolSize * 2)

        repeat(poolSize.clampMax(3)) {
            launch {
                try {
                    while (true) {
                        action(channel.receive())
                    }
                } catch (ex: Exception) {
                    // channel closed
                }
            }
        }

        return channel
    }

    private fun <T> transaction(statement: Transaction.() -> T) = transaction(database!!, statement)

    companion object {
        init {
            Database.registerDialect("mariadb") {
                Class.forName("org.jetbrains.exposed.sql.vendors.MysqlDialect").newInstance() as DatabaseDialect
            }
        }
    }

    override fun init() {
        synchronized {
            if (isShutdown || isConnected) throw IllegalStateException()
            val dataSource = dataSourceFactory()
            this.dataSource = dataSource
            val database = Database.connect(dataSource)
            this.database = database
            transaction(database) {
                create(WorldsT, ProfilesT, ParcelsT, ParcelOptionsT, PrivilegesLocalT, PrivilegesGlobalT)
            }
        }
    }

    override fun shutdown() {
        synchronized {
            if (isShutdown) throw IllegalStateException()
            isShutdown = true
            coroutineContext.cancel(CancellationException("ExposedBacking shutdown"))
            dataSource?.let {
                (it as? HikariDataSource)?.close()
            }
            database = null
        }
    }

    @Suppress("RedundantObjectTypeCheck")
    private fun PlayerProfile.toOwnerProfile(): PlayerProfile {
        if (this is PlayerProfile.Star) return PlayerProfile.Fake(name)
        return this
    }

    private fun PlayerProfile.Unresolved.toResolvedProfile(): PlayerProfile.Real {
        return resolve(getPlayerUuidForName(name) ?: throwException())
    }

    private fun PlayerProfile.toResolvedProfile(): PlayerProfile {
        if (this is PlayerProfile.Unresolved) return toResolvedProfile()
        return this
    }

    private fun PlayerProfile.toRealProfile(): PlayerProfile.Real = when (this) {
        is PlayerProfile.Real -> this
        is PlayerProfile.Fake -> throw IllegalArgumentException("Fake profiles are not accepted")
        is PlayerProfile.Unresolved -> toResolvedProfile()
        else -> throw InternalError("Case should not be reached")
    }


    override fun getWorldCreationTime(worldId: ParcelWorldId): DateTime? {
        return WorldsT.getWorldCreationTime(worldId)
    }

    override fun setWorldCreationTime(worldId: ParcelWorldId, time: DateTime) {
        WorldsT.setWorldCreationTime(worldId, time)
    }

    override fun getPlayerUuidForName(name: String): UUID? {
        return ProfilesT.slice(ProfilesT.uuid).select { ProfilesT.name.upperCase() eq name.toUpperCase() }
            .firstOrNull()?.let { it[ProfilesT.uuid]?.toUUID() }
    }

    override fun updatePlayerName(uuid: UUID, name: String) {
        val binaryUuid = uuid.toByteArray()
        ProfilesT.upsert(ProfilesT.uuid) {
            it[ProfilesT.uuid] = binaryUuid
            it[ProfilesT.name] = name
        }
    }

    override fun transmitParcelData(channel: SendChannel<DataPair>, parcels: Sequence<ParcelId>) {
        for (parcel in parcels) {
            val data = readParcelData(parcel)
            channel.offer(parcel to data)
        }
        channel.close()
    }

    override fun transmitAllParcelData(channel: SendChannel<DataPair>) {
        ParcelsT.selectAll().forEach { row ->
            val parcel = ParcelsT.getItem(row) ?: return@forEach
            val data = rowToParcelData(row)
            channel.offer(parcel to data)
        }
        channel.close()
    }

    override fun readParcelData(parcel: ParcelId): ParcelDataHolder? {
        val row = ParcelsT.getRow(parcel) ?: return null
        return rowToParcelData(row)
    }

    override fun getOwnedParcels(user: PlayerProfile): List<ParcelId> {
        val user_id = ProfilesT.getId(user.toOwnerProfile()) ?: return emptyList()
        return ParcelsT.select { ParcelsT.owner_id eq user_id }
            .orderBy(ParcelsT.claim_time, isAsc = true)
            .mapNotNull(ParcelsT::getItem)
            .toList()
    }

    override fun setParcelData(parcel: ParcelId, data: ParcelDataHolder?) {
        if (data == null) {
            transaction {
                ParcelsT.getId(parcel)?.let { id ->
                    ParcelsT.deleteIgnoreWhere { ParcelsT.id eq id }

                    // Below should cascade automatically
                    /*
                    PrivilegesLocalT.deleteIgnoreWhere { PrivilegesLocalT.parcel_id eq id }
                    ParcelOptionsT.deleteIgnoreWhere(limit = 1) { ParcelOptionsT.parcel_id eq id }
                    */
                }

            }
            return
        }

        transaction {
            val id = ParcelsT.getOrInitId(parcel)
            PrivilegesLocalT.deleteIgnoreWhere { PrivilegesLocalT.attach_id eq id }
        }

        setParcelOwner(parcel, data.owner)

        for ((profile, privilege) in data.privilegeMap) {
            PrivilegesLocalT.setPrivilege(parcel, profile, privilege)
        }

        data.privilegeOfStar.takeIf { it != Privilege.DEFAULT }?.let { privilege ->
            PrivilegesLocalT.setPrivilege(parcel, PlayerProfile.Star, privilege)
        }

        setParcelOptionsInteractConfig(parcel, data.interactableConfig)
    }

    override fun setParcelOwner(parcel: ParcelId, owner: PlayerProfile?) {
        val id = if (owner == null)
            ParcelsT.getId(parcel) ?: return
        else
            ParcelsT.getOrInitId(parcel)

        val owner_id = owner?.let { ProfilesT.getOrInitId(it.toOwnerProfile()) }
        val time = owner?.let { DateTime.now() }

        ParcelsT.update({ ParcelsT.id eq id }) {
            it[ParcelsT.owner_id] = owner_id
            it[claim_time] = time
            it[sign_oudated] = false
        }
    }

    override fun setParcelOwnerSignOutdated(parcel: ParcelId, outdated: Boolean) {
        val id = ParcelsT.getId(parcel) ?: return
        ParcelsT.update({ ParcelsT.id eq id }) {
            it[sign_oudated] = outdated
        }
    }

    override fun setLocalPrivilege(parcel: ParcelId, player: PlayerProfile, privilege: Privilege) {
        PrivilegesLocalT.setPrivilege(parcel, player.toRealProfile(), privilege)
    }

    override fun setParcelOptionsInteractConfig(parcel: ParcelId, config: InteractableConfiguration) {
        val bitmaskArray = (config as? BitmaskInteractableConfiguration ?: return).bitmaskArray
        val isAllZero = !bitmaskArray.fold(false) { cur, elem -> cur || elem != 0 }

        if (isAllZero) {
            val id = ParcelsT.getId(parcel) ?: return
            ParcelOptionsT.deleteWhere { ParcelOptionsT.parcel_id eq id }
            return
        }

        if (bitmaskArray.size != 1) throw IllegalArgumentException()
        val array = bitmaskArray.toByteArray()
        val id = ParcelsT.getOrInitId(parcel)
        ParcelOptionsT.upsert(ParcelOptionsT.parcel_id) {
            it[parcel_id] = id
            it[interact_bitmask] = array
        }
    }

    override fun transmitAllGlobalPrivileges(channel: SendChannel<PrivilegePair<PlayerProfile>>) {
        PrivilegesGlobalT.sendAllPrivilegesH(channel)
        channel.close()
    }

    override fun readGlobalPrivileges(owner: PlayerProfile): PrivilegesHolder? {
        return PrivilegesGlobalT.readPrivileges(ProfilesT.getId(owner.toOwnerProfile()) ?: return null)
    }

    override fun setGlobalPrivilege(owner: PlayerProfile, player: PlayerProfile, privilege: Privilege) {
        PrivilegesGlobalT.setPrivilege(owner, player.toRealProfile(), privilege)
    }

    private fun rowToParcelData(row: ResultRow) = ParcelDataHolder().apply {
        owner = row[ParcelsT.owner_id]?.let { ProfilesT.getItem(it) }
        lastClaimTime = row[ParcelsT.claim_time]
        isOwnerSignOutdated = row[ParcelsT.sign_oudated]

        val id = row[ParcelsT.id]
        ParcelOptionsT.select { ParcelOptionsT.parcel_id eq id }.firstOrNull()?.let { optrow ->
            val source = optrow[ParcelOptionsT.interact_bitmask].toIntArray()
            val target = (interactableConfig as? BitmaskInteractableConfiguration ?: return@let).bitmaskArray
            System.arraycopy(source, 0, target, 0, source.size.clampMax(target.size))
        }

        val privileges = PrivilegesLocalT.readPrivileges(id)
        if (privileges != null) {
            copyPrivilegesFrom(privileges)
        }
    }

}