-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCollectionManagerVector.kt
More file actions
153 lines (139 loc) · 5.36 KB
/
CollectionManagerVector.kt
File metadata and controls
153 lines (139 loc) · 5.36 KB
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
/**
* CollectionManagerVector.kt
*
* Vector index support for Android/Kotlin implementation.
* This code should be integrated into CollectionManager.kt
*/
package com.cblreactnative
import com.couchbase.lite.Collection
import com.couchbase.lite.VectorIndexConfiguration
import com.couchbase.lite.VectorEncoding
import com.couchbase.lite.DistanceMetric
import com.couchbase.lite.ScalarQuantizerType
/**
* Creates a vector index on the specified collection.
*
* This method handles the "vector" case in the createIndex switch/when statement.
*
* @param indexName Name for the new index
* @param indexConfig Map containing vector index configuration
* @param collection The collection to create the index on
* @throws Exception if index creation fails
*/
fun createVectorIndex(
indexName: String,
indexConfig: Map<String, Any>,
collection: Collection
) {
// Extract required parameters
val expression = indexConfig["expression"] as? String
?: throw Exception("Vector index requires 'expression' parameter")
val dimensions = (indexConfig["dimensions"] as? Number)?.toLong()
?: throw Exception("Vector index requires 'dimensions' parameter")
val centroids = (indexConfig["centroids"] as? Number)?.toLong()
?: throw Exception("Vector index requires 'centroids' parameter")
// Create vector index configuration
val vectorConfig = VectorIndexConfiguration(expression, dimensions, centroids)
// Set distance metric
val metricStr = indexConfig["metric"] as? String
if (metricStr != null) {
vectorConfig.metric = when (metricStr) {
"cosine" -> DistanceMetric.COSINE
"euclidean" -> DistanceMetric.EUCLIDEAN
"euclideanSquared" -> DistanceMetric.EUCLIDEAN_SQUARED
"dot" -> DistanceMetric.DOT
else -> DistanceMetric.EUCLIDEAN_SQUARED // Default
}
}
// Set encoding
val encodingConfig = indexConfig["encoding"] as? Map<String, Any>
if (encodingConfig != null) {
val encodingType = encodingConfig["type"] as? String
vectorConfig.encoding = when (encodingType) {
"none" -> VectorEncoding.none()
"SQ" -> {
// Scalar Quantizer - determine type from config or default to SQ8
val sqType = encodingConfig["sqType"] as? String
when (sqType) {
"SQ4" -> VectorEncoding.scalarQuantizer(ScalarQuantizerType.SQ4)
"SQ6" -> VectorEncoding.scalarQuantizer(ScalarQuantizerType.SQ6)
"SQ8" -> VectorEncoding.scalarQuantizer(ScalarQuantizerType.SQ8)
else -> VectorEncoding.scalarQuantizer(ScalarQuantizerType.SQ8)
}
}
"PQ" -> {
// Product Quantizer
val subquantizers = (encodingConfig["subquantizers"] as? Number)?.toLong() ?: 0
val bits = (encodingConfig["bits"] as? Number)?.toLong() ?: 8
VectorEncoding.productQuantizer(subquantizers, bits)
}
else -> VectorEncoding.none()
}
}
// Set optional training parameters
val minTrainingSize = (indexConfig["minTrainingSize"] as? Number)?.toLong()
if (minTrainingSize != null && minTrainingSize > 0) {
vectorConfig.minTrainingSize = minTrainingSize
}
val maxTrainingSize = (indexConfig["maxTrainingSize"] as? Number)?.toLong()
if (maxTrainingSize != null && maxTrainingSize > 0) {
vectorConfig.maxTrainingSize = maxTrainingSize
}
// Set number of probes (affects search accuracy vs speed)
val numProbes = (indexConfig["numProbes"] as? Number)?.toLong()
if (numProbes != null && numProbes > 0) {
vectorConfig.numProbes = numProbes
}
// Set lazy indexing (index built on first query)
val isLazy = indexConfig["isLazy"] as? Boolean
if (isLazy != null) {
vectorConfig.isLazy = isLazy
}
// Create the index
collection.createIndex(indexName, vectorConfig)
}
/*
* INTEGRATION INSTRUCTIONS
* ========================
*
* To integrate this into CollectionManager.kt:
*
* 1. Add the imports at the top of the file:
*
* import com.couchbase.lite.VectorIndexConfiguration
* import com.couchbase.lite.VectorEncoding
* import com.couchbase.lite.DistanceMetric
* import com.couchbase.lite.ScalarQuantizerType
*
* 2. Modify the createIndex function signature to accept optional config:
*
* fun createIndex(
* indexName: String,
* indexType: String,
* items: List<List<Any>>,
* indexConfig: Map<String, Any>? = null, // ADD THIS
* collectionName: String,
* scopeName: String,
* databaseName: String
* )
*
* 3. Add the vector case to the when statement:
*
* when (indexType) {
* "value" -> {
* // existing code...
* }
* "full-text" -> {
* // existing code...
* }
* "vector" -> {
* val config = indexConfig
* ?: throw Exception("Vector index requires config")
* createVectorIndex(indexName, config, collection)
* }
* else -> throw Exception("Unknown index type: $indexType")
* }
*
* 4. Update the React Native bridge module (CblReactnativeModule.kt) to
* extract and pass the full config map for vector indexes.
*/