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
Merge branch 'main' into implement-metadata-storage
# Conflicts:
#	internal/backends/sqlite/backend.go
#	internal/backends/sqlite/database.go
#	internal/handlers/sqlite/msg_ismaster.go
#	internal/handlers/sqlite/msg_listcollections.go
#	internal/handlers/sqlite/msg_listdatabases.go
  • Loading branch information
Dmitry committed May 17, 2023
commit 83d508b544212f47405e7912d747eb17a5e57d1e
4 changes: 2 additions & 2 deletions internal/backends/sqlite/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func NewBackend(params *NewBackendParams) (backends.Backend, error) {
}

// Database implements backends.Backend interface.
func (b *backend) Database(ctx context.Context, params *backends.DatabaseParams) backends.Database {
return newDatabase(params.Name, b)
func (b *backend) Database(name string) backends.Database {
return newDatabase(b, name)
}

// ListDatabases implements backends.Backend interface.
Expand Down
5 changes: 2 additions & 3 deletions internal/backends/sqlite/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ import (

// database implements backends.Database interface.
type database struct {
b *backend
name string

b *backend
}

// newDatabase creates a new Database.
func newDatabase(name string, b *backend) backends.Database {
func newDatabase(b *backend, name string) backends.Database {
return backends.DatabaseContract(&database{
b: b,
name: name,
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/sqlite/msg_ismaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (h *Handler) MsgIsMaster(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg
"maxWriteBatchSize", int32(100000),
"localTime", time.Now(),
// logicalSessionTimeoutMinutes
// connectionId
"connectionId", int32(42),
"minWireVersion", common.MinWireVersion,
"maxWireVersion", common.MaxWireVersion,
"readOnly", false,
Expand Down
40 changes: 35 additions & 5 deletions internal/handlers/sqlite/msg_listcollections.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package sqlite
import (
"context"

"github.com/FerretDB/FerretDB/internal/backends"
"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commonparams"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/must"
Expand All @@ -32,24 +32,54 @@ func (h *Handler) MsgListCollections(ctx context.Context, msg *wire.OpMsg) (*wir
return nil, lazyerrors.Error(err)
}

var filter *types.Document
if filter, err = common.GetOptionalParam(document, "filter", filter); err != nil {
return nil, err
}

common.Ignored(document, h.L, "comment", "authorizedCollections")

db, err := common.GetRequiredParam[string](document, "$db")
if err != nil {
return nil, err
}

result, err := h.b.Database(ctx, &backends.DatabaseParams{Name: db}).ListCollections(ctx, nil)
var nameOnly bool

if v, _ := document.Get("nameOnly"); v != nil {
if nameOnly, err = commonparams.GetBoolOptionalParam("nameOnly", v); err != nil {
return nil, err
}
}

res, err := h.b.Database(db).ListCollections(ctx, nil)
if err != nil {
return nil, lazyerrors.Error(err)
}

collections := types.MakeArray(len(result.Collections))
collections := types.MakeArray(len(res.Collections))

for _, col := range result.Collections {
for _, collection := range res.Collections {
d := must.NotFail(types.NewDocument(
"name", col.Name,
"name", collection.Name,
"type", "collection",
))

matches, err := common.FilterDocument(d, filter)
if err != nil {
return nil, lazyerrors.Error(err)
}

if !matches {
continue
}

if nameOnly {
d = must.NotFail(types.NewDocument(
"name", collection.Name,
))
}

collections.Append(d)
}

Expand Down
80 changes: 66 additions & 14 deletions internal/handlers/sqlite/msg_listdatabases.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package sqlite
import (
"context"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commonparams"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/must"
Expand All @@ -25,31 +27,81 @@ import (

// MsgListDatabases implements HandlerInterface.
func (h *Handler) MsgListDatabases(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
response, err := h.b.ListDatabases(ctx, nil)
document, err := msg.Document()
if err != nil {
return nil, lazyerrors.Error(err)
}

databases := types.MakeArray(len(response.Databases))
var filter *types.Document
if filter, err = common.GetOptionalParam(document, "filter", filter); err != nil {
return nil, err
}

common.Ignored(document, h.L, "comment", "authorizedDatabases")

var nameOnly bool

if v, _ := document.Get("nameOnly"); v != nil {
if nameOnly, err = commonparams.GetBoolOptionalParam("nameOnly", v); err != nil {
return nil, err
}
}

res, err := h.b.ListDatabases(ctx, nil)
if err != nil {
return nil, lazyerrors.Error(err)
}

var totalSize int64
var databases *types.Array

for _, db := range response.Databases {
databases.Append(types.NewDocument(
for _, db := range res.Databases {
d := must.NotFail(types.NewDocument(
"name", db.Name,
"sizeOnDisk", int64(0),
"empty", false,
"sizeOnDisk", db.Size,
"empty", db.Size == 0,
))

totalSize += db.Size

matches, err := common.FilterDocument(d, filter)
if err != nil {
return nil, lazyerrors.Error(err)
}

if !matches {
continue
}

if nameOnly {
d = must.NotFail(types.NewDocument(
"name", db.Name,
))
}

databases.Append(d)
}

var reply wire.OpMsg

must.NoError(reply.SetSections(wire.OpMsgSection{
Documents: []*types.Document{must.NotFail(types.NewDocument(
"databases", databases,
"totalSize", int64(0),
"totalSizeMb", int64(0),
"ok", float64(1),
))},
}))
switch {
case nameOnly:
must.NoError(reply.SetSections(wire.OpMsgSection{
Documents: []*types.Document{must.NotFail(types.NewDocument(
"databases", databases,
"ok", float64(1),
))},
}))
default:
must.NoError(reply.SetSections(wire.OpMsgSection{
Documents: []*types.Document{must.NotFail(types.NewDocument(
"databases", databases,
"totalSize", totalSize,
"totalSizeMb", totalSize/1024/1024,
"ok", float64(1),
))},
}))
}

return &reply, nil
}
You are viewing a condensed version of this merge commit. You can view the full changes here.