add metastore, switching sequence to use it

metastore is for storing metadata. This will be used later when moving
to distributed master mode.
This commit is contained in:
Chris Lu
2013-11-05 01:59:00 -08:00
parent b579451db9
commit 5cb6590eae
5 changed files with 164 additions and 19 deletions

View File

@@ -0,0 +1,30 @@
package metastore
import (
"testing"
)
func TestMemoryBacking(t *testing.T) {
ms := &MetaStore{NewMetaStoreMemoryBacking()}
verifySetGet(t, ms)
}
func TestFileBacking(t *testing.T) {
ms := &MetaStore{NewMetaStoreFileBacking()}
verifySetGet(t, ms)
}
func verifySetGet(t *testing.T, ms *MetaStore) {
data := uint64(234234)
ms.SetUint64(data, "/tmp", "sequence")
if !ms.Has("/tmp", "sequence") {
t.Errorf("Failed to set data")
}
if val, err := ms.GetUint64("/tmp", "sequence"); err == nil {
if val != data {
t.Errorf("Set %d, but read back %d", data, val)
}
} else {
t.Errorf("Failed to get back data:%s", err)
}
}

View File

@@ -0,0 +1,34 @@
package metastore
import (
"io/ioutil"
"os"
"path"
)
// store data on disk, enough for most cases
type MetaStoreFileBacking struct {
}
func NewMetaStoreFileBacking() MetaStoreFileBacking {
mms := MetaStoreFileBacking{}
return mms
}
func (mms MetaStoreFileBacking) Set(val []byte, elem ...string) error {
return ioutil.WriteFile(path.Join(elem...), val, 0644)
}
func (mms MetaStoreFileBacking) Get(elem ...string) (val []byte, err error) {
return ioutil.ReadFile(path.Join(elem...))
}
func (mms MetaStoreFileBacking) Has(elem ...string) (ok bool) {
seqFile, se := os.OpenFile(path.Join(elem...), os.O_RDONLY, 0644)
if se != nil {
return false
}
defer seqFile.Close()
return true
}

View File

@@ -0,0 +1,37 @@
package metastore
import (
"fmt"
"path"
)
//this is for testing only
type MetaStoreMemoryBacking struct {
m map[string][]byte
}
func NewMetaStoreMemoryBacking() MetaStoreMemoryBacking {
mms := MetaStoreMemoryBacking{}
mms.m = make(map[string][]byte)
return mms
}
func (mms MetaStoreMemoryBacking) Set(val []byte, elem ...string) error {
mms.m[path.Join(elem...)] = val
return nil
}
func (mms MetaStoreMemoryBacking) Get(elem ...string) (val []byte, err error) {
var ok bool
val, ok = mms.m[path.Join(elem...)]
if !ok {
return nil, fmt.Errorf("Missing value for %s", path.Join(elem...))
}
return
}
func (mms MetaStoreMemoryBacking) Has(elem ...string) (ok bool) {
_, ok = mms.m[path.Join(elem...)]
return
}

35
go/metastore/metastore.go Normal file
View File

@@ -0,0 +1,35 @@
package metastore
import (
"code.google.com/p/weed-fs/go/util"
"errors"
"path"
)
type MetaStoreBacking interface {
Get(elem ...string) ([]byte, error)
Set(val []byte, elem ...string) error
Has(elem ...string) bool
}
type MetaStore struct {
MetaStoreBacking
}
func (m *MetaStore) SetUint64(val uint64, elem ...string) error {
b := make([]byte, 8)
util.Uint64toBytes(b, val)
return m.Set(b, elem...)
}
func (m *MetaStore) GetUint64(elem ...string) (val uint64, err error) {
if b, e := m.Get(elem...); e == nil && len(b) == 8 {
val = util.BytesToUint64(b)
} else {
if e != nil {
return 0, e
}
err = errors.New("Not found value for " + path.Join(elem...))
}
return
}