Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement metadata storage #2656

Merged
merged 41 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP
  • Loading branch information
Dmitry committed May 18, 2023
commit 6ba1f0040a3287d9e9e4de613d39c423f19e718a
15 changes: 13 additions & 2 deletions internal/backends/sqlite/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"

"github.com/FerretDB/FerretDB/internal/backends"
"github.com/FerretDB/FerretDB/internal/handlers/commonerrors"
"github.com/FerretDB/FerretDB/internal/handlers/sjson"
"github.com/FerretDB/FerretDB/internal/util/iterator"
)
Expand All @@ -40,7 +41,14 @@ func newCollection(db *database, name string) backends.Collection {

// Insert implements backends.Collection interface.
func (c *collection) Insert(ctx context.Context, params *backends.InsertParams) (*backends.InsertResult, error) {
conn, err := c.db.b.pool.DB(c.db.name)
path, err := c.db.b.metadataStorage.databasePath(c.db.name)
if errors.Is(err, errDatabaseNotFound) {
if err = c.db.create(ctx); err != nil {
return nil, err
}
}

conn, err := c.db.b.pool.DB(path)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -97,7 +105,10 @@ func (c *collection) Insert(ctx context.Context, params *backends.InsertParams)
return nil, err
}

return &backends.InsertResult{InsertedCount: inserted}, nil
return &backends.InsertResult{
InsertedCount: inserted,
Errors: &commonerrors.WriteErrors{},
}, nil
}

// check interfaces
Expand Down
24 changes: 18 additions & 6 deletions internal/backends/sqlite/metadata_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,21 @@ func (m *metadataStorage) createCollection(ctx context.Context, database, collec
return "", err
}

query := fmt.Sprintf("INSERT INTO %s (sjson) VALUES (?)", dbMetadataTableName)
tableQuery := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (sjson TEXT)", tableName)

bytes, err := sjson.Marshal(must.NotFail(types.NewDocument("collection", collection, "table", tableName)))
_, err = conn.ExecContext(ctx, tableQuery)
if err != nil {
return "", err
}

_, err = conn.ExecContext(ctx, query, bytes)
query := fmt.Sprintf("INSERT INTO %s (sjson) VALUES (?)", dbMetadataTableName)

bytes, err := sjson.Marshal(must.NotFail(types.NewDocument("collection", collection, "table", tableName)))
if err != nil {
return "", err
}

tableQuery := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (sjson TEXT)", tableName)

_, err = conn.ExecContext(ctx, tableQuery)
_, err = conn.ExecContext(ctx, query, bytes)
if err != nil {
return "", err
}
Expand All @@ -225,6 +225,18 @@ func (m *metadataStorage) createCollection(ctx context.Context, database, collec
return tableName, nil
}

func (m *metadataStorage) databasePath(database string) (string, error) {
m.mx.Lock()
defer m.mx.Unlock()

db, ok := m.dbs[database]
if !ok {
return "", errDatabaseNotFound
}

return db.path, nil
}

// collectionInfo returns table name for given database name and collection name.
func (m *metadataStorage) collectionInfo(dbName string, collName string) (string, error) {
m.mx.Lock()
Expand Down