Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
virtcontainers: store: Add an internal backend interface
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 2 deletions.
50 changes: 50 additions & 0 deletions virtcontainers/store/backend.go
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
}
70 changes: 70 additions & 0 deletions virtcontainers/store/filesystem.go
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
}
18 changes: 16 additions & 2 deletions virtcontainers/store/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ type Store struct {
scheme string
path string
host string

backend backend
}

type manager struct {
Expand Down Expand Up @@ -152,6 +154,18 @@ func New(ctx context.Context, storeURL string) (*Store, error) {
host: u.Host,
}

backend, err := newBackend(s.scheme)
if err != nil {
return nil, err
}

s.backend = backend

// Create new backend
if err := s.backend.new(ctx, s.path, s.host); err != nil {
return nil, err
}

if err := stores.addStore(s); err != nil {
return nil, err
}
Expand Down Expand Up @@ -193,7 +207,7 @@ func (s *Store) Load(item Item, data interface{}) error {
s.RLock()
defer s.RUnlock()

return nil
return s.backend.load(item, data)
}

// Store stores a virtcontainers item into a Store.
Expand All @@ -206,5 +220,5 @@ func (s *Store) Store(item Item, data interface{}) error {
s.Lock()
defer s.Unlock()

return nil
return s.backend.store(item, data)
}

0 comments on commit d22cdf2

Please sign in to comment.