Skip to content

Commit a61bfa0

Browse files
author
zhanglei28
committed
support packed type
1 parent 4d409ce commit a61bfa0

8 files changed

+1512
-391
lines changed

breeze.go

+80-24
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,57 @@ import (
66

77
// breeze type
88
const (
9-
NULL = iota
10-
TRUE
11-
FALSE
12-
STRING
13-
BYTE
14-
BYTES
15-
INT16
16-
INT32
17-
INT64
18-
FLOAT32
19-
FLOAT64
20-
21-
MAP = 20
22-
ARRAY = 21
23-
MESSAGE = 22
24-
SCHEMA = 23
9+
StringType = 0x3f
10+
DirectStringMinType = 0x00
11+
DirectStringMaxType = 0x3e
12+
Int32Type = 0x7f
13+
DirectInt32MinType = 0x40
14+
DirectInt32MaxType = 0x7e
15+
Int64Type = 0x98
16+
DirectInt64MinType = 0x80
17+
DirectInt64MaxType = 0x97
18+
NullType = 0x99
19+
TrueType = 0x9a
20+
FalseType = 0x9b
21+
ByteType = 0x9c
22+
BytesType = 0x9d
23+
Int16Type = 0x9e
24+
Float32Type = 0x9f
25+
Float64Type = 0xa0
26+
MapType = 0xd9
27+
ArrayType = 0xda
28+
PackedMapType = 0xdb
29+
PackedArrayType = 0xdc
30+
SchemaType = 0xdd
31+
MessageType = 0xde
32+
RefMessageType = 0xdf
33+
DirectRefMessageMaxType = 0xff
34+
)
35+
36+
// direct value limit
37+
const (
38+
Int32Zero int32 = 0x50
39+
Int64Zero int64 = 0x88
40+
DirectStringMaxLength = DirectStringMaxType
41+
DirectInt32MinValue = DirectInt32MinType - Int32Zero
42+
DirectInt32MaxValue = DirectInt32MaxType - Int32Zero
43+
DirectInt64MinValue = DirectInt64MinType - Int64Zero
44+
DirectInt64MaxValue = DirectInt64MaxType - Int64Zero
45+
DirectRefMessageMaxValue = DirectRefMessageMaxType - RefMessageType
2546
)
2647

2748
// configure
2849
var (
2950
MaxWriteCount = 0 // TODO check circular reference
51+
MaxElemSize = 100000
3052
)
3153

3254
// common errors
3355
var (
34-
ErrNoSchema = errors.New("Breeze: not have breeze schema")
35-
ErrWrongSize = errors.New("Breeze: read byte size not correct")
36-
ErrNotEnough = errors.New("Breeze: not enough bytes")
37-
ErrOverflow = errors.New("Breeze: integer overflow")
56+
ErrNoSchema = errors.New("breeze: not have breeze schema")
57+
ErrWrongSize = errors.New("breeze: read byte size not correct")
58+
ErrNotEnough = errors.New("breeze: not enough bytes")
59+
ErrOverflow = errors.New("breeze: integer overflow")
3860
)
3961

4062
// Message is a interface of breeze message. all breeze message must implement Message
@@ -52,6 +74,40 @@ type Enum interface {
5274
ReadEnum(buf *Buffer, asAddr bool) (Enum, error)
5375
}
5476

77+
// Context is a context of breeze encode/decode in BreezeBuffer
78+
type Context struct {
79+
messageTypeRefCount int
80+
messageTypeRefName map[int]string
81+
messageTypeRefIndex map[string]int
82+
}
83+
84+
func (c *Context) getMessageTypeName(index int) (name string) {
85+
if c.messageTypeRefName != nil {
86+
name = c.messageTypeRefName[index]
87+
}
88+
return name
89+
}
90+
91+
func (c *Context) getMessageTypeIndex(name string) int {
92+
if c.messageTypeRefIndex != nil {
93+
index, ok := c.messageTypeRefIndex[name]
94+
if ok {
95+
return index
96+
}
97+
}
98+
return -1
99+
}
100+
101+
func (c *Context) putMessageType(name string) {
102+
if c.messageTypeRefName == nil {
103+
c.messageTypeRefName = make(map[int]string, 16)
104+
c.messageTypeRefIndex = make(map[string]int, 16)
105+
}
106+
c.messageTypeRefCount++
107+
c.messageTypeRefName[c.messageTypeRefCount] = name
108+
c.messageTypeRefIndex[name] = c.messageTypeRefCount
109+
}
110+
55111
// GenericMessage is a generic breeze message. it can receive any breeze message
56112
type GenericMessage struct {
57113
Name string
@@ -60,23 +116,23 @@ type GenericMessage struct {
60116
fields map[int]interface{}
61117
}
62118

63-
// GetAlias return breeze message alias for multi language compat
119+
// GetAlias return breeze message alias for multi language compatible
64120
func (g *GenericMessage) GetAlias() string {
65121
return g.Alias
66122
}
67123

68124
// WriteTo write breeze message to breeze buffer.
69125
func (g *GenericMessage) WriteTo(buf *Buffer) error {
70-
return WriteMessage(buf, g.Name, func(buf *Buffer) {
126+
return WriteMessageWithoutType(buf, func(buf *Buffer) {
71127
for k, v := range g.fields {
72-
WriteMessageField(buf, k, v)
128+
WriteField(buf, k, v)
73129
}
74130
})
75131
}
76132

77133
// ReadFrom read a breeze message from breeze buffer
78134
func (g *GenericMessage) ReadFrom(buf *Buffer) error {
79-
return ReadMessageByField(buf, func(buf *Buffer, index int) (err error) {
135+
return ReadMessageField(buf, func(buf *Buffer, index int) (err error) {
80136
v, err := ReadValue(buf, nil)
81137
if err != nil {
82138
return err

breezeBuffer.go

+36-25
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@ import (
88
// Buffer is A variable-sized buffer of bytes with Read and Write methods.
99
// Buffer is not thread safe for multi goroutine operation.
1010
type Buffer struct {
11-
buf []byte // contents are the bytes buf[0 : woff] in write, are the bytes buf[roff: len(buf)] in read
12-
rpos int // read position
13-
wpos int // write position
14-
order binary.ByteOrder
15-
temp []byte
11+
buf []byte // contents are the bytes buf[0 : wpos] in write, are the bytes buf[rpos: len(buf)] in read
12+
rpos int // read position
13+
wpos int // write position
14+
order binary.ByteOrder
15+
temp []byte
16+
context *Context
1617
}
1718

1819
// NewBuffer create A empty Buffer with initial size
19-
func NewBuffer(initsize int) *Buffer {
20-
return NewBufferWithOrder(initsize, binary.BigEndian)
20+
func NewBuffer(initSize int) *Buffer {
21+
return NewBufferWithOrder(initSize, binary.BigEndian)
2122
}
2223

2324
// NewBufferWithOrder create A empty Buffer with initial size and byte order
24-
func NewBufferWithOrder(initsize int, order binary.ByteOrder) *Buffer {
25-
return &Buffer{buf: make([]byte, initsize),
25+
func NewBufferWithOrder(initSize int, order binary.ByteOrder) *Buffer {
26+
return &Buffer{buf: make([]byte, initSize),
2627
order: order,
2728
temp: make([]byte, 8),
2829
}
@@ -77,11 +78,13 @@ func (b *Buffer) WriteByte(c byte) {
7778
// Write write A byte array append the Buffer, and the wpos will increase len(bytes)
7879
func (b *Buffer) Write(bytes []byte) {
7980
l := len(bytes)
80-
if len(b.buf) < b.wpos+l {
81-
b.grow(l)
81+
if l > 0 {
82+
if len(b.buf) < b.wpos+l {
83+
b.grow(l)
84+
}
85+
copy(b.buf[b.wpos:], bytes)
86+
b.wpos += l
8287
}
83-
copy(b.buf[b.wpos:], bytes)
84-
b.wpos += l
8588
}
8689

8790
// WriteUint16 write A uint16 append the Buffer according to buffer's order
@@ -116,16 +119,16 @@ func (b *Buffer) WriteUint64(u uint64) {
116119

117120
// WriteZigzag32 write a uint32 append to the Buffer with zigzag algorithm
118121
func (b *Buffer) WriteZigzag32(u uint32) int {
119-
return b.WriteVarint(uint64((u << 1) ^ uint32(int32(u)>>31)))
122+
return b.WriteVarInt(uint64((u << 1) ^ uint32(int32(u)>>31)))
120123
}
121124

122125
// WriteZigzag64 write a uint64 append to the Buffer with zigzag algorithm
123126
func (b *Buffer) WriteZigzag64(u uint64) int {
124-
return b.WriteVarint(uint64((u << 1) ^ uint64(int64(u)>>63)))
127+
return b.WriteVarInt(uint64((u << 1) ^ uint64(int64(u)>>63)))
125128
}
126129

127-
// WriteVarint write a uint64 into buffer with variable length
128-
func (b *Buffer) WriteVarint(u uint64) int {
130+
// WriteVarInt write a uint64 into buffer with variable length
131+
func (b *Buffer) WriteVarInt(u uint64) int {
129132
l := 0
130133
for u >= 1<<7 {
131134
b.WriteByte(uint8(u&0x7f | 0x80))
@@ -143,7 +146,7 @@ func (b *Buffer) grow(n int) {
143146
b.buf = buf
144147
}
145148

146-
// Bytes return a bytes slice of under bytebuffer.
149+
// Bytes return a bytes slice of under byte buffer.
147150
func (b *Buffer) Bytes() []byte { return b.buf[:b.wpos] }
148151

149152
// Read read buffer's byte to byte array. return value n is read size.
@@ -206,28 +209,28 @@ func (b *Buffer) ReadUint64() (n uint64, err error) {
206209
return n, nil
207210
}
208211

209-
// ReadZigzag64 read a zigzaged uint64 from buffer.
212+
// ReadZigzag64 read a zigzag uint64 from buffer.
210213
func (b *Buffer) ReadZigzag64() (x uint64, err error) {
211-
x, err = b.ReadVarint()
214+
x, err = b.ReadVarInt()
212215
if err != nil {
213216
return
214217
}
215218
x = (x >> 1) ^ uint64(-int64(x&1))
216219
return
217220
}
218221

219-
// ReadZigzag32 read a zigzaged uint32 from buffer.
222+
// ReadZigzag32 read a zigzag uint32 from buffer.
220223
func (b *Buffer) ReadZigzag32() (x uint64, err error) {
221-
x, err = b.ReadVarint()
224+
x, err = b.ReadVarInt()
222225
if err != nil {
223226
return
224227
}
225228
x = uint64((uint32(x) >> 1) ^ uint32(-int32(x&1)))
226229
return
227230
}
228231

229-
// ReadVarint read a variable length uint64 form buffer
230-
func (b *Buffer) ReadVarint() (x uint64, err error) {
232+
// ReadVarInt read a variable length uint64 form buffer
233+
func (b *Buffer) ReadVarInt() (x uint64, err error) {
231234
var temp byte
232235
for offset := uint(0); offset < 64; offset += 7 {
233236
temp, err = b.ReadByte()
@@ -280,5 +283,13 @@ func (b *Buffer) Remain() int { return b.wpos - b.rpos }
280283
// Len return the len of buffer' bytes,
281284
func (b *Buffer) Len() int { return b.wpos - 0 }
282285

283-
// Cap return the capacity of the under bytebuffer
286+
// Cap return the capacity of the under byte buffer
284287
func (b *Buffer) Cap() int { return cap(b.buf) }
288+
289+
// GetContext get breeze context
290+
func (b *Buffer) GetContext() *Context {
291+
if b.context == nil {
292+
b.context = &Context{}
293+
}
294+
return b.context
295+
}

0 commit comments

Comments
 (0)