forked from perkeep/perkeep
/
blobpacked.go
769 lines (683 loc) · 21.5 KB
/
blobpacked.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/*
Copyright 2014 The Camlistore AUTHORS
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Package blobpacked registers the "blobpacked" blobserver storage type,
storing blobs initially as one physical blob per logical blob, but then
rearranging little physical blobs into large contiguous blobs organized by
how they'll likely be accessed. An index tracks the mapping from logical to
physical blobs.
Example low-level config:
"/storage/": {
"handler": "storage-blobpacked",
"handlerArgs": {
"smallBlobs": "/small/",
"largeBlobs": "/large/",
"metaIndex": {
"type": "mysql",
.....
}
}
}
The resulting large blobs are valid zip files. Those blobs may up be up to
16 MB and contain the original contiguous file (or fractions of it), as well
as metadata about how the file is cut up. The zip file will have the
following structure:
foo.jpg (or whatever)
camlistore/sha1-beb1df0b75952c7d277905ad14de71ef7ef90c44.json (some file ref)
camlistore/sha1-a0ceb10b04403c9cc1d032e07a9071db5e711c9a.json (some bytes ref)
camlistore/sha1-7b4d9c8529c27d592255c6dfb17188493db96ccc.json (another bytes ref)
camlistore/camlistore-pack-manifest.json
The camlistore-pack-manifest.json is documented on the exported
Manifest type. It looks like this:
{
"wholeRef": "sha1-0e64816d731a56915e8bb4ae4d0ac7485c0b84da",
"wholeSize": 2962227200, // 2.8GB; so will require ~176-180 16MB chunks
"wholePartIndex": 17, // 0-based
"dataBlobsOrigin": "sha1-355705cf62a56669303d2561f29e0620a676c36e",
"dataBlobs": [
{"blob": "sha1-f1d2d2f924e986ac86fdf7b36c94bcdf32beec15", "offset": 0, "size": 273048},
{"blob": "sha1-e242ed3bffccdf271b7fbaf34ed72d089537b42f", "offset": 273048, "size": 112783},
{"blob": "sha1-6eadeac2dade6347e87c0d24fd455feffa7069f0", "offset": 385831, ...},
{"blob": "sha1-beb1df0b75952c7d277905ad14de71ef7ef90c44", "offset": ...},
{"blob": "sha1-a0ceb10b04403c9cc1d032e07a9071db5e711c9a", "offset": ...},
{"blob": "sha1-7b4d9c8529c27d592255c6dfb17188493db96ccc", "offset": ...}
],
}
The 'blobs' property list all the logical blobs. Those are the blobs that
Camlistore reports that it has and were previously stored individually. Now
they're stored as part of a larger blob. The manifest.json ensures that if
the metadata index is lost, the data can be reconstructed from the raw zip
files (using the BlobStreamer interface).
The 'wholeRef' property specifies which large file that this zip is building
up. If the file is less than 15.5 MB or so (leaving room for the zip
overhead and manifest size), it will probably all be in one zip and the
first file in the zip will be the whole thing. Otherwise it'll be cut across
multiple zip files, each no larger than 16MB. In that case, each part of the
file will have a different 'wholePartIndex' number, starting at index
0. Each will have the same 'wholeSize'.
*/
package blobpacked
// TODO: BlobStreamer using the zip manifests, for recovery.
// TODO: option to not even delete from the source?
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"runtime"
"strings"
"sync"
"camlistore.org/pkg/blob"
"camlistore.org/pkg/blobserver"
"camlistore.org/pkg/constants"
"camlistore.org/pkg/context"
"camlistore.org/pkg/jsonconfig"
"camlistore.org/pkg/pools"
"camlistore.org/pkg/schema"
"camlistore.org/pkg/sorted"
"camlistore.org/pkg/strutil"
"camlistore.org/pkg/syncutil"
"camlistore.org/third_party/go/pkg/archive/zip"
)
// TODO: evaluate whether this should even be 0, to keep the schema blobs together at least.
// Files under this size aren't packed.
const packThreshold = 512 << 10
// overhead for zip magic, file headers, TOC, footers. Without measuring accurately,
// saying 50kB for now.
const zipOverhead = 50 << 10
// meta key prefixes
const (
blobMetaPrefix = "b:"
blobMetaPrefixLimit = "b;"
wholeMetaPrefix = "w:"
)
type subFetcherStorage interface {
blobserver.Storage
blob.SubFetcher
}
type storage struct {
small blobserver.Storage
large subFetcherStorage
// meta key -> value rows are:
//
// For logical blobs, "b:" prefix:
// b:sha1-xxxx -> "<size> s"
// b:sha1-xxxx -> "<size> l <big-blobref> <offset_u32>"
//
// For wholerefs:
// w:sha1-xxxx(wholeref) -> "<nbytes_total_u64> <nchunks_u32>"
// Then for each big nchunk of the file:
// w:sha1-xxxx:0 -> "<chunk-blobref> <offset-in-whole_u64> <length_u32>"
// w:sha1-xxxx:1 -> "<chunk-blobref> <offset_in-whole-u64> <length_u32>"
// ...
meta sorted.KeyValue
packGate *syncutil.Gate
}
func (s *storage) String() string {
return fmt.Sprintf("\"blobpacked\" storage")
}
func (s *storage) init() {
s.packGate = syncutil.NewGate(10)
}
func init() {
blobserver.RegisterStorageConstructor("blobpacked", blobserver.StorageConstructor(newFromConfig))
}
func newFromConfig(ld blobserver.Loader, conf jsonconfig.Obj) (blobserver.Storage, error) {
var (
smallPrefix = conf.RequiredString("smallBlobs")
largePrefix = conf.RequiredString("largeBlobs")
metaConf = conf.RequiredObject("metaIndex")
)
if err := conf.Validate(); err != nil {
return nil, err
}
small, err := ld.GetStorage(smallPrefix)
if err != nil {
return nil, fmt.Errorf("failed to load smallBlobs at %s: %v", smallPrefix, err)
}
large, err := ld.GetStorage(largePrefix)
if err != nil {
return nil, fmt.Errorf("failed to load largeBlobs at %s: %v", largePrefix, err)
}
largeSubber, ok := large.(subFetcherStorage)
if !ok {
return nil, fmt.Errorf("largeBlobs at %q of type %T doesn't support fetching sub-ranges of blobs",
largePrefix, large)
}
meta, err := sorted.NewKeyValue(metaConf)
if err != nil {
return nil, fmt.Errorf("failed to setup blobpacked metaIndex: %v", err)
}
sto := &storage{
small: small,
large: largeSubber,
meta: meta,
}
sto.init()
return sto, nil
}
func (s *storage) Close() error {
return nil
}
type meta struct {
exists bool
size uint32
largeRef blob.Ref // if invalid, then on small if exists
largeOff uint32
}
// if not found, err == nil.
func (s *storage) getMetaRow(br blob.Ref) (meta, error) {
v, err := s.meta.Get(blobMetaPrefix + br.String())
if err == sorted.ErrNotFound {
return meta{}, nil
}
return parseMetaRow([]byte(v))
}
var singleSpace = []byte{' '}
// parses one of:
// "<size_u32> s"
// "<size_u32> l <big-blobref> <big-offset>"
func parseMetaRow(v []byte) (m meta, err error) {
row := v
sp := bytes.IndexByte(v, ' ')
if sp < 1 || sp == len(v)-1 {
return meta{}, fmt.Errorf("invalid metarow %q", v)
}
m.exists = true
size, err := strutil.ParseUintBytes(v[:sp], 10, 32)
if err != nil {
return meta{}, fmt.Errorf("invalid metarow size %q", v)
}
m.size = uint32(size)
v = v[sp+1:]
switch v[0] {
default:
return meta{}, fmt.Errorf("invalid metarow type %q", v)
case 's':
if len(v) > 1 {
return meta{}, fmt.Errorf("invalid small metarow %q", v)
}
return
case 'l':
if len(v) < 2 || v[1] != ' ' {
err = errors.New("length")
break
}
v = v[2:] // remains: "<big-blobref> <big-offset>"
if bytes.Count(v, singleSpace) != 1 {
err = errors.New("number of spaces")
break
}
sp := bytes.IndexByte(v, ' ')
largeRef, ok := blob.ParseBytes(v[:sp])
if !ok {
err = fmt.Errorf("bad blobref %q", v[:sp])
break
}
m.largeRef = largeRef
off, err := strutil.ParseUintBytes(v[sp+1:], 10, 32)
if err != nil {
break
}
m.largeOff = uint32(off)
return m, nil
}
return meta{}, fmt.Errorf("invalid metarow %q: %v", row, err)
}
func parseMetaRowSizeOnly(v []byte) (size uint32, err error) {
sp := bytes.IndexByte(v, ' ')
if sp < 1 || sp == len(v)-1 {
return 0, fmt.Errorf("invalid metarow %q", v)
}
size64, err := strutil.ParseUintBytes(v[:sp], 10, 32)
if err != nil {
return 0, fmt.Errorf("invalid metarow size %q", v)
}
return uint32(size64), nil
}
func (s *storage) ReceiveBlob(br blob.Ref, source io.Reader) (sb blob.SizedRef, err error) {
buf := pools.BytesBuffer()
defer pools.PutBuffer(buf)
if _, err := io.Copy(buf, source); err != nil {
return sb, err
}
size := uint32(buf.Len())
isFile := false
fileBlob, err := schema.BlobFromReader(br, bytes.NewReader(buf.Bytes()))
if err == nil && fileBlob.Type() == "file" {
isFile = true
}
meta, err := s.getMetaRow(br)
if err != nil {
return sb, err
}
if meta.exists {
sb = blob.SizedRef{Size: size, Ref: br}
} else {
sb, err = s.small.ReceiveBlob(br, buf)
if err != nil {
return sb, err
}
if err := s.meta.Set(blobMetaPrefix+br.String(), fmt.Sprintf("%d s", size)); err != nil {
return sb, err
}
}
if !isFile || meta.largeRef.Valid() || fileBlob.PartsSize() < packThreshold {
return sb, nil
}
// Pack the blob.
s.packGate.Start()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer s.packGate.Done()
defer wg.Done()
if err := s.packFile(br); err != nil {
log.Printf("Error packing file %s: %v", br, err)
}
}()
wg.Wait()
return sb, nil
}
func (s *storage) Fetch(br blob.Ref) (io.ReadCloser, uint32, error) {
m, err := s.getMetaRow(br)
if err != nil {
return nil, 0, err
}
if !m.exists {
return nil, 0, os.ErrNotExist
}
if !m.largeRef.Valid() {
return s.small.Fetch(br)
}
rc, err := s.large.SubFetch(m.largeRef, int64(m.largeOff), int64(m.size))
if err != nil {
return nil, 0, err
}
return rc, m.size, nil
}
func (s *storage) RemoveBlobs(blobs []blob.Ref) error {
// TODO: how to support? only delete from index? delete from
// small if only there? if in big file, re-break apart into
// its chunks? no reverse index from big chunk to all its
// constituent chunks, though. I suppose we could read the chunks
// from the metadata file in the zip.
return errors.New("not implemented")
}
func (s *storage) StatBlobs(dest chan<- blob.SizedRef, blobs []blob.Ref) (err error) {
for _, br := range blobs {
m, err := s.getMetaRow(br)
if err != nil {
return err
}
if m.exists {
dest <- blob.SizedRef{Ref: br, Size: m.size}
}
}
return nil
}
func (s *storage) EnumerateBlobs(ctx *context.Context, dest chan<- blob.SizedRef, after string, limit int) (err error) {
defer close(dest)
t := s.meta.Find(blobMetaPrefix+after, blobMetaPrefixLimit)
defer func() {
closeErr := t.Close()
if err == nil {
err = closeErr
}
}()
n := 0
afterb := []byte(after)
for n < limit && t.Next() {
key := t.KeyBytes()[len(blobMetaPrefix):]
if n == 0 && bytes.Equal(key, afterb) {
continue
}
n++
br, ok := blob.ParseBytes(key)
if !ok {
return fmt.Errorf("unknown key %q in meta index", t.Key())
}
size, err := parseMetaRowSizeOnly(t.ValueBytes())
if err != nil {
return err
}
dest <- blob.SizedRef{Ref: br, Size: size}
}
return nil
}
func (s *storage) blobSource() blob.Fetcher {
// TODO: find or use a cache. make all uploaded blobs to the
// server go to the cache too. Put the cache on fast local
// disk or memory. Make sure it works well no GCE too, where
// reconstruction of the packFile in the common case should
// never do GET requests to Google Cloud Storage.
// For now, just use the small store:
return s.small
}
func (s *storage) packFile(fileRef blob.Ref) error {
fr, err := schema.NewFileReader(s.blobSource(), fileRef)
if err != nil {
return err
}
return newPacker(s, fileRef, fr).pack()
}
func newPacker(s *storage, fileRef blob.Ref, fr *schema.FileReader) *packer {
return &packer{
s: s,
src: s.blobSource(),
fileRef: fileRef,
fr: fr,
dataSize: map[blob.Ref]uint32{},
schemaBlob: map[blob.Ref]*blob.Blob{},
schemaParent: map[blob.Ref][]blob.Ref{},
}
}
// A packer writes a file out
type packer struct {
s *storage
fileRef blob.Ref
src blob.Fetcher
fr *schema.FileReader
wholeRef blob.Ref
wholeSize int64
dataRefs []blob.Ref // in order
dataSize map[blob.Ref]uint32
schemaRefs []blob.Ref // in order, but irrelevant
schemaBlob map[blob.Ref]*blob.Blob
schemaParent map[blob.Ref][]blob.Ref // data blob -> its parent/ancestor schema blob(s)
chunksRemain []blob.Ref
zips []writtenZip
}
type writtenZip struct {
blob.SizedRef
dataRefs []blob.Ref
}
func (pk *packer) pack() error {
if err := pk.scanChunks(); err != nil {
return err
}
// TODO: decide as a fuction of schemaRefs and dataRefs
// already in s.large whether it makes sense to still compact
// this from a savings standpoint. For now we just always do.
// Maybe we'd have knobs in the future. Ideally not.
// Don't pack a file if we already have its wholeref stored
// otherwise (perhaps under a different filename). But that means
// we have to compute its wholeref first. We assume the blobSource
// will cache these lookups so it's not too expensive to do two
// passes over the input.
h := blob.NewHash()
var err error
pk.wholeSize, err = io.Copy(h, pk.fr)
if err != nil {
return err
}
pk.wholeRef = blob.RefFromHash(h)
if _, err = pk.s.meta.Get(wholeMetaPrefix + pk.wholeRef.String()); err == nil {
// Nil error means there was some knowledge of this wholeref.
return fmt.Errorf("already have wholeref %v packed; not packing again", pk.wholeRef)
} else if err != sorted.ErrNotFound {
return err
}
pk.chunksRemain = pk.dataRefs
var trunc blob.Ref
MakingZips:
for len(pk.chunksRemain) > 0 {
if err := pk.writeAZip(trunc); err != nil {
if needTrunc, ok := err.(needsTruncatedAfterError); ok {
trunc = needTrunc.Ref
continue MakingZips
}
return err
}
trunc = blob.Ref{}
}
return nil
}
func (pk *packer) scanChunks() error {
schemaSeen := map[blob.Ref]bool{}
return pk.fr.ForeachChunk(func(schemaPath []blob.Ref, p schema.BytesPart) error {
if !p.BlobRef.Valid() {
return errors.New("sparse files are not packed")
}
if p.Offset != 0 {
// TODO: maybe care about this later, if we ever start making
// these sorts of files.
return errors.New("file uses complicated schema. not packing.")
}
pk.schemaParent[p.BlobRef] = append([]blob.Ref(nil), schemaPath...) // clone it
pk.dataSize[p.BlobRef] = uint32(p.Size)
for _, schemaRef := range schemaPath {
if schemaSeen[schemaRef] {
continue
}
schemaSeen[schemaRef] = true
pk.schemaRefs = append(pk.schemaRefs, schemaRef)
if b, err := blob.FromFetcher(pk.src, schemaRef); err != nil {
return err
} else {
pk.schemaBlob[schemaRef] = b
}
}
pk.dataRefs = append(pk.dataRefs, p.BlobRef)
return nil
})
}
// needsTruncatedAfterError is returend by writeAZip if it failed in its estimation and the zip file
// was over the 16MB (or whatever) max blob size limit. In this case the caller tries again
type needsTruncatedAfterError struct{ blob.Ref }
func (e needsTruncatedAfterError) Error() string { return "needs truncation after " + e.Ref.String() }
// check should only be used for things which really shouldn't ever happen, but should
// still be checked. If there is interesting logic in the 'else', then don't use this.
func check(err error) {
if err != nil {
b := make([]byte, 2<<10)
b = b[:runtime.Stack(b, false)]
log.Printf("Unlikely error condition triggered: %v at %s", err, b)
panic(err)
}
}
// trunc is a hint about which blob to truncate after. It may be zero.
func (pk *packer) writeAZip(trunc blob.Ref) (err error) {
defer func() {
if e := recover(); e != nil {
if v, ok := e.(error); ok && err == nil {
err = v
} else {
panic(e)
}
}
}()
mf := Manifest{
WholeRef: pk.wholeRef,
WholeSize: pk.wholeSize,
WholePartIndex: len(pk.zips),
}
var zbuf bytes.Buffer
cw := &countWriter{w: &zbuf}
zw := zip.NewWriter(cw)
var approxSize int // can't use zbuf.Len because zw buffers
var dataRefsWritten []blob.Ref
var schemaBlobSeen = map[blob.Ref]bool{}
var schemaBlobs []blob.Ref // to add after the main file
baseFileName := pk.fr.FileName()
if strings.Contains(baseFileName, "/") || strings.Contains(baseFileName, "\\") {
return fmt.Errorf("File schema blob %v filename had a slash in it: %q", pk.fr.SchemaBlobRef(), baseFileName)
}
fh := &zip.FileHeader{
Name: baseFileName,
Method: zip.Store, // uncompressed
}
fh.SetModTime(pk.fr.ModTime())
fh.SetMode(0644)
fw, err := zw.CreateHeader(fh)
check(err)
check(zw.Flush())
dataStart := cw.n
chunks := pk.chunksRemain
truncated := false
chunkWholeHash := blob.NewHash()
for len(chunks) > 0 {
dr := chunks[0] // the next chunk to maybe write
if trunc.Valid() && trunc == dr {
if approxSize == 0 {
return errors.New("first blob is too large to pack, once you add the zip overhead")
}
truncated = true
break
}
schemaBlobsSave := schemaBlobs
for _, parent := range pk.schemaParent[dr] {
if !schemaBlobSeen[parent] {
schemaBlobSeen[parent] = true
schemaBlobs = append(schemaBlobs, parent)
approxSize += int(pk.schemaBlob[parent].Size())
}
}
thisSize := pk.dataSize[dr]
approxSize += int(thisSize)
if approxSize+mf.approxSerializedSize()+zipOverhead > constants.MaxBlobSize {
schemaBlobs = schemaBlobsSave // restore it
truncated = true
break
}
// Copy the data to the zip.
rc, size, err := pk.src.Fetch(dr)
check(err)
if size != thisSize {
rc.Close()
return errors.New("unexpected size")
}
if n, err := io.Copy(io.MultiWriter(fw, chunkWholeHash), rc); err != nil || n != int64(size) {
rc.Close()
return fmt.Errorf("copy to zip = %v, %v; want %v bytes", n, err, size)
}
rc.Close()
dataRefsWritten = append(dataRefsWritten, dr)
chunks = chunks[1:]
}
mf.DataBlobsOrigin = blob.RefFromHash(chunkWholeHash)
// zipBlobs is where a schema or data blob is relative to the beginning
// of the zip file.
var zipBlobs []BlobAndPos
var dataOffset int64
for _, br := range dataRefsWritten {
size := pk.dataSize[br]
mf.DataBlobs = append(mf.DataBlobs, BlobAndPos{blob.SizedRef{br, size}, dataOffset})
zipBlobs = append(zipBlobs, BlobAndPos{blob.SizedRef{br, size}, dataStart + dataOffset})
dataOffset += int64(size)
}
for _, br := range schemaBlobs {
fw, err := zw.CreateHeader(&zip.FileHeader{
Name: "camlistore/" + br.String() + ".json",
Method: zip.Store, // uncompressed
})
check(err)
check(zw.Flush())
b := pk.schemaBlob[br]
zipBlobs = append(zipBlobs, BlobAndPos{blob.SizedRef{br, b.Size()}, cw.n})
rc := b.Open()
n, err := io.Copy(fw, rc)
rc.Close()
check(err)
if n != int64(b.Size()) {
return fmt.Errorf("failed to write all of schema blob %v: %n bytes, not wanted %d", br, n, b.Size())
}
}
// Manifest file
fw, err = zw.Create("camlistore/camlistore-pack-manifest.json")
check(err)
enc, err := json.MarshalIndent(mf, "", " ")
check(err)
_, err = fw.Write(enc)
check(err)
err = zw.Close()
check(err)
if zbuf.Len() > constants.MaxBlobSize {
// We guessed wrong. Back up. Find out how many blobs we went over.
overage := zbuf.Len() - constants.MaxBlobSize
for i := len(dataRefsWritten) - 1; i >= 0; i-- {
dr := dataRefsWritten[i]
if overage <= 0 {
return needsTruncatedAfterError{dr}
}
overage -= int(pk.dataSize[dr])
}
return errors.New("file is unpackable; first blob is too big to fit")
}
zipRef := blob.SHA1FromBytes(zbuf.Bytes())
zipSB, err := blobserver.ReceiveNoHash(pk.s.large, zipRef, bytes.NewReader(zbuf.Bytes()))
if err != nil {
return err
}
pk.zips = append(pk.zips, writtenZip{
SizedRef: zipSB,
dataRefs: dataRefsWritten,
})
bm := pk.s.meta.BeginBatch()
for _, zb := range zipBlobs {
bm.Set(blobMetaPrefix+zb.Ref.String(), fmt.Sprintf("%d l %v %d", zb.Size, zipRef, zb.Offset))
}
if err := pk.s.meta.CommitBatch(bm); err != nil {
return err
}
_ = truncated
// On success, consume the chunks we wrote from pk.chunksRemain.
pk.chunksRemain = pk.chunksRemain[len(dataRefsWritten):]
return nil
}
type BlobAndPos struct {
blob.SizedRef
Offset int64 `json:"offset"`
}
// Manifest is the JSON description type representing the
// "camlistore/camlistore-pack-manifest.json" file found in a blobpack
// zip file.
type Manifest struct {
// WholeRef is the blobref of the entire file that this zip is
// either fully or partially describing. For files under
// around 16MB, the WholeRef and DataBlobsOrigin will be
// the same.
WholeRef blob.Ref `json:"wholeRef"`
// WholeSize is the number of bytes in the original file being
// cut up.
WholeSize int64 `json:"wholeSize"`
// WholePartIndex is the chunk number (0-based) of this zip file.
// If a client has 'n' zip files with the same WholeRef whose
// WholePartIndexes are contiguous (including 0) and the sum of
// the DataBlobs equals WholeSize, the client has the entire
// original file.
WholePartIndex int `json:"wholePartIndex"`
// DataBlobsOrigin is the blobref of the contents of the first
// file in the zip pack file. It is the origin of all the logical data
// blobs referenced in DataBlobs.
DataBlobsOrigin blob.Ref `json:"dataBlobsOrigin"`
// DataBlobs describes all the logical blobs that are
// concatenated together in the DataBlobsOrigin.
DataBlobs []BlobAndPos `json:"dataBlobs"`
}
func (mf *Manifest) approxSerializedSize() int {
// Conservative:
return 250 + len(mf.DataBlobs)*100
}
type countWriter struct {
w io.Writer
n int64
}
func (cw *countWriter) Write(p []byte) (n int, err error) {
n, err = cw.w.Write(p)
cw.n += int64(n)
return
}