-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix issue#12 #14
base: master
Are you sure you want to change the base?
Fix issue#12 #14
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ package recordio | |
import "io" | ||
|
||
type Appender interface { | ||
Append(Record) (int64, error) | ||
Append(Encoder) (int64, error) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep Append(Record) |
||
} | ||
|
||
type appender struct { | ||
|
@@ -15,13 +15,13 @@ func NewAppender(w io.WriteSeeker) Appender { | |
} | ||
|
||
// Not thread-safe | ||
func (ap *appender) Append(r Record) (offset int64, err error) { | ||
func (ap *appender) Append(r Encoder) (offset int64, err error) { | ||
offset, err = ap.w.Seek(0, 2) | ||
if err != nil { | ||
return -1, err | ||
} | ||
|
||
err = (&r).encodeTo(ap.w) | ||
err = r.EncodeTo(ap.w) | ||
if err != nil { | ||
return -1, err | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ package recordio | |
import "io" | ||
|
||
type Fetcher interface { | ||
Fetch(offset int64) (Record, error) | ||
Fetch(offset int64, d Decoder) error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fetch(offset int64, r Record) |
||
} | ||
|
||
type fetcher struct { | ||
|
@@ -14,17 +14,16 @@ func NewFetcher(r io.ReadSeeker) Fetcher { | |
return &fetcher{r} | ||
} | ||
|
||
func (fc *fetcher) Fetch(offset int64) (Record, error) { | ||
func (fc *fetcher) Fetch(offset int64, d Decoder) error { | ||
_, err := fc.r.Seek(offset, 0) | ||
if err != nil { | ||
return Record{}, err | ||
return err | ||
} | ||
|
||
r := Record{} | ||
err = (&r).decodeFrom(fc.r) | ||
err = d.DecodeFrom(fc.r) | ||
if err != nil { | ||
return Record{}, err | ||
return err | ||
} | ||
|
||
return r, nil | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,29 @@ const ( | |
sizeOfLength = 4 | ||
) | ||
|
||
// Encoder is an interface implemented by an object that can | ||
// encode itself into a binary representation with an io.Writer | ||
type Encoder interface { | ||
EncodeTo(wr io.Writer) error | ||
} | ||
|
||
// Decoder is an interface implemented by an object that can | ||
// decodes itself from a binary representation provided by | ||
// an io.Reader | ||
type Decoder interface { | ||
DecodeFrom(rd io.Reader) error | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. type Record interface {
Encoder
Decoder
} |
||
// Record is a struct that holds some binary data and can be | ||
// encoded into/decoded from the following binary form | ||
// [lenth of record] : 4 bytes | ||
// [content of record] | ||
type Record struct { | ||
Data []byte | ||
} | ||
|
||
func (r *Record) encodeTo(wr io.Writer) error { | ||
// EncodeTo encodes a binary data in record object to an io.Writer | ||
func (r *Record) EncodeTo(wr io.Writer) error { | ||
// Write length | ||
lBuf := make([]byte, sizeOfLength) | ||
binary.LittleEndian.PutUint32(lBuf, uint32(len(r.Data))) | ||
|
@@ -28,7 +46,8 @@ func (r *Record) encodeTo(wr io.Writer) error { | |
return nil | ||
} | ||
|
||
func (r *Record) decodeFrom(rd io.Reader) error { | ||
// DecodeFrom decodes binary data out of io.Reader | ||
func (r *Record) DecodeFrom(rd io.Reader) error { | ||
var length uint32 | ||
// Read length | ||
err := binary.Read(rd, binary.LittleEndian, &length) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rec := &recordio.Record{}