File: store.go

package info (click to toggle)
golang-github-lunny-nodb 0.0~git20160621.0.fc1ef06-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 424 kB
  • sloc: python: 259; sh: 50; makefile: 3
file content (46 lines) | stat: -rw-r--r-- 734 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package driver

import (
	"fmt"

	"github.com/lunny/nodb/config"
)

type Store interface {
	String() string
	Open(path string, cfg *config.Config) (IDB, error)
	Repair(path string, cfg *config.Config) error
}

var dbs = map[string]Store{}

func Register(s Store) {
	name := s.String()
	if _, ok := dbs[name]; ok {
		panic(fmt.Errorf("store %s is registered", s))
	}

	dbs[name] = s
}

func ListStores() []string {
	s := []string{}
	for k, _ := range dbs {
		s = append(s, k)
	}

	return s
}

func GetStore(cfg *config.Config) (Store, error) {
	if len(cfg.DBName) == 0 {
		cfg.DBName = config.DefaultDBName
	}

	s, ok := dbs[cfg.DBName]
	if !ok {
		return nil, fmt.Errorf("store %s is not registered", cfg.DBName)
	}

	return s, nil
}