/
cmd_createversionstoreindex.go
106 lines (91 loc) · 3.9 KB
/
cmd_createversionstoreindex.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
package commands
import (
"time"
"github.com/DanEngelbrecht/golongtail/longtaillib"
"github.com/DanEngelbrecht/golongtail/longtailutils"
"github.com/DanEngelbrecht/golongtail/remotestore"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func createVersionStoreIndex(
numWorkerCount int,
blobStoreURI string,
s3EndpointResolverURI string,
sourceFilePath string,
versionLocalStoreIndexPath string) ([]longtailutils.StoreStat, []longtailutils.TimeStat, error) {
const fname = "createVersionStoreIndex"
log := logrus.WithFields(logrus.Fields{
"fname": fname,
"numWorkerCount": numWorkerCount,
"blobStoreURI": blobStoreURI,
"s3EndpointResolverURI": s3EndpointResolverURI,
"sourceFilePath": sourceFilePath,
"versionLocalStoreIndexPath": versionLocalStoreIndexPath,
})
log.Info(fname)
storeStats := []longtailutils.StoreStat{}
timeStats := []longtailutils.TimeStat{}
setupStartTime := time.Now()
jobs := longtaillib.CreateBikeshedJobAPI(uint32(numWorkerCount), 0)
defer jobs.Dispose()
indexStore, err := remotestore.CreateBlockStoreForURI(blobStoreURI, nil, jobs, numWorkerCount, 8388608, 1024, remotestore.ReadOnly, false, longtailutils.WithS3EndpointResolverURI(s3EndpointResolverURI))
if err != nil {
return storeStats, timeStats, errors.Wrap(err, fname)
}
defer indexStore.Dispose()
setupTime := time.Since(setupStartTime)
timeStats = append(timeStats, longtailutils.TimeStat{"Setup", setupTime})
readSourceStartTime := time.Now()
vbuffer, err := longtailutils.ReadFromURI(sourceFilePath, longtailutils.WithS3EndpointResolverURI(s3EndpointResolverURI))
if err != nil {
return storeStats, timeStats, errors.Wrap(err, fname)
}
sourceVersionIndex, err := longtaillib.ReadVersionIndexFromBuffer(vbuffer)
if err != nil {
err = errors.Wrapf(err, "Cant parse version index from `%s`", sourceFilePath)
return storeStats, timeStats, errors.Wrap(err, fname)
}
defer sourceVersionIndex.Dispose()
readSourceTime := time.Since(readSourceStartTime)
timeStats = append(timeStats, longtailutils.TimeStat{"Read source index", readSourceTime})
getExistingContentStartTime := time.Now()
chunkHashes := sourceVersionIndex.GetChunkHashes()
retargettedVersionStoreIndex, err := longtailutils.GetExistingStoreIndexSync(indexStore, chunkHashes, 0)
if err != nil {
return storeStats, timeStats, errors.Wrap(err, fname)
}
defer retargettedVersionStoreIndex.Dispose()
getExistingContentTime := time.Since(getExistingContentStartTime)
timeStats = append(timeStats, longtailutils.TimeStat{"Get content index", getExistingContentTime})
writeVersionLocalStoreIndexStartTime := time.Now()
versionLocalStoreIndexBuffer, err := longtaillib.WriteStoreIndexToBuffer(retargettedVersionStoreIndex)
if err != nil {
err = errors.Wrapf(err, "Cant serialize store index for `%s`", sourceFilePath)
return storeStats, timeStats, errors.Wrap(err, fname)
}
defer versionLocalStoreIndexBuffer.Dispose()
err = longtailutils.WriteToURI(versionLocalStoreIndexPath, versionLocalStoreIndexBuffer.ToBuffer(), longtailutils.WithS3EndpointResolverURI(s3EndpointResolverURI))
if err != nil {
return storeStats, timeStats, errors.Wrap(err, fname)
}
writeVersionLocalStoreIndexTime := time.Since(writeVersionLocalStoreIndexStartTime)
timeStats = append(timeStats, longtailutils.TimeStat{"Write version store index", writeVersionLocalStoreIndexTime})
return storeStats, timeStats, nil
}
type CreateVersionStoreIndexCmd struct {
StorageURIOption
S3EndpointResolverURLOption
SourceUriOption
VersionLocalStoreIndexPathOption
}
func (r *CreateVersionStoreIndexCmd) Run(ctx *Context) error {
storeStats, timeStats, err := createVersionStoreIndex(
ctx.NumWorkerCount,
r.StorageURI,
r.S3EndpointResolverURL,
r.SourcePath,
r.VersionLocalStoreIndexPath)
ctx.StoreStats = append(ctx.StoreStats, storeStats...)
ctx.TimeStats = append(ctx.TimeStats, timeStats...)
return err
}