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"
dbMsgTypeUpd = "upd"
dbMsgTypeNew = "new"
dbMsgTypeDelete = "delete"
dbMsgTypeDel = "del"
dbMsgTypeWarning = "warning"
dbApiSeperator = "|"
@ -91,7 +91,7 @@ func (api *DatabaseAPI) handler() {
// 125|sub|<query>
// 125|upd|<key>|<data>
// 125|new|<key>|<data>
// 125|delete|<key>|<data>
// 127|del|<key>
// 125|warning|<message> // error with single record, operation continues
// 127|qsub|<query>
// 127|ok|<key>|<data>
@ -99,7 +99,7 @@ func (api *DatabaseAPI) handler() {
// 127|error|<message>
// 127|upd|<key>|<data>
// 127|new|<key>|<data>
// 127|delete|<key>|<data>
// 127|del|<key>
// 127|warning|<message> // error with single record, operation continues
// 128|create|<key>|<data>
@ -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

View file

@ -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)

View file

@ -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)