Skip to content

Commit e23b6c6

Browse files
authored
Merge pull request #6 from zweihander/plainfiles
feat(dl): add plainfiles cache
2 parents a7a23d6 + 0da4881 commit e23b6c6

File tree

9 files changed

+100
-130
lines changed

9 files changed

+100
-130
lines changed

cmd/getdoc/main.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ func main() {
2828
if err != nil {
2929
panic(err)
3030
}
31-
defer func() {
32-
if err := client.Close(); err != nil {
33-
panic(err)
34-
}
35-
}()
3631

3732
ctx := context.Background()
3833
fmt.Println("Extracting")

dl/dl.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,48 @@ import (
2020
type Client struct {
2121
rate ratelimit.Limiter
2222
http HTTPClient
23-
cache *cache.Cacher
23+
cache cache.Cacher
2424
readonly bool
2525
}
2626

2727
type Options struct {
2828
Client HTTPClient
2929
Path string
3030
Readonly bool
31+
FromZip bool
3132
}
3233

3334
type HTTPClient interface {
3435
Do(req *http.Request) (*http.Response, error)
3536
}
3637

3738
func NewClient(opt Options) (*Client, error) {
38-
c, err := cache.NewCacher(opt.Path, opt.Readonly)
39-
if err != nil {
40-
return nil, err
41-
}
42-
4339
if opt.Client == nil {
4440
opt.Client = http.DefaultClient
4541
}
4642

47-
return &Client{
43+
c := &Client{
4844
http: opt.Client,
4945
readonly: opt.Readonly,
5046
rate: ratelimit.New(10),
51-
cache: c,
52-
}, nil
53-
}
47+
}
5448

55-
var ErrReadOnly = errors.New("write operation in read only mode")
49+
if opt.FromZip {
50+
zipCache, err := cache.NewFromZip(opt.Path)
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
c.cache = zipCache
56+
} else {
57+
c.cache = cache.NewFromDirectory(opt.Path)
58+
}
5659

57-
func (c *Client) Close() error {
58-
return c.cache.Close()
60+
return c, nil
5961
}
6062

63+
var ErrReadOnly = errors.New("write operation in read only mode")
64+
6165
// NoLayer can be passed as "layer" argument.
6266
const NoLayer = 0
6367

@@ -117,9 +121,9 @@ func (c *Client) download(ctx context.Context, layer int, key string) ([]byte, e
117121
//
118122
// Blank key is invalid.
119123
func (c *Client) Get(ctx context.Context, layer int, key string) ([]byte, error) {
120-
cacheKey := fmt.Sprintf("%d/%s", layer, key)
121-
if layer == NoLayer {
122-
cacheKey = "default-layer/" + key
124+
cacheKey := key
125+
if layer != NoLayer {
126+
cacheKey = fmt.Sprintf("%d/%s", layer, key)
123127
}
124128

125129
// Trying to get from cache.

dl/dl_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@ func TestClient_Download(t *testing.T) {
1818
Client: unusableHTTPClient{},
1919
Path: filepath.Join("_testdata", "121.zip"),
2020
Readonly: true,
21+
FromZip: true,
2122
})
2223
if err != nil {
2324
t.Fatal(err)
2425
}
25-
defer func() {
26-
if err := c.Close(); err != nil {
27-
t.Fatal(err)
28-
}
29-
}()
3026

3127
data, err := c.Get(context.Background(), 121, "schema")
3228
if err != nil {

dl/internal/cache/cache.go

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

dl/internal/cache/cacher.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cache
2+
3+
import "errors"
4+
5+
type Cacher interface {
6+
Get(key string) ([]byte, error)
7+
Set(key string, value []byte) error
8+
}
9+
10+
var ErrNotFound = errors.New("not found")

dl/internal/cache/plain.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cache
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"path/filepath"
7+
)
8+
9+
type PlainFiles struct {
10+
path string
11+
}
12+
13+
func NewFromDirectory(path string) PlainFiles {
14+
return PlainFiles{path}
15+
}
16+
17+
func (p PlainFiles) Get(key string) ([]byte, error) {
18+
path := filepath.Join(p.path, key)
19+
if _, err := os.Stat(path); err != nil {
20+
if os.IsNotExist(err) {
21+
return nil, ErrNotFound
22+
}
23+
}
24+
25+
return ioutil.ReadFile(path)
26+
}
27+
28+
func (p PlainFiles) Set(key string, value []byte) error {
29+
path := filepath.Join(p.path, key)
30+
if err := os.MkdirAll(filepath.Dir(path), 0750); err != nil {
31+
return err
32+
}
33+
34+
return ioutil.WriteFile(path, value, 0600)
35+
}

dl/internal/cache/utils.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"archive/zip"
55
"bytes"
66
"io/ioutil"
7-
"os"
8-
"path/filepath"
97
)
108

119
func filesFromZip(p string) (map[string][]byte, error) {
@@ -42,32 +40,3 @@ func filesFromZip(p string) (map[string][]byte, error) {
4240

4341
return files, nil
4442
}
45-
46-
func dumpZip(files map[string][]byte, path string) error {
47-
if err := os.MkdirAll(filepath.Dir(path), 0750); err != nil {
48-
return err
49-
}
50-
51-
f, err := os.Create(path)
52-
if err != nil {
53-
return err
54-
}
55-
defer f.Close()
56-
57-
w := zip.NewWriter(f)
58-
defer w.Close()
59-
60-
for path, data := range files {
61-
path = filepath.ToSlash(path)
62-
fw, err := w.Create(path)
63-
if err != nil {
64-
return err
65-
}
66-
67-
if _, err := fw.Write(data); err != nil {
68-
return err
69-
}
70-
}
71-
72-
return nil
73-
}

dl/internal/cache/zip.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cache
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type ZipReadonly struct {
8+
files map[string][]byte
9+
}
10+
11+
func NewFromZip(path string) (*ZipReadonly, error) {
12+
files, err := filesFromZip(path)
13+
if err != nil {
14+
return nil, err
15+
}
16+
17+
return &ZipReadonly{
18+
files: files,
19+
}, nil
20+
}
21+
22+
func (c *ZipReadonly) Get(key string) ([]byte, error) {
23+
data, found := c.files[key]
24+
if !found {
25+
return nil, ErrNotFound
26+
}
27+
28+
return data, nil
29+
}
30+
31+
func (c *ZipReadonly) Set(key string, value []byte) error {
32+
return fmt.Errorf("unsupported operation: set")
33+
}

extract_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@ func TestExtract(t *testing.T) {
2222
Client: unusableHTTPClient{},
2323
Path: filepath.Join("dl", "_testdata", "121.zip"),
2424
Readonly: true,
25+
FromZip: true,
2526
})
2627
if err != nil {
2728
t.Fatal(err)
2829
}
29-
defer func() {
30-
if err := c.Close(); err != nil {
31-
t.Fatal(err)
32-
}
33-
}()
3430

3531
doc, err := ExtractLayer(context.Background(), 121, c)
3632
if err != nil {

0 commit comments

Comments
 (0)