2019-01-10 16:07:16 +00:00
|
|
|
package mailserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2019-01-10 16:07:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DBKeyLength is a size of the envelope key.
|
2019-11-23 17:57:05 +00:00
|
|
|
DBKeyLength = types.HashLength + timestampLength + types.TopicLength
|
|
|
|
CursorLength = types.HashLength + timestampLength
|
2019-01-10 16:07:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrInvalidByteSize is returned when DBKey can't be created
|
|
|
|
// from a byte slice because it has invalid length.
|
|
|
|
ErrInvalidByteSize = errors.New("byte slice has invalid length")
|
|
|
|
)
|
|
|
|
|
|
|
|
// DBKey key to be stored in a db.
|
|
|
|
type DBKey struct {
|
2019-05-09 10:58:02 +00:00
|
|
|
raw []byte
|
2019-01-10 16:07:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes returns a bytes representation of the DBKey.
|
|
|
|
func (k *DBKey) Bytes() []byte {
|
|
|
|
return k.raw
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (k *DBKey) Topic() types.TopicType {
|
|
|
|
return types.BytesToTopic(k.raw[timestampLength+types.HashLength:])
|
2019-05-09 10:58:02 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (k *DBKey) EnvelopeHash() types.Hash {
|
|
|
|
return types.BytesToHash(k.raw[timestampLength : types.HashLength+timestampLength])
|
2019-05-09 10:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k *DBKey) Cursor() []byte {
|
|
|
|
// We don't use the whole cursor for backward compatibility (also it's not needed)
|
|
|
|
return k.raw[:CursorLength]
|
|
|
|
}
|
|
|
|
|
2019-01-10 16:07:16 +00:00
|
|
|
// NewDBKey creates a new DBKey with the given values.
|
2019-11-23 17:57:05 +00:00
|
|
|
func NewDBKey(timestamp uint32, topic types.TopicType, h types.Hash) *DBKey {
|
2019-01-10 16:07:16 +00:00
|
|
|
var k DBKey
|
|
|
|
k.raw = make([]byte, DBKeyLength)
|
2019-05-09 10:58:02 +00:00
|
|
|
binary.BigEndian.PutUint32(k.raw, timestamp)
|
|
|
|
copy(k.raw[timestampLength:], h[:])
|
2019-11-23 17:57:05 +00:00
|
|
|
copy(k.raw[timestampLength+types.HashLength:], topic[:])
|
2019-01-10 16:07:16 +00:00
|
|
|
return &k
|
|
|
|
}
|