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 { ...@@ -476,7 +476,7 @@ func (fs *Datastore) doPut(key datastore.Key, val []byte) error {
return nil 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() fs.shutdownLock.RLock()
defer fs.shutdownLock.RUnlock() defer fs.shutdownLock.RUnlock()
if fs.shutdown { if fs.shutdown {
...@@ -484,13 +484,24 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error { ...@@ -484,13 +484,24 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error {
} }
var dirsToSync []string var dirsToSync []string
files := make(map[*os.File]*op)
for key, value := range data { files := make(map[*os.File]*op, len(data))
val, ok := value.([]byte) ops := make(map[*os.File]int, len(data))
if !ok {
return datastore.ErrInvalidType 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) dir, path := fs.encode(key)
if err := fs.makeDirNoSync(dir); err != nil { if err := fs.makeDirNoSync(dir); err != nil {
return err return err
...@@ -502,7 +513,7 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error { ...@@ -502,7 +513,7 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error {
return err return err
} }
if _, err := tmp.Write(val); err != nil { if _, err := tmp.Write(value); err != nil {
return err return err
} }
...@@ -514,24 +525,9 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error { ...@@ -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 // Now we sync everything
// sync and close files // sync and close files
for fi, _ := range files { for fi := range files {
if fs.sync { if fs.sync {
if err := syncFile(fi); err != nil { if err := syncFile(fi); err != nil {
return err return err
...@@ -548,7 +544,10 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error { ...@@ -548,7 +544,10 @@ func (fs *Datastore) putMany(data map[datastore.Key]interface{}) error {
// move files to their proper places // move files to their proper places
for fi, op := range files { for fi, op := range files {
fs.doWriteOp(op) err := fs.doWriteOp(op)
if err != nil {
return err
}
// signify removed // signify removed
ops[fi] = 2 ops[fi] = 2
} }
...@@ -1063,7 +1062,7 @@ func (fs *Datastore) Close() error { ...@@ -1063,7 +1062,7 @@ func (fs *Datastore) Close() error {
} }
type flatfsBatch struct { type flatfsBatch struct {
puts map[datastore.Key]interface{} puts map[datastore.Key][]byte
deletes map[datastore.Key]struct{} deletes map[datastore.Key]struct{}
ds *Datastore ds *Datastore
...@@ -1071,7 +1070,7 @@ type flatfsBatch struct { ...@@ -1071,7 +1070,7 @@ type flatfsBatch struct {
func (fs *Datastore) Batch() (datastore.Batch, error) { func (fs *Datastore) Batch() (datastore.Batch, error) {
return &flatfsBatch{ return &flatfsBatch{
puts: make(map[datastore.Key]interface{}), puts: make(map[datastore.Key][]byte),
deletes: make(map[datastore.Key]struct{}), deletes: make(map[datastore.Key]struct{}),
ds: fs, ds: fs,
}, nil }, 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