Skip to content

Commit 3ffd241

Browse files
committed
feat: unique index
1 parent ec1e45f commit 3ffd241

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

src/main/java/dev/qixils/quasicord/db/DatabaseManager.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package dev.qixils.quasicord.db
88
import com.mongodb.ConnectionString
99
import com.mongodb.MongoClientSettings
1010
import com.mongodb.client.model.IndexModel
11+
import com.mongodb.client.model.IndexOptions
1112
import com.mongodb.client.model.Indexes
1213
import com.mongodb.kotlin.client.coroutine.MongoClient
1314
import com.mongodb.kotlin.client.coroutine.MongoCollection
@@ -62,7 +63,7 @@ class DatabaseManager(dbPrefix: String, dbSuffix: String) : Closeable {
6263
.map { index -> IndexModel(Indexes.compoundIndex(index.value.map { key ->
6364
if (key.order == IndexKeyOrder.DESCENDING) Indexes.descending(key.value)
6465
else Indexes.ascending(key.value)
65-
})) }
66+
}), IndexOptions().background(true).unique(index.unique)) }
6667
if (indices.isEmpty()) return collection
6768
collection.createIndexes(indices).collect() // TODO: run this in an off thread ?
6869
// TODO: drop old indexes?
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
package dev.qixils.quasicord.db
22

3-
// TODO: unique
4-
3+
/**
4+
* Defines an index for a collection.
5+
* You may define multiple indexes.
6+
*/
57
@Repeatable
6-
annotation class Index(vararg val value: IndexKey)
8+
annotation class Index(
9+
/**
10+
* Whether this index is unique, meaning no two objects can share the same key values.
11+
*/
12+
val unique: Boolean = false,
13+
/**
14+
* The keys to index against.
15+
*/
16+
vararg val value: IndexKey,
17+
)
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
package dev.qixils.quasicord.db
22

3-
enum class IndexKeyOrder{ ASCENDING, DESCENDING }
3+
/**
4+
* The order in which to sort the values of a key.
5+
*/
6+
enum class IndexKeyOrder {
7+
/**
8+
* Sorts in ascending order.
9+
*/
10+
ASCENDING,
11+
/**
12+
* Sorts in descending order.
13+
*/
14+
DESCENDING,
15+
}
416

5-
annotation class IndexKey(val value: String, val order: IndexKeyOrder = IndexKeyOrder.ASCENDING)
17+
/**
18+
* A key by which to index.
19+
*/
20+
annotation class IndexKey(
21+
/**
22+
* The field to index. For a parameter `snowflake`, this would be `snowflake`.
23+
* For a parameter `id` within a parameter `where`, this would be `where.id`.
24+
*/
25+
val value: String,
26+
/**
27+
* The order in which to sort the values.
28+
*/
29+
val order: IndexKeyOrder = IndexKeyOrder.ASCENDING,
30+
)

src/main/java/dev/qixils/quasicord/db/collection/LocaleConfig.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import java.util.*
1919
* This stores the selected locale for a user, channel, or guild.
2020
*/
2121
@CollectionName("locale")
22-
@Index(IndexKey("snowflake"), IndexKey("entryType"))
22+
@Index(true, IndexKey("snowflake"), IndexKey("entryType"))
2323
data class LocaleConfig @ApiStatus.Internal constructor(
2424
/**
2525
* Returns the database ID of this object.

src/main/java/dev/qixils/quasicord/db/collection/TimeZoneConfig.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import java.time.ZoneId
1717
* This stores the selected timezone for a user.
1818
*/
1919
@CollectionName("timezone")
20-
@Index(IndexKey("snowflake"))
20+
@Index(true, IndexKey("snowflake"))
2121
data class TimeZoneConfig
2222
/**
2323
* Constructs a new TimeZoneConfig entry.

0 commit comments

Comments
 (0)