Commit 6fb155e5 authored by Steven Allen's avatar Steven Allen

fix putMany error handling and types

1. Use []byte instead of interface{}.
2. Handle rename errors.
3. Correctly temporary files.
parent 1d9a4fcf
......@@ -476,7 +476,7 @@ func (fs *Datastore) doPut(key datastore.Key, val []byte) error {
return nil
}
func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error {
func (fs *Datastore) putMany(data map[datastore.Key][]byte) error {
fs.shutdownLock.RLock()
defer fs.shutdownLock.RUnlock()
if fs.shutdown {
......@@ -484,13 +484,24 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error {
}
var dirsToSync []string
files := make(map[*os.File]*op)
for key, value := range data {
val, ok := value.([]byte)
if !ok {
return datastore.ErrInvalidType
files := make(map[*os.File]*op, len(data))
ops := make(map[*os.File]int, len(data))
defer func() {
for fi := range files {
val, _ := ops[fi]
switch val {
case 0:
_ = fi.Close()
fallthrough
case 1:
_ = os.Remove(fi.Name())
}
}
}()
for key, value := range data {
dir, path := fs.encode(key)
if err := fs.makeDirNoSync(dir); err != nil {
return err
......@@ -502,7 +513,7 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error {
return err
}
if _, err := tmp.Write(val); err != nil {
if _, err := tmp.Write(value); err != nil {
return err
}
......@@ -514,24 +525,9 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error {
}
}
ops := make(map[*os.File]int)
defer func() {
for fi, _ := range files {
val, _ := ops[fi]
switch val {
case 0:
_ = fi.Close()
fallthrough
case 1:
_ = os.Remove(fi.Name())
}
}
}()
// Now we sync everything
// sync and close files
for fi, _ := range files {
for fi := range files {
if fs.sync {
if err := syncFile(fi); err != nil {
return err
......@@ -548,7 +544,10 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error {
// move files to their proper places
for fi, op := range files {
fs.doWriteOp(op)
err := fs.doWriteOp(op)
if err != nil {
return err
}
// signify removed
ops[fi] = 2
}
......@@ -1063,7 +1062,7 @@ func (fs *Datastore) Close() error {
}
type flatfsBatch struct {
puts map[datastore.Key]interface{}
puts map[datastore.Key][]byte
deletes map[datastore.Key]struct{}
ds *Datastore
......@@ -1071,7 +1070,7 @@ type flatfsBatch struct {
func (fs *Datastore) Batch() (datastore.Batch, error) {
return &flatfsBatch{
puts: make(map[datastore.Key]interface{}),
puts: make(map[datastore.Key][]byte),
deletes: make(map[datastore.Key]struct{}),
ds: fs,
}, nil
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment