datastore.go 735 Bytes
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package core

import (
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
4 5 6 7
	"fmt"
	ds "github.com/jbenet/datastore.go"
	lds "github.com/jbenet/datastore.go/leveldb"
	config "github.com/jbenet/go-ipfs/config"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8 9 10
)

func makeDatastore(cfg *config.Datastore) (ds.Datastore, error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
11 12 13
	if cfg == nil || len(cfg.Type) == 0 {
		return nil, fmt.Errorf("config datastore.type required")
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
15 16 17 18 19 20
	switch cfg.Type {
	case "leveldb":
		return makeLevelDBDatastore(cfg)
	case "memory":
		return ds.NewMapDatastore(), nil
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
22
	return nil, fmt.Errorf("Unknown datastore type: %s", cfg.Type)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
23 24 25
}

func makeLevelDBDatastore(cfg *config.Datastore) (ds.Datastore, error) {
Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
26 27 28
	if len(cfg.Path) == 0 {
		return nil, fmt.Errorf("config datastore.path required for leveldb")
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29

Juan Batiz-Benet's avatar
go fmt  
Juan Batiz-Benet committed
30
	return lds.NewDatastore(cfg.Path, nil)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31
}