ledisdb/ledis/event.go

136 lines
3.4 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"
2014-09-24 08:34:21 +04:00
"github.com/siddontang/go/hack"
2014-07-11 09:28:34 +04:00
"strconv"
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)
db.index = k[0]
//to do format at respective place
switch k[1] {
case KVType:
if key, err := db.decodeKVKey(k); err != nil {
return nil, err
} else {
2014-09-24 08:34:21 +04:00
buf = strconv.AppendQuote(buf, hack.String(key))
2014-07-11 09:28:34 +04:00
}
case HashType:
if key, field, err := db.hDecodeHashKey(k); err != nil {
return nil, err
} else {
2014-09-24 08:34:21 +04:00
buf = strconv.AppendQuote(buf, hack.String(key))
2014-07-11 09:28:34 +04:00
buf = append(buf, ' ')
2014-09-24 08:34:21 +04:00
buf = strconv.AppendQuote(buf, hack.String(field))
2014-07-11 09:28:34 +04:00
}
case HSizeType:
if key, err := db.hDecodeSizeKey(k); err != nil {
return nil, err
} else {
2014-09-24 08:34:21 +04:00
buf = strconv.AppendQuote(buf, hack.String(key))
2014-07-11 09:28:34 +04:00
}
case ListType:
if key, seq, err := db.lDecodeListKey(k); err != nil {
return nil, err
} else {
2014-09-24 08:34:21 +04:00
buf = strconv.AppendQuote(buf, hack.String(key))
2014-07-11 09:28:34 +04:00
buf = append(buf, ' ')
buf = strconv.AppendInt(buf, int64(seq), 10)
}
case LMetaType:
if key, err := db.lDecodeMetaKey(k); err != nil {
return nil, err
} else {
2014-09-24 08:34:21 +04:00
buf = strconv.AppendQuote(buf, hack.String(key))
2014-07-11 09:28:34 +04:00
}
case ZSetType:
if key, m, err := db.zDecodeSetKey(k); err != nil {
return nil, err
} else {
2014-09-24 08:34:21 +04:00
buf = strconv.AppendQuote(buf, hack.String(key))
2014-07-11 09:28:34 +04:00
buf = append(buf, ' ')
2014-09-24 08:34:21 +04:00
buf = strconv.AppendQuote(buf, hack.String(m))
2014-07-11 09:28:34 +04:00
}
case ZSizeType:
if key, err := db.zDecodeSizeKey(k); err != nil {
return nil, err
} else {
2014-09-24 08:34:21 +04:00
buf = strconv.AppendQuote(buf, hack.String(key))
2014-07-11 09:28:34 +04:00
}
case ZScoreType:
if key, m, score, err := db.zDecodeScoreKey(k); err != nil {
return nil, err
} else {
2014-09-24 08:34:21 +04:00
buf = strconv.AppendQuote(buf, hack.String(key))
2014-07-11 09:28:34 +04:00
buf = append(buf, ' ')
2014-09-24 08:34:21 +04:00
buf = strconv.AppendQuote(buf, hack.String(m))
2014-07-11 09:28:34 +04:00
buf = append(buf, ' ')
buf = strconv.AppendInt(buf, score, 10)
}
2015-03-03 04:21:00 +03:00
// case BitType:
// if key, seq, err := db.bDecodeBinKey(k); err != nil {
// return nil, err
// } else {
// buf = strconv.AppendQuote(buf, hack.String(key))
// buf = append(buf, ' ')
// buf = strconv.AppendUint(buf, uint64(seq), 10)
// }
// case BitMetaType:
// if key, err := db.bDecodeMetaKey(k); err != nil {
// return nil, err
// } else {
// buf = strconv.AppendQuote(buf, hack.String(key))
// }
2014-08-25 10:18:23 +04:00
case SetType:
if key, member, err := db.sDecodeSetKey(k); err != nil {
return nil, err
} else {
2014-09-24 08:34:21 +04:00
buf = strconv.AppendQuote(buf, hack.String(key))
2014-08-25 10:18:23 +04:00
buf = append(buf, ' ')
2014-09-24 08:34:21 +04:00
buf = strconv.AppendQuote(buf, hack.String(member))
2014-08-25 10:18:23 +04:00
}
case SSizeType:
if key, err := db.sDecodeSizeKey(k); err != nil {
return nil, err
} else {
2014-09-24 08:34:21 +04:00
buf = strconv.AppendQuote(buf, hack.String(key))
2014-08-25 10:18:23 +04:00
}
2014-07-11 09:28:34 +04:00
case ExpTimeType:
if tp, key, t, err := db.expDecodeTimeKey(k); err != nil {
return nil, err
} else {
buf = append(buf, TypeName[tp]...)
buf = append(buf, ' ')
2014-09-24 08:34:21 +04:00
buf = strconv.AppendQuote(buf, hack.String(key))
2014-07-11 09:28:34 +04:00
buf = append(buf, ' ')
buf = strconv.AppendInt(buf, t, 10)
}
case ExpMetaType:
if tp, key, err := db.expDecodeMetaKey(k); err != nil {
return nil, err
} else {
buf = append(buf, TypeName[tp]...)
buf = append(buf, ' ')
2014-09-24 08:34:21 +04:00
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
}