@@ -171,15 +171,36 @@ func (l *LibsqlStore) Set(ns types.TNamespace, key string, value types.TValue) e
171
171
return err
172
172
}
173
173
174
+ const maxPlaceholders = 32_766
175
+ const placeHoldersPerRow = 4
176
+
174
177
func (l * LibsqlStore ) SetMany (ns types.TNamespace , values []types.TValue , opts * types.SetOptions ) error {
175
- // IMPORTANT: This is not a transaction and will be a max of 2000 rows at a time
178
+ // IMPORTANT: This is not a transaction and will be a max of maxPlaceholders placeholders at a time
179
+ // cache table has 4 columns so we need to multiply by placeHoldersPerRow
180
+ totalPlaceholders := placeHoldersPerRow * len (values )
181
+
176
182
chunks := make ([][]types.TValue , 0 )
177
- chunkSize := 2000
178
- for i , v := range values {
179
- if i % chunkSize == 0 {
180
- chunks = append (chunks , make ([]types.TValue , 0 ))
183
+ if totalPlaceholders <= maxPlaceholders {
184
+ // If we’re below the limit, no need to split into chunks
185
+ chunks = append (chunks , values )
186
+ } else {
187
+ currentChunk := make ([]types.TValue , 0 )
188
+ currentPlaceholders := 0
189
+
190
+ for _ , v := range values {
191
+ if currentPlaceholders + placeHoldersPerRow > maxPlaceholders {
192
+ chunks = append (chunks , currentChunk )
193
+ currentChunk = make ([]types.TValue , 0 )
194
+ currentPlaceholders = 0
195
+ }
196
+
197
+ currentChunk = append (currentChunk , v )
198
+ currentPlaceholders += placeHoldersPerRow
199
+ }
200
+
201
+ if len (currentChunk ) > 0 {
202
+ chunks = append (chunks , currentChunk )
181
203
}
182
- chunks [len (chunks )- 1 ] = append (chunks [len (chunks )- 1 ], v )
183
204
}
184
205
185
206
for _ , chunk := range chunks {
0 commit comments