| 1 |
// Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT license found in the LICENSE file. |
| 3 |
|
| 4 |
// +build ignore |
| 5 |
|
| 6 |
package codec |
| 7 |
|
| 8 |
import ( |
| 9 |
"math/rand" |
| 10 |
"time" |
| 11 |
) |
| 12 |
|
| 13 |
// NoopHandle returns a no-op handle. It basically does nothing. |
| 14 |
// It is only useful for benchmarking, as it gives an idea of the |
| 15 |
// overhead from the codec framework. |
| 16 |
// |
| 17 |
// LIBRARY USERS: *** DO NOT USE *** |
| 18 |
func NoopHandle(slen int) *noopHandle { |
| 19 |
h := noopHandle{} |
| 20 |
h.rand = rand.New(rand.NewSource(time.Now().UnixNano())) |
| 21 |
h.B = make([][]byte, slen) |
| 22 |
h.S = make([]string, slen) |
| 23 |
for i := 0; i < len(h.S); i++ { |
| 24 |
b := make([]byte, i+1) |
| 25 |
for j := 0; j < len(b); j++ { |
| 26 |
b[j] = 'a' + byte(i) |
| 27 |
} |
| 28 |
h.B[i] = b |
| 29 |
h.S[i] = string(b) |
| 30 |
} |
| 31 |
return &h |
| 32 |
} |
| 33 |
|
| 34 |
// noopHandle does nothing. |
| 35 |
// It is used to simulate the overhead of the codec framework. |
| 36 |
type noopHandle struct { |
| 37 |
BasicHandle |
| 38 |
binaryEncodingType |
| 39 |
noopDrv // noopDrv is unexported here, so we can get a copy of it when needed. |
| 40 |
} |
| 41 |
|
| 42 |
type noopDrv struct { |
| 43 |
d *Decoder |
| 44 |
e *Encoder |
| 45 |
i int |
| 46 |
S []string |
| 47 |
B [][]byte |
| 48 |
mks []bool // stack. if map (true), else if array (false) |
| 49 |
mk bool // top of stack. what container are we on? map or array? |
| 50 |
ct valueType // last response for IsContainerType. |
| 51 |
cb int // counter for ContainerType |
| 52 |
rand *rand.Rand |
| 53 |
} |
| 54 |
|
| 55 |
func (h *noopDrv) r(v int) int { return h.rand.Intn(v) } |
| 56 |
func (h *noopDrv) m(v int) int { h.i++; return h.i % v } |
| 57 |
|
| 58 |
func (h *noopDrv) newEncDriver(e *Encoder) encDriver { h.e = e; return h } |
| 59 |
func (h *noopDrv) newDecDriver(d *Decoder) decDriver { h.d = d; return h } |
| 60 |
|
| 61 |
func (h *noopDrv) reset() {} |
| 62 |
func (h *noopDrv) uncacheRead() {} |
| 63 |
|
| 64 |
// --- encDriver |
| 65 |
|
| 66 |
// stack functions (for map and array) |
| 67 |
func (h *noopDrv) start(b bool) { |
| 68 |
// println("start", len(h.mks)+1) |
| 69 |
h.mks = append(h.mks, b) |
| 70 |
h.mk = b |
| 71 |
} |
| 72 |
func (h *noopDrv) end() { |
| 73 |
// println("end: ", len(h.mks)-1) |
| 74 |
h.mks = h.mks[:len(h.mks)-1] |
| 75 |
if len(h.mks) > 0 { |
| 76 |
h.mk = h.mks[len(h.mks)-1] |
| 77 |
} else { |
| 78 |
h.mk = false |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func (h *noopDrv) EncodeBuiltin(rt uintptr, v interface{}) {} |
| 83 |
func (h *noopDrv) EncodeNil() {} |
| 84 |
func (h *noopDrv) EncodeInt(i int64) {} |
| 85 |
func (h *noopDrv) EncodeUint(i uint64) {} |
| 86 |
func (h *noopDrv) EncodeBool(b bool) {} |
| 87 |
func (h *noopDrv) EncodeFloat32(f float32) {} |
| 88 |
func (h *noopDrv) EncodeFloat64(f float64) {} |
| 89 |
func (h *noopDrv) EncodeRawExt(re *RawExt, e *Encoder) {} |
| 90 |
func (h *noopDrv) EncodeArrayStart(length int) { h.start(true) } |
| 91 |
func (h *noopDrv) EncodeMapStart(length int) { h.start(false) } |
| 92 |
func (h *noopDrv) EncodeEnd() { h.end() } |
| 93 |
|
| 94 |
func (h *noopDrv) EncodeString(c charEncoding, v string) {} |
| 95 |
func (h *noopDrv) EncodeSymbol(v string) {} |
| 96 |
func (h *noopDrv) EncodeStringBytes(c charEncoding, v []byte) {} |
| 97 |
|
| 98 |
func (h *noopDrv) EncodeExt(rv interface{}, xtag uint64, ext Ext, e *Encoder) {} |
| 99 |
|
| 100 |
// ---- decDriver |
| 101 |
func (h *noopDrv) initReadNext() {} |
| 102 |
func (h *noopDrv) CheckBreak() bool { return false } |
| 103 |
func (h *noopDrv) IsBuiltinType(rt uintptr) bool { return false } |
| 104 |
func (h *noopDrv) DecodeBuiltin(rt uintptr, v interface{}) {} |
| 105 |
func (h *noopDrv) DecodeInt(bitsize uint8) (i int64) { return int64(h.m(15)) } |
| 106 |
func (h *noopDrv) DecodeUint(bitsize uint8) (ui uint64) { return uint64(h.m(35)) } |
| 107 |
func (h *noopDrv) DecodeFloat(chkOverflow32 bool) (f float64) { return float64(h.m(95)) } |
| 108 |
func (h *noopDrv) DecodeBool() (b bool) { return h.m(2) == 0 } |
| 109 |
func (h *noopDrv) DecodeString() (s string) { return h.S[h.m(8)] } |
| 110 |
func (h *noopDrv) DecodeStringAsBytes() []byte { return h.DecodeBytes(nil, true) } |
| 111 |
|
| 112 |
func (h *noopDrv) DecodeBytes(bs []byte, zerocopy bool) []byte { return h.B[h.m(len(h.B))] } |
| 113 |
|
| 114 |
func (h *noopDrv) ReadEnd() { h.end() } |
| 115 |
|
| 116 |
// toggle map/slice |
| 117 |
func (h *noopDrv) ReadMapStart() int { h.start(true); return h.m(10) } |
| 118 |
func (h *noopDrv) ReadArrayStart() int { h.start(false); return h.m(10) } |
| 119 |
|
| 120 |
func (h *noopDrv) ContainerType() (vt valueType) { |
| 121 |
// return h.m(2) == 0 |
| 122 |
// handle kStruct, which will bomb is it calls this and doesn't get back a map or array. |
| 123 |
// consequently, if the return value is not map or array, reset it to one of them based on h.m(7) % 2 |
| 124 |
// for kstruct: at least one out of every 2 times, return one of valueTypeMap or Array (else kstruct bombs) |
| 125 |
// however, every 10th time it is called, we just return something else. |
| 126 |
var vals = [...]valueType{valueTypeArray, valueTypeMap} |
| 127 |
// ------------ TAKE ------------ |
| 128 |
// if h.cb%2 == 0 { |
| 129 |
// if h.ct == valueTypeMap || h.ct == valueTypeArray { |
| 130 |
// } else { |
| 131 |
// h.ct = vals[h.m(2)] |
| 132 |
// } |
| 133 |
// } else if h.cb%5 == 0 { |
| 134 |
// h.ct = valueType(h.m(8)) |
| 135 |
// } else { |
| 136 |
// h.ct = vals[h.m(2)] |
| 137 |
// } |
| 138 |
// ------------ TAKE ------------ |
| 139 |
// if h.cb%16 == 0 { |
| 140 |
// h.ct = valueType(h.cb % 8) |
| 141 |
// } else { |
| 142 |
// h.ct = vals[h.cb%2] |
| 143 |
// } |
| 144 |
h.ct = vals[h.cb%2] |
| 145 |
h.cb++ |
| 146 |
return h.ct |
| 147 |
|
| 148 |
// if h.ct == valueTypeNil || h.ct == valueTypeString || h.ct == valueTypeBytes { |
| 149 |
// return h.ct |
| 150 |
// } |
| 151 |
// return valueTypeUnset |
| 152 |
// TODO: may need to tweak this so it works. |
| 153 |
// if h.ct == valueTypeMap && vt == valueTypeArray || h.ct == valueTypeArray && vt == valueTypeMap { |
| 154 |
// h.cb = !h.cb |
| 155 |
// h.ct = vt |
| 156 |
// return h.cb |
| 157 |
// } |
| 158 |
// // go in a loop and check it. |
| 159 |
// h.ct = vt |
| 160 |
// h.cb = h.m(7) == 0 |
| 161 |
// return h.cb |
| 162 |
} |
| 163 |
func (h *noopDrv) TryDecodeAsNil() bool { |
| 164 |
if h.mk { |
| 165 |
return false |
| 166 |
} else { |
| 167 |
return h.m(8) == 0 |
| 168 |
} |
| 169 |
} |
| 170 |
func (h *noopDrv) DecodeExt(rv interface{}, xtag uint64, ext Ext) uint64 { |
| 171 |
return 0 |
| 172 |
} |
| 173 |
|
| 174 |
func (h *noopDrv) DecodeNaked() { |
| 175 |
// use h.r (random) not h.m() because h.m() could cause the same value to be given. |
| 176 |
var sk int |
| 177 |
if h.mk { |
| 178 |
// if mapkey, do not support values of nil OR bytes, array, map or rawext |
| 179 |
sk = h.r(7) + 1 |
| 180 |
} else { |
| 181 |
sk = h.r(12) |
| 182 |
} |
| 183 |
n := &h.d.n |
| 184 |
switch sk { |
| 185 |
case 0: |
| 186 |
n.v = valueTypeNil |
| 187 |
case 1: |
| 188 |
n.v, n.b = valueTypeBool, false |
| 189 |
case 2: |
| 190 |
n.v, n.b = valueTypeBool, true |
| 191 |
case 3: |
| 192 |
n.v, n.i = valueTypeInt, h.DecodeInt(64) |
| 193 |
case 4: |
| 194 |
n.v, n.u = valueTypeUint, h.DecodeUint(64) |
| 195 |
case 5: |
| 196 |
n.v, n.f = valueTypeFloat, h.DecodeFloat(true) |
| 197 |
case 6: |
| 198 |
n.v, n.f = valueTypeFloat, h.DecodeFloat(false) |
| 199 |
case 7: |
| 200 |
n.v, n.s = valueTypeString, h.DecodeString() |
| 201 |
case 8: |
| 202 |
n.v, n.l = valueTypeBytes, h.B[h.m(len(h.B))] |
| 203 |
case 9: |
| 204 |
n.v = valueTypeArray |
| 205 |
case 10: |
| 206 |
n.v = valueTypeMap |
| 207 |
default: |
| 208 |
n.v = valueTypeExt |
| 209 |
n.u = h.DecodeUint(64) |
| 210 |
n.l = h.B[h.m(len(h.B))] |
| 211 |
} |
| 212 |
h.ct = n.v |
| 213 |
return |
| 214 |
} |