Commit 6c431a55 authored by Levin Zimmermann's avatar Levin Zimmermann

proto: Update 'Compression' to int to support different compression algorithms

With nexedi/neoppod@fd80cc30 NEO/py added support to encode the compression
algorithm with the 'Compression' parameter. Before this, compression could
only be true (= with compression) or false (= without compression). Now
the absence of compression is encoded with 0. Any other number than 0
encodes a compression algorithm. The mapping is currently:

	1 = zlib

In the future, 2 could mean zstd [1].

[1] https://github.com/facebook/zstd/issues/1134
parent b7b950fa
......@@ -381,7 +381,10 @@ func (c *Client) Load(ctx context.Context, xid zodb.Xid) (buf *mem.Buf, serial z
}
}
if resp.Compression {
if resp.Compression != 0 {
if resp.Compression != 1 {
return nil, 0, fmt.Errorf("unsupported compression algorithm: %v", resp.Compression)
}
buf2 := &mem.Buf{Data: nil}
udata, err := xzlib.Decompress(buf.Data)
buf.Release()
......
......@@ -257,7 +257,7 @@ func _TestMasterStorage(t0 *tEnv) {
Oid: xid1.Oid,
Serial: serial1,
NextSerial: proto.INVALID_TID,
Compression: false,
Compression: 0,
Data: buf1,
DataSerial: 0, // XXX
Checksum: sha1.Sum(buf1.Data),
......@@ -292,7 +292,7 @@ func _TestMasterStorage(t0 *tEnv) {
Oid: xid1prev.Oid,
Serial: serial1prev,
NextSerial: serial1,
Compression: false,
Compression: 0,
Data: buf1prev,
DataSerial: 0, // XXX
Checksum: sha1.Sum(buf1prev.Data),
......
......@@ -336,6 +336,11 @@ func (a *Address) neoDecodeN(b []byte) (uint64, bool) {
// Checksum is a SHA1 hash.
type Checksum [20]byte
// Compression is an integer that tells the compression algorithm:
// 0 = no compression algorithm is used
// 1 = zlib is used
type Compression uint8
// PTid is Partition Table identifier.
//
// Zero value means "invalid id" (<-> None in py.PPTID, nil in msgpack)
......@@ -713,7 +718,7 @@ type AnswerRebaseObject struct {
Serial zodb.Tid
ConflictSerial zodb.Tid
// FIXME POption('data')
Compression bool
Compression Compression
Checksum Checksum
Data *mem.Buf
}
......@@ -727,7 +732,7 @@ type AnswerRebaseObject struct {
type StoreObject struct {
Oid zodb.Oid
Serial zodb.Tid
Compression bool
Compression Compression
Checksum Checksum
Data []byte // TODO -> msg.Buf, separately (for writev)
DataSerial zodb.Tid
......@@ -782,7 +787,7 @@ type AnswerObject struct {
Oid zodb.Oid
Serial zodb.Tid
NextSerial zodb.Tid
Compression bool
Compression Compression
Checksum Checksum
Data *mem.Buf // TODO encode -> separately (for writev)
DataSerial zodb.Tid
......@@ -1217,7 +1222,7 @@ type AddTransaction struct {
type AddObject struct {
Oid zodb.Oid
Serial zodb.Tid
Compression bool
Compression Compression
Checksum Checksum
Data *mem.Buf
DataSerial zodb.Tid
......
......@@ -182,7 +182,7 @@ func TestMsgMarshal(t *testing.T) {
{&StoreObject{
Oid: 0x0102030405060708,
Serial: 0x0a0b0c0d0e0f0102,
Compression: false,
Compression: 0,
Checksum: Checksum{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}, // XXX simpler?
Data: []byte("hello world"),
DataSerial: 0x0a0b0c0d0e0f0103,
......@@ -198,7 +198,7 @@ func TestMsgMarshal(t *testing.T) {
hex("97") +
hex("c408") + hex("0102030405060708") +
hex("c408") + hex("0a0b0c0d0e0f0102") +
hex("c2") +
hex("00") +
hex("c414") + hex("0102030405060708090a0b0c0d0e0f1011121314") +
hex("c40b") + "hello world" +
hex("c408") + hex("0a0b0c0d0e0f0103") +
......
......@@ -4833,7 +4833,7 @@ func (p *AnswerRebaseObject) neoMsgEncodedLenN() int {
func (p *AnswerRebaseObject) neoMsgEncodeN(data []byte) {
binary.BigEndian.PutUint64(data[0:], uint64(p.Serial))
binary.BigEndian.PutUint64(data[8:], uint64(p.ConflictSerial))
(data[16:])[0] = bool2byte(p.Compression)
(data[16:])[0] = uint8(p.Compression)
copy(data[17:], p.Checksum[:])
{
l := uint32(len(p.Data.XData()))
......@@ -4851,7 +4851,7 @@ func (p *AnswerRebaseObject) neoMsgDecodeN(data []byte) (int, error) {
}
p.Serial = zodb.Tid(binary.BigEndian.Uint64(data[0 : 0+8]))
p.ConflictSerial = zodb.Tid(binary.BigEndian.Uint64(data[8 : 8+8]))
p.Compression = byte2bool((data[16 : 16+1])[0])
p.Compression = Compression((data[16 : 16+1])[0])
copy(p.Checksum[:], data[17:37])
{
l := binary.BigEndian.Uint32(data[37 : 37+4])
......@@ -4882,7 +4882,7 @@ func (p *AnswerRebaseObject) neoMsgEncodedLenM() int {
} else {
size += 1 + 1 + 8 // mbin8 + 8 + [8]data
}
return 23 + msgpack.ArrayHeadSize(reflect.TypeOf((*p)).NumField()) + msgpack.BinHeadSize(len(p.Data.XData())) + len(p.Data.XData()) + size
return 22 + msgpack.ArrayHeadSize(reflect.TypeOf((*p)).NumField()) + msgpack.Uint8Size(uint8(p.Compression)) + msgpack.BinHeadSize(len(p.Data.XData())) + len(p.Data.XData()) + size
}
func (p *AnswerRebaseObject) neoMsgEncodeM(data []byte) {
......@@ -4905,14 +4905,17 @@ func (p *AnswerRebaseObject) neoMsgEncodeM(data []byte) {
binary.BigEndian.PutUint64(data[2:], uint64(p.ConflictSerial))
data = data[10:]
}
data[0] = byte(msgpack.Bool(p.Compression))
data[1] = byte(msgpack.Bin8)
data[2] = 20
copy(data[3:], p.Checksum[:])
{
n := msgpack.PutUint8(data[0:], uint8(p.Compression))
data = data[0+n:]
}
data[0] = byte(msgpack.Bin8)
data[1] = 20
copy(data[2:], p.Checksum[:])
{
l := len(p.Data.XData())
n := msgpack.PutBinHead(data[23:], l)
data = data[23+n:]
n := msgpack.PutBinHead(data[22:], l)
data = data[22+n:]
copy(data, p.Data.XData())
data = data[l:]
}
......@@ -4962,25 +4965,26 @@ func (p *AnswerRebaseObject) neoMsgDecodeM(data []byte) (int, error) {
p.ConflictSerial = zodb.Tid(binary.BigEndian.Uint64(data[2:]))
data = data[10:]
}
if len(data) < 23 {
goto overflow
{
v, tail, err := msgp.ReadUint64Bytes(data)
if err != nil {
return 0, mdecodeErr("AnswerRebaseObject.Compression", err)
}
switch op := msgpack.Op(data[0]); op {
default:
return 0, mdecodeOpErr("AnswerRebaseObject.Compression", op, msgpack.True, msgpack.False)
case msgpack.True:
p.Compression = true
case msgpack.False:
p.Compression = false
p.Compression = Compression(v)
nread += uint64(len(data) - len(tail))
data = tail
}
if len(data) < 22 {
goto overflow
}
if op := msgpack.Op(data[1]); op != msgpack.Bin8 {
if op := msgpack.Op(data[0]); op != msgpack.Bin8 {
return 0, mdecodeOpErr("AnswerRebaseObject.Checksum", op, msgpack.Bin8)
}
if l := data[2]; l != 20 {
if l := data[1]; l != 20 {
return 0, mdecodeLen8Err("AnswerRebaseObject.Checksum", l, 20)
}
copy(p.Checksum[:], data[3:23])
data = data[23:]
copy(p.Checksum[:], data[2:22])
data = data[22:]
{
b, tail, err := msgp.ReadBytesZC(data)
if err != nil {
......@@ -4991,7 +4995,7 @@ func (p *AnswerRebaseObject) neoMsgDecodeM(data []byte) (int, error) {
nread += uint64(len(data) - len(tail))
data = tail
}
return 23 + int(nread), nil
return 22 + int(nread), nil
overflow:
return 0, ErrDecodeOverflow
......@@ -5010,7 +5014,7 @@ func (p *StoreObject) neoMsgEncodedLenN() int {
func (p *StoreObject) neoMsgEncodeN(data []byte) {
binary.BigEndian.PutUint64(data[0:], uint64(p.Oid))
binary.BigEndian.PutUint64(data[8:], uint64(p.Serial))
(data[16:])[0] = bool2byte(p.Compression)
(data[16:])[0] = uint8(p.Compression)
copy(data[17:], p.Checksum[:])
{
l := uint32(len(p.Data))
......@@ -5030,7 +5034,7 @@ func (p *StoreObject) neoMsgDecodeN(data []byte) (int, error) {
}
p.Oid = zodb.Oid(binary.BigEndian.Uint64(data[0 : 0+8]))
p.Serial = zodb.Tid(binary.BigEndian.Uint64(data[8 : 8+8]))
p.Compression = byte2bool((data[16 : 16+1])[0])
p.Compression = Compression((data[16 : 16+1])[0])
copy(p.Checksum[:], data[17:37])
{
l := binary.BigEndian.Uint32(data[37 : 37+4])
......@@ -5073,7 +5077,7 @@ func (p *StoreObject) neoMsgEncodedLenM() int {
} else {
size += 1 + 1 + 8 // mbin8 + 8 + [8]data
}
return 23 + msgpack.ArrayHeadSize(reflect.TypeOf((*p)).NumField()) + msgpack.BinHeadSize(len(p.Data)) + len(p.Data) + size
return 22 + msgpack.ArrayHeadSize(reflect.TypeOf((*p)).NumField()) + msgpack.Uint8Size(uint8(p.Compression)) + msgpack.BinHeadSize(len(p.Data)) + len(p.Data) + size
}
func (p *StoreObject) neoMsgEncodeM(data []byte) {
......@@ -5096,14 +5100,17 @@ func (p *StoreObject) neoMsgEncodeM(data []byte) {
binary.BigEndian.PutUint64(data[2:], uint64(p.Serial))
data = data[10:]
}
data[0] = byte(msgpack.Bool(p.Compression))
data[1] = byte(msgpack.Bin8)
data[2] = 20
copy(data[3:], p.Checksum[:])
{
n := msgpack.PutUint8(data[0:], uint8(p.Compression))
data = data[0+n:]
}
data[0] = byte(msgpack.Bin8)
data[1] = 20
copy(data[2:], p.Checksum[:])
{
l := len(p.Data)
n := msgpack.PutBinHead(data[23:], l)
data = data[23+n:]
n := msgpack.PutBinHead(data[22:], l)
data = data[22+n:]
copy(data, p.Data)
data = data[l:]
}
......@@ -5171,25 +5178,26 @@ func (p *StoreObject) neoMsgDecodeM(data []byte) (int, error) {
p.Serial = zodb.Tid(binary.BigEndian.Uint64(data[2:]))
data = data[10:]
}
if len(data) < 23 {
goto overflow
{
v, tail, err := msgp.ReadUint64Bytes(data)
if err != nil {
return 0, mdecodeErr("StoreObject.Compression", err)
}
switch op := msgpack.Op(data[0]); op {
default:
return 0, mdecodeOpErr("StoreObject.Compression", op, msgpack.True, msgpack.False)
case msgpack.True:
p.Compression = true
case msgpack.False:
p.Compression = false
p.Compression = Compression(v)
nread += uint64(len(data) - len(tail))
data = tail
}
if len(data) < 22 {
goto overflow
}
if op := msgpack.Op(data[1]); op != msgpack.Bin8 {
if op := msgpack.Op(data[0]); op != msgpack.Bin8 {
return 0, mdecodeOpErr("StoreObject.Checksum", op, msgpack.Bin8)
}
if l := data[2]; l != 20 {
if l := data[1]; l != 20 {
return 0, mdecodeLen8Err("StoreObject.Checksum", l, 20)
}
copy(p.Checksum[:], data[3:23])
data = data[23:]
copy(p.Checksum[:], data[2:22])
data = data[22:]
{
b, tail, err := msgp.ReadBytesZC(data)
if err != nil {
......@@ -5234,7 +5242,7 @@ func (p *StoreObject) neoMsgDecodeM(data []byte) (int, error) {
p.Tid = zodb.Tid(binary.BigEndian.Uint64(data[2:]))
data = data[10:]
}
return 23 + int(nread), nil
return 22 + int(nread), nil
overflow:
return 0, ErrDecodeOverflow
......@@ -6033,7 +6041,7 @@ func (p *AnswerObject) neoMsgEncodeN(data []byte) {
binary.BigEndian.PutUint64(data[0:], uint64(p.Oid))
binary.BigEndian.PutUint64(data[8:], uint64(p.Serial))
binary.BigEndian.PutUint64(data[16:], uint64(p.NextSerial))
(data[24:])[0] = bool2byte(p.Compression)
(data[24:])[0] = uint8(p.Compression)
copy(data[25:], p.Checksum[:])
{
l := uint32(len(p.Data.XData()))
......@@ -6053,7 +6061,7 @@ func (p *AnswerObject) neoMsgDecodeN(data []byte) (int, error) {
p.Oid = zodb.Oid(binary.BigEndian.Uint64(data[0 : 0+8]))
p.Serial = zodb.Tid(binary.BigEndian.Uint64(data[8 : 8+8]))
p.NextSerial = zodb.Tid(binary.BigEndian.Uint64(data[16 : 16+8]))
p.Compression = byte2bool((data[24 : 24+1])[0])
p.Compression = Compression((data[24 : 24+1])[0])
copy(p.Checksum[:], data[25:45])
{
l := binary.BigEndian.Uint32(data[45 : 45+4])
......@@ -6095,7 +6103,7 @@ func (p *AnswerObject) neoMsgEncodedLenM() int {
} else {
size += 1 + 1 + 8 // mbin8 + 8 + [8]data
}
return 23 + msgpack.ArrayHeadSize(reflect.TypeOf((*p)).NumField()) + msgpack.BinHeadSize(len(p.Data.XData())) + len(p.Data.XData()) + size
return 22 + msgpack.ArrayHeadSize(reflect.TypeOf((*p)).NumField()) + msgpack.Uint8Size(uint8(p.Compression)) + msgpack.BinHeadSize(len(p.Data.XData())) + len(p.Data.XData()) + size
}
func (p *AnswerObject) neoMsgEncodeM(data []byte) {
......@@ -6127,14 +6135,17 @@ func (p *AnswerObject) neoMsgEncodeM(data []byte) {
binary.BigEndian.PutUint64(data[2:], uint64(p.NextSerial))
data = data[10:]
}
data[0] = byte(msgpack.Bool(p.Compression))
data[1] = byte(msgpack.Bin8)
data[2] = 20
copy(data[3:], p.Checksum[:])
{
n := msgpack.PutUint8(data[0:], uint8(p.Compression))
data = data[0+n:]
}
data[0] = byte(msgpack.Bin8)
data[1] = 20
copy(data[2:], p.Checksum[:])
{
l := len(p.Data.XData())
n := msgpack.PutBinHead(data[23:], l)
data = data[23+n:]
n := msgpack.PutBinHead(data[22:], l)
data = data[22+n:]
copy(data, p.Data.XData())
data = data[l:]
}
......@@ -6210,25 +6221,26 @@ func (p *AnswerObject) neoMsgDecodeM(data []byte) (int, error) {
p.NextSerial = zodb.Tid(binary.BigEndian.Uint64(data[2:]))
data = data[10:]
}
if len(data) < 23 {
goto overflow
{
v, tail, err := msgp.ReadUint64Bytes(data)
if err != nil {
return 0, mdecodeErr("AnswerObject.Compression", err)
}
switch op := msgpack.Op(data[0]); op {
default:
return 0, mdecodeOpErr("AnswerObject.Compression", op, msgpack.True, msgpack.False)
case msgpack.True:
p.Compression = true
case msgpack.False:
p.Compression = false
p.Compression = Compression(v)
nread += uint64(len(data) - len(tail))
data = tail
}
if len(data) < 22 {
goto overflow
}
if op := msgpack.Op(data[1]); op != msgpack.Bin8 {
if op := msgpack.Op(data[0]); op != msgpack.Bin8 {
return 0, mdecodeOpErr("AnswerObject.Checksum", op, msgpack.Bin8)
}
if l := data[2]; l != 20 {
if l := data[1]; l != 20 {
return 0, mdecodeLen8Err("AnswerObject.Checksum", l, 20)
}
copy(p.Checksum[:], data[3:23])
data = data[23:]
copy(p.Checksum[:], data[2:22])
data = data[22:]
{
b, tail, err := msgp.ReadBytesZC(data)
if err != nil {
......@@ -6256,7 +6268,7 @@ func (p *AnswerObject) neoMsgDecodeM(data []byte) (int, error) {
p.DataSerial = zodb.Tid(binary.BigEndian.Uint64(data[2:]))
data = data[10:]
}
return 23 + int(nread), nil
return 22 + int(nread), nil
overflow:
return 0, ErrDecodeOverflow
......@@ -13006,7 +13018,7 @@ func (p *AddObject) neoMsgEncodedLenN() int {
func (p *AddObject) neoMsgEncodeN(data []byte) {
binary.BigEndian.PutUint64(data[0:], uint64(p.Oid))
binary.BigEndian.PutUint64(data[8:], uint64(p.Serial))
(data[16:])[0] = bool2byte(p.Compression)
(data[16:])[0] = uint8(p.Compression)
copy(data[17:], p.Checksum[:])
{
l := uint32(len(p.Data.XData()))
......@@ -13025,7 +13037,7 @@ func (p *AddObject) neoMsgDecodeN(data []byte) (int, error) {
}
p.Oid = zodb.Oid(binary.BigEndian.Uint64(data[0 : 0+8]))
p.Serial = zodb.Tid(binary.BigEndian.Uint64(data[8 : 8+8]))
p.Compression = byte2bool((data[16 : 16+1])[0])
p.Compression = Compression((data[16 : 16+1])[0])
copy(p.Checksum[:], data[17:37])
{
l := binary.BigEndian.Uint32(data[37 : 37+4])
......@@ -13062,7 +13074,7 @@ func (p *AddObject) neoMsgEncodedLenM() int {
} else {
size += 1 + 1 + 8 // mbin8 + 8 + [8]data
}
return 23 + msgpack.ArrayHeadSize(reflect.TypeOf((*p)).NumField()) + msgpack.BinHeadSize(len(p.Data.XData())) + len(p.Data.XData()) + size
return 22 + msgpack.ArrayHeadSize(reflect.TypeOf((*p)).NumField()) + msgpack.Uint8Size(uint8(p.Compression)) + msgpack.BinHeadSize(len(p.Data.XData())) + len(p.Data.XData()) + size
}
func (p *AddObject) neoMsgEncodeM(data []byte) {
......@@ -13085,14 +13097,17 @@ func (p *AddObject) neoMsgEncodeM(data []byte) {
binary.BigEndian.PutUint64(data[2:], uint64(p.Serial))
data = data[10:]
}
data[0] = byte(msgpack.Bool(p.Compression))
data[1] = byte(msgpack.Bin8)
data[2] = 20
copy(data[3:], p.Checksum[:])
{
n := msgpack.PutUint8(data[0:], uint8(p.Compression))
data = data[0+n:]
}
data[0] = byte(msgpack.Bin8)
data[1] = 20
copy(data[2:], p.Checksum[:])
{
l := len(p.Data.XData())
n := msgpack.PutBinHead(data[23:], l)
data = data[23+n:]
n := msgpack.PutBinHead(data[22:], l)
data = data[22+n:]
copy(data, p.Data.XData())
data = data[l:]
}
......@@ -13151,25 +13166,26 @@ func (p *AddObject) neoMsgDecodeM(data []byte) (int, error) {
p.Serial = zodb.Tid(binary.BigEndian.Uint64(data[2:]))
data = data[10:]
}
if len(data) < 23 {
goto overflow
{
v, tail, err := msgp.ReadUint64Bytes(data)
if err != nil {
return 0, mdecodeErr("AddObject.Compression", err)
}
switch op := msgpack.Op(data[0]); op {
default:
return 0, mdecodeOpErr("AddObject.Compression", op, msgpack.True, msgpack.False)
case msgpack.True:
p.Compression = true
case msgpack.False:
p.Compression = false
p.Compression = Compression(v)
nread += uint64(len(data) - len(tail))
data = tail
}
if len(data) < 22 {
goto overflow
}
if op := msgpack.Op(data[1]); op != msgpack.Bin8 {
if op := msgpack.Op(data[0]); op != msgpack.Bin8 {
return 0, mdecodeOpErr("AddObject.Checksum", op, msgpack.Bin8)
}
if l := data[2]; l != 20 {
if l := data[1]; l != 20 {
return 0, mdecodeLen8Err("AddObject.Checksum", l, 20)
}
copy(p.Checksum[:], data[3:23])
data = data[23:]
copy(p.Checksum[:], data[2:22])
data = data[22:]
{
b, tail, err := msgp.ReadBytesZC(data)
if err != nil {
......@@ -13197,7 +13213,7 @@ func (p *AddObject) neoMsgDecodeM(data []byte) (int, error) {
p.DataSerial = zodb.Tid(binary.BigEndian.Uint64(data[2:]))
data = data[10:]
}
return 23 + int(nread), nil
return 22 + int(nread), nil
overflow:
return 0, ErrDecodeOverflow
......
......@@ -87,7 +87,7 @@ func (f *Backend) Load(ctx context.Context, xid zodb.Xid) (*proto.AnswerObject,
Serial: serial,
NextSerial: nextSerial,
Compression: false,
Compression: 0,
Data: buf,
Checksum: xsha1.NEOSum(buf.Data), // XXX computing every time
......
......@@ -68,7 +68,7 @@ loop:
buf := obj.Data
if obj.Compression {
if obj.Compression != 0 {
b.Fatal("compression was used")
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment