Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
Merge pull request #687 from mcastelino/db_cleanup
Browse files Browse the repository at this point in the history
Provide a generic database implementation.
  • Loading branch information
rbradford authored Oct 17, 2016
2 parents 6fd5661 + fc0f78b commit d5405e5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,62 @@ import (
"github.com/golang/glog"
)

const (
dbFile = "docker_plugin.db"
)
//DbTable interface that needs to be supported
//for the table to be handled by the database
type DbTable interface {
// Creates the backing map
NewTable()

// Name of the table as stored in the database
Name() string

// Allocates and returns a single value in the table
NewElement() interface{}

// Add an value to the in memory table
Add(k string, v interface{}) error
}

// A TableDBProvider represents a persistent database provider
// that can be used to store map, arrays or any other type of
// tables into a database
type TableDBProvider interface {
//Initializes the Database
DbInit(dir string, file string) error
//Populates the in-memory table from the database
DbTableRebuild(table DbTable) error
//Closes the database
DbClose() error
//Creates the tables if the tables do not already exist in the database
DbTableInit(tables []string) error
//Adds the key value pair to the table
DbAdd(table string, key string, value interface{}) error
//Adds the key value pair to the table
DbDelete(table string, key string) error
//Retrives the value corresponding to the key from the table
DbGet(table string, key string) (interface{}, error)
}

type dockerBoltDB struct {
type tableBoltDB struct {
Name string
DB *bolt.DB
}

func newDockerBoltDb() *dockerBoltDB {
return &dockerBoltDB{
Name: "docker_bolt.DB",
func newTableBoltDb() *tableBoltDB {
return &tableBoltDB{
Name: "tableBolt.DB",
}
}

type dbProvider dockerBoltDB
type dbProvider tableBoltDB

//NewDockerBoltDBProvider returns a bolt based database that conforms
//to the DockerDBProvider interface
func NewDockerBoltDBProvider() DockerDBProvider {
return (*dbProvider)(newDockerBoltDb())
//NewTableBoltDBProvider returns a bolt based database that conforms
//to the tableDBProvider interface
func NewTableBoltDBProvider() TableDBProvider {
return (*dbProvider)(newTableBoltDb())
}

func (db *dbProvider) DbInit(dbDir string) error {
func (db *dbProvider) DbInit(dbDir string, dbFile string) error {

if err := os.MkdirAll(dbDir, 0755); err != nil {
return fmt.Errorf("Unable to create db directory (%s) %v", dbDir, err)
Expand Down
43 changes: 5 additions & 38 deletions networking/libsnnet/docker_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,50 +128,17 @@ var DockerPluginCfg = struct {
Dir string
Addr string
DataDir string
DbFile string
Timeout time.Duration
}{
Name: "ciao",
Dir: "/etc/docker/plugins",
Addr: "127.0.0.1:9999",
DataDir: "/var/lib/ciao/networking",
DbFile: "docker_plugin.db",
Timeout: 1 * time.Second,
}

//DbTable interface that needs to be supported
//for the table to be handled by the database
type DbTable interface {
// Creates the backing map
NewTable()

// Name of the table as stored in the database
Name() string

// Allocates and returns a single value in the table
NewElement() interface{}

// Add an value to the in memory table
Add(k string, v interface{}) error
}

// A DockerDBProvider represents a persistent data base provider
// that can be used by the DockerPlugin to store its internal state
type DockerDBProvider interface {
//Initializes the Database
DbInit(dir string) error
//Populates the DockerPlugin cache from the database
DbTableRebuild(table DbTable) error
//Closes the database
DbClose() error
//Creates the tables if the tables do not already exist in the database
DbTableInit(tables []string) error
//Adds the key value pair to the table
DbAdd(table string, key string, value interface{}) error
//Adds the key value pair to the table
DbDelete(table string, key string) error
//Retrives the value corresponding to the key from the table
DbGet(table string, key string) (interface{}, error)
}

//DockerEpVal stores ciao VNIC info for a particular docker endpoint
type DockerEpVal struct {
ID string
Expand Down Expand Up @@ -256,7 +223,7 @@ func (d *DockerNwMap) Add(k string, v interface{}) error {
// DockerPlugin describes a single instance of a docker plugin
// In the current design the plugin acts as an IPAM and Network Plugin
type DockerPlugin struct {
DockerDBProvider //Database used to persist the Docker to ciao Mapping
TableDBProvider //Database used to persist the Docker to ciao Mapping
//This is needed as the Docker Daemon and ciao have
//different life cycles and UUIDs
*mux.Router
Expand Down Expand Up @@ -779,7 +746,7 @@ func DockerHandler(d *DockerPlugin,
//NewDockerPlugin instantiates a new Docker Plugin instance
func NewDockerPlugin() *DockerPlugin {
return &DockerPlugin{
DockerDBProvider: NewDockerBoltDBProvider(),
TableDBProvider: NewTableBoltDBProvider(),
}
}

Expand Down Expand Up @@ -819,7 +786,7 @@ func (d *DockerPlugin) Init() error {
return err
}

if err := d.DbInit(DockerPluginCfg.DataDir); err != nil {
if err := d.DbInit(DockerPluginCfg.DataDir, DockerPluginCfg.DbFile); err != nil {
return err
}
if err := d.DbTableRebuild(&d.DockerNwMap); err != nil {
Expand Down

0 comments on commit d5405e5

Please sign in to comment.