Skip to content

Commit

Permalink
IndexDescriptionCreateRequest in more places; not yet in tests; DO NO…
Browse files Browse the repository at this point in the history
…T MERGE
  • Loading branch information
ChrisBQu committed Nov 26, 2024
1 parent 02c5c50 commit 2d5cb89
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 21 deletions.
6 changes: 3 additions & 3 deletions cli/index_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Example: create a unique index for 'Users' collection on 'name' in ascending ord
})
}

desc := client.IndexDescription{
desc := client.IndexDescriptionCreateRequest{
Name: nameArg,
Fields: fields,
Unique: uniqueArg,
Expand All @@ -79,11 +79,11 @@ Example: create a unique index for 'Users' collection on 'name' in ascending ord
return err
}

desc, err = col.CreateIndex(cmd.Context(), desc)
descWithID, err := col.CreateIndex(cmd.Context(), desc)
if err != nil {
return err
}
return writeJSON(cmd, desc)
return writeJSON(cmd, descWithID)
},
}
cmd.Flags().StringVarP(&collectionArg, "collection", "c", "", "Collection name")
Expand Down
2 changes: 1 addition & 1 deletion client/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ type Collection interface {
// only contain letters, numbers, and underscores.
// If the name of the index is not provided, it will be generated.
// WARNING: This method can not create index for a collection that has a policy.
CreateIndex(context.Context, IndexDescription) (IndexDescription, error)
CreateIndex(context.Context, IndexDescriptionCreateRequest) (IndexDescription, error)

// DropIndex drops an index from the collection.
DropIndex(ctx context.Context, indexName string) error
Expand Down
4 changes: 2 additions & 2 deletions client/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ type IndexDescription struct {
Unique bool
}

// IndexCreateRequestDescription describes an index creation request.
// IndexDescriptionCreateRequest describes an index creation request.
// It does not contain the ID, as it is not a valid field for the request body.
// Instead it should be automatically generated.
type IndexCreateRequestDescription struct {
type IndexDescriptionCreateRequest struct {
// Name contains the name of the index.
Name string
// Fields contains the fields that are being indexed.
Expand Down
2 changes: 1 addition & 1 deletion http/client_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func (c *Collection) GetAllDocIDs(

func (c *Collection) CreateIndex(
ctx context.Context,
indexDesc client.IndexDescription,
indexDesc client.IndexDescriptionCreateRequest,
) (client.IndexDescription, error) {
if !c.Description().Name.HasValue() {
return client.IndexDescription{}, client.ErrOperationNotPermittedOnNamelessCols
Expand Down
7 changes: 6 additions & 1 deletion http/handler_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,12 @@ func (s *collectionHandler) CreateIndex(rw http.ResponseWriter, req *http.Reques
responseJSON(rw, http.StatusBadRequest, errorResponse{err})
return
}
index, err := col.CreateIndex(req.Context(), indexDesc)
descWithoutID := client.IndexDescriptionCreateRequest{
Name: indexDesc.Name,
Fields: indexDesc.Fields,
Unique: indexDesc.Unique,
}
index, err := col.CreateIndex(req.Context(), descWithoutID)
if err != nil {
responseJSON(rw, http.StatusBadRequest, errorResponse{err})
return
Expand Down
2 changes: 1 addition & 1 deletion http/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var openApiSchemas = map[string]any{
"schema": &client.SchemaDescription{},
"collection_definition": &client.CollectionDefinition{},
"index": &client.IndexDescription{},
"index_create_request": &client.IndexCreateRequestDescription{},
"index_create_request": &client.IndexDescriptionCreateRequest{},
"delete_result": &client.DeleteResult{},
"update_result": &client.UpdateResult{},
"lens_config": &client.LensConfig{},
Expand Down
7 changes: 6 additions & 1 deletion internal/db/collection_define.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ func (db *db) createCollections(
col := db.newCollection(desc, def.Schema)

for _, index := range desc.Indexes {
if _, err := col.createIndex(ctx, index); err != nil {
descWithoutID := client.IndexDescriptionCreateRequest{
Name: index.Name,
Fields: index.Fields,
Unique: index.Unique,
}
if _, err := col.createIndex(ctx, descWithoutID); err != nil {
return nil, err
}
}
Expand Down
25 changes: 14 additions & 11 deletions internal/db/collection_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
func (db *db) createCollectionIndex(
ctx context.Context,
collectionName string,
desc client.IndexDescription,
desc client.IndexDescriptionCreateRequest,
) (client.IndexDescription, error) {
col, err := db.getCollectionByName(ctx, collectionName)
if err != nil {
Expand Down Expand Up @@ -218,7 +218,7 @@ func (c *collection) deleteIndexedDocWithID(
// the documents will be indexed by the new index.
func (c *collection) CreateIndex(
ctx context.Context,
desc client.IndexDescription,
desc client.IndexDescriptionCreateRequest,
) (client.IndexDescription, error) {
ctx, txn, err := ensureContextTxn(ctx, c.db, false)
if err != nil {
Expand All @@ -235,7 +235,7 @@ func (c *collection) CreateIndex(

func (c *collection) createIndex(
ctx context.Context,
desc client.IndexDescription,
desc client.IndexDescriptionCreateRequest,
) (CollectionIndex, error) {
if desc.Name != "" && !schema.IsValidIndexName(desc.Name) {
return nil, schema.NewErrIndexWithInvalidName("!")
Expand Down Expand Up @@ -266,9 +266,15 @@ func (c *collection) createIndex(
if err != nil {
return nil, err
}
desc.ID = uint32(colID)

buf, err := json.Marshal(desc)
descWithID := client.IndexDescription{
Name: desc.Name,
ID: uint32(colID),
Fields: desc.Fields,
Unique: desc.Unique,
}

buf, err := json.Marshal(descWithID)
if err != nil {
return nil, err
}
Expand All @@ -278,7 +284,7 @@ func (c *collection) createIndex(
if err != nil {
return nil, err
}
colIndex, err := NewCollectionIndex(c, desc)
colIndex, err := NewCollectionIndex(c, descWithID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -491,7 +497,7 @@ func (c *collection) checkExistingFieldsAndAdjustRelFieldNames(

func (c *collection) generateIndexNameIfNeededAndCreateKey(
ctx context.Context,
desc *client.IndexDescription,
desc *client.IndexDescriptionCreateRequest,
) (keys.CollectionIndexKey, error) {
// callers of this function must set a context transaction
txn := mustGetContextTxn(ctx)
Expand Down Expand Up @@ -524,10 +530,7 @@ func (c *collection) generateIndexNameIfNeededAndCreateKey(
return indexKey, nil
}

func validateIndexDescription(desc client.IndexDescription) error {
if desc.ID != 0 {
return NewErrNonZeroIndexIDProvided(desc.ID)
}
func validateIndexDescription(desc client.IndexDescriptionCreateRequest) error {
if len(desc.Fields) == 0 {
return ErrIndexMissingFields
}
Expand Down

0 comments on commit 2d5cb89

Please sign in to comment.