From 610cd418ca8493865d411795e4cbd77b1fa43b8d Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Fri, 7 Apr 2017 13:29:07 -0400 Subject: [PATCH] *: fixes to be working on new abstractions Signed-off-by: Vincent Batts --- dbutil/dbutil.go | 38 +++++++++++++++++++++++++++++++------- dbutil/mongo/handle.go | 12 ++++++++++-- server.go | 3 ++- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/dbutil/dbutil.go b/dbutil/dbutil.go index bcc6705..8e8a2e7 100644 --- a/dbutil/dbutil.go +++ b/dbutil/dbutil.go @@ -7,11 +7,11 @@ import ( ) // Handles are all the register backing Handlers -var Handles map[string]Handler +var Handles = map[string]Handler{} // Handler is the means of getting "files" from the backing database type Handler interface { - Init(config interface{}) error + Init(config []byte, err error) error Close() error Open(filename string) (File, error) @@ -33,14 +33,38 @@ type Handler interface { GetKeywords() (kp []types.IdCount, err error) } -type MetaDataer interface { - GetMeta(result interface{}) (err error) - SetMeta(metadata interface{}) -} - +// File is what is stored and fetched from the backing database type File interface { io.Reader io.Writer io.Closer MetaDataer } + +// MetaDataer allows set/get for optional metadata +type MetaDataer interface { + /* + GetMeta unmarshals the optional "metadata" field associated with the file into + the result parameter. The meaning of keys under that field is user-defined. For + example: + + result := struct{ INode int }{} + err = file.GetMeta(&result) + if err != nil { + panic(err.String()) + } + fmt.Printf("inode: %d\n", result.INode) + */ + GetMeta(result interface{}) (err error) + /* + SetMeta changes the optional "metadata" field associated with the file. The + meaning of keys under that field is user-defined. For example: + + file.SetMeta(bson.M{"inode": inode}) + + It is a runtime error to call this function when the file is not open for + writing. + + */ + SetMeta(metadata interface{}) +} diff --git a/dbutil/mongo/handle.go b/dbutil/mongo/handle.go index 18e69c8..2e012e8 100644 --- a/dbutil/mongo/handle.go +++ b/dbutil/mongo/handle.go @@ -1,6 +1,7 @@ package mongo import ( + "encoding/json" "strings" "github.com/vbatts/imgsrv/dbutil" @@ -29,8 +30,15 @@ type mongoHandle struct { Gfs *mgo.GridFS } -func (h *mongoHandle) Init(config interface{}) (err error) { - h.config = config.(dbConfig) +func (h *mongoHandle) Init(config []byte, err error) error { + if err != nil { + return err + } + + h.config = dbConfig{} + if err := json.Unmarshal(config, &h.config); err != nil { + return err + } h.Session, err = mgo.Dial(h.config.Seed) if err != nil { diff --git a/server.go b/server.go index c411c42..175ca78 100644 --- a/server.go +++ b/server.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "fmt" "io" "log" @@ -45,7 +46,7 @@ func runServer(c *config.Config) { serverConfig.MongoDbName, } - if err := du.Init(duConfig); err != nil { + if err := du.Init(json.Marshal(duConfig)); err != nil { log.Fatal(err) } defer du.Close() // TODO this ought to catch a signal to cleanup