This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
blob.go
155 lines (135 loc) · 2.62 KB
/
blob.go
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
// Copyright 2014 The ql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ql
import (
"bytes"
"encoding/gob"
"math/big"
"sync"
"time"
)
const shortBlob = 256 // bytes
var (
gobInitDuration = time.Duration(278)
gobInitInt = big.NewInt(42)
gobInitRat = big.NewRat(355, 113)
gobInitTime time.Time
)
func init() {
var err error
if gobInitTime, err = time.ParseInLocation(
"Jan 2, 2006 at 3:04pm (MST)",
"Jul 9, 2012 at 5:02am (CEST)",
time.FixedZone("XYZ", 1234),
); err != nil {
panic(err)
}
newGobCoder()
}
type gobCoder struct {
buf bytes.Buffer
dec *gob.Decoder
enc *gob.Encoder
mu sync.Mutex
}
func newGobCoder() (g *gobCoder) {
g = &gobCoder{}
g.enc = gob.NewEncoder(&g.buf)
if err := g.enc.Encode(gobInitInt); err != nil {
panic(err)
}
if err := g.enc.Encode(gobInitRat); err != nil {
panic(err)
}
if err := g.enc.Encode(gobInitTime); err != nil {
panic(err)
}
if err := g.enc.Encode(gobInitDuration); err != nil {
panic(err)
}
g.dec = gob.NewDecoder(&g.buf)
i := big.NewInt(0)
if err := g.dec.Decode(i); err != nil {
panic(err)
}
r := big.NewRat(3, 5)
if err := g.dec.Decode(r); err != nil {
panic(err)
}
t := time.Now()
if err := g.dec.Decode(&t); err != nil {
panic(err)
}
var d time.Duration
if err := g.dec.Decode(&d); err != nil {
panic(err)
}
return
}
func isBlobType(v interface{}) (bool, Type) {
switch v.(type) {
case []byte:
return true, Blob
case *big.Int:
return true, BigInt
case *big.Rat:
return true, BigRat
case time.Time:
return true, Time
case time.Duration:
return true, Duration
default:
return false, -1
}
}
func (g *gobCoder) encode(v interface{}) (b []byte, err error) {
g.mu.Lock()
defer g.mu.Unlock()
g.buf.Reset()
switch x := v.(type) {
case []byte:
return x, nil
case *big.Int:
err = g.enc.Encode(x)
case *big.Rat:
err = g.enc.Encode(x)
case time.Time:
err = g.enc.Encode(x)
case time.Duration:
err = g.enc.Encode(int64(x))
default:
panic("internal error 002")
}
b = g.buf.Bytes()
return
}
func (g *gobCoder) decode(b []byte, typ int) (v interface{}, err error) {
g.mu.Lock()
defer g.mu.Unlock()
g.buf.Reset()
g.buf.Write(b)
switch typ {
case qBlob:
return b, nil
case qBigInt:
x := big.NewInt(0)
err = g.dec.Decode(&x)
v = x
case qBigRat:
x := big.NewRat(1, 1)
err = g.dec.Decode(&x)
v = x
case qTime:
var x time.Time
err = g.dec.Decode(&x)
v = x
case qDuration:
var x int64
err = g.dec.Decode(&x)
v = time.Duration(x)
default:
panic("internal error 003")
}
return
}