Fix database api

This commit is contained in:
Daniel 2018-10-05 22:21:43 +02:00
parent f36d82bf19
commit 852f7ab3c6
3 changed files with 22 additions and 14 deletions

View file

@ -24,7 +24,7 @@ const (
dbMsgTypeSuccess = "success" dbMsgTypeSuccess = "success"
dbMsgTypeUpd = "upd" dbMsgTypeUpd = "upd"
dbMsgTypeNew = "new" dbMsgTypeNew = "new"
dbMsgTypeDelete = "delete" dbMsgTypeDel = "del"
dbMsgTypeWarning = "warning" dbMsgTypeWarning = "warning"
dbApiSeperator = "|" dbApiSeperator = "|"
@ -91,7 +91,7 @@ func (api *DatabaseAPI) handler() {
// 125|sub|<query> // 125|sub|<query>
// 125|upd|<key>|<data> // 125|upd|<key>|<data>
// 125|new|<key>|<data> // 125|new|<key>|<data>
// 125|delete|<key>|<data> // 127|del|<key>
// 125|warning|<message> // error with single record, operation continues // 125|warning|<message> // error with single record, operation continues
// 127|qsub|<query> // 127|qsub|<query>
// 127|ok|<key>|<data> // 127|ok|<key>|<data>
@ -99,7 +99,7 @@ func (api *DatabaseAPI) handler() {
// 127|error|<message> // 127|error|<message>
// 127|upd|<key>|<data> // 127|upd|<key>|<data>
// 127|new|<key>|<data> // 127|new|<key>|<data>
// 127|delete|<key>|<data> // 127|del|<key>
// 127|warning|<message> // error with single record, operation continues // 127|warning|<message> // error with single record, operation continues
// 128|create|<key>|<data> // 128|create|<key>|<data>
@ -240,7 +240,7 @@ func (api *DatabaseAPI) handleGet(opID []byte, key string) {
api.send(opID, dbMsgTypeError, err.Error(), nil) api.send(opID, dbMsgTypeError, err.Error(), nil)
return return
} }
api.send(opID, dbMsgTypeOk, r.DatabaseKey(), data) api.send(opID, dbMsgTypeOk, r.Key(), data)
} }
func (api *DatabaseAPI) handleQuery(opID []byte, queryText string) { 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 { if err != nil {
api.send(opID, dbMsgTypeWarning, err.Error(), 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 { if it.Err != nil {
api.send(opID, dbMsgTypeError, it.Err.Error(), 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) api.send(opID, dbMsgTypeWarning, err.Error(), nil)
} }
// TODO: use upd, new and delete msgTypes // 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 { if sub.Err != nil {
api.send(opID, dbMsgTypeError, sub.Err.Error(), 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 raw[0] = record.JSON
copy(raw[1:], data) copy(raw[1:], data)
dbName, dbKey := record.ParseKey(key) r, err := record.NewWrapper(key, nil, record.JSON, data)
r, err := record.NewRawWrapper(dbName, dbKey, raw)
if err != nil { if err != nil {
api.send(opID, dbMsgTypeError, err.Error(), nil) api.send(opID, dbMsgTypeError, err.Error(), nil)
return return

View file

@ -37,11 +37,20 @@ func (s *ConfigStorageInterface) Get(key string) (record.Record, error) {
// Put stores a record in the database. // Put stores a record in the database.
func (s *ConfigStorageInterface) Put(r record.Record) error { func (s *ConfigStorageInterface) Put(r record.Record) error {
if r.Meta().Deleted > 0 {
return setConfigOption(r.DatabaseKey(), nil, false)
}
acc := r.GetAccessor(r) acc := r.GetAccessor(r)
if acc == nil { if acc == nil {
return errors.New("invalid data") return errors.New("invalid data")
} }
val, ok := acc.Get("Value")
if !ok || val == nil {
return setConfigOption(r.DatabaseKey(), nil, false)
}
optionsLock.RLock() optionsLock.RLock()
option, ok := options[r.DatabaseKey()] option, ok := options[r.DatabaseKey()]
optionsLock.RUnlock() optionsLock.RUnlock()
@ -61,7 +70,7 @@ func (s *ConfigStorageInterface) Put(r record.Record) error {
value, ok = acc.GetBool("Value") value, ok = acc.GetBool("Value")
} }
if !ok { if !ok {
return errors.New("expected new value in \"Value\"") return errors.New("received invalid value in \"Value\"")
} }
err := setConfigOption(r.DatabaseKey(), value, false) err := setConfigOption(r.DatabaseKey(), value, false)
@ -99,7 +108,7 @@ func (s *ConfigStorageInterface) processQuery(q *query.Query, it *iterator.Itera
sort.Sort(sortableOptions(opts)) sort.Sort(sortableOptions(opts))
for _, opt := range options { for _, opt := range opts {
r, err := opt.Export() r, err := opt.Export()
if err != nil { if err != nil {
it.Finish(err) it.Finish(err)

View file

@ -218,9 +218,6 @@ func (i *Interface) Delete(key string) error {
return err return err
} }
r.Lock()
defer r.Unlock()
i.options.Apply(r) i.options.Apply(r)
r.Meta().Delete() r.Meta().Delete()
return db.Put(r) return db.Put(r)