Skip to content

Commit

Permalink
fix database index
Browse files Browse the repository at this point in the history
  • Loading branch information
storytellerF committed Dec 24, 2024
1 parent 4e08521 commit 2c01f62
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 38 deletions.
2 changes: 1 addition & 1 deletion backend/src/main/kotlin/com/storyteller_f/BaseTable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.jetbrains.exposed.sql.kotlin.datetime.datetime

abstract class BaseTable : Table() {
val id = customPrimaryKey("id")
val createdTime = datetime("created_time").index()
val createdTime = datetime("created_time")

override val primaryKey = PrimaryKey(id)
}
Expand Down
8 changes: 2 additions & 6 deletions backend/src/main/kotlin/com/storyteller_f/tables/Aids.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import com.storyteller_f.objectType
import org.jetbrains.exposed.sql.Table

object Aids : Table() {
val value = varchar("value", 100).index()
val objectId = customPrimaryKey("object_id")
val value = varchar("value", 100).uniqueIndex()
val objectId = customPrimaryKey("object_id").uniqueIndex()
val objectType = objectType("object_type")

init {
index("aids-main", true, objectId, objectType)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.Table

object EncryptedTopics : Table() {
val topicId = customPrimaryKey("topic_id")
val topicId = customPrimaryKey("topic_id").index()
val content = blob("content")

override val primaryKey = PrimaryKey(topicId)
Expand All @@ -21,9 +21,13 @@ class EncryptedTopic(val topicId: PrimaryKey, val content: ByteArray) {
}

object EncryptedTopicKeys : Table() {
val topicId = customPrimaryKey("topic_id")
val topicId = customPrimaryKey("topic_id").index()
val uid = customPrimaryKey("uid")
val encryptedAes = blob("encrypted_aes")

init {
index("encrypted-topic-key-main", true, topicId, uid)
}
}

class EncryptedTopicKey(val topicId: PrimaryKey, val uid: PrimaryKey, val encryptedAes: ByteArray) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object MemberJoins : Table() {
val uid = customPrimaryKey("uid").index()
val objectId = customPrimaryKey("object_id").index()
val objectType = objectType("object_type")
val joinTime = datetime("join_time").index()
val joinTime = datetime("join_time")

init {
index("member-join-main", true, objectId, uid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.jetbrains.exposed.sql.*
object Reactions : BaseTable() {
val emoji = emoji()
val uid = customPrimaryKey("uid")
val objectId = customPrimaryKey("object_id")
val objectId = customPrimaryKey("object_id").index()
val objectType = objectType("object_type")

init {
Expand Down
4 changes: 2 additions & 2 deletions backend/src/main/kotlin/com/storyteller_f/tables/Topics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import org.jetbrains.exposed.sql.statements.api.ExposedBlob

object Topics : BaseTable() {
val author = customPrimaryKey("author").index()
val parentId = customPrimaryKey("parent_id")
val parentId = customPrimaryKey("parent_id").index()
val parentType = objectType("parent_type")
val rootId = customPrimaryKey("root_id")
val rootId = customPrimaryKey("root_id").index()
val rootType = objectType("root_type")
val lastModifiedTime = datetime("last_modified_time").nullable()

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ private fun ProjectDialogInternal(dismiss: () -> Unit) {
dismiss()
appNav.gotoAbout()
}
ButtonNav(Icons.Default.Download, "Download latest app") {
dismiss()
uriHandler.openUri(
"https://nightly.link/storytellerF/A/workflows/alpha/alpha/Signed%20A%20Bundle.zip"
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import com.storyteller_f.a.client_lib.*
import com.storyteller_f.shared.model.CommunityInfo
import com.storyteller_f.shared.type.ObjectType
import com.storyteller_f.shared.type.PrimaryKey
import io.ktor.client.*
import kotlinx.coroutines.launch
import org.jetbrains.compose.resources.stringResource

Expand Down Expand Up @@ -263,7 +262,7 @@ fun CommunityDialogInternal(communityInfo: CommunityInfo, dismiss: () -> Unit) {
scope.launch {
globalDialogState.use {
val info = client.exitCommunity(communityId).getOrThrow()
bus.emit(OnCommunityExited(communityId, info))
bus.emit(OnCommunityExited(info))
}
}
}
Expand All @@ -272,7 +271,7 @@ fun CommunityDialogInternal(communityInfo: CommunityInfo, dismiss: () -> Unit) {
scope.launch {
globalDialogState.use {
val info = client.joinCommunity(communityId).getOrThrow()
bus.emit(OnCommunityJoined(communityId, info))
bus.emit(OnCommunityJoined(info))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ import kotlinx.coroutines.launch
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

data class OnCommunityJoined(val communityId: PrimaryKey, val newInfo: CommunityInfo)
data class OnCommunityExited(val communityId: PrimaryKey, val newInfo: CommunityInfo)
data class OnCommunityJoined(val newInfo: CommunityInfo)
data class OnCommunityExited(val newInfo: CommunityInfo)

data class OnMediaUploaded(val mediaInfo: MediaInfo)

class CommunityViewModel(private val requestInfo: suspend HttpClient.() -> Result<CommunityInfo>) :
SimpleViewModel<CommunityInfo>() {
Expand All @@ -52,13 +54,13 @@ class CommunityViewModel(private val requestInfo: suspend HttpClient.() -> Resul
val id = handler.data.value?.id
when (i) {
is OnCommunityJoined -> {
if (i.communityId == id) {
if (i.newInfo.id == id) {
update(i.newInfo)
}
}

is OnCommunityExited -> {
if (i.communityId == id) {
if (i.newInfo.id == id) {
update(i.newInfo)
}
}
Expand Down Expand Up @@ -99,8 +101,8 @@ class RoomsViewModel(
}
})

data class OnRoomJoined(val id: PrimaryKey, val newInfo: RoomInfo)
data class OnRoomExited(val id: PrimaryKey, val newInfo: RoomInfo)
data class OnRoomJoined(val newInfo: RoomInfo)
data class OnRoomExited(val newInfo: RoomInfo)

@OptIn(ExperimentalPagingApi::class)
class TopicsViewModel(id: PrimaryKey, val type: ObjectType) : PagingViewModel<PrimaryKey, TopicInfo>({
Expand Down Expand Up @@ -215,19 +217,17 @@ class RoomViewModel(private val requestInfo: suspend HttpClient.() -> Result<Roo
load()
viewModelScope.launch {
bus.collect { i ->
if (handler.state.value !is LoadingState.Loading) {
val id = handler.data.value?.id
when (i) {
is OnRoomJoined -> {
if (i.id == id) {
update(i.newInfo)
}
val id = handler.data.value?.id
when (i) {
is OnRoomJoined -> {
if (i.newInfo.id == id) {
update(i.newInfo)
}
}

is OnRoomExited -> {
if (i.id == id) {
update(i.newInfo)
}
is OnRoomExited -> {
if (i.newInfo.id == id) {
update(i.newInfo)
}
}
}
Expand All @@ -254,10 +254,19 @@ class MediaListViewModel(private val objectId: PrimaryKey, private val objectTyp
SimpleViewModel<ServerResponse<MediaInfo>>() {
init {
load()
viewModelScope.launch {
bus.collect {
if (it is OnMediaUploaded) {
val old = handler.data.value ?: ServerResponse(emptyList())
update(old.copy(data = old.data + it.mediaInfo))
}
}
}
}

override suspend fun loadInternal() = client.getMediaList(objectId, objectType)
}

class UserViewModel(private val requestInfo: suspend HttpClient.() -> Result<UserInfo>) : SimpleViewModel<UserInfo>() {
constructor(userId: PrimaryKey) : this({
getUserInfo(userId)
Expand Down Expand Up @@ -342,6 +351,7 @@ class TopicViewModel(private val requestInfo: suspend HttpClient.() -> Result<To
}
}
}

class RoomKeysViewModel(private val id: PrimaryKey, private: Boolean) :
SimpleViewModel<List<Pair<PrimaryKey, String>>>() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import com.storyteller_f.shared.model.TopicInfo
import com.storyteller_f.shared.obj.RoomFrame
import com.storyteller_f.shared.type.ObjectType
import com.storyteller_f.shared.type.PrimaryKey
import io.ktor.client.plugins.websocket.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.jetbrains.compose.resources.getString
Expand Down Expand Up @@ -393,15 +392,15 @@ private suspend fun joinRoom(roomInfo: RoomInfo, onSuccess: () -> Unit) {
}
}
val info = client.joinRoom(roomInfo.id).getOrThrow()
bus.emit(OnRoomJoined(roomInfo.id, info))
bus.emit(OnRoomJoined(info))
onSuccess()
}
}

private suspend fun exitRoom(roomInfo: RoomInfo, onSuccess: () -> Unit) {
globalDialogState.use {
val info = client.exitRoom(roomInfo.id).getOrThrow()
bus.emit(OnRoomExited(roomInfo.id, info))
bus.emit(OnRoomExited(info))
onSuccess()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ import com.dokar.sonner.rememberToasterState
import com.mohamedrejeb.richeditor.model.RichTextState
import com.mohamedrejeb.richeditor.model.rememberRichTextState
import com.mohamedrejeb.richeditor.ui.BasicRichTextEditor
import com.storyteller_f.a.app.bus
import com.storyteller_f.a.app.client
import com.storyteller_f.a.app.common.StateView
import com.storyteller_f.a.app.common.getOrCreateCollection
import com.storyteller_f.a.app.globalDialogState
import com.storyteller_f.a.app.model.MediaListViewModel
import com.storyteller_f.a.app.model.OnMediaUploaded
import com.storyteller_f.a.app.model.createMediaListViewModel
import com.storyteller_f.a.client_lib.LoginViewModel
import com.storyteller_f.a.client_lib.createNewTopic
Expand Down Expand Up @@ -150,7 +152,17 @@ private fun TopicComposeDrawer(
if (f != null) {
val size = f.getSize()
if (size != null && size <= 100 * 1024 * 1024) {
client.upload(f.readBytes(), f.name, f.extension, id, ObjectType.USER)
val response = client.upload(
f.readBytes(),
f.name,
f.extension,
id,
ObjectType.USER
)
.getOrThrow()
response.data.firstOrNull()?.let {
bus.emit(OnMediaUploaded(it))
}
} else {
toasterState.show("size is null or size too big", duration = 1.seconds)
}
Expand Down

0 comments on commit 2c01f62

Please sign in to comment.