Skip to content

Commit 322f92f

Browse files
author
Hongchao Deng
committed
refactor on recordio
1 parent fce9ea4 commit 322f92f

File tree

12 files changed

+209
-379
lines changed

12 files changed

+209
-379
lines changed

db/backend.go

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,58 @@
11
package db
22

33
import (
4-
"bytes"
54
"io/ioutil"
65
"os"
76
"strings"
87

9-
"github.com/go-distributed/xtree/db/record"
8+
"github.com/go-distributed/xtree/db/recordio"
109
"github.com/go-distributed/xtree/third-party/github.com/google/btree"
1110
)
1211

1312
type backend struct {
14-
bt *btree.BTree
15-
cache *cache
16-
rev int
17-
reader *record.Reader
18-
writer *record.Writer
13+
bt *btree.BTree
14+
cache *cache
15+
rev int
16+
fc recordio.Fetcher
17+
ap recordio.Appender
1918
}
2019

2120
func newBackend() *backend {
2221
bt := btree.New(10)
2322

24-
file, err := os.OpenFile("test-records",
25-
os.O_RDWR|os.O_APPEND|os.O_CREATE|os.O_TRUNC,
26-
os.FileMode(0644))
23+
// temporary file IO to test in-disk values
24+
writeFile, err := ioutil.TempFile("", "backend")
2725
if err != nil {
28-
panic("can't open file")
26+
panic("can't create temp file")
27+
}
28+
readFile, err := os.Open(writeFile.Name())
29+
if err != nil {
30+
panic("can't open temp file")
2931
}
3032

3133
return &backend{
32-
bt: bt,
33-
cache: newCache(),
34-
reader: record.NewReader(file, new(record.LittleEndianDecoder)),
35-
writer: record.NewWriter(file, new(record.LittleEndianEncoder)),
34+
bt: bt,
35+
cache: newCache(),
36+
fc: recordio.NewFetcher(readFile),
37+
ap: recordio.NewAppender(writeFile),
3638
}
3739
}
3840

39-
func (b *backend) Close() {
40-
os.Remove("test-records")
41-
}
42-
4341
func (b *backend) getData(offset int64) []byte {
44-
reader, err := b.reader.ReadFromIndex(offset)
45-
if err != nil {
46-
panic("unimplemented")
47-
}
48-
data, err := ioutil.ReadAll(reader)
42+
rec, err := b.fc.Fetch(offset)
4943
if err != nil {
5044
panic("unimplemented")
5145
}
52-
return data
46+
return rec.Data
5347
}
5448

5549
// if it couldn't find anything related to path, it return Value of 0 rev.
5650
func (b *backend) Get(rev int, path Path) Value {
51+
if b.cache != nil {
52+
if v, ok := b.cache.get(revpath{rev: rev, path: path.p}); ok {
53+
return v
54+
}
55+
}
5756
item := b.bt.Get(&path)
5857
if item == nil {
5958
return Value{}
@@ -88,14 +87,12 @@ func (b *backend) Put(rev int, path Path, data []byte) {
8887
}
8988

9089
b.rev++
91-
offset, err := b.writer.Append(bytes.NewBuffer(data))
90+
offset, err := b.ap.Append(recordio.Record{data})
9291
if err != nil {
9392
panic("unimplemented")
9493
}
9594

9695
nv.offset = offset
97-
98-
b.writer.Flush()
9996
}
10097

10198
// one-level listing

db/backend_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ func TestPut(t *testing.T) {
2727
t.Errorf("#%d: data = %s, want %s", i, v.data, tt.data)
2828
}
2929
}
30-
b.Close()
3130
}
3231

3332
func TestPutOnExistingPath(t *testing.T) {
@@ -62,7 +61,6 @@ func TestPutOnExistingPath(t *testing.T) {
6261
t.Errorf("#%d 2: data = %s, want %s", i, v.data, tt.data2)
6362
}
6463
}
65-
b.Close()
6664
}
6765

6866
func TestGetMVCC(t *testing.T) {
@@ -98,7 +96,6 @@ func TestGetMVCC(t *testing.T) {
9896
t.Errorf("#%d: data = %s, want %s", i, v.data, tt.data)
9997
}
10098
}
101-
b.Close()
10299
}
103100

104101
func TestLs(t *testing.T) {
@@ -131,7 +128,6 @@ func TestLs(t *testing.T) {
131128
}
132129
}
133130
}
134-
back.Close()
135131
}
136132

137133
func BenchmarkPut(b *testing.B) {
@@ -147,7 +143,6 @@ func BenchmarkPut(b *testing.B) {
147143
for i := 1; i < b.N; i++ {
148144
back.Put(i, path[i], d)
149145
}
150-
back.Close()
151146
}
152147

153148
func BenchmarkGetWithCache(b *testing.B) {
@@ -168,7 +163,6 @@ func BenchmarkGetWithCache(b *testing.B) {
168163
back.Get(i, path[i])
169164
}
170165
}
171-
back.Close()
172166
}
173167

174168
func BenchmarkGetWithOutCache(b *testing.B) {
@@ -190,5 +184,4 @@ func BenchmarkGetWithOutCache(b *testing.B) {
190184
back.Get(i, path[i])
191185
}
192186
}
193-
back.Close()
194187
}

db/record/reader.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

db/record/reader_writer_test.go

Lines changed: 0 additions & 86 deletions
This file was deleted.

db/record/record.go

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)