This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 151
Storage Factory #1558
Merged
Merged
Storage Factory #1558
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5b144b7
go mod tidy
gdbelvin 64861eb
Storage factory
gdbelvin 18d90da
Use new storage factory
gdbelvin f768ba3
Fix mysql package name
gdbelvin c80bdef
Fix layering violation for docstrings
gdbelvin 0044a83
rename imports
gdbelvin 57055db
outdated comment
gdbelvin 4e6e349
Fix package doc string
gdbelvin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2020 Google Inc. All Rights Reserved. | ||
// | ||
// 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 impl | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"cloud.google.com/go/spanner" | ||
"github.com/google/keytransparency/core/directory" | ||
"github.com/google/keytransparency/core/sequencer" | ||
"github.com/google/keytransparency/core/water" | ||
"github.com/google/keytransparency/impl/mysql" | ||
|
||
pb "github.com/google/keytransparency/core/api/v1/keytransparency_go_proto" | ||
mysqldir "github.com/google/keytransparency/impl/mysql/directory" | ||
mysqlmutations "github.com/google/keytransparency/impl/mysql/mutationstorage" | ||
spanbatch "github.com/google/keytransparency/impl/spanner/batch" | ||
spandir "github.com/google/keytransparency/impl/spanner/directory" | ||
spanmutations "github.com/google/keytransparency/impl/spanner/mutations" | ||
) | ||
|
||
// Storage holds an abstract storage implementation | ||
type Storage struct { | ||
Directories directory.Storage | ||
Logs interface { | ||
sequencer.LogsReader | ||
|
||
// Copied methods from adminserver.LogsAdmin because of duplicate method. | ||
|
||
// AddLogs creates and adds new logs for writing to a directory. | ||
AddLogs(ctx context.Context, directoryID string, logIDs ...int64) error | ||
// SetWritable enables or disables new writes from going to logID. | ||
SetWritable(ctx context.Context, directoryID string, logID int64, enabled bool) error | ||
|
||
// Copied methods from keyserver.MutationLogs | ||
SendBatch(ctx context.Context, directoryID string, logID int64, batch []*pb.EntryUpdate) (water.Mark, error) | ||
} | ||
Batches sequencer.Batcher | ||
healthCheck func() error | ||
Close func() | ||
} | ||
|
||
// HealthCheck reports on the health of the underlying database connection. | ||
func (s *Storage) HealthCheck() error { return s.healthCheck() } | ||
|
||
// StorageEngines returns a list of supported storage engines. | ||
func StorageEngines() []string { return []string{"mysql", "spanner"} } | ||
|
||
// NewStorage returns a Storage with the requested engine. | ||
func NewStorage(ctx context.Context, engine, db string) (*Storage, error) { | ||
switch engine { | ||
case "mysql": | ||
return mysqlStorage(db) | ||
case "spanner": | ||
return spannerStorage(ctx, db) | ||
default: | ||
return nil, fmt.Errorf("unknown db engine %s", engine) | ||
} | ||
} | ||
|
||
func spannerStorage(ctx context.Context, db string) (*Storage, error) { | ||
spanClient, err := spanner.NewClient(ctx, db) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Storage{ | ||
Directories: spandir.New(spanClient), | ||
Batches: spanbatch.New(spanClient), | ||
Logs: spanmutations.New(spanClient), | ||
healthCheck: func() error { return nil }, | ||
Close: spanClient.Close, | ||
}, nil | ||
} | ||
|
||
func mysqlStorage(db string) (*Storage, error) { | ||
sqldb, err := mysql.Open(db) | ||
if err != nil { | ||
return nil, err | ||
} | ||
directories, err := mysqldir.NewStorage(sqldb) | ||
if err != nil { | ||
sqldb.Close() | ||
return nil, fmt.Errorf("failed to create directory storage: %w", err) | ||
} | ||
logs, err := mysqlmutations.New(sqldb) | ||
if err != nil { | ||
sqldb.Close() | ||
return nil, fmt.Errorf("failed to create mutations storage: %w", err) | ||
} | ||
return &Storage{ | ||
Directories: directories, | ||
Batches: logs, | ||
Logs: logs, | ||
healthCheck: sqldb.Ping, | ||
Close: func() { sqldb.Close() }, | ||
}, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you get a Go Native to review this? It looks a bit clunky to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit of a known issue: golang/go#6977
https://golang.org/ref/spec#Uniqueness_of_identifiers
I think these could be merged if the interfaces were in the same package, because they are in different packages, they are different identifiers.