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

Add new PostgreSQL backend stub #3251

Merged
merged 6 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions build/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
// The following Go build tags (also known as build constraints) affect all builds of FerretDB,
// including embedded usage:
//
// ferretdb_debug - enables debug build (see below; implied by builds with race detector)
// ferretdb_hana - enables Hana backend handler (alpha)
// ferretdb_debug - enables debug build (see below; implied by builds with race detector)
// ferretdb_hana - enables Hana backend (alpha)
// ferretdb_newpg - replaces PostgreSQL backend with a new version (alpha)
//
// # Debug builds
//
Expand Down
76 changes: 76 additions & 0 deletions internal/backends/postgresql/backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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 postgresql

import (
"context"

"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"

"github.com/FerretDB/FerretDB/internal/backends"
)

// backend implements backends.Backend interface.
type backend struct{}

// NewBackendParams represents the parameters of NewBackend function.
//
//nolint:vet // for readability
type NewBackendParams struct {
URI string
L *zap.Logger
}

// NewBackend creates a new backend for PostgreSQL-compatible database.
func NewBackend(params *NewBackendParams) (backends.Backend, error) {
return backends.BackendContract(new(backend)), nil
}

// Close implements backends.Backend interface.
func (b *backend) Close() {
}

// Database implements backends.Backend interface.
func (b *backend) Database(name string) (backends.Database, error) {
return newDatabase(name), nil
}

// ListDatabases implements backends.Backend interface.
//
//nolint:lll // for readability
func (b *backend) ListDatabases(ctx context.Context, params *backends.ListDatabasesParams) (*backends.ListDatabasesResult, error) {
panic("not implemented")
}

// DropDatabase implements backends.Backend interface.
func (b *backend) DropDatabase(ctx context.Context, params *backends.DropDatabaseParams) error {
panic("not implemented")
}

// Describe implements prometheus.Collector.
func (b *backend) Describe(ch chan<- *prometheus.Desc) {
panic("not implemented")
}

// Collect implements prometheus.Collector.
func (b *backend) Collect(ch chan<- prometheus.Metric) {
panic("not implemented")
}

// check interfaces
var (
_ backends.Backend = (*backend)(nil)
)
65 changes: 65 additions & 0 deletions internal/backends/postgresql/collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 postgresql

import (
"context"

"github.com/FerretDB/FerretDB/internal/backends"
)

// collection implements backends.Collection interface.
type collection struct {
dbName string
name string
}

// newCollection creates a new Collection.
func newCollection(dbName, name string) backends.Collection {
return backends.CollectionContract(&collection{
dbName: dbName,
name: name,
})
}

// Query implements backends.Collection interface.
func (c *collection) Query(ctx context.Context, params *backends.QueryParams) (*backends.QueryResult, error) {
panic("not implemented")
}

// Insert implements backends.Collection interface.
func (c *collection) Insert(ctx context.Context, params *backends.InsertParams) (*backends.InsertResult, error) {
panic("not implemented")
}

// Update implements backends.Collection interface.
func (c *collection) Update(ctx context.Context, params *backends.UpdateParams) (*backends.UpdateResult, error) {
panic("not implemented")
}

// Delete implements backends.Collection interface.
func (c *collection) Delete(ctx context.Context, params *backends.DeleteParams) (*backends.DeleteResult, error) {
panic("not implemented")
}

// Explain implements backends.Collection interface.
func (c *collection) Explain(ctx context.Context, params *backends.ExplainParams) (*backends.ExplainResult, error) {
panic("not implemented")
}

// check interfaces
var (
_ backends.Collection = (*collection)(nil)
)
70 changes: 70 additions & 0 deletions internal/backends/postgresql/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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 postgresql

import (
"context"

"github.com/FerretDB/FerretDB/internal/backends"
)

// database implements backends.Database interface.
type database struct {
name string
}

// newDatabase creates a new Database.
func newDatabase(name string) backends.Database {
return backends.DatabaseContract(&database{
name: name,
})
}

// Close implements backends.Database interface.
func (db *database) Close() {
// nothing
}

// Collection implements backends.Database interface.
func (db *database) Collection(name string) (backends.Collection, error) {
return newCollection(db.name, name), nil
}

// ListCollections implements backends.Database interface.
//
//nolint:lll // for readability
func (db *database) ListCollections(ctx context.Context, params *backends.ListCollectionsParams) (*backends.ListCollectionsResult, error) {
panic("not implemented")
}

// CreateCollection implements backends.Database interface.
func (db *database) CreateCollection(ctx context.Context, params *backends.CreateCollectionParams) error {
panic("not implemented")
}

// DropCollection implements backends.Database interface.
func (db *database) DropCollection(ctx context.Context, params *backends.DropCollectionParams) error {
panic("not implemented")
}

// RenameCollection implements backends.Database interface.
func (db *database) RenameCollection(ctx context.Context, params *backends.RenameCollectionParams) error {
panic("not implemented")
}

// check interfaces
var (
_ backends.Database = (*database)(nil)
)
16 changes: 16 additions & 0 deletions internal/backends/postgresql/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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 postgresql provides backend for PostgreSQL and compatible databases.
package postgresql
2 changes: 2 additions & 0 deletions internal/backends/sqlite/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type backend struct {
}

// NewBackendParams represents the parameters of NewBackend function.
//
//nolint:vet // for readability
type NewBackendParams struct {
URI string
L *zap.Logger
Expand Down
42 changes: 42 additions & 0 deletions internal/handlers/registry/newpg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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.

//go:build ferretdb_newpg

package registry

import (
"github.com/FerretDB/FerretDB/internal/handlers"
"github.com/FerretDB/FerretDB/internal/handlers/sqlite"
)

// init registers new "pg" handler.
func init() {
registry["pg"] = func(opts *NewHandlerOpts) (handlers.Interface, error) {
opts.Logger.Warn("New PostgreSQL backend is in alpha.")

handlerOpts := &sqlite.NewOpts{
Backend: "postgresql",
URI: opts.PostgreSQLURL,

L: opts.Logger.Named("postgresql"),
ConnMetrics: opts.ConnMetrics,
StateProvider: opts.StateProvider,

DisableFilterPushdown: opts.DisableFilterPushdown,
}

return sqlite.New(handlerOpts)
}
}
4 changes: 3 additions & 1 deletion internal/handlers/registry/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !ferretdb_newpg

package registry

import (
"github.com/FerretDB/FerretDB/internal/handlers"
"github.com/FerretDB/FerretDB/internal/handlers/pg"
)

// init registers "pg" handler.
// init registers old "pg" handler.
func init() {
registry["pg"] = func(opts *NewHandlerOpts) (handlers.Interface, error) {
handlerOpts := &pg.NewOpts{
Expand Down
4 changes: 4 additions & 0 deletions internal/handlers/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,9 @@ func Handlers() []string {
res = append(res, h)
}

if len(res) != len(registry) {
panic("registry is not in sync")
}

return res
}
5 changes: 3 additions & 2 deletions internal/handlers/registry/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ import (
// init registers "sqlite" handler.
func init() {
registry["sqlite"] = func(opts *NewHandlerOpts) (handlers.Interface, error) {
opts.Logger.Warn("SQLite handler is in beta.")
opts.Logger.Warn("SQLite backend is in beta.")

handlerOpts := &sqlite.NewOpts{
URI: opts.SQLiteURL,
Backend: "sqlite",
URI: opts.SQLiteURL,

L: opts.Logger.Named("sqlite"),
ConnMetrics: opts.ConnMetrics,
Expand Down
26 changes: 21 additions & 5 deletions internal/handlers/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"go.uber.org/zap"

"github.com/FerretDB/FerretDB/internal/backends"
"github.com/FerretDB/FerretDB/internal/backends/postgresql"
"github.com/FerretDB/FerretDB/internal/backends/sqlite"
"github.com/FerretDB/FerretDB/internal/clientconn/connmetrics"
"github.com/FerretDB/FerretDB/internal/clientconn/cursor"
Expand Down Expand Up @@ -51,7 +52,8 @@ type Handler struct {
//
//nolint:vet // for readability
type NewOpts struct {
URI string
Backend string
URI string

L *zap.Logger
ConnMetrics *connmetrics.ConnMetrics
Expand All @@ -63,10 +65,24 @@ type NewOpts struct {

// New returns a new handler.
func New(opts *NewOpts) (handlers.Interface, error) {
b, err := sqlite.NewBackend(&sqlite.NewBackendParams{
URI: opts.URI,
L: opts.L,
})
var b backends.Backend
var err error

switch opts.Backend {
case "postgresql":
b, err = postgresql.NewBackend(&postgresql.NewBackendParams{
URI: opts.URI,
L: opts.L,
})
case "sqlite":
b, err = sqlite.NewBackend(&sqlite.NewBackendParams{
URI: opts.URI,
L: opts.L,
})
default:
panic("unknown backend: " + opts.Backend)
}

if err != nil {
return nil, err
}
Expand Down