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
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
Next Next commit
WIP
  • Loading branch information
Dmitry committed May 16, 2023
commit f801b753f240fbdb1b32caa3f9d2c4abd4c9f89c
94 changes: 94 additions & 0 deletions internal/handlers/mdb/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package mdb

import (
"errors"
"sync"

"github.com/FerretDB/FerretDB/internal/handlers/sjson"
"github.com/FerretDB/FerretDB/internal/types"
)

var (
ErrCollectionNotFound = errors.New("collection not found")
ErrDatabaseNotFound = errors.New("database not found")
ErrMalformedMetadata = errors.New("malformed metadata")
)

type MetadataStorage interface {
CreateCollection(name string) error

GetDatabasesList() ([]byte, error)

RemoveCollection(database, collection string) error
RemoveDatabase(database string) error
}

type Metadata struct {
storage MetadataStorage

mx sync.Mutex
dbs *types.Document
}

func (m *Metadata) GetDatabasesList() ([]string, error) {
m.mx.Lock()
defer m.mx.Unlock()

// short path
if m.dbs != nil {
return m.dbs.Keys(), nil
}

list, err := m.storage.GetDatabasesList()
if err != nil {
return nil, err
}

dbs, err := sjson.Unmarshal(list)
if err != nil {
return nil, err
}

m.dbs = dbs

return m.dbs.Keys(), nil
}

func (m *Metadata) GetCollectionsList(database string) ([]string, error) {
m.mx.Lock()
defer m.mx.Unlock()

if !m.dbs.Has(database) {
return nil, ErrDatabaseNotFound
}

dbMetadataDoc, err := m.dbs.Get(database)
if err != nil {
return nil, err
}

dbMetadata, ok := dbMetadataDoc.(*types.Document)
if !ok {
return nil, ErrMalformedMetadata
}

collMetadataDoc, err := dbMetadata.Get("collections")
if err != nil {
return nil, err
}

collMetadata, ok := collMetadataDoc.(*types.Document)
if !ok {
return nil, ErrMalformedMetadata
}

return collMetadata.Keys(), nil
}

func (m *Metadata) RemoveCollection(database, collection string) error {
return nil
}

func (m *Metadata) RemoveDatabase(database string) error {
return nil
}