adding fail channel to bubble errors
This commit is contained in:
+46
-54
@@ -1,26 +1,26 @@
|
|||||||
package scribble
|
package scribble
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/nanobox-core/utils"
|
"github.com/nanobox-core/hatchet"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
//
|
||||||
DefaultDir = "./tmp/db"
|
const Version = "0.0.1"
|
||||||
Version = "0.0.1"
|
|
||||||
)
|
|
||||||
|
|
||||||
//
|
//
|
||||||
type (
|
type (
|
||||||
|
|
||||||
// Driver represents
|
// Driver
|
||||||
Driver struct {
|
Driver struct {
|
||||||
channels map[string]chan int
|
channels map[string]chan int
|
||||||
dir string
|
dir string
|
||||||
|
log *hatchet.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transaction represents
|
// Transaction represents
|
||||||
@@ -32,90 +32,83 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
//
|
// New
|
||||||
var (
|
func New(dir string, logger hatchet.Logger) (*Driver, error) {
|
||||||
debugging bool
|
fmt.Printf("Creating database directory at '%v'...\n", dir)
|
||||||
)
|
|
||||||
|
|
||||||
// Init
|
scribble := &Driver{}
|
||||||
func (d *Driver) Init(opts map[string]string) int {
|
scribble.dir = dir
|
||||||
fmt.Printf("Creating database directory at '%v'...\n", opts["db_dir"])
|
scribble.channels = make(map[string]chan int)
|
||||||
|
|
||||||
debugging = (opts["debugging"] == "true")
|
|
||||||
|
|
||||||
d.dir = opts["db_dir"]
|
|
||||||
|
|
||||||
//
|
//
|
||||||
d.channels = make(map[string]chan int)
|
if err := mkDir(scribble.dir); err != nil {
|
||||||
|
return nil, err
|
||||||
// make a ping channel
|
|
||||||
ping := make(chan int)
|
|
||||||
d.channels["ping"] = ping
|
|
||||||
|
|
||||||
//
|
|
||||||
if err := mkDir(d.dir); err != nil {
|
|
||||||
fmt.Printf("Unable to create dir '%v': %v", d.dir, err)
|
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
return 0
|
return scribble, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transact
|
// Transact
|
||||||
func (d *Driver) Transact(trans Transaction) {
|
func (d *Driver) Transact(trans Transaction) error {
|
||||||
|
|
||||||
//
|
//
|
||||||
done := d.getOrCreateChan(trans.Collection)
|
done := d.getOrCreateChan(trans.Collection)
|
||||||
|
fail := make(chan error)
|
||||||
|
|
||||||
//
|
//
|
||||||
switch trans.Action {
|
switch trans.Action {
|
||||||
case "write":
|
case "write":
|
||||||
go d.write(trans, done)
|
go d.write(trans, done, fail)
|
||||||
case "read":
|
case "read":
|
||||||
go d.read(trans, done)
|
go d.read(trans, done, fail)
|
||||||
case "readall":
|
case "readall":
|
||||||
go d.readAll(trans, done)
|
go d.readAll(trans, done, fail)
|
||||||
case "delete":
|
case "delete":
|
||||||
go d.delete(trans, done)
|
go d.delete(trans, done, fail)
|
||||||
default:
|
default:
|
||||||
fmt.Println("Unsupported action ", trans.Action)
|
fmt.Println("Unsupported action ", trans.Action)
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait...
|
// wait until we're done, or error
|
||||||
<-done
|
select {
|
||||||
|
case <-done:
|
||||||
|
return nil
|
||||||
|
case err := <-fail:
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// private
|
// private
|
||||||
|
|
||||||
// write
|
// write
|
||||||
func (d *Driver) write(trans Transaction, done chan<- int) {
|
func (d *Driver) write(trans Transaction, done chan<- int, fail chan<- error) {
|
||||||
|
|
||||||
//
|
//
|
||||||
dir := d.dir + "/" + trans.Collection
|
dir := d.dir + "/" + trans.Collection
|
||||||
|
|
||||||
//
|
//
|
||||||
if err := mkDir(dir); err != nil {
|
if err := mkDir(dir); err != nil {
|
||||||
fmt.Println("Unable to create dir '%v': %v", dir, err)
|
fail <- err
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
file, err := os.Create(dir + "/" + trans.Resource)
|
file, err := os.Create(dir + "/" + trans.Resource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Unable to create file %v/%v: %v", trans.Collection, trans.Resource, err)
|
fail <- err
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
//
|
//
|
||||||
b := utils.ToJSONIndent(trans.Container)
|
b, err := json.MarshalIndent(trans.Container, "", "\t")
|
||||||
|
if err != nil {
|
||||||
|
fail <- err
|
||||||
|
}
|
||||||
|
|
||||||
_, err = file.WriteString(string(b))
|
_, err = file.WriteString(string(b))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Unable to write to file %v: %v", trans.Resource, err)
|
fail <- err
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// release...
|
// release...
|
||||||
@@ -123,7 +116,7 @@ func (d *Driver) write(trans Transaction, done chan<- int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// read
|
// read
|
||||||
func (d *Driver) read(trans Transaction, done chan<- int) interface{} {
|
func (d *Driver) read(trans Transaction, done chan<- int, fail chan<- error) interface{} {
|
||||||
|
|
||||||
dir := d.dir + "/" + trans.Collection
|
dir := d.dir + "/" + trans.Collection
|
||||||
|
|
||||||
@@ -133,8 +126,8 @@ func (d *Driver) read(trans Transaction, done chan<- int) interface{} {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := utils.FromJSON(b, trans.Container); err != nil {
|
if err := json.Unmarshal(b, trans.Container); err != nil {
|
||||||
panic(err)
|
fail <- err
|
||||||
}
|
}
|
||||||
|
|
||||||
// release...
|
// release...
|
||||||
@@ -144,14 +137,14 @@ func (d *Driver) read(trans Transaction, done chan<- int) interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// readAll
|
// readAll
|
||||||
func (d *Driver) readAll(trans Transaction, done chan<- int) {
|
func (d *Driver) readAll(trans Transaction, done chan<- int, fail chan<- error) {
|
||||||
|
|
||||||
dir := d.dir + "/" + trans.Collection
|
dir := d.dir + "/" + trans.Collection
|
||||||
|
|
||||||
//
|
//
|
||||||
files, err := ioutil.ReadDir(dir)
|
files, err := ioutil.ReadDir(dir)
|
||||||
|
|
||||||
// if there is an error here it just means there are no evars so dont do anything
|
// an error here just means an empty collection so do nothing
|
||||||
if err != nil {
|
if err != nil {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,15 +153,15 @@ func (d *Driver) readAll(trans Transaction, done chan<- int) {
|
|||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
b, err := ioutil.ReadFile(dir + "/" + file.Name())
|
b, err := ioutil.ReadFile(dir + "/" + file.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
fail <- err
|
||||||
}
|
}
|
||||||
|
|
||||||
f = append(f, string(b))
|
f = append(f, string(b))
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
if err := utils.FromJSON([]byte("["+strings.Join(f, ",")+"]"), trans.Container); err != nil {
|
if err := json.Unmarshal([]byte("["+strings.Join(f, ",")+"]"), trans.Container); err != nil {
|
||||||
panic(err)
|
fail <- err
|
||||||
}
|
}
|
||||||
|
|
||||||
// release...
|
// release...
|
||||||
@@ -176,14 +169,13 @@ func (d *Driver) readAll(trans Transaction, done chan<- int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// delete
|
// delete
|
||||||
func (d *Driver) delete(trans Transaction, done chan<- int) {
|
func (d *Driver) delete(trans Transaction, done chan<- int, fail chan<- error) {
|
||||||
|
|
||||||
dir := d.dir + "/" + trans.Collection
|
dir := d.dir + "/" + trans.Collection
|
||||||
|
|
||||||
err := os.Remove(dir + "/" + trans.Resource)
|
err := os.Remove(dir + "/" + trans.Resource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Unable to delete file %v/%v: %v", trans.Collection, trans.Resource, err)
|
fail <- err
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// release...
|
// release...
|
||||||
Reference in New Issue
Block a user