This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
virtcontainers: store: Add an internal backend interface
All Store backends will have to implement that simple interface. Signed-off-by: Samuel Ortiz <[email protected]>
- Loading branch information
Samuel Ortiz
committed
Feb 6, 2019
1 parent
6b87ecf
commit d22cdf2
Showing
3 changed files
with
136 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) 2019 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package store | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
type backendType string | ||
|
||
const ( | ||
filesystemBackend backendType = "filesystem" | ||
) | ||
|
||
const ( | ||
filesystemScheme string = "file" | ||
) | ||
|
||
func schemeToBackendType(scheme string) (backendType, error) { | ||
switch scheme { | ||
case filesystemScheme: | ||
return filesystemBackend, nil | ||
} | ||
|
||
return "", fmt.Errorf("Unsupported scheme %s", scheme) | ||
} | ||
|
||
func newBackend(scheme string) (backend, error) { | ||
t, err := schemeToBackendType(scheme) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch t { | ||
case filesystemBackend: | ||
return &filesystem{}, nil | ||
} | ||
|
||
return nil, fmt.Errorf("Unsupported scheme %s", scheme) | ||
} | ||
|
||
type backend interface { | ||
new(ctx context.Context, path string, host string) error | ||
load(item Item, data interface{}) error | ||
store(item Item, data interface{}) error | ||
} |
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,70 @@ | ||
// Copyright (c) 2019 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package store | ||
|
||
import ( | ||
"context" | ||
|
||
opentracing "github.com/opentracing/opentracing-go" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type filesystem struct { | ||
ctx context.Context | ||
|
||
path string | ||
} | ||
|
||
// Logger returns a logrus logger appropriate for logging Store filesystem messages | ||
func (f *filesystem) logger() *logrus.Entry { | ||
return storeLog.WithFields(logrus.Fields{ | ||
"subsystem": "store", | ||
"backend": "filesystem", | ||
"path": f.path, | ||
}) | ||
} | ||
|
||
func (f *filesystem) trace(name string) (opentracing.Span, context.Context) { | ||
if f.ctx == nil { | ||
f.logger().WithField("type", "bug").Error("trace called before context set") | ||
f.ctx = context.Background() | ||
} | ||
|
||
span, ctx := opentracing.StartSpanFromContext(f.ctx, name) | ||
|
||
span.SetTag("subsystem", "store") | ||
span.SetTag("type", "filesystem") | ||
span.SetTag("path", f.path) | ||
|
||
return span, ctx | ||
} | ||
|
||
func (f *filesystem) new(ctx context.Context, path string, host string) error { | ||
f.ctx = ctx | ||
f.path = path | ||
|
||
f.logger().Infof("New filesystem store backend for %s", path) | ||
|
||
return nil | ||
} | ||
|
||
func (f *filesystem) load(item Item, data interface{}) error { | ||
span, _ := f.trace("load") | ||
defer span.Finish() | ||
|
||
span.SetTag("item", item) | ||
|
||
return nil | ||
} | ||
|
||
func (f *filesystem) store(item Item, data interface{}) error { | ||
span, _ := f.trace("store") | ||
defer span.Finish() | ||
|
||
span.SetTag("item", item) | ||
|
||
return nil | ||
} |
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