Skip to content

Commit a3819a7

Browse files
authored
Do not recompress chunks if we already have the compressed form (#289)
1 parent f1b01fc commit a3819a7

File tree

8 files changed

+21
-43
lines changed

8 files changed

+21
-43
lines changed

chunk.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,17 @@ func (c *Chunk) ID() ChunkID {
8686
c.idCalculated = true
8787
return c.id
8888
}
89+
90+
// Storage returns the chunk data in compressed form. If the chunk was created
91+
// with compressed data and same modifiers, this data will be returned as is. The
92+
// caller must not modify the data in the returned slice.
93+
func (c *Chunk) Storage(modifiers Converters) ([]byte, error) {
94+
if len(c.storage) > 0 && modifiers.equal(c.converters) {
95+
return c.storage, nil
96+
}
97+
b, err := c.Data()
98+
if err != nil {
99+
return nil, err
100+
}
101+
return modifiers.toStorage(b)
102+
}

gcs.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,7 @@ func (s GCStore) StoreChunk(chunk *Chunk) error {
143143
})
144144
)
145145

146-
b, err := chunk.Data()
147-
if err != nil {
148-
log.WithError(err).Error("Cannot retrieve chunk data")
149-
return err
150-
}
151-
b, err = s.converters.toStorage(b)
146+
b, err := chunk.Storage(s.converters)
152147
if err != nil {
153148
log.WithError(err).Error("Cannot retrieve chunk data")
154149
return err

httphandler.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,7 @@ func (h HTTPHandler) get(id ChunkID, w http.ResponseWriter) {
5757
var b []byte
5858
chunk, err := h.s.GetChunk(id)
5959
if err == nil {
60-
// Optimization for when the chunk modifiers match those
61-
// of the chunk server. In that case it's not necessary
62-
// to convert back and forth. Just use the raw data as loaded
63-
// from the store.
64-
if len(chunk.storage) > 0 && h.converters.equal(chunk.converters) {
65-
b = chunk.storage
66-
} else {
67-
b, err = chunk.Data()
68-
if err == nil {
69-
b, err = h.converters.toStorage(b)
70-
}
71-
}
60+
b, err = chunk.Storage(h.converters)
7261
}
7362
h.HTTPHandlerBase.get(id.String(), b, err, w)
7463
}

local.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,7 @@ func (s LocalStore) RemoveChunk(id ChunkID) error {
6868
// StoreChunk adds a new chunk to the store
6969
func (s LocalStore) StoreChunk(chunk *Chunk) error {
7070
d, p := s.nameFromID(chunk.ID())
71-
b, err := chunk.Data()
72-
if err != nil {
73-
return err
74-
}
75-
b, err = s.converters.toStorage(b)
71+
b, err := chunk.Storage(s.converters)
7672
if err != nil {
7773
return err
7874
}

protocolserver.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ func (s *ProtocolServer) Serve(ctx context.Context) error {
6161
}
6262
return errors.Wrap(err, "unable to read chunk from store")
6363
}
64-
b, err := chunk.Data()
65-
if err != nil {
66-
return err
67-
}
68-
b, err = Compressor{}.toStorage(b)
64+
b, err := chunk.Storage([]converter{Compressor{}})
6965
if err != nil {
7066
return err
7167
}

remotehttp.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,7 @@ func (r *RemoteHTTP) HasChunk(id ChunkID) (bool, error) {
247247
// StoreChunk adds a new chunk to the store
248248
func (r *RemoteHTTP) StoreChunk(chunk *Chunk) error {
249249
p := r.nameFromID(chunk.ID())
250-
b, err := chunk.Data()
251-
if err != nil {
252-
return err
253-
}
254-
b, err = r.converters.toStorage(b)
250+
b, err := chunk.Storage(r.converters)
255251
if err != nil {
256252
return err
257253
}

s3.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,7 @@ retry:
125125
func (s S3Store) StoreChunk(chunk *Chunk) error {
126126
contentType := "application/zstd"
127127
name := s.nameFromID(chunk.ID())
128-
b, err := chunk.Data()
129-
if err != nil {
130-
return err
131-
}
132-
b, err = s.converters.toStorage(b)
128+
b, err := chunk.Storage(s.converters)
133129
if err != nil {
134130
return err
135131
}

sftp.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,7 @@ func (s *SFTPStore) StoreChunk(chunk *Chunk) error {
191191
c := <-s.pool
192192
defer func() { s.pool <- c }()
193193
name := c.nameFromID(chunk.ID())
194-
b, err := chunk.Data()
195-
if err != nil {
196-
return err
197-
}
198-
b, err = s.converters.toStorage(b)
194+
b, err := chunk.Storage(s.converters)
199195
if err != nil {
200196
return err
201197
}

0 commit comments

Comments
 (0)