ledisdb/ledis/event.go

137 lines
2.9 KiB
Go
Raw Permalink Normal View History

2014-05-29 11:07:14 +04:00
package ledis
import (
2014-06-04 10:42:02 +04:00
"errors"
2014-07-11 09:28:34 +04:00
"fmt"
"strconv"
2015-05-04 17:42:28 +03:00
"github.com/siddontang/go/hack"
2014-06-04 10:42:02 +04:00
)
var errInvalidEvent = errors.New("invalid event")
2014-07-11 09:28:34 +04:00
2014-09-17 13:54:04 +04:00
func formatEventKey(buf []byte, k []byte) ([]byte, error) {
2014-07-11 09:28:34 +04:00
if len(k) < 2 {
2014-09-17 13:54:04 +04:00
return nil, errInvalidEvent
2014-07-11 09:28:34 +04:00
}
buf = append(buf, fmt.Sprintf("DB:%2d ", k[0])...)
buf = append(buf, fmt.Sprintf("%s ", TypeName[k[1]])...)
db := new(DB)
2015-03-15 15:36:14 +03:00
index, _, err := decodeDBIndex(k)
if err != nil {
return nil, err
}
db.setIndex(index)
2014-07-11 09:28:34 +04:00
//to do format at respective place
switch k[1] {
case KVType:
2018-03-29 15:33:36 +03:00
key, err := db.decodeKVKey(k)
if err != nil {
2014-07-11 09:28:34 +04:00
return nil, err
}
2018-03-29 15:33:36 +03:00
buf = strconv.AppendQuote(buf, hack.String(key))
2014-07-11 09:28:34 +04:00
case HashType:
2018-03-29 15:33:36 +03:00
key, field, err := db.hDecodeHashKey(k)
if err != nil {
2014-07-11 09:28:34 +04:00
return nil, err
}
2018-03-29 15:33:36 +03:00
buf = strconv.AppendQuote(buf, hack.String(key))
buf = append(buf, ' ')
buf = strconv.AppendQuote(buf, hack.String(field))
2014-07-11 09:28:34 +04:00
case HSizeType:
2018-03-29 15:33:36 +03:00
key, err := db.hDecodeSizeKey(k)
if err != nil {
2014-07-11 09:28:34 +04:00
return nil, err
}
2018-03-29 15:33:36 +03:00
buf = strconv.AppendQuote(buf, hack.String(key))
2014-07-11 09:28:34 +04:00
case ListType:
2018-03-29 15:33:36 +03:00
key, seq, err := db.lDecodeListKey(k)
if err != nil {
2014-07-11 09:28:34 +04:00
return nil, err
}
2018-03-29 15:33:36 +03:00
buf = strconv.AppendQuote(buf, hack.String(key))
buf = append(buf, ' ')
buf = strconv.AppendInt(buf, int64(seq), 10)
2014-07-11 09:28:34 +04:00
case LMetaType:
2018-03-29 15:33:36 +03:00
key, err := db.lDecodeMetaKey(k)
if err != nil {
2014-07-11 09:28:34 +04:00
return nil, err
}
2018-03-29 15:33:36 +03:00
buf = strconv.AppendQuote(buf, hack.String(key))
2014-07-11 09:28:34 +04:00
case ZSetType:
2018-03-29 15:33:36 +03:00
key, m, err := db.zDecodeSetKey(k)
if err != nil {
2014-07-11 09:28:34 +04:00
return nil, err
}
2018-03-29 15:33:36 +03:00
buf = strconv.AppendQuote(buf, hack.String(key))
buf = append(buf, ' ')
buf = strconv.AppendQuote(buf, hack.String(m))
2014-07-11 09:28:34 +04:00
case ZSizeType:
2018-03-29 15:33:36 +03:00
key, err := db.zDecodeSizeKey(k)
if err != nil {
2014-07-11 09:28:34 +04:00
return nil, err
}
2018-03-29 15:33:36 +03:00
buf = strconv.AppendQuote(buf, hack.String(key))
2014-07-11 09:28:34 +04:00
case ZScoreType:
2018-03-29 15:33:36 +03:00
key, m, score, err := db.zDecodeScoreKey(k)
if err != nil {
2014-07-11 09:28:34 +04:00
return nil, err
}
2018-03-29 15:33:36 +03:00
buf = strconv.AppendQuote(buf, hack.String(key))
buf = append(buf, ' ')
buf = strconv.AppendQuote(buf, hack.String(m))
buf = append(buf, ' ')
buf = strconv.AppendInt(buf, score, 10)
2014-08-25 10:18:23 +04:00
case SetType:
2018-03-29 15:33:36 +03:00
key, member, err := db.sDecodeSetKey(k)
if err != nil {
2014-08-25 10:18:23 +04:00
return nil, err
}
2018-03-29 15:33:36 +03:00
buf = strconv.AppendQuote(buf, hack.String(key))
buf = append(buf, ' ')
buf = strconv.AppendQuote(buf, hack.String(member))
2014-08-25 10:18:23 +04:00
case SSizeType:
2018-03-29 15:33:36 +03:00
key, err := db.sDecodeSizeKey(k)
if err != nil {
2014-08-25 10:18:23 +04:00
return nil, err
}
2018-03-29 15:33:36 +03:00
buf = strconv.AppendQuote(buf, hack.String(key))
2014-07-11 09:28:34 +04:00
case ExpTimeType:
2018-03-29 15:33:36 +03:00
tp, key, t, err := db.expDecodeTimeKey(k)
if err != nil {
2014-07-11 09:28:34 +04:00
return nil, err
}
2018-03-29 15:33:36 +03:00
buf = append(buf, TypeName[tp]...)
buf = append(buf, ' ')
buf = strconv.AppendQuote(buf, hack.String(key))
buf = append(buf, ' ')
buf = strconv.AppendInt(buf, t, 10)
2014-07-11 09:28:34 +04:00
case ExpMetaType:
2018-03-29 15:33:36 +03:00
tp, key, err := db.expDecodeMetaKey(k)
if err != nil {
2014-07-11 09:28:34 +04:00
return nil, err
}
2018-03-29 15:33:36 +03:00
buf = append(buf, TypeName[tp]...)
buf = append(buf, ' ')
buf = strconv.AppendQuote(buf, hack.String(key))
2014-07-11 09:28:34 +04:00
default:
2014-09-17 13:54:04 +04:00
return nil, errInvalidEvent
2014-07-11 09:28:34 +04:00
}
return buf, nil
}