Skip to content

Commit

Permalink
remove commitlog dependecy on protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
klavs authored and travisjeffery committed Feb 3, 2017
1 parent 2d43357 commit 74e190e
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 17 deletions.
2 changes: 2 additions & 0 deletions commitlog/commitlog.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commitlog

import (
"encoding/binary"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -14,6 +15,7 @@ import (

var (
ErrSegmentNotFound = errors.New("segment not found")
Encoding = binary.BigEndian
)

const (
Expand Down
4 changes: 2 additions & 2 deletions commitlog/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func newIndex(opts options) (idx *index, err error) {
func (idx *index) WriteEntry(entry Entry) (err error) {
b := new(bytes.Buffer)
relEntry := newRelEntry(entry, idx.baseOffset)
if err = binary.Write(b, binary.BigEndian, relEntry); err != nil {
if err = binary.Write(b, Encoding, relEntry); err != nil {
return errors.Wrap(err, "binary write failed")
}
idx.WriteAt(b.Bytes(), idx.position)
Expand All @@ -111,7 +111,7 @@ func (idx *index) ReadEntry(e *Entry, offset int64) error {
idx.ReadAt(p, offset)
b := bytes.NewReader(p)
rel := &relEntry{}
err := binary.Read(b, binary.BigEndian, rel)
err := binary.Read(b, Encoding, rel)
if err != nil {
return errors.Wrap(err, "binary read failed")
}
Expand Down
6 changes: 0 additions & 6 deletions commitlog/message.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package commitlog

import "encoding/binary"

var (
big = binary.BigEndian
)

type Message []byte

func NewMessage(p []byte) Message {
Expand Down
10 changes: 5 additions & 5 deletions commitlog/message_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ type MessageSet []byte
func NewMessageSet(offset uint64, msgs ...Message) MessageSet {
ms := make([]byte, msgSetHeaderLen)
var n uint32
big.PutUint64(ms[offsetPos:offsetPos+8], offset)
Encoding.PutUint64(ms[offsetPos:offsetPos+8], offset)
for _, m := range msgs {
ms = append(ms, m...)
n += uint32(len(m))
}
big.PutUint32(ms[sizePos:sizePos+4], n)
Encoding.PutUint32(ms[sizePos:sizePos+4], n)
return ms
}

func (ms MessageSet) Offset() int64 {
return int64(big.Uint64(ms[offsetPos : offsetPos+8]))
return int64(Encoding.Uint64(ms[offsetPos : offsetPos+8]))
}

func (ms MessageSet) PutOffset(offset int64) {
big.PutUint64(ms[offsetPos:offsetPos+8], uint64(offset))
Encoding.PutUint64(ms[offsetPos:offsetPos+8], uint64(offset))
}

func (ms MessageSet) Size() int32 {
return int32(big.Uint32(ms[sizePos:sizePos+4]) + msgSetHeaderLen)
return int32(Encoding.Uint32(ms[sizePos:sizePos+4]) + msgSetHeaderLen)
}

func (ms MessageSet) Payload() []byte {
Expand Down
5 changes: 2 additions & 3 deletions commitlog/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"sync"

"github.com/pkg/errors"
"github.com/travisjeffery/jocko/protocol"
)

const (
Expand Down Expand Up @@ -82,13 +81,13 @@ func (s *Segment) SetupIndex(path string) (err error) {
if err != nil {
return err
}
s.NextOffset = int64(protocol.Encoding.Uint64(b.Bytes()[0:8]))
s.NextOffset = int64(Encoding.Uint64(b.Bytes()[0:8]))

_, err = io.CopyN(b, s.log, 4)
if err != nil {
return err
}
size := int64(protocol.Encoding.Uint32(b.Bytes()[8:12]))
size := int64(Encoding.Uint32(b.Bytes()[8:12]))

_, err = io.CopyN(b, s.log, size)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package protocol
import (
"encoding/binary"
"io"

"github.com/travisjeffery/jocko/commitlog"
)

var Encoding = binary.BigEndian
var Encoding = commitlog.Encoding

func Read(r io.Reader, data interface{}) error {
return binary.Read(r, Encoding, data)
Expand Down

0 comments on commit 74e190e

Please sign in to comment.