本文整理匯總了Golang中github.com/reborndb/qdb/pkg/engine.NewBatch函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewBatch函數的具體用法?Golang NewBatch怎麽用?Golang NewBatch使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewBatch函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: MSetNX
// MSETNX key value [key value ...]
func (s *Store) MSetNX(db uint32, args [][]byte) (int64, error) {
if len(args) == 0 || len(args)%2 != 0 {
return 0, errArguments("len(args) = %d, expect != 0 && mod 2 = 0", len(args))
}
if err := s.acquire(); err != nil {
return 0, err
}
defer s.release()
for i := 0; i < len(args); i += 2 {
o, err := s.loadStoreRow(db, args[i], true)
if err != nil || o != nil {
return 0, err
}
}
ms := &markSet{}
bt := engine.NewBatch()
for i := len(args)/2 - 1; i >= 0; i-- {
key, value := args[i*2], args[i*2+1]
if !ms.Has(key) {
o := newStringRow(db, key)
o.Value = value
bt.Set(o.DataKey(), o.DataValue())
bt.Set(o.MetaKey(), o.MetaValue())
ms.Set(key)
}
}
fw := &Forward{DB: db, Op: "MSet", Args: args}
return 1, s.commit(bt, fw)
}
示例2: RPop
// RPOP key
func (s *Store) RPop(db uint32, args [][]byte) ([]byte, error) {
if len(args) != 1 {
return nil, errArguments("len(args) = %d, expect = 1", len(args))
}
key := args[0]
if err := s.acquire(); err != nil {
return nil, errors.Trace(err)
}
defer s.release()
o, err := s.loadListRow(db, key)
if err != nil || o == nil {
return nil, errors.Trace(err)
}
o.Index = o.Rindex - 1
if _, err := o.LoadDataValue(s); err != nil {
return nil, errors.Trace(err)
} else {
bt := engine.NewBatch()
bt.Del(o.DataKey())
if o.Rindex--; o.Lindex < o.Rindex {
bt.Set(o.MetaKey(), o.MetaValue())
} else {
bt.Del(o.MetaKey())
}
fw := &Forward{DB: db, Op: "RPop", Args: args}
return o.Value, s.commit(bt, fw)
}
}
示例3: incrInt
func (s *Store) incrInt(db uint32, key []byte, delta int64) (int64, error) {
o, err := s.loadStringRow(db, key, true)
if err != nil {
return 0, err
}
bt := engine.NewBatch()
if o != nil {
_, err := o.LoadDataValue(s)
if err != nil {
return 0, err
}
v, err := ParseInt(o.Value)
if err != nil {
return 0, err
}
delta += v
} else {
o = newStringRow(db, key)
bt.Set(o.MetaKey(), o.MetaValue())
}
o.Value = FormatInt(delta)
bt.Set(o.DataKey(), o.DataValue())
fw := &Forward{DB: db, Op: "IncrBy", Args: [][]byte{key, FormatInt(delta)}}
return delta, s.commit(bt, fw)
}
示例4: Append
// APPEND key value
func (s *Store) Append(db uint32, args [][]byte) (int64, error) {
if len(args) != 2 {
return 0, errArguments("len(args) = %d, expect = 2", len(args))
}
key := args[0]
value := args[1]
if err := s.acquire(); err != nil {
return 0, err
}
defer s.release()
o, err := s.loadStringRow(db, key, true)
if err != nil {
return 0, err
}
bt := engine.NewBatch()
if o != nil {
_, err := o.LoadDataValue(s)
if err != nil {
return 0, err
}
o.Value = append(o.Value, value...)
} else {
o = newStringRow(db, key)
o.Value = value
bt.Set(o.MetaKey(), o.MetaValue())
}
bt.Set(o.DataKey(), o.DataValue())
fw := &Forward{DB: db, Op: "Append", Args: args}
return int64(len(o.Value)), s.commit(bt, fw)
}
示例5: SetNX
// SETNX key value
func (s *Store) SetNX(db uint32, args [][]byte) (int64, error) {
if len(args) != 2 {
return 0, errArguments("len(args) = %d, expect = 2", len(args))
}
key := args[0]
value := args[1]
if err := s.acquire(); err != nil {
return 0, err
}
defer s.release()
o, err := s.loadStoreRow(db, key, true)
if err != nil || o != nil {
return 0, err
} else {
o := newStringRow(db, key)
o.Value = value
bt := engine.NewBatch()
bt.Set(o.DataKey(), o.DataValue())
bt.Set(o.MetaKey(), o.MetaValue())
fw := &Forward{DB: db, Op: "Set", Args: args}
return 1, s.commit(bt, fw)
}
}
示例6: Set
// SET key value
func (s *Store) Set(db uint32, args [][]byte) error {
if len(args) != 2 {
return errArguments("len(args) = %d, expect = 2", len(args))
}
key := args[0]
value := args[1]
if err := s.acquire(); err != nil {
return err
}
defer s.release()
bt := engine.NewBatch()
_, err := s.deleteIfExists(bt, db, key)
if err != nil {
return err
}
o := newStringRow(db, key)
o.Value = value
bt.Set(o.DataKey(), o.DataValue())
bt.Set(o.MetaKey(), o.MetaValue())
fw := &Forward{DB: db, Op: "Set", Args: args}
return s.commit(bt, fw)
}
示例7: SlotsRestore
// SLOTSRESTORE key ttlms value [key ttlms value ...]
func (s *Store) SlotsRestore(db uint32, args [][]byte) error {
if len(args) == 0 || len(args)%3 != 0 {
return errArguments("len(args) = %d, expect != 0 && mod 3 = 0", len(args))
}
objs := make([]*rdb.ObjEntry, len(args)/3)
for i := 0; i < len(objs); i++ {
key := args[i*3]
ttlms, err := ParseInt(args[i*3+1])
if err != nil {
return errArguments("parse args failed - %s", err)
}
value := args[i*3+2]
expireat := int64(0)
if ttlms != 0 {
if v, ok := TTLmsToExpireAt(ttlms); ok && v > 0 {
expireat = v
} else {
return errArguments("parse args[%d] ttlms = %d", i*3+1, ttlms)
}
}
obj, err := rdb.DecodeDump(value)
if err != nil {
return errArguments("decode args[%d] failed, %s", i*3+2, err)
}
objs[i] = &rdb.ObjEntry{
DB: db,
Key: key,
ExpireAt: uint64(expireat),
Value: obj,
}
}
if err := s.acquire(); err != nil {
return errors.Trace(err)
}
defer s.release()
ms := &markSet{}
bt := engine.NewBatch()
for i := len(objs) - 1; i >= 0; i-- {
e := objs[i]
if ms.Has(e.Key) {
log.Debugf("[%d] restore batch, db = %d, key = %v, ignore", i, e.DB, e.Key)
continue
} else {
log.Debugf("[%d] restore batch, db = %d, key = %v", i, e.DB, e.Key)
}
if err := s.restore(bt, e.DB, e.Key, int64(e.ExpireAt), e.Value); err != nil {
log.Warningf("restore object failed, db = %d, key = %v, err = %s", e.DB, e.Key, err)
return errors.Trace(err)
}
ms.Set(e.Key)
}
fw := &Forward{DB: db, Op: "SlotsRestore", Args: args}
return s.commit(bt, fw)
}
示例8: SPop
// SPOP key
func (s *Store) SPop(db uint32, args [][]byte) ([]byte, error) {
if len(args) != 1 {
return nil, errArguments("len(args) = %d, expect = 1", len(args))
}
key := args[0]
if err := s.acquire(); err != nil {
return nil, err
}
defer s.release()
o, err := s.loadSetRow(db, key)
if err != nil || o == nil {
return nil, err
}
members, err := o.getMembers(s, 1)
if err != nil || len(members) == 0 {
return nil, err
}
o.Member = members[0]
bt := engine.NewBatch()
bt.Del(o.DataKey())
if o.Size--; o.Size > 0 {
bt.Set(o.MetaKey(), o.MetaValue())
} else {
bt.Del(o.MetaKey())
}
fw := &Forward{DB: db, Op: "SRem", Args: [][]byte{key, members[0]}}
return o.Member, s.commit(bt, fw)
}
示例9: Persist
// PERSIST key
func (s *Store) Persist(db uint32, args [][]byte) (int64, error) {
if len(args) != 1 {
return 0, errArguments("len(args) = %d, expect = 1", len(args))
}
key := args[0]
if err := s.acquire(); err != nil {
return 0, err
}
defer s.release()
o, err := s.loadStoreRow(db, key, true)
if err != nil || o == nil {
return 0, err
}
if o.GetExpireAt() == 0 {
return 0, nil
}
fw := &Forward{DB: db, Op: "Persist", Args: args}
bt := engine.NewBatch()
o.SetExpireAt(0)
bt.Set(o.MetaKey(), o.MetaValue())
return 1, s.commit(bt, fw)
}
示例10: Del
// DEL key [key ...]
func (s *Store) Del(db uint32, args [][]byte) (int64, error) {
if len(args) == 0 {
return 0, errArguments("len(args) = %d, expect != 0", len(args))
}
keys := args
if err := s.acquire(); err != nil {
return 0, err
}
defer s.release()
for _, key := range keys {
_, err := s.loadStoreRow(db, key, true)
if err != nil {
return 0, err
}
}
ms := &markSet{}
bt := engine.NewBatch()
for _, key := range keys {
if !ms.Has(key) {
exists, err := s.deleteIfExists(bt, db, key)
if err != nil {
return 0, err
}
if exists {
ms.Set(key)
}
}
}
fw := &Forward{DB: db, Op: "Del", Args: args}
return ms.Len(), s.commit(bt, fw)
}
示例11: MSet
// MSET key value [key value ...]
func (s *Store) MSet(db uint32, args [][]byte) error {
if len(args) == 0 || len(args)%2 != 0 {
return errArguments("len(args) = %d, expect != 0 && mod 2 = 0", len(args))
}
if err := s.acquire(); err != nil {
return err
}
defer s.release()
ms := &markSet{}
bt := engine.NewBatch()
for i := len(args)/2 - 1; i >= 0; i-- {
key, value := args[i*2], args[i*2+1]
if !ms.Has(key) {
_, err := s.deleteIfExists(bt, db, key)
if err != nil {
return err
}
o := newStringRow(db, key)
o.Value = value
bt.Set(o.DataKey(), o.DataValue())
bt.Set(o.MetaKey(), o.MetaValue())
ms.Set(key)
}
}
fw := &Forward{DB: db, Op: "MSet", Args: args}
return s.commit(bt, fw)
}
示例12: incrFloat
func (s *Store) incrFloat(db uint32, key []byte, delta float64) (float64, error) {
o, err := s.loadStringRow(db, key, true)
if err != nil {
return 0, err
}
bt := engine.NewBatch()
if o != nil {
_, err := o.LoadDataValue(s)
if err != nil {
return 0, err
}
v, err := ParseFloat(o.Value)
if err != nil {
return 0, err
}
delta += v
} else {
o = newStringRow(db, key)
bt.Set(o.MetaKey(), o.MetaValue())
}
if math.IsNaN(delta) || math.IsInf(delta, 0) {
return 0, errors.Errorf("increment would produce NaN or Infinity")
}
o.Value = FormatFloat(delta)
bt.Set(o.DataKey(), o.DataValue())
fw := &Forward{DB: db, Op: "IncrByFloat", Args: [][]byte{key, FormatFloat(delta)}}
return delta, s.commit(bt, fw)
}
示例13: HIncrByFloat
// HINCRBYFLOAT key field delta
func (s *Store) HIncrByFloat(db uint32, args [][]byte) (float64, error) {
if len(args) != 3 {
return 0, errArguments("len(args) = %d, expect = 2", len(args))
}
key := args[0]
field := args[1]
delta, err := ParseFloat(args[2])
if err != nil {
return 0, errArguments("parse args failed - %s", err)
}
if err := s.acquire(); err != nil {
return 0, err
}
defer s.release()
o, err := s.loadHashRow(db, key)
if err != nil {
return 0, err
}
var exists bool = false
if o != nil {
o.Field = field
exists, err = o.LoadDataValue(s)
if err != nil {
return 0, err
}
} else {
o = newHashRow(db, key)
o.Field = field
}
bt := engine.NewBatch()
if exists {
v, err := ParseFloat(o.Value)
if err != nil {
return 0, err
}
delta += v
} else {
o.Size++
bt.Set(o.MetaKey(), o.MetaValue())
}
if math.IsNaN(delta) || math.IsInf(delta, 0) {
return 0, errors.Errorf("increment would produce NaN or Infinity")
}
o.Value = FormatFloat(delta)
bt.Set(o.DataKey(), o.DataValue())
fw := &Forward{DB: db, Op: "HIncrByFloat", Args: args}
return delta, s.commit(bt, fw)
}
示例14: ZIncrBy
// ZINCRBY key delta member
func (s *Store) ZIncrBy(db uint32, args [][]byte) (float64, error) {
if len(args) != 3 {
return 0, errArguments("len(args) = %d, expect = 3", len(args))
}
key := args[0]
delta, err := ParseFloat(args[1])
if err != nil {
return 0, errArguments("parse args failed - %s", err)
}
member := args[2]
if err := s.acquire(); err != nil {
return 0, err
}
defer s.release()
o, err := s.loadZSetRow(db, key)
if err != nil {
return 0, err
}
bt := engine.NewBatch()
var exists bool = false
if o != nil {
o.Member = member
exists, err = o.LoadDataValue(s)
if err != nil {
return 0, err
} else if exists {
bt.Del(o.IndexKey())
}
} else {
o = newZSetRow(db, key)
o.Member = member
}
if exists {
delta += o.Score
} else {
o.Size++
bt.Set(o.MetaKey(), o.MetaValue())
}
o.Score = delta
if math.IsNaN(delta) {
return 0, errors.Errorf("invalid nan score")
}
bt.Set(o.DataKey(), o.DataValue())
bt.Set(o.IndexKey(), o.IndexValue())
fw := &Forward{DB: db, Op: "ZIncrBy", Args: args}
return delta, s.commit(bt, fw)
}
示例15: ZRemRangeByScore
// ZREMRANGEBYSCORE key min max
func (s *Store) ZRemRangeByScore(db uint32, args [][]byte) (int64, error) {
if len(args) != 3 {
return 0, errArguments("len(args) = %d, expect 3", len(args))
}
key := args[0]
min := args[1]
max := args[2]
r, err := parseRangeSpec(min, max)
if err != nil {
return 0, errors.Trace(err)
}
if err := s.acquire(); err != nil {
return 0, err
}
defer s.release()
o, err := s.loadZSetRow(db, key)
if err != nil {
return 0, err
} else if o == nil {
return 0, nil
}
bt := engine.NewBatch()
n := int64(0)
f := func(o *zsetRow) error {
bt.Del(o.DataKey())
bt.Del(o.IndexKey())
n++
return nil
}
if err := o.travelInRange(s, r, f); err != nil {
return 0, errors.Trace(err)
}
if n > 0 {
if o.Size -= n; o.Size > 0 {
bt.Set(o.MetaKey(), o.MetaValue())
} else {
bt.Del(o.MetaKey())
}
}
fw := &Forward{DB: db, Op: "ZRemRangeByScore", Args: args}
return n, s.commit(bt, fw)
}