datastore.go 816 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
	"fmt"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
5

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

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

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

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

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

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