Increment the next resource ID

This commit is contained in:
boosh 2017-07-24 21:20:03 +01:00
parent eabb4d6277
commit e21b74e6e5
2 changed files with 30 additions and 10 deletions

View File

@ -13,6 +13,7 @@ import (
"path" "path"
"strings" "strings"
"strconv" "strconv"
"log"
) )
// Version is the current version of the project // Version is the current version of the project
@ -103,10 +104,14 @@ func (d *Driver) Write(collection, resource string, v interface{}) error {
return d.writeFile(collection, resource, v) return d.writeFile(collection, resource, v)
} }
func getCollectionDir(dir string, collection string) string {
return filepath.Join(dir, collection)
}
// Writes a file to disk // Writes a file to disk
func (d *Driver) writeFile(collection string, resource string, v interface{}) error { func (d *Driver) writeFile(collection string, resource string, v interface{}) error {
// //
dir := filepath.Join(d.dir, collection) dir := getCollectionDir(d.dir, collection)
fnlPath := filepath.Join(dir, resource+".json") fnlPath := filepath.Join(dir, resource+".json")
tmpPath := fnlPath + ".tmp" tmpPath := fnlPath + ".tmp"
@ -144,13 +149,19 @@ func (d *Driver) WriteAutoId(collection string, v interface{}) (resourceId int64
mutex.Lock() mutex.Lock()
defer mutex.Unlock() defer mutex.Unlock()
dir := getCollectionDir(d.dir, collection)
// list the directory, sort it, take the last entry then parse and increment the last ID // list the directory, sort it, take the last entry then parse and increment the last ID
files, err := ioutil.ReadDir(d.dir) files, err := ioutil.ReadDir(dir)
if err != nil { if err != nil {
fmt.Printf("Error listing directory %s: %s", d.dir, err.Error()) if !os.IsNotExist(err) {
return resourceId, err fmt.Printf("Error listing directory %s: %s", dir, err.Error())
return resourceId, err
}
} }
log.Println("len", len(files))
if len(files) == 0 { if len(files) == 0 {
resourceId = 1 resourceId = 1
} else { } else {
@ -160,11 +171,13 @@ func (d *Driver) WriteAutoId(collection string, v interface{}) (resourceId int64
baseName := strings.TrimSuffix(lastFile.Name(), ext) baseName := strings.TrimSuffix(lastFile.Name(), ext)
resourceId, err := strconv.ParseInt(baseName, 10, 8) resourceId, err = strconv.ParseInt(baseName, 10, 8)
if err != nil { if err != nil {
fmt.Printf("Error parsing string '%s' as integer", baseName, err.Error()) fmt.Printf("Error parsing string '%s' as integer", baseName, err.Error())
return resourceId, err return resourceId, err
} }
resourceId = resourceId + 1
} }
stringResourceId := fmt.Sprintf("%d", resourceId) stringResourceId := fmt.Sprintf("%d", resourceId)

View File

@ -3,7 +3,6 @@ package scribble
import ( import (
"os" "os"
"testing" "testing"
"fmt"
) )
// //
@ -97,14 +96,22 @@ func TestWriteAutoIdAndRead(t *testing.T) {
t.Error("Create fish failed: ", err.Error()) t.Error("Create fish failed: ", err.Error())
} }
if id <= 0 { if id != 1 {
t.Error("Auto-generated ID should have been > 0") t.Error("Auto-generated ID should have been 1")
} }
stringId := fmt.Sprintf("%d", id) // add another fish to database
id, err = db.WriteAutoId(collection, bluefish);
if err != nil {
t.Error("Create fish failed: ", err.Error())
}
if id != 2 {
t.Error("Auto-generated ID should have been 2")
}
// read fish from database // read fish from database
if err := db.Read(collection, stringId, &onefish); err != nil { if err := db.Read(collection, "1", &onefish); err != nil {
t.Error("Failed to read: ", err.Error()) t.Error("Failed to read: ", err.Error())
} }