/
registry.go
1044 lines (839 loc) · 26.7 KB
/
registry.go
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package metadata
import (
"context"
"fmt"
"hash/fnv"
"regexp"
"slices"
"sort"
"strings"
"sync"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"golang.org/x/exp/maps"
"github.com/FerretDB/FerretDB/internal/backends"
"github.com/FerretDB/FerretDB/internal/backends/postgresql/metadata/pool"
"github.com/FerretDB/FerretDB/internal/clientconn/conninfo"
"github.com/FerretDB/FerretDB/internal/handler/sjson"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/must"
"github.com/FerretDB/FerretDB/internal/util/observability"
"github.com/FerretDB/FerretDB/internal/util/state"
)
const (
// PostgreSQL table name where FerretDB metadata is stored.
metadataTableName = backends.ReservedPrefix + "database_metadata"
// PostgreSQL max table name length.
maxTableNameLength = 63
// PostgreSQL max index name length.
maxIndexNameLength = 63
)
// Parts of Prometheus metric names.
const (
namespace = "ferretdb"
subsystem = "postgresql_metadata"
)
// specialCharacters are unsupported characters of PostgreSQL table name that are replaced with `_`.
var specialCharacters = regexp.MustCompile("[^a-z][^a-z0-9_]*")
// Registry provides access to PostgreSQL databases and collections information.
//
// Exported methods and [getPool] are safe for concurrent use. Other unexported methods are not.
//
// All methods should call [getPool] to check authentication.
// There is no authorization yet – if username/password combination is correct,
// all databases and collections are visible as far as Registry is concerned.
//
// Registry metadata is loaded upon first call by client, using [conninfo] in the context of the client.
//
//nolint:vet // for readability
type Registry struct {
p *pool.Pool
l *zap.Logger
BatchSize int
// rw protects colls but also acts like a global lock for the whole registry.
// The latter effectively replaces transactions (see the postgresql backend package description for more info).
// One global lock should be replaced by more granular locks – one per database or even one per collection.
// But that requires some redesign.
// TODO https://github.com/FerretDB/FerretDB/issues/2755
rw sync.RWMutex
colls map[string]map[string]*Collection // database name -> collection name -> collection
}
// NewRegistry creates a registry for PostgreSQL databases with a given base URI.
func NewRegistry(u string, batchSize int, l *zap.Logger, sp *state.Provider) (*Registry, error) {
p, err := pool.New(u, l, sp)
if err != nil {
return nil, err
}
r := &Registry{
p: p,
l: l,
BatchSize: batchSize,
}
return r, nil
}
// Close closes the registry.
func (r *Registry) Close() {
r.p.Close()
}
// getPool returns a pool of connections to PostgreSQL database
// for the username/password combination in the context using [conninfo]
// (or any pool if authentication is bypassed).
//
// It loads metadata if it hasn't been loaded from the database yet.
//
// It acquires read lock to check metadata, if metadata is empty it acquires write lock
// to load metadata, so it is safe for concurrent use.
//
// All methods should use this method to check authentication and load metadata.
func (r *Registry) getPool(ctx context.Context) (*pgxpool.Pool, error) {
connInfo := conninfo.Get(ctx)
var p *pgxpool.Pool
if connInfo.BypassBackendAuth() {
if p = r.p.GetAny(); p == nil {
var err error
// use credential from the base URI by passing empty values
if p, err = r.p.Get("", ""); err != nil {
return nil, lazyerrors.Error(err)
}
}
} else {
username, password, _ := connInfo.Auth()
var err error
if p, err = r.p.Get(username, password); err != nil {
return nil, lazyerrors.Error(err)
}
}
r.rw.RLock()
if r.colls != nil {
r.rw.RUnlock()
return p, nil
}
r.rw.RUnlock()
r.rw.Lock()
defer r.rw.Unlock()
dbNames, err := r.initDBs(ctx, p)
if err != nil {
return nil, lazyerrors.Error(err)
}
r.colls = make(map[string]map[string]*Collection, len(dbNames))
for _, dbName := range dbNames {
if err = r.initCollections(ctx, dbName, p); err != nil {
return nil, lazyerrors.Error(err)
}
}
return p, nil
}
// initDBs returns a list of database names using schema information.
// It fetches existing schema (excluding ones reserved for PostgreSQL),
// then finds and returns schema that contains FerretDB metadata table.
func (r *Registry) initDBs(ctx context.Context, p *pgxpool.Pool) ([]string, error) {
// schema names with pg_ prefix are reserved for postgresql hence excluded,
// a collection cannot be created in a database with pg_ prefix
q := strings.TrimSpace(`
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name NOT LIKE 'pg_%'
`)
rows, err := p.Query(ctx, q)
if err != nil {
return nil, lazyerrors.Error(err)
}
defer rows.Close()
if err = rows.Err(); err != nil {
return nil, lazyerrors.Error(err)
}
var dbNames []string
for rows.Next() {
var dbName string
if err = rows.Scan(&dbName); err != nil {
return nil, lazyerrors.Error(err)
}
// schema created by PostgreSQL (such as `public`) can be used as
// a FerretDB database, but if it does not contain FerretDB metadata table,
// it is not used by FerretDB
q := strings.TrimSpace(`
SELECT EXISTS (
SELECT 1
FROM information_schema.columns
WHERE table_schema = $1 AND table_name = $2
)
`)
var exists bool
if err = p.QueryRow(ctx, q, dbName, metadataTableName).Scan(&exists); err != nil {
return nil, lazyerrors.Error(err)
}
if exists {
dbNames = append(dbNames, dbName)
}
}
return dbNames, nil
}
// initCollections loads collections metadata from the database during initialization.
func (r *Registry) initCollections(ctx context.Context, dbName string, p *pgxpool.Pool) error {
defer observability.FuncCall(ctx)()
q := fmt.Sprintf(
`SELECT %s FROM %s`,
DefaultColumn,
pgx.Identifier{dbName, metadataTableName}.Sanitize(),
)
rows, err := p.Query(ctx, q)
if err != nil {
return lazyerrors.Error(err)
}
defer rows.Close()
colls := map[string]*Collection{}
for rows.Next() {
var c Collection
if err = rows.Scan(&c); err != nil {
return lazyerrors.Error(err)
}
colls[c.Name] = &c
}
if err = rows.Err(); err != nil {
return lazyerrors.Error(err)
}
r.colls[dbName] = colls
return nil
}
// DatabaseList returns a sorted list of existing databases.
//
// If the user is not authenticated, it returns error.
func (r *Registry) DatabaseList(ctx context.Context) ([]string, error) {
defer observability.FuncCall(ctx)()
_, err := r.getPool(ctx)
if err != nil {
return nil, lazyerrors.Error(err)
}
r.rw.RLock()
defer r.rw.RUnlock()
res := maps.Keys(r.colls)
sort.Strings(res)
return res, nil
}
// DatabaseGetExisting returns a connection to existing database or nil if it doesn't exist.
//
// If the user is not authenticated, it returns error.
func (r *Registry) DatabaseGetExisting(ctx context.Context, dbName string) (*pgxpool.Pool, error) {
defer observability.FuncCall(ctx)()
p, err := r.getPool(ctx)
if err != nil {
return nil, lazyerrors.Error(err)
}
r.rw.RLock()
defer r.rw.RUnlock()
db := r.colls[dbName]
if db == nil {
return nil, nil
}
return p, nil
}
// DatabaseGetOrCreate returns a connection to existing database or newly created database.
//
// The dbName must be a validated database name.
//
// If the user is not authenticated, it returns error.
func (r *Registry) DatabaseGetOrCreate(ctx context.Context, dbName string) (*pgxpool.Pool, error) {
defer observability.FuncCall(ctx)()
p, err := r.getPool(ctx)
if err != nil {
return nil, lazyerrors.Error(err)
}
r.rw.Lock()
defer r.rw.Unlock()
return r.databaseGetOrCreate(ctx, p, dbName)
}
// databaseGetOrCreate returns a connection to existing database or newly created database.
//
// The dbName must be a validated database name.
//
// It does not hold the lock.
func (r *Registry) databaseGetOrCreate(ctx context.Context, p *pgxpool.Pool, dbName string) (*pgxpool.Pool, error) {
defer observability.FuncCall(ctx)()
db := r.colls[dbName]
if db != nil {
return p, nil
}
q := fmt.Sprintf(
`CREATE SCHEMA IF NOT EXISTS %s`,
pgx.Identifier{dbName}.Sanitize(),
)
var err error
if _, err = p.Exec(ctx, q); err != nil {
return nil, lazyerrors.Error(err)
}
q = fmt.Sprintf(
`CREATE TABLE %s (%s jsonb)`,
pgx.Identifier{dbName, metadataTableName}.Sanitize(),
DefaultColumn,
)
if _, err = p.Exec(ctx, q); err != nil {
_, _ = r.databaseDrop(ctx, p, dbName)
return nil, lazyerrors.Error(err)
}
q = fmt.Sprintf(
`CREATE UNIQUE INDEX %s ON %s (((%s)))`,
pgx.Identifier{metadataTableName + "_id_idx"}.Sanitize(),
pgx.Identifier{dbName, metadataTableName}.Sanitize(),
IDColumn,
)
if _, err = p.Exec(ctx, q); err != nil {
_, _ = r.databaseDrop(ctx, p, dbName)
return nil, lazyerrors.Error(err)
}
q = fmt.Sprintf(
`CREATE UNIQUE INDEX %s ON %s (((%s->'table')))`,
pgx.Identifier{metadataTableName + "_table_idx"}.Sanitize(),
pgx.Identifier{dbName, metadataTableName}.Sanitize(),
DefaultColumn,
)
if _, err = p.Exec(ctx, q); err != nil {
_, _ = r.databaseDrop(ctx, p, dbName)
return nil, lazyerrors.Error(err)
}
r.colls[dbName] = map[string]*Collection{}
return p, nil
}
// DatabaseDrop drops the database.
//
// Returned boolean value indicates whether the database was dropped.
// If database does not exist, (false, nil) is returned.
//
// If the user is not authenticated, it returns error.
func (r *Registry) DatabaseDrop(ctx context.Context, dbName string) (bool, error) {
defer observability.FuncCall(ctx)()
p, err := r.getPool(ctx)
if err != nil {
return false, lazyerrors.Error(err)
}
r.rw.Lock()
defer r.rw.Unlock()
return r.databaseDrop(ctx, p, dbName)
}
// DatabaseDrop drops the database.
//
// Returned boolean value indicates whether the database was dropped.
// If database does not exist, (false, nil) is returned.
//
// It does not hold the lock.
func (r *Registry) databaseDrop(ctx context.Context, p *pgxpool.Pool, dbName string) (bool, error) {
defer observability.FuncCall(ctx)()
db := r.colls[dbName]
if db == nil {
return false, nil
}
q := fmt.Sprintf(
`DROP SCHEMA %s CASCADE`,
pgx.Identifier{dbName}.Sanitize(),
)
if _, err := p.Exec(ctx, q); err != nil {
return false, lazyerrors.Error(err)
}
delete(r.colls, dbName)
return true, nil
}
// CollectionList returns a sorted copy of collections in the database.
//
// If database does not exist, no error is returned.
//
// If the user is not authenticated, it returns error.
func (r *Registry) CollectionList(ctx context.Context, dbName string) ([]*Collection, error) {
defer observability.FuncCall(ctx)()
if _, err := r.getPool(ctx); err != nil {
return nil, lazyerrors.Error(err)
}
r.rw.RLock()
defer r.rw.RUnlock()
db := r.colls[dbName]
if db == nil {
return nil, nil
}
res := make([]*Collection, 0, len(r.colls[dbName]))
for _, c := range r.colls[dbName] {
res = append(res, c.deepCopy())
}
sort.Slice(res, func(i, j int) bool { return res[i].Name < res[j].Name })
return res, nil
}
// CollectionCreateParams contains parameters for CollectionCreate.
type CollectionCreateParams struct {
DBName string
Name string
CappedSize int64
CappedDocuments int64
_ struct{} // prevent unkeyed literals
}
// Capped returns true if capped collection creation is requested.
func (ccp *CollectionCreateParams) Capped() bool {
return ccp.CappedSize > 0 // TODO https://github.com/FerretDB/FerretDB/issues/3631
}
// CollectionCreate creates a collection in the database.
// Database will be created automatically if needed.
//
// Returned boolean value indicates whether the collection was created.
// If collection already exists, (false, nil) is returned.
//
// If the user is not authenticated, it returns error.
func (r *Registry) CollectionCreate(ctx context.Context, params *CollectionCreateParams) (bool, error) {
defer observability.FuncCall(ctx)()
p, err := r.getPool(ctx)
if err != nil {
return false, lazyerrors.Error(err)
}
r.rw.Lock()
defer r.rw.Unlock()
return r.collectionCreate(ctx, p, params)
}
// collectionCreate creates a collection in the database.
// Database will be created automatically if needed.
//
// Returned boolean value indicates whether the collection was created.
// If collection already exists, (false, nil) is returned.
//
// It does not hold the lock.
func (r *Registry) collectionCreate(ctx context.Context, p *pgxpool.Pool, params *CollectionCreateParams) (bool, error) {
defer observability.FuncCall(ctx)()
dbName, collectionName := params.DBName, params.Name
_, err := r.databaseGetOrCreate(ctx, p, dbName)
if err != nil {
return false, lazyerrors.Error(err)
}
colls := r.colls[dbName]
if colls != nil && colls[collectionName] != nil {
return false, nil
}
h := fnv.New32a()
must.NotFail(h.Write([]byte(collectionName)))
s := h.Sum32()
var tableName string
list := maps.Values(colls)
for {
tableName = specialCharacters.ReplaceAllString(strings.ToLower(collectionName), "_")
suffixHash := fmt.Sprintf("_%08x", s)
if l := maxTableNameLength - len(suffixHash); len(tableName) > l {
tableName = tableName[:l]
}
tableName = fmt.Sprintf("%s%s", tableName, suffixHash)
if !slices.ContainsFunc(list, func(c *Collection) bool { return c.TableName == tableName }) {
break
}
// table already exists, generate a new table name by incrementing the hash
s++
}
c := &Collection{
Name: collectionName,
UUID: uuid.NewString(),
TableName: tableName,
CappedSize: params.CappedSize,
CappedDocuments: params.CappedDocuments,
}
q := fmt.Sprintf(`CREATE TABLE %s (`, pgx.Identifier{dbName, tableName}.Sanitize())
if params.Capped() {
q += fmt.Sprintf(`%s bigint PRIMARY KEY, `, RecordIDColumn)
}
q += fmt.Sprintf(`%s jsonb)`, DefaultColumn)
if _, err = p.Exec(ctx, q); err != nil {
return false, lazyerrors.Error(err)
}
q = fmt.Sprintf(
`INSERT INTO %s (%s) VALUES ($1)`,
pgx.Identifier{dbName, metadataTableName}.Sanitize(),
DefaultColumn,
)
if _, err = p.Exec(ctx, q, c); err != nil {
q = fmt.Sprintf(`DROP TABLE %s`, pgx.Identifier{dbName, tableName}.Sanitize())
_, _ = p.Exec(ctx, q)
return false, lazyerrors.Error(err)
}
if r.colls[dbName] == nil {
r.colls[dbName] = map[string]*Collection{}
}
r.colls[dbName][collectionName] = c
err = r.indexesCreate(ctx, p, dbName, collectionName, []IndexInfo{{
Name: "_id_",
Key: []IndexKeyPair{{Field: "_id"}},
Unique: true,
}})
if err != nil {
_, _ = r.collectionDrop(ctx, p, dbName, collectionName)
return false, lazyerrors.Error(err)
}
return true, nil
}
// CollectionGet returns a copy of collection metadata.
// It can be safely modified by a caller.
//
// If database or collection does not exist, nil is returned.
//
// If the user is not authenticated, it returns error.
func (r *Registry) CollectionGet(ctx context.Context, dbName, collectionName string) (*Collection, error) {
defer observability.FuncCall(ctx)()
if _, err := r.getPool(ctx); err != nil {
return nil, lazyerrors.Error(err)
}
r.rw.RLock()
defer r.rw.RUnlock()
return r.collectionGet(dbName, collectionName), nil
}
// collectionGet returns a copy of collection metadata.
// It can be safely modified by a caller.
//
// If database or collection does not exist, nil is returned.
//
// It does not hold the lock.
func (r *Registry) collectionGet(dbName, collectionName string) *Collection {
colls := r.colls[dbName]
if colls == nil {
return nil
}
return colls[collectionName].deepCopy()
}
// CollectionDrop drops a collection in the database.
//
// Returned boolean value indicates whether the collection was dropped.
// If database or collection did not exist, (false, nil) is returned.
//
// If the user is not authenticated, it returns error.
func (r *Registry) CollectionDrop(ctx context.Context, dbName, collectionName string) (bool, error) {
defer observability.FuncCall(ctx)()
p, err := r.getPool(ctx)
if err != nil {
return false, lazyerrors.Error(err)
}
r.rw.Lock()
defer r.rw.Unlock()
return r.collectionDrop(ctx, p, dbName, collectionName)
}
// collectionDrop drops a collection in the database.
//
// Returned boolean value indicates whether the collection was dropped.
// If database or collection did not exist, (false, nil) is returned.
//
// It does not hold the lock.
func (r *Registry) collectionDrop(ctx context.Context, p *pgxpool.Pool, dbName, collectionName string) (bool, error) {
defer observability.FuncCall(ctx)()
db := r.colls[dbName]
if db == nil {
return false, nil
}
c := r.collectionGet(dbName, collectionName)
if c == nil {
return false, nil
}
arg, err := sjson.MarshalSingleValue(c.Name)
if err != nil {
return false, lazyerrors.Error(err)
}
// TODO https://github.com/FerretDB/FerretDB/issues/811
q := fmt.Sprintf(
`DROP TABLE %s CASCADE`,
pgx.Identifier{dbName, c.TableName}.Sanitize(),
)
if _, err = p.Exec(ctx, q); err != nil {
return false, lazyerrors.Error(err)
}
q = fmt.Sprintf(
`DELETE FROM %s WHERE %s IN ($1)`,
pgx.Identifier{dbName, metadataTableName}.Sanitize(),
IDColumn,
)
if _, err := p.Exec(ctx, q, arg); err != nil {
// the table has been dropped but metadata related to the table is out of sync
return false, lazyerrors.Error(err)
}
delete(r.colls[dbName], collectionName)
return true, nil
}
// CollectionRename renames a collection in the database.
//
// The collection name is updated, but original table name is kept.
//
// Returned boolean value indicates whether the collection was renamed.
// If database or collection did not exist, (false, nil) is returned.
//
// If the user is not authenticated, it returns error.
func (r *Registry) CollectionRename(ctx context.Context, dbName, oldCollectionName, newCollectionName string) (bool, error) {
defer observability.FuncCall(ctx)()
p, err := r.getPool(ctx)
if err != nil {
return false, lazyerrors.Error(err)
}
r.rw.Lock()
defer r.rw.Unlock()
db := r.colls[dbName]
if db == nil {
return false, nil
}
c := r.collectionGet(dbName, oldCollectionName)
if c == nil {
return false, nil
}
c.Name = newCollectionName
b, err := sjson.Marshal(c.marshal())
if err != nil {
return false, lazyerrors.Error(err)
}
arg, err := sjson.MarshalSingleValue(oldCollectionName)
if err != nil {
return false, lazyerrors.Error(err)
}
q := fmt.Sprintf(
`UPDATE %s SET %s = $1 WHERE %s = $2`,
pgx.Identifier{dbName, metadataTableName}.Sanitize(),
DefaultColumn,
IDColumn,
)
if _, err := p.Exec(ctx, q, string(b), arg); err != nil {
return false, lazyerrors.Error(err)
}
r.colls[dbName][newCollectionName] = c
delete(r.colls[dbName], oldCollectionName)
return true, nil
}
// IndexesCreate creates indexes in the collection.
//
// Existing indexes with given names are ignored.
//
// If the user is not authenticated, it returns error.
func (r *Registry) IndexesCreate(ctx context.Context, dbName, collectionName string, indexes []IndexInfo) error {
defer observability.FuncCall(ctx)()
p, err := r.getPool(ctx)
if err != nil {
return lazyerrors.Error(err)
}
r.rw.Lock()
defer r.rw.Unlock()
return r.indexesCreate(ctx, p, dbName, collectionName, indexes)
}
// indexesCreate creates indexes in the collection.
//
// Existing indexes with given names are ignored.
//
// It does not hold the lock.
func (r *Registry) indexesCreate(ctx context.Context, p *pgxpool.Pool, dbName, collectionName string, indexes []IndexInfo) error {
defer observability.FuncCall(ctx)()
_, err := r.collectionCreate(ctx, p, &CollectionCreateParams{DBName: dbName, Name: collectionName})
if err != nil {
return lazyerrors.Error(err)
}
db := r.colls[dbName]
if db == nil {
panic("database does not exist")
}
c := r.collectionGet(dbName, collectionName)
if c == nil {
panic("collection does not exist")
}
allIndexes := make(map[string]string, len(db)) // to check if the index already exists
allPgIndexes := make(map[string]string, len(db)) // to ensure there are no indexes with the same name in the pg schema
for _, coll := range db {
for _, index := range coll.Indexes {
allIndexes[index.Name] = coll.Name
allPgIndexes[index.PgIndex] = coll.Name
}
}
created := make([]string, 0, len(indexes))
for _, index := range indexes {
if coll, ok := allIndexes[index.Name]; ok && coll == collectionName {
continue
}
tableNamePart := c.TableName
tableNamePartMax := maxIndexNameLength/2 - 1 // 1 for the separator between table name and index name
if len(tableNamePart) > tableNamePartMax {
tableNamePart = tableNamePart[:tableNamePartMax]
}
indexNamePart := specialCharacters.ReplaceAllString(strings.ToLower(index.Name), "_")
h := fnv.New32a()
must.NotFail(h.Write([]byte(index.Name)))
s := h.Sum32()
var pgIndexName string
for {
suffixHash := fmt.Sprintf("_%08x_idx", s)
if l := maxIndexNameLength/2 - len(suffixHash); len(indexNamePart) > l {
indexNamePart = indexNamePart[:l]
}
pgIndexName = fmt.Sprintf("%s_%s%s", tableNamePart, indexNamePart, suffixHash)
// indexes must be unique across the whole database, so we check for duplicates for all collections
_, duplicate := allPgIndexes[pgIndexName]
if !duplicate {
break
}
s++
}
index.PgIndex = pgIndexName
q := "CREATE "
if index.Unique {
q += "UNIQUE "
}
q += "INDEX %s ON %s (%s)"
columns := make([]string, len(index.Key))
for i, key := range index.Key {
// if the field is nested (e.g. foo.bar), it needs to be translated to the correct json path (foo -> bar)
fs := strings.Split(key.Field, ".")
transformedParts := make([]string, len(fs))
for j, f := range fs {
// It's important to sanitize field.Field data here, as it's a user-provided value.
transformedParts[j] = quoteString(f)
}
columns[i] = fmt.Sprintf("((%s->%s))", DefaultColumn, strings.Join(transformedParts, " -> "))
if key.Descending {
columns[i] += " DESC"
}
}
q = fmt.Sprintf(
q,
pgx.Identifier{index.PgIndex}.Sanitize(),
pgx.Identifier{dbName, c.TableName}.Sanitize(),
strings.Join(columns, ", "),
)
if _, err = p.Exec(ctx, q); err != nil {
_ = r.indexesDrop(ctx, p, dbName, collectionName, created)
return lazyerrors.Error(err)
}
created = append(created, index.Name)
c.Indexes = append(c.Indexes, index)
allIndexes[index.Name] = collectionName
allPgIndexes[index.PgIndex] = collectionName
}
b, err := sjson.Marshal(c.marshal())
if err != nil {
return lazyerrors.Error(err)
}
arg, err := sjson.MarshalSingleValue(collectionName)
if err != nil {
return lazyerrors.Error(err)
}
q := fmt.Sprintf(
`UPDATE %s SET %s = $1 WHERE %s = $2`,
pgx.Identifier{dbName, metadataTableName}.Sanitize(),
DefaultColumn,
IDColumn,
)
if _, err := p.Exec(ctx, q, string(b), arg); err != nil {
return lazyerrors.Error(err)
}
r.colls[dbName][collectionName] = c
return nil
}
// IndexesDrop removes given connection's indexes.
//
// Non-existing indexes are ignored.
//
// If database or collection does not exist, nil is returned.
//
// If the user is not authenticated, it returns error.
func (r *Registry) IndexesDrop(ctx context.Context, dbName, collectionName string, indexNames []string) error {
defer observability.FuncCall(ctx)()
p, err := r.getPool(ctx)
if err != nil {
return lazyerrors.Error(err)
}
r.rw.Lock()
defer r.rw.Unlock()
return r.indexesDrop(ctx, p, dbName, collectionName, indexNames)
}
// indexesDrop removes given connection's indexes.
//
// Non-existing indexes are ignored.
//
// If database or collection does not exist, nil is returned.
//
// It does not hold the lock.
func (r *Registry) indexesDrop(ctx context.Context, p *pgxpool.Pool, dbName, collectionName string, indexNames []string) error {
defer observability.FuncCall(ctx)()
c := r.collectionGet(dbName, collectionName)
if c == nil {
return nil
}
for _, name := range indexNames {
i := slices.IndexFunc(c.Indexes, func(i IndexInfo) bool { return name == i.Name })
if i < 0 {
continue
}
q := fmt.Sprintf("DROP INDEX %s", pgx.Identifier{dbName, c.Indexes[i].PgIndex}.Sanitize())
if _, err := p.Exec(ctx, q); err != nil {
return lazyerrors.Error(err)
}
c.Indexes = slices.Delete(c.Indexes, i, i+1)
}
b, err := sjson.Marshal(c.marshal())
if err != nil {
return lazyerrors.Error(err)
}
arg, err := sjson.MarshalSingleValue(collectionName)
if err != nil {
return lazyerrors.Error(err)
}
q := fmt.Sprintf(
`UPDATE %s SET %s = $1 WHERE %s = $2`,
pgx.Identifier{dbName, metadataTableName}.Sanitize(),
DefaultColumn,
IDColumn,
)
if _, err := p.Exec(ctx, q, string(b), arg); err != nil {
return lazyerrors.Error(err)
}
r.colls[dbName][collectionName] = c
return nil
}
// quoteString returns a string that is safe to use in SQL queries.
//
// Deprecated: Warning! Avoid using this function unless there is no other way.
// Ideally, use a placeholder and pass the value as a parameter instead of calling this function.
//
// See https://github.com/jackc/pgx/blob/v5.5.0/internal/sanitize/sanitize.go#L90-L92
func quoteString(str string) string {
// We need "standard_conforming_strings=on" and "client_encoding=UTF8" (checked in checkConnection),