// Inc increments the integer value at key. If the key does not exist it will // be created with an initial value of 0 which will then be incremented. If the // key exists but was set using Put or CPut an error will be returned. // // A new result will be appended to the batch which will contain a single row // and Result.Err will indicate success or failure. // // key can be either a byte slice, a string, a fmt.Stringer or an // encoding.BinaryMarshaler. func (b *Batch) Inc(key interface{}, value int64) { k, err := marshalKey(key) if err != nil { b.initResult(0, 1, err) return } b.calls = append(b.calls, proto.IncrementCall(proto.Key(k), value)) b.initResult(1, 1, nil) }
// IncStruct increments the specified column in the structured table identify // by obj. The primary key columns within obj are used to identify which row to // modify. The obj type must have previously been bound to a table using // BindModel. func (b *Batch) IncStruct(obj interface{}, value int64, column string) { v := reflect.ValueOf(obj) m, err := b.DB.getModel(v.Type(), true) if err != nil { b.initResult(0, 0, err) return } v = reflect.Indirect(v) primaryKey, err := m.encodePrimaryKey(v) if err != nil { b.initResult(0, 0, err) return } col, ok := m.columnsByName[strings.ToLower(column)] if !ok { b.initResult(0, 0, fmt.Errorf("%s: unable to find column %s", m.name, column)) return } key := m.encodeColumnKey(primaryKey, col.ID) if log.V(2) { log.Infof("Inc %q", key) } c := proto.IncrementCall(proto.Key(key), value) c.Post = func() error { reply := c.Reply.(*proto.IncrementResponse) // TODO(pmattis): This isn't very efficient. Should be able to pass the // integer value directly instead of encoding it into a []byte. pv := &proto.Value{} pv.SetInteger(reply.NewValue) return unmarshalValue(pv, v.FieldByIndex(col.field.Index)) } b.calls = append(b.calls, c) b.initResult(1, 1, nil) }