Skip to content

Commit f8f4e93

Browse files
authored
Merge pull request #30 from ipfs/refmt
Refactor to refmt
2 parents 4a4f9db + a1a7802 commit f8f4e93

9 files changed

+733
-188
lines changed

.travis.yml

+8-10
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ sudo: false
33

44
language: go
55
go:
6-
- 'tip'
7-
8-
install:
9-
- go get github.com/whyrusleeping/gx
10-
- go get github.com/whyrusleeping/gx-go
11-
- gx install --global
6+
- 1.9.x
7+
8+
before_install:
9+
- make deps
10+
1211
script:
13-
- gx test -v -race -coverprofile=coverage.txt -covermode=atomic .
14-
12+
- bash <(curl -s https://raw.githubusercontent.com/ipfs/ci-helpers/master/travis-ci/run-standard-tests.sh)
13+
1514
after_success:
1615
- bash <(curl -s https://codecov.io/bash)
1716

@@ -20,5 +19,4 @@ cache:
2019
- $GOPATH/src/gx
2120

2221
notifications:
23-
email: false
24-
22+
email: false

encoding/cloner.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package encoding
2+
3+
import (
4+
"sync"
5+
6+
refmt "github.com/polydawn/refmt"
7+
"github.com/polydawn/refmt/obj/atlas"
8+
)
9+
10+
// PooledCloner is a thread-safe pooled object cloner.
11+
type PooledCloner struct {
12+
pool sync.Pool
13+
}
14+
15+
// NewPooledCloner returns a PooledCloner with the given atlas. Do not copy
16+
// after use.
17+
func NewPooledCloner(atl atlas.Atlas) PooledCloner {
18+
return PooledCloner{
19+
pool: sync.Pool{
20+
New: func() interface{} {
21+
return refmt.NewCloner(atl)
22+
},
23+
},
24+
}
25+
}
26+
27+
// Clone clones a into b using a cloner from the pool.
28+
func (p *PooledCloner) Clone(a, b interface{}) error {
29+
c := p.pool.Get().(refmt.Cloner)
30+
err := c.Clone(a, b)
31+
p.pool.Put(c)
32+
return err
33+
}

encoding/marshaller.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package encoding
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"sync"
7+
8+
cbor "github.com/polydawn/refmt/cbor"
9+
"github.com/polydawn/refmt/obj/atlas"
10+
)
11+
12+
type proxyWriter struct {
13+
w io.Writer
14+
}
15+
16+
func (w *proxyWriter) Write(b []byte) (int, error) {
17+
return w.w.Write(b)
18+
}
19+
20+
// Marshaller is a reusbale CBOR marshaller.
21+
type Marshaller struct {
22+
marshal *cbor.Marshaller
23+
writer proxyWriter
24+
}
25+
26+
// NewMarshallerAtlased constructs a new cbor Marshaller using the given atlas.
27+
func NewMarshallerAtlased(atl atlas.Atlas) *Marshaller {
28+
m := new(Marshaller)
29+
m.marshal = cbor.NewMarshallerAtlased(&m.writer, atl)
30+
return m
31+
}
32+
33+
// Encode encodes the given object to the given writer.
34+
func (m *Marshaller) Encode(obj interface{}, w io.Writer) error {
35+
m.writer.w = w
36+
err := m.marshal.Marshal(obj)
37+
m.writer.w = nil
38+
return err
39+
}
40+
41+
// Marshal marshels the given object to a byte slice.
42+
func (m *Marshaller) Marshal(obj interface{}) ([]byte, error) {
43+
var buf bytes.Buffer
44+
if err := m.Encode(obj, &buf); err != nil {
45+
return nil, err
46+
}
47+
return buf.Bytes(), nil
48+
}
49+
50+
// PooledMarshaller is a thread-safe pooled CBOR marshaller.
51+
type PooledMarshaller struct {
52+
pool sync.Pool
53+
}
54+
55+
// NewPooledMarshaller returns a PooledMarshaller with the given atlas. Do not
56+
// copy after use.
57+
func NewPooledMarshaller(atl atlas.Atlas) PooledMarshaller {
58+
return PooledMarshaller{
59+
pool: sync.Pool{
60+
New: func() interface{} {
61+
return NewMarshallerAtlased(atl)
62+
},
63+
},
64+
}
65+
}
66+
67+
// Marshal marshals the passed object using the pool of marshallers.
68+
func (p *PooledMarshaller) Marshal(obj interface{}) ([]byte, error) {
69+
m := p.pool.Get().(*Marshaller)
70+
bts, err := m.Marshal(obj)
71+
p.pool.Put(m)
72+
return bts, err
73+
}
74+
75+
// Encode encodes the passed object to the given writer using the pool of
76+
// marshallers.
77+
func (p *PooledMarshaller) Encode(obj interface{}, w io.Writer) error {
78+
m := p.pool.Get().(*Marshaller)
79+
err := m.Encode(obj, w)
80+
p.pool.Put(m)
81+
return err
82+
}

encoding/unmarshaller.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package encoding
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"sync"
7+
8+
cbor "github.com/polydawn/refmt/cbor"
9+
"github.com/polydawn/refmt/obj/atlas"
10+
)
11+
12+
type proxyReader struct {
13+
r io.Reader
14+
}
15+
16+
func (r *proxyReader) Read(b []byte) (int, error) {
17+
return r.r.Read(b)
18+
}
19+
20+
// Unmarshaller is a reusable CBOR unmarshaller.
21+
type Unmarshaller struct {
22+
unmarshal *cbor.Unmarshaller
23+
reader proxyReader
24+
}
25+
26+
// NewUnmarshallerAtlased creates a new reusable unmarshaller.
27+
func NewUnmarshallerAtlased(atl atlas.Atlas) *Unmarshaller {
28+
m := new(Unmarshaller)
29+
m.unmarshal = cbor.NewUnmarshallerAtlased(&m.reader, atl)
30+
return m
31+
}
32+
33+
// Decode reads a CBOR object from the given reader and decodes it into the
34+
// given object.
35+
func (m *Unmarshaller) Decode(r io.Reader, obj interface{}) error {
36+
m.reader.r = r
37+
err := m.unmarshal.Unmarshal(obj)
38+
m.reader.r = nil
39+
return err
40+
}
41+
42+
// Unmarshal unmarshals the given CBOR byte slice into the given object.
43+
func (m *Unmarshaller) Unmarshal(b []byte, obj interface{}) error {
44+
return m.Decode(bytes.NewReader(b), obj)
45+
}
46+
47+
// PooledUnmarshaller is a thread-safe pooled CBOR unmarshaller.
48+
type PooledUnmarshaller struct {
49+
pool sync.Pool
50+
}
51+
52+
// NewPooledUnmarshaller returns a PooledUnmarshaller with the given atlas. Do
53+
// not copy after use.
54+
func NewPooledUnmarshaller(atl atlas.Atlas) PooledUnmarshaller {
55+
return PooledUnmarshaller{
56+
pool: sync.Pool{
57+
New: func() interface{} {
58+
return NewUnmarshallerAtlased(atl)
59+
},
60+
},
61+
}
62+
}
63+
64+
// Decode decodes an object from the passed reader into the given object using
65+
// the pool of unmarshallers.
66+
func (p *PooledUnmarshaller) Decode(r io.Reader, obj interface{}) error {
67+
u := p.pool.Get().(*Unmarshaller)
68+
err := u.Decode(r, obj)
69+
p.pool.Put(u)
70+
return err
71+
}
72+
73+
// Unmarshal unmarshals the passed object using the pool of unmarshallers.
74+
func (p *PooledUnmarshaller) Unmarshal(b []byte, obj interface{}) error {
75+
u := p.pool.Get().(*Unmarshaller)
76+
err := u.Unmarshal(b, obj)
77+
p.pool.Put(u)
78+
return err
79+
}

0 commit comments

Comments
 (0)