Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
mlmhl committed Aug 17, 2016
1 parent 70e8e1d commit 6a55646
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 36 deletions.
76 changes: 62 additions & 14 deletions gstac/codebyte/buffer.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,81 @@
package codebyte

import (
"io"
"os"

files "github.com/mlmhl/goutil/io/files"
)

const (
THRESHOLD = 1024 * 1024 // 1M
)

type CodeByteBuffer struct {
buffer []byte
file *os.File
empty bool
}

func NewCodeByteBuffer() *CodeByteBuffer {
func NewCodeByteBuffer(fileName string) (*CodeByteBuffer, error) {
file, err := files.Create(fileName)
if err != nil {
return nil, err
}
return &CodeByteBuffer{
buffer: []byte{},
}
file: file,
empty: false,
}, nil
}

func (buffer *CodeByteBuffer) IsEmpty() bool {
return buffer.Len() == 0
return buffer.empty
}

func (buffer *CodeByteBuffer) Len() int {
return len(buffer.buffer)
}
func (buffer *CodeByteBuffer) Write(b byte) {
buffer.WriteSlice([]byte{b})

func (buffer *CodeByteBuffer) Append(b byte) {
buffer.buffer = append(buffer.buffer, b)
}

func (buffer *CodeByteBuffer) AppendSlice(slice []byte) {
func (buffer *CodeByteBuffer) WriteSlice(slice []byte) {
buffer.buffer = append(buffer.buffer, slice...)
if len(buffer.buffer) >= THRESHOLD {
buffer.Sync()
}
}

func (buffer *CodeByteBuffer) Pop() byte {
b:= buffer.buffer[0]
buffer.buffer = buffer.buffer[1:]
return b
}
func (buffer *CodeByteBuffer) Read(b []byte) (int, error) {
if buffer.IsEmpty() {
return 0, nil
}
cnt, err := buffer.file.Read(b)
if err != nil {
if err == io.EOF {
buffer.empty = true
} else {
return cnt, err
}
}
return cnt, nil
}

func (buffer *CodeByteBuffer) Sync() error {
buffer.flush()
return buffer.file.Sync()
}

func (buffer *CodeByteBuffer) Close() error {
return buffer.file.Close()
}

func (buffer *CodeByteBuffer) flush() {
for {
cnt, _ := buffer.file.Write(buffer.buffer)
if cnt == len(buffer.buffer) {
break
}
buffer.buffer = buffer.buffer[cnt:]
}
buffer.buffer = []byte{}
}
77 changes: 55 additions & 22 deletions gstac/codebyte/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,78 @@ package codebyte

import (
"testing"

files "github.com/mlmhl/goutil/io/files"
)

const (
fileName = "test"
)

var (
buffer *CodeByteBuffer = nil
)

func TestCodeByteBuffer(t *testing.T) {
buffer := NewCodeByteBuffer()
func TestCodeByteBuffer_Write(t *testing.T) {
setup()

t.Log("Test: CodeByteBuffer Append...")
t.Log("Test: CodeByteBuffer Write...")

slice1 := []byte("hello world")
for _, b := range(slice1) {
buffer.Append(b)
}
length := len(slice1)
if buffer.Len() != length {
t.Fatalf("Wrong size: Wanted %d, got %d", length, buffer.Len())
for _, b := range slice1 {
buffer.Write(b)
}
readFromCodeByteBuffer(buffer, slice1, t)

t.Log("Passed...")

t.Log("Test: codeByteBuffer AppendSlice...")
cleanup()
}

func TestCodeByteBuffer_WriteSlice(t *testing.T) {
setup()

t.Log("Test: codeByteBuffer WriteSlice...")

slice2 := []byte("Hello World")
buffer.AppendSlice(slice2)
length += len(slice1)
if buffer.Len() != length {
t.Fatalf("Wrong size: Wanted %d, got %d", length, buffer.Len())
}
buffer.WriteSlice(slice2)
readFromCodeByteBuffer(buffer, slice2, t)

t.Log("Passed...")

t.Log("Test: CodeByteBuffer Pop...")
cleanup()
}

func setup() {
if buffer == nil {
buffer, _ = NewCodeByteBuffer(fileName)
}
}

func readFromCodeByteBuffer(buffer *CodeByteBuffer, target []byte, t *testing.T) {
content := []byte{}
buf := make([]byte, 1024)

bytes := []byte{}
buffer.Sync()
for !buffer.IsEmpty() {
bytes = append(bytes, buffer.Pop())
cnt, err := buffer.Read(buf)
if err != nil {
t.Fatalf("Read from CodeByteBuffer error: %v", err)
}
content = append(content, buf[:cnt]...)
}
target := string(slice1) + string(slice2)
if string(bytes) != target {
t.Fatalf("Wrong content: Wanted %s, got %s", target, string(bytes))

if string(content) != string(target) {
t.Fatalf("Wrong content: Wanted `%s`, got `%s`", string(target), string(content))
}

t.Log("Passed...")
cleanup()
}

func cleanup() {
if buffer != nil {
buffer.Close()
files.Remove(fileName)
buffer = nil
}
}

0 comments on commit 6a55646

Please sign in to comment.