Tweak multi-field fset to return the count of updated fields

This commit is contained in:
Alex Roitman 2017-11-10 14:31:38 -08:00
parent 79acc0efe5
commit a3bf8b6572
3 changed files with 13 additions and 12 deletions

View File

@ -235,7 +235,7 @@ func (c *Collection) SetField(id, field string, value float64) (obj geojson.Obje
// SetFields is similar to SetField, just setting multiple fields at once // SetFields is similar to SetField, just setting multiple fields at once
func (c *Collection) SetFields(id string, in_fields []string, in_values []float64) ( func (c *Collection) SetFields(id string, in_fields []string, in_values []float64) (
obj geojson.Object, fields []float64, updated bool, ok bool, obj geojson.Object, fields []float64, updated_count int, ok bool,
) { ) {
i := c.items.Get(&itemT{id: id}) i := c.items.Get(&itemT{id: id})
if i == nil { if i == nil {
@ -244,10 +244,11 @@ func (c *Collection) SetFields(id string, in_fields []string, in_values []float6
} }
item := i.(*itemT) item := i.(*itemT)
for idx, field := range in_fields { for idx, field := range in_fields {
_upd := c.setField(item, field, in_values[idx]) if c.setField(item, field, in_values[idx]) {
updated = updated || _upd updated_count++
} }
return item.object, c.getFieldValues(id), updated, true }
return item.object, c.getFieldValues(id), updated_count, true
} }
func (c *Collection) setField(item *itemT, field string, value float64) (updated bool) { func (c *Collection) setField(item *itemT, field string, value float64) (updated bool) {

View File

@ -829,6 +829,7 @@ func (c *Controller) cmdFset(msg *server.Message) (res resp.Value, d commandDeta
var fields []string var fields []string
var values []float64 var values []float64
var xx bool var xx bool
var updated_count int
d, fields, values, xx, err = c.parseFSetArgs(vs) d, fields, values, xx, err = c.parseFSetArgs(vs)
col := c.getCol(d.key) col := c.getCol(d.key)
@ -837,7 +838,7 @@ func (c *Controller) cmdFset(msg *server.Message) (res resp.Value, d commandDeta
return return
} }
var ok bool var ok bool
d.obj, d.fields, d.updated, ok = col.SetFields(d.id, fields, values) d.obj, d.fields, updated_count, ok = col.SetFields(d.id, fields, values)
if !(ok || xx) { if !(ok || xx) {
err = errIDNotFound err = errIDNotFound
return return
@ -845,6 +846,7 @@ func (c *Controller) cmdFset(msg *server.Message) (res resp.Value, d commandDeta
if ok { if ok {
d.command = "fset" d.command = "fset"
d.timestamp = time.Now() d.timestamp = time.Now()
d.updated = updated_count > 0
fmap := col.FieldMap() fmap := col.FieldMap()
d.fmap = make(map[string]int) d.fmap = make(map[string]int)
for key, idx := range fmap { for key, idx := range fmap {
@ -856,11 +858,7 @@ func (c *Controller) cmdFset(msg *server.Message) (res resp.Value, d commandDeta
case server.JSON: case server.JSON:
res = resp.StringValue(`{"ok":true,"elapsed":"` + time.Now().Sub(start).String() + "\"}") res = resp.StringValue(`{"ok":true,"elapsed":"` + time.Now().Sub(start).String() + "\"}")
case server.RESP: case server.RESP:
if d.updated { res = resp.IntegerValue(updated_count)
res = resp.IntegerValue(1)
} else {
res = resp.IntegerValue(0)
}
} }
return return
} }

View File

@ -80,10 +80,12 @@ func keys_FSET_test(mc *mockServer) error {
{"GET", "mykey", "myid", "WITHFIELDS", "HASH", 7}, {"[9my5xp7]"}, {"GET", "mykey", "myid", "WITHFIELDS", "HASH", 7}, {"[9my5xp7]"},
{"FSET", "mykey", "myid", "f1", 105.6}, {1}, {"FSET", "mykey", "myid", "f1", 105.6}, {1},
{"GET", "mykey", "myid", "WITHFIELDS", "HASH", 7}, {"[9my5xp7 [f1 105.6]]"}, {"GET", "mykey", "myid", "WITHFIELDS", "HASH", 7}, {"[9my5xp7 [f1 105.6]]"},
{"FSET", "mykey", "myid", "f1", 1.1, "f2", 2.2}, {1}, {"FSET", "mykey", "myid", "f1", 1.1, "f2", 2.2}, {2},
{"GET", "mykey", "myid", "WITHFIELDS", "HASH", 7}, {"[9my5xp7 [f1 1.1 f2 2.2]]"}, {"GET", "mykey", "myid", "WITHFIELDS", "HASH", 7}, {"[9my5xp7 [f1 1.1 f2 2.2]]"},
{"FSET", "mykey", "myid", "f1", 1.1, "f2", 22.22}, {1},
{"GET", "mykey", "myid", "WITHFIELDS", "HASH", 7}, {"[9my5xp7 [f1 1.1 f2 22.22]]"},
{"FSET", "mykey", "myid", "f1", 0}, {1}, {"FSET", "mykey", "myid", "f1", 0}, {1},
{"GET", "mykey", "myid", "WITHFIELDS", "HASH", 7}, {"[9my5xp7 [f2 2.2]]"}, {"GET", "mykey", "myid", "WITHFIELDS", "HASH", 7}, {"[9my5xp7 [f2 22.22]]"},
{"FSET", "mykey", "myid", "f2", 0}, {1}, {"FSET", "mykey", "myid", "f2", 0}, {1},
{"GET", "mykey", "myid", "WITHFIELDS", "HASH", 7}, {"[9my5xp7]"}, {"GET", "mykey", "myid", "WITHFIELDS", "HASH", 7}, {"[9my5xp7]"},
{"FSET", "mykey", "myid2", "xx", "f1", 1.1, "f2", 2.2}, {0}, {"FSET", "mykey", "myid2", "xx", "f1", 1.1, "f2", 2.2}, {0},