aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/lunny/nodb/binlog_util.go
blob: 22124dda07d8435c9c0bd9c053a45eadec50fcd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package nodb

import (
	"encoding/binary"
	"errors"
	"fmt"
	"strconv"
)

var (
	errBinLogDeleteType  = errors.New("invalid bin log delete type")
	errBinLogPutType     = errors.New("invalid bin log put type")
	errBinLogCommandType = errors.New("invalid bin log command type")
)

func encodeBinLogDelete(key []byte) []byte {
	buf := make([]byte, 1+len(key))
	buf[0] = BinLogTypeDeletion
	copy(buf[1:], key)
	return buf
}

func decodeBinLogDelete(sz []byte) ([]byte, error) {
	if len(sz) < 1 || sz[0] != BinLogTypeDeletion {
		return nil, errBinLogDeleteType
	}

	return sz[1:], nil
}

func encodeBinLogPut(key []byte, value []byte) []byte {
	buf := make([]byte, 3+len(key)+len(value))
	buf[0] = BinLogTypePut
	pos := 1
	binary.BigEndian.PutUint16(buf[pos:], uint16(len(key)))
	pos += 2
	copy(buf[pos:], key)
	pos += len(key)
	copy(buf[pos:], value)

	return buf
}

func decodeBinLogPut(sz []byte) ([]byte, []byte, error) {
	if len(sz) < 3 || sz[0] != BinLogTypePut {
		return nil, nil, errBinLogPutType
	}

	keyLen := int(binary.BigEndian.Uint16(sz[1:]))
	if 3+keyLen > len(sz) {
		return nil, nil, errBinLogPutType
	}

	return sz[3 : 3+keyLen], sz[3+keyLen:], nil
}

func FormatBinLogEvent(event []byte) (string, error) {
	logType := uint8(event[0])

	var err error
	var k []byte
	var v []byte

	var buf []byte = make([]byte, 0, 1024)

	switch logType {
	case BinLogTypePut:
		k, v, err = decodeBinLogPut(event)
		buf = append(buf, "PUT "...)
	case BinLogTypeDeletion:
		k, err = decodeBinLogDelete(event)
		buf = append(buf, "DELETE "...)
	default:
		err = errInvalidBinLogEvent
	}

	if err != nil {
		return "", err
	}

	if buf, err = formatDataKey(buf, k); err != nil {
		return "", err
	}

	if v != nil && len(v) != 0 {
		buf = append(buf, fmt.Sprintf(" %q", v)...)
	}

	return String(buf), nil
}

func formatDataKey(buf []byte, k []byte) ([]byte, error) {
	if len(k) < 2 {
		return nil, errInvalidBinLogEvent
	}

	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 {
			buf = strconv.AppendQuote(buf, String(key))
		}
	case HashType:
		if key, field, err := db.hDecodeHashKey(k); err != nil {
			return nil, err
		} else {
			buf = strconv.AppendQuote(buf, String(key))
			buf = append(buf, ' ')
			buf = strconv.AppendQuote(buf, String(field))
		}
	case HSizeType:
		if key, err := db.hDecodeSizeKey(k); err != nil {
			return nil, err
		} else {
			buf = strconv.AppendQuote(buf, String(key))
		}
	case ListType:
		if key, seq, err := db.lDecodeListKey(k); err != nil {
			return nil, err
		} else {
			buf = strconv.AppendQuote(buf, String(key))
			buf = append(buf, ' ')
			buf = strconv.AppendInt(buf, int64(seq), 10)
		}
	case LMetaType:
		if key, err := db.lDecodeMetaKey(k); err != nil {
			return nil, err
		} else {
			buf = strconv.AppendQuote(buf, String(key))
		}
	case ZSetType:
		if key, m, err := db.zDecodeSetKey(k); err != nil {
			return nil, err
		} else {
			buf = strconv.AppendQuote(buf, String(key))
			buf = append(buf, ' ')
			buf = strconv.AppendQuote(buf, String(m))
		}
	case ZSizeType:
		if key, err := db.zDecodeSizeKey(k); err != nil {
			return nil, err
		} else {
			buf = strconv.AppendQuote(buf, String(key))
		}
	case ZScoreType:
		if key, m, score, err := db.zDecodeScoreKey(k); err != nil {
			return nil, err
		} else {
			buf = strconv.AppendQuote(buf, String(key))
			buf = append(buf, ' ')
			buf = strconv.AppendQuote(buf, String(m))
			buf = append(buf, ' ')
			buf = strconv.AppendInt(buf, score, 10)
		}
	case BitType:
		if key, seq, err := db.bDecodeBinKey(k); err != nil {
			return nil, err
		} else {
			buf = strconv.AppendQuote(buf, 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, String(key))
		}
	case SetType:
		if key, member, err := db.sDecodeSetKey(k); err != nil {
			return nil, err
		} else {
			buf = strconv.AppendQuote(buf, String(key))
			buf = append(buf, ' ')
			buf = strconv.AppendQuote(buf, String(member))
		}
	case SSizeType:
		if key, err := db.sDecodeSizeKey(k); err != nil {
			return nil, err
		} else {
			buf = strconv.AppendQuote(buf, String(key))
		}
	case ExpTimeType:
		if tp, key, t, err := db.expDecodeTimeKey(k); err != nil {
			return nil, err
		} else {
			buf = append(buf, TypeName[tp]...)
			buf = append(buf, ' ')
			buf = strconv.AppendQuote(buf, String(key))
			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, ' ')
			buf = strconv.AppendQuote(buf, String(key))
		}
	default:
		return nil, errInvalidBinLogEvent
	}

	return buf, nil
}