-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
202 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright (c) Roman Atachiants and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for details. | ||
|
||
package commit | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
|
||
"github.com/kelindar/iostream" | ||
"github.com/klauspost/compress/s2" | ||
) | ||
|
||
// Logger represents a contract that a commit logger must implement | ||
type Logger interface { | ||
Append(commit Commit) error | ||
} | ||
|
||
var _ Logger = new(Channel) | ||
var _ Logger = new(Log) | ||
|
||
// --------------------------- Channel ---------------------------- | ||
|
||
// Channel represents an impementation of a commit writer that simply sends each commit | ||
// into the channel. | ||
type Channel chan Commit | ||
|
||
// Append clones the commit and writes it into the logger | ||
func (w *Channel) Append(commit Commit) error { | ||
*w <- commit.Clone() | ||
return nil | ||
} | ||
|
||
// --------------------------- Log ---------------------------- | ||
|
||
// Log represents a commit log that can be used to write the changes to the collection | ||
// during a snapshot. It also supports reading a commit log back. | ||
type Log struct { | ||
lock sync.Mutex | ||
writer *iostream.Writer | ||
reader *iostream.Reader | ||
} | ||
|
||
// Open opens a commit log file (or stream) for both read and write. | ||
func Open(file io.ReadWriter) *Log { | ||
return &Log{ | ||
writer: iostream.NewWriter(s2.NewWriter(file)), | ||
reader: iostream.NewReader(s2.NewReader(file)), | ||
} | ||
} | ||
|
||
// Append writes the commit into the log destination | ||
func (l *Log) Append(commit Commit) (err error) { | ||
l.lock.Lock() | ||
defer l.lock.Unlock() | ||
|
||
// Write the commit into the stream | ||
if _, err = commit.WriteTo(l.writer); err == nil { | ||
err = l.writer.Flush() | ||
} | ||
return | ||
} | ||
|
||
// Range iterates over all the commits in the log and calls the provided | ||
// callback function on each of them. If the callback returns an error, the | ||
// iteration will stop. | ||
func (l *Log) Range(fn func(Commit) error) error { | ||
l.lock.Lock() | ||
defer l.lock.Unlock() | ||
|
||
for { | ||
var commit Commit | ||
_, err := commit.ReadFrom(l.reader) | ||
switch { | ||
case err == io.EOF: | ||
return nil | ||
case err != nil: | ||
return err | ||
} | ||
|
||
// Read the commit | ||
if err := fn(commit); err != nil { | ||
return err | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright (c) Roman Atachiants and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for details. | ||
|
||
package commit | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// --------------------------- Commit Encoding ---------------------------- | ||
|
||
func TestCommitWriteToFailures(t *testing.T) { | ||
for size := 0; size < 30; size++ { | ||
output := &limitWriter{Limit: size} | ||
commit := newCommit(1) | ||
_, err := commit.WriteTo(output) | ||
assert.Error(t, err) | ||
} | ||
} | ||
|
||
func TestCommitReadFromFailures(t *testing.T) { | ||
commit := newCommit(1) | ||
buffer := bytes.NewBuffer(nil) | ||
n, err := commit.WriteTo(buffer) | ||
assert.NoError(t, err) | ||
|
||
for size := 0; size < int(n)-1; size++ { | ||
output := new(Commit) | ||
_, err := output.ReadFrom(bytes.NewReader(buffer.Bytes()[:size])) | ||
assert.Error(t, err) | ||
} | ||
} | ||
|
||
func newCommit(id int) Commit { | ||
return Commit{ | ||
ID: uint64(id), | ||
Chunk: 0, | ||
Updates: []*Buffer{ | ||
newInterleaved("a"), | ||
newInterleaved("b"), | ||
}, | ||
} | ||
} | ||
|
||
// --------------------------- Log Operations ---------------------------- | ||
|
||
func TestLogAppendRange(t *testing.T) { | ||
buffer := bytes.NewBuffer(nil) | ||
logger := Open(buffer) | ||
|
||
assert.NoError(t, logger.Append(newCommit(1))) | ||
assert.NoError(t, logger.Append(newCommit(2))) | ||
|
||
var arr []uint64 | ||
assert.NoError(t, logger.Range(func(commit Commit) error { | ||
arr = append(arr, commit.ID) | ||
return nil | ||
})) | ||
|
||
assert.Equal(t, []uint64{1, 2}, arr) | ||
} | ||
|
||
func TestLogRangeFailures(t *testing.T) { | ||
buffer := bytes.NewBuffer(nil) | ||
logger := Open(buffer) | ||
assert.NoError(t, logger.Append(newCommit(1))) | ||
assert.NoError(t, logger.Append(newCommit(2))) | ||
|
||
n := buffer.Len() | ||
for size := 0; size < int(n)-1; size++ { | ||
tmp := bytes.NewBuffer(buffer.Bytes()[:size]) | ||
out := Open(tmp) | ||
|
||
count := 0 | ||
out.Range(func(commit Commit) error { | ||
count++ | ||
return nil | ||
}) | ||
assert.Less(t, count, 2, fmt.Sprintf("size=%v", size)) | ||
} | ||
} | ||
|
||
func TestLogRangeStopOnError(t *testing.T) { | ||
buffer := bytes.NewBuffer(nil) | ||
logger := Open(buffer) | ||
assert.NoError(t, logger.Append(newCommit(1))) | ||
assert.NoError(t, logger.Append(newCommit(2))) | ||
|
||
assert.Error(t, logger.Range(func(commit Commit) error { | ||
return io.ErrClosedPipe | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters