Skip to content

Commit

Permalink
feat: unique index
Browse files Browse the repository at this point in the history
  • Loading branch information
qixils committed Jan 3, 2025
1 parent ec1e45f commit 3ffd241
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/main/java/dev/qixils/quasicord/db/DatabaseManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package dev.qixils.quasicord.db
import com.mongodb.ConnectionString
import com.mongodb.MongoClientSettings
import com.mongodb.client.model.IndexModel
import com.mongodb.client.model.IndexOptions
import com.mongodb.client.model.Indexes
import com.mongodb.kotlin.client.coroutine.MongoClient
import com.mongodb.kotlin.client.coroutine.MongoCollection
Expand Down Expand Up @@ -62,7 +63,7 @@ class DatabaseManager(dbPrefix: String, dbSuffix: String) : Closeable {
.map { index -> IndexModel(Indexes.compoundIndex(index.value.map { key ->
if (key.order == IndexKeyOrder.DESCENDING) Indexes.descending(key.value)
else Indexes.ascending(key.value)
})) }
}), IndexOptions().background(true).unique(index.unique)) }
if (indices.isEmpty()) return collection
collection.createIndexes(indices).collect() // TODO: run this in an off thread ?
// TODO: drop old indexes?
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/dev/qixils/quasicord/db/Index.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
package dev.qixils.quasicord.db

// TODO: unique

/**
* Defines an index for a collection.
* You may define multiple indexes.
*/
@Repeatable
annotation class Index(vararg val value: IndexKey)
annotation class Index(
/**
* Whether this index is unique, meaning no two objects can share the same key values.
*/
val unique: Boolean = false,
/**
* The keys to index against.
*/
vararg val value: IndexKey,
)
29 changes: 27 additions & 2 deletions src/main/java/dev/qixils/quasicord/db/IndexKey.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
package dev.qixils.quasicord.db

enum class IndexKeyOrder{ ASCENDING, DESCENDING }
/**
* The order in which to sort the values of a key.
*/
enum class IndexKeyOrder {
/**
* Sorts in ascending order.
*/
ASCENDING,
/**
* Sorts in descending order.
*/
DESCENDING,
}

annotation class IndexKey(val value: String, val order: IndexKeyOrder = IndexKeyOrder.ASCENDING)
/**
* A key by which to index.
*/
annotation class IndexKey(
/**
* The field to index. For a parameter `snowflake`, this would be `snowflake`.
* For a parameter `id` within a parameter `where`, this would be `where.id`.
*/
val value: String,
/**
* The order in which to sort the values.
*/
val order: IndexKeyOrder = IndexKeyOrder.ASCENDING,
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import java.util.*
* This stores the selected locale for a user, channel, or guild.
*/
@CollectionName("locale")
@Index(IndexKey("snowflake"), IndexKey("entryType"))
@Index(true, IndexKey("snowflake"), IndexKey("entryType"))
data class LocaleConfig @ApiStatus.Internal constructor(
/**
* Returns the database ID of this object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import java.time.ZoneId
* This stores the selected timezone for a user.
*/
@CollectionName("timezone")
@Index(IndexKey("snowflake"))
@Index(true, IndexKey("snowflake"))
data class TimeZoneConfig
/**
* Constructs a new TimeZoneConfig entry.
Expand Down

0 comments on commit 3ffd241

Please sign in to comment.