Merge pull request #1 from nanobox-io/fix/cleanup

Fix/cleanup
This commit is contained in:
Steve Domino 2015-10-07 11:50:37 -06:00
commit cf862b12ad
1 changed files with 20 additions and 31 deletions

View File

@ -99,6 +99,7 @@ func (d *Driver) write(trans Transaction) error {
mutex := d.getOrCreateMutex(trans.Collection) mutex := d.getOrCreateMutex(trans.Collection)
mutex.Lock() mutex.Lock()
defer mutex.Unlock()
// //
dir := d.dir + "/" + trans.Collection dir := d.dir + "/" + trans.Collection
@ -114,14 +115,16 @@ func (d *Driver) write(trans Transaction) error {
return err return err
} }
finalPath := dir + "/" + trans.ResourceID + ".json"
tmpPath := finalPath + "~"
// write marshaled data to a file, named by the resourceID // write marshaled data to a file, named by the resourceID
if err := ioutil.WriteFile(dir+"/"+trans.ResourceID, b, 0666); err != nil { if err := ioutil.WriteFile(tmpPath, b, 0666); err != nil {
return err return err
} }
mutex.Unlock() // move final file into place
return os.Rename(tmpPath, finalPath)
return nil
} }
// read does the opposite operation as write. Reading a record from the database // read does the opposite operation as write. Reading a record from the database
@ -138,11 +141,7 @@ func (d *Driver) read(trans Transaction) error {
} }
// unmarshal data into the transaction.Container // unmarshal data into the transaction.Container
if err := json.Unmarshal(b, trans.Container); err != nil { return json.Unmarshal(b, trans.Container)
return err
}
return nil
} }
// readAll does the same operation as read, reading all the records in the specified // readAll does the same operation as read, reading all the records in the specified
@ -174,11 +173,7 @@ func (d *Driver) readAll(trans Transaction) error {
} }
// unmarhsal the read files as a comma delimeted byte array // unmarhsal the read files as a comma delimeted byte array
if err := json.Unmarshal([]byte("["+strings.Join(f, ",")+"]"), trans.Container); err != nil { return json.Unmarshal([]byte("["+strings.Join(f, ",")+"]"), trans.Container)
return err
}
return nil
} }
// delete locks that database and then proceeds to remove the record (specified by // delete locks that database and then proceeds to remove the record (specified by
@ -187,18 +182,12 @@ func (d *Driver) delete(trans Transaction) error {
mutex := d.getOrCreateMutex(trans.Collection) mutex := d.getOrCreateMutex(trans.Collection)
mutex.Lock() mutex.Lock()
defer mutex.Unlock()
dir := d.dir + "/" + trans.Collection dir := d.dir + "/" + trans.Collection
// remove record from database // remove record from database
err := os.Remove(dir + "/" + trans.ResourceID) return os.Remove(dir + "/" + trans.ResourceID)
if err != nil {
return err
}
mutex.Unlock()
return nil
} }
// helpers // helpers
@ -211,8 +200,8 @@ func (d *Driver) getOrCreateMutex(collection string) sync.Mutex {
// if the mutex doesn't exist make it // if the mutex doesn't exist make it
if !ok { if !ok {
d.mutexes[collection] = sync.Mutex{} c = sync.Mutex{}
return d.mutexes[collection] d.mutexes[collection] = c
} }
return c return c
@ -220,17 +209,17 @@ func (d *Driver) getOrCreateMutex(collection string) sync.Mutex {
// mkDir is a simple wrapper that attempts to make a directory at a specified // mkDir is a simple wrapper that attempts to make a directory at a specified
// location // location
func mkDir(d string) error { func mkDir(d string) (err error) {
// //
dir, _ := os.Stat(d) dir, _ := os.Stat(d)
if dir == nil { switch {
err := os.MkdirAll(d, 0755) case dir == nil:
if err != nil { err = os.MkdirAll(d, 0755)
return err case !dir.IsDir():
} err = os.ErrInvalid
} }
return nil return
} }