-
Notifications
You must be signed in to change notification settings - Fork 0
/
buzhashChunker.go
149 lines (122 loc) · 3.41 KB
/
buzhashChunker.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
package buzhashChunker
import (
"bytes"
"crypto/sha512"
"fmt"
"io"
"runtime"
"sync"
"github.com/i5heu/ouroboros-db/pkg/types"
chunker "github.com/ipfs/boxo/chunker"
)
func ChunkBytes(data []byte) (types.ChunkCollection, types.ChunkMetaCollection, error) {
reader := bytes.NewReader(data)
return ChunkReader(reader)
}
func ChunkReader(reader io.Reader) (types.ChunkCollection, types.ChunkMetaCollection, error) {
bz := chunker.NewBuzhash(reader)
// get thread count
numberOfCPUs := runtime.NumCPU()
numberOfWorkers := (numberOfCPUs * 5) - 1
hashChan := make(chan chunkInformation, numberOfWorkers+1)
workerLimit := make(chan struct{}, numberOfWorkers)
var wg sync.WaitGroup
var collectorWg sync.WaitGroup
// spawn collector
resultChan := make(chan chunkResult, 1)
collectorWg.Add(1)
go collectChunkData(&collectorWg, hashChan, resultChan)
// Read and process chunks
for chunkIndex := 0; ; chunkIndex++ {
chunk, err := bz.NextBytes()
if err == io.EOF {
wg.Wait()
close(hashChan)
break
}
if err != nil {
return nil, nil, fmt.Errorf("error reading chunk: %w", err)
}
wg.Add(1)
workerLimit <- struct{}{}
go calculateSha512(&wg, hashChan, chunk, chunkIndex, workerLimit)
}
collectorWg.Wait()
close(resultChan)
finalResult, ok := <-resultChan
if !ok {
return nil, nil, fmt.Errorf("failed to read from result channel")
}
return finalResult.chunks, finalResult.meta, nil
}
func ChunkBytesSynchronously(data []byte) (types.ChunkCollection, types.ChunkMetaCollection, error) {
reader := bytes.NewReader(data)
return ChunkReaderSynchronously(reader)
}
func ChunkReaderSynchronously(reader io.Reader) (types.ChunkCollection, types.ChunkMetaCollection, error) {
bz := chunker.NewBuzhash(reader)
var chunks types.ChunkCollection
var meta types.ChunkMetaCollection
for chunkIndex := 0; ; chunkIndex++ {
chunk, err := bz.NextBytes()
if err == io.EOF {
break
}
if err != nil {
return nil, nil, fmt.Errorf("error reading chunk: %w", err)
}
hash := sha512.Sum512(chunk)
chunkData := types.Chunk{
ChunkMeta: types.ChunkMeta{
Hash: hash,
DataLength: uint32(len(chunk)),
},
Data: chunk,
}
chunks = append(chunks, chunkData)
meta = append(meta, chunkData.ChunkMeta)
}
return chunks, meta, nil
}
type chunkInformation struct {
chunkNumber int
hash types.Hash
data []byte
}
type chunkResult struct {
chunks types.ChunkCollection
meta types.ChunkMetaCollection
}
func collectChunkData(collectorWg *sync.WaitGroup, chunkChan <-chan chunkInformation, resultChan chan<- chunkResult) {
defer collectorWg.Done()
chunkMap := map[int]types.Chunk{}
for hashInfo := range chunkChan {
chunkMap[hashInfo.chunkNumber] = types.Chunk{
ChunkMeta: types.ChunkMeta{
Hash: hashInfo.hash,
DataLength: uint32(len(hashInfo.data)),
},
Data: hashInfo.data,
}
}
chunks := make(types.ChunkCollection, len(chunkMap))
meta := make(types.ChunkMetaCollection, len(chunkMap))
for i := 0; i < len(chunkMap); i++ {
chunks[i] = chunkMap[i]
meta[i] = chunkMap[i].ChunkMeta
}
resultChan <- chunkResult{
chunks: chunks,
meta: meta,
}
}
func calculateSha512(wg *sync.WaitGroup, hashChan chan<- chunkInformation, data []byte, chunkNumber int, workerLimit chan struct{}) {
defer wg.Done()
hash := sha512.Sum512(data)
hashChan <- chunkInformation{
chunkNumber: chunkNumber,
hash: hash,
data: data,
}
<-workerLimit
}