From 852f7ab3c63fde2259d3977a82b6f325d85adcff Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 5 Oct 2018 22:21:43 +0200 Subject: [PATCH] Fix database api --- api/database.go | 20 +++++++++++--------- config/database.go | 13 +++++++++++-- database/interface.go | 3 --- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/api/database.go b/api/database.go index d3f63f6..ce815db 100644 --- a/api/database.go +++ b/api/database.go @@ -24,7 +24,7 @@ const ( dbMsgTypeSuccess = "success" dbMsgTypeUpd = "upd" dbMsgTypeNew = "new" - dbMsgTypeDelete = "delete" + dbMsgTypeDel = "del" dbMsgTypeWarning = "warning" dbApiSeperator = "|" @@ -91,7 +91,7 @@ func (api *DatabaseAPI) handler() { // 125|sub| // 125|upd|| // 125|new|| - // 125|delete|| + // 127|del| // 125|warning| // error with single record, operation continues // 127|qsub| // 127|ok|| @@ -99,7 +99,7 @@ func (api *DatabaseAPI) handler() { // 127|error| // 127|upd|| // 127|new|| - // 127|delete|| + // 127|del| // 127|warning| // error with single record, operation continues // 128|create|| @@ -240,7 +240,7 @@ func (api *DatabaseAPI) handleGet(opID []byte, key string) { api.send(opID, dbMsgTypeError, err.Error(), nil) return } - api.send(opID, dbMsgTypeOk, r.DatabaseKey(), data) + api.send(opID, dbMsgTypeOk, r.Key(), data) } func (api *DatabaseAPI) handleQuery(opID []byte, queryText string) { @@ -274,7 +274,7 @@ func (api *DatabaseAPI) processQuery(opID []byte, q *query.Query) (ok bool) { if err != nil { api.send(opID, dbMsgTypeWarning, err.Error(), nil) } - api.send(opID, dbMsgTypeOk, r.DatabaseKey(), data) + api.send(opID, dbMsgTypeOk, r.Key(), data) } if it.Err != nil { api.send(opID, dbMsgTypeError, it.Err.Error(), nil) @@ -325,7 +325,11 @@ func (api *DatabaseAPI) processSub(opID []byte, sub *database.Subscription) { api.send(opID, dbMsgTypeWarning, err.Error(), nil) } // TODO: use upd, new and delete msgTypes - api.send(opID, dbMsgTypeUpd, r.DatabaseKey(), data) + if r.Meta().Deleted > 0 { + api.send(opID, dbMsgTypeDel, r.Key(), nil) + } else { + api.send(opID, dbMsgTypeUpd, r.Key(), data) + } } if sub.Err != nil { api.send(opID, dbMsgTypeError, sub.Err.Error(), nil) @@ -374,9 +378,7 @@ func (api *DatabaseAPI) handlePut(opID []byte, key string, data []byte, create b raw[0] = record.JSON copy(raw[1:], data) - dbName, dbKey := record.ParseKey(key) - - r, err := record.NewRawWrapper(dbName, dbKey, raw) + r, err := record.NewWrapper(key, nil, record.JSON, data) if err != nil { api.send(opID, dbMsgTypeError, err.Error(), nil) return diff --git a/config/database.go b/config/database.go index 73247a1..f72d334 100644 --- a/config/database.go +++ b/config/database.go @@ -37,11 +37,20 @@ func (s *ConfigStorageInterface) Get(key string) (record.Record, error) { // Put stores a record in the database. func (s *ConfigStorageInterface) Put(r record.Record) error { + if r.Meta().Deleted > 0 { + return setConfigOption(r.DatabaseKey(), nil, false) + } + acc := r.GetAccessor(r) if acc == nil { return errors.New("invalid data") } + val, ok := acc.Get("Value") + if !ok || val == nil { + return setConfigOption(r.DatabaseKey(), nil, false) + } + optionsLock.RLock() option, ok := options[r.DatabaseKey()] optionsLock.RUnlock() @@ -61,7 +70,7 @@ func (s *ConfigStorageInterface) Put(r record.Record) error { value, ok = acc.GetBool("Value") } if !ok { - return errors.New("expected new value in \"Value\"") + return errors.New("received invalid value in \"Value\"") } err := setConfigOption(r.DatabaseKey(), value, false) @@ -99,7 +108,7 @@ func (s *ConfigStorageInterface) processQuery(q *query.Query, it *iterator.Itera sort.Sort(sortableOptions(opts)) - for _, opt := range options { + for _, opt := range opts { r, err := opt.Export() if err != nil { it.Finish(err) diff --git a/database/interface.go b/database/interface.go index 8db89d5..2f5ad4d 100644 --- a/database/interface.go +++ b/database/interface.go @@ -218,9 +218,6 @@ func (i *Interface) Delete(key string) error { return err } - r.Lock() - defer r.Unlock() - i.options.Apply(r) r.Meta().Delete() return db.Put(r)