本文整理汇总了Golang中github.com/jmhodges/levigo.WriteBatch类的典型用法代码示例。如果您正苦于以下问题:Golang WriteBatch类的具体用法?Golang WriteBatch怎么用?Golang WriteBatch使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了WriteBatch类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Sadd
func Sadd(args [][]byte, wb *levigo.WriteBatch) interface{} {
var newMembers uint32
key := NewKeyBuffer(SetKey, args[0], len(args[1]))
mk := metaKey(args[0])
card, err := scard(mk, nil)
if err != nil {
return err
}
for _, member := range args[1:] {
key.SetSuffix(member)
if card > 0 {
res, err := DB.Get(DefaultReadOptions, key.Key())
if err != nil {
return err
}
if res != nil {
continue
}
}
wb.Put(key.Key(), []byte{})
newMembers++
}
if newMembers > 0 {
setCard(mk, card+newMembers, wb)
}
return newMembers
}
示例2: Hincrby
func Hincrby(args [][]byte, wb *levigo.WriteBatch) interface{} {
mk := metaKey(args[0])
length, err := hlen(mk, nil)
if err != nil {
return err
}
key := NewKeyBufferWithSuffix(HashKey, args[0], args[1]).Key()
res, err := DB.Get(DefaultReadOptions, key)
if err != nil {
return err
}
var current int64
if res != nil {
current, err = bconv.ParseInt(res, 10, 64)
if err != nil {
return InvalidIntError
}
}
increment, err := bconv.ParseInt(args[2], 10, 64)
if err != nil {
return InvalidIntError
}
result := strconv.AppendInt(nil, current+increment, 10)
wb.Put(key, result)
// if is a new key, increment the hash length
if res == nil {
setHlen(mk, length+1, wb)
}
return result
}
示例3: Hdel
func Hdel(args [][]byte, wb *levigo.WriteBatch) interface{} {
mk := metaKey(args[0])
length, err := hlen(mk, nil)
if err != nil {
return err
}
if length == 0 {
return 0
}
var deleted uint32
key := NewKeyBuffer(HashKey, args[0], len(args[1]))
for _, field := range args[1:] {
key.SetSuffix(field)
res, err := DB.Get(ReadWithoutCacheFill, key.Key())
if err != nil {
return err
}
if res == nil {
continue
}
wb.Delete(key.Key())
deleted++
}
if deleted == length {
wb.Delete(mk)
} else if deleted > 0 {
setHlen(mk, length-deleted, wb)
}
return deleted
}
示例4: Hincrbyfloat
func Hincrbyfloat(args [][]byte, wb *levigo.WriteBatch) interface{} {
mk := metaKey(args[0])
length, err := hlen(mk, nil)
if err != nil {
return err
}
key := NewKeyBufferWithSuffix(HashKey, args[0], args[1]).Key()
res, err := DB.Get(DefaultReadOptions, key)
if err != nil {
return err
}
var current float64
if res != nil {
current, err = bconv.ParseFloat(res, 64)
if err != nil {
return fmt.Errorf("hash value is not a valid float")
}
}
increment, err := bconv.ParseFloat(args[2], 64)
if err != nil {
return fmt.Errorf("value is not a valid float")
}
result := strconv.AppendFloat(nil, current+increment, 'f', -1, 64)
wb.Put(key, result)
// if is a new key, increment the hash length
if res == nil {
setHlen(mk, length+1, wb)
}
return result
}
示例5: Hmset
func Hmset(args [][]byte, wb *levigo.WriteBatch) interface{} {
if (len(args)-1)%2 != 0 {
return fmt.Errorf("wrong number of arguments for 'hmset' command")
}
mk := metaKey(args[0])
length, err := hlen(mk, nil)
if err != nil {
return err
}
var added uint32
key := NewKeyBuffer(HashKey, args[0], len(args[1]))
for i := 1; i < len(args); i += 2 {
key.SetSuffix(args[i])
var res []byte
if length > 0 {
res, err = DB.Get(DefaultReadOptions, key.Key())
if err != nil {
return err
}
}
if res == nil {
added++
}
wb.Put(key.Key(), args[i+1])
}
if added > 0 {
setHlen(mk, length+added, wb)
}
return ReplyOK
}
示例6: Srem
func Srem(args [][]byte, wb *levigo.WriteBatch) interface{} {
mk := metaKey(args[0])
card, err := scard(mk, nil)
if err != nil {
return err
}
if card == 0 {
return card
}
var deleted uint32
key := NewKeyBuffer(SetKey, args[0], len(args[1]))
for _, member := range args[1:] {
key.SetSuffix(member)
res, err := DB.Get(ReadWithoutCacheFill, key.Key())
if err != nil {
return err
}
if res == nil {
continue
}
wb.Delete(key.Key())
deleted++
}
if deleted == card {
wb.Delete(mk)
} else if deleted > 0 { // decrement the cardinality
setCard(mk, card-deleted, wb)
}
return deleted
}
示例7: lpush
func lpush(args [][]byte, left bool, create bool, wb *levigo.WriteBatch) (interface{}, error) {
mk := metaKey(args[0])
l, err := llen(mk, nil)
if err != nil {
return nil, err
}
if create || l.length > 0 {
key := NewKeyBuffer(ListKey, args[0], 8)
for _, value := range args[1:] {
l.length++
var seq int64
if left {
seq = l.left
l.left--
} else {
seq = l.right
l.right++
}
// To sort negative ints in order before positive, we subtract math.MinInt64
// which wraps the numbers around and sorts correctly
binary.BigEndian.PutUint64(key.SuffixForRead(8), uint64(seq-math.MinInt64))
wb.Put(key.Key(), value)
}
setLlen(mk, l, wb)
}
return l.length, nil
}
示例8: setLlen
func setLlen(key []byte, l *listDetails, wb *levigo.WriteBatch) {
data := make([]byte, 22)
data[0] = ListLengthValue
binary.BigEndian.PutUint32(data[1:], l.length)
data[5] = l.flags
binary.BigEndian.PutUint64(data[6:], uint64(l.left))
binary.BigEndian.PutUint64(data[14:], uint64(l.right))
wb.Put(key, data)
}
示例9: AddIndex
func AddIndex(index [2]string, key []byte, indexDb *levigo.DB, wb *levigo.WriteBatch) error {
searchKey := []byte(index[0] + "~" + index[1])
keys, err := indexDb.Get(LReadOptions, searchKey)
if err != nil {
return err
}
keys = appendDataKey(keys, key)
wb.Put(searchKey, keys)
return nil
}
示例10: combineSet
func combineSet(keys [][]byte, op int, wb *levigo.WriteBatch) interface{} {
var count uint32
res := []interface{}{}
members := make(chan *iterSetMember)
var storeKey *KeyBuffer
var mk []byte
if wb != nil {
mk = metaKey(keys[0])
_, err := delKey(mk, wb)
if err != nil {
return err
}
storeKey = NewKeyBuffer(SetKey, keys[0], 0)
keys = keys[1:]
}
go multiSetIter(keys, members, op != setUnion)
combine:
for m := range members {
switch op {
case setInter:
for _, k := range m.exists {
if !k {
continue combine
}
}
case setDiff:
for i, k := range m.exists {
if i == 0 && !k || i > 0 && k {
continue combine
}
}
}
if wb != nil {
storeKey.SetSuffix(m.member)
wb.Put(storeKey.Key(), []byte{})
count++
} else {
res = append(res, m.member)
}
}
if wb != nil {
if count > 0 {
setCard(mk, count, wb)
}
return count
}
return res
}
示例11: DelHash
func DelHash(key []byte, wb *levigo.WriteBatch) {
it := DB.NewIterator(ReadWithoutCacheFill)
defer it.Close()
iterKey := NewKeyBuffer(HashKey, key, 0)
for it.Seek(iterKey.Key()); it.Valid(); it.Next() {
k := it.Key()
if !iterKey.IsPrefixOf(k) {
break
}
wb.Delete(k)
}
}
示例12: RemoveIndex
// TODO: refactor with above.
func RemoveIndex(index [2]string, key []byte, indexDb *levigo.DB, wb *levigo.WriteBatch) error {
searchKey := []byte(index[0] + "~" + index[1])
keys, err := indexDb.Get(LReadOptions, searchKey)
if err != nil {
return err
}
keys = removeDataKey(keys, key)
if len(keys) > 0 {
wb.Put(searchKey, keys)
} else {
wb.Delete(searchKey)
}
return nil
}
示例13: DelSet
func DelSet(key []byte, wb *levigo.WriteBatch) {
it := DB.NewIterator(ReadWithoutCacheFill)
defer it.Close()
iterKey := NewKeyBuffer(SetKey, key, 0)
for it.Seek(iterKey.Key()); it.Valid(); it.Next() {
k := it.Key()
// If the prefix of the current key doesn't match the iteration key,
// we have reached the end of the set
if !iterKey.IsPrefixOf(k) {
break
}
wb.Delete(k)
}
}
示例14: delKey
func delKey(key []byte, wb *levigo.WriteBatch) (deleted bool, err error) {
res, err := DB.Get(ReadWithoutCacheFill, key)
if err != nil {
return
}
if res == nil {
return
}
if len(res) < 1 {
return false, InvalidDataError
}
del(key[1:], res[0], wb)
wb.Delete(key)
return true, nil
}
示例15: lpop
func lpop(key []byte, left bool, wb *levigo.WriteBatch) (interface{}, error) {
mk := metaKey(key)
l, err := llen(mk, nil)
if err != nil {
return nil, err
}
if l.length == 0 {
return nil, nil
}
iterKey := NewKeyBuffer(ListKey, key, 0)
it := DB.NewIterator(ReadWithoutCacheFill)
defer it.Close()
if !left {
iterKey.ReverseIterKey()
}
it.Seek(iterKey.Key())
if !left {
it.Prev()
}
if !it.Valid() {
return nil, nil
}
k := it.Key()
if !iterKey.IsPrefixOf(k) {
return nil, nil
}
res := it.Value()
wb.Delete(k)
l.length--
if l.length == 0 {
wb.Delete(mk)
} else {
// decode the sequence number from the list item key
seq := int64(binary.BigEndian.Uint64(k[len(key)+5:])) + math.MinInt64
if left {
l.left = seq
} else {
l.right = seq
}
setLlen(mk, l, wb)
}
return res, nil
}