forked from rpcpool/yellowstone-faithful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readers.go
117 lines (100 loc) · 2.34 KB
/
readers.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
package main
import (
"errors"
"fmt"
"io"
"os"
"time"
"github.com/dustin/go-humanize"
"github.com/rpcpool/yellowstone-faithful/carreader"
"github.com/rpcpool/yellowstone-faithful/ipld/ipldbindcode"
"github.com/rpcpool/yellowstone-faithful/iplddecoders"
)
func isDirEmpty(dir string) (bool, error) {
file, err := os.Open(dir)
if err != nil {
return false, err
}
defer file.Close()
_, err = file.Readdir(1)
if errors.Is(err, io.EOF) {
return true, nil
}
return false, err
}
func getFileSize(path string) (uint64, error) {
st, err := os.Stat(path)
if err != nil {
return 0, err
}
return uint64(st.Size()), nil
}
func carCountItems(carPath string) (uint64, error) {
file, err := os.Open(carPath)
if err != nil {
return 0, err
}
defer file.Close()
rd, err := carreader.New(file)
if err != nil {
return 0, fmt.Errorf("failed to open car file: %w", err)
}
var count uint64
for {
_, _, err := rd.NextInfo()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return 0, err
}
count++
}
return count, nil
}
func carCountItemsByFirstByte(carPath string) (map[byte]uint64, *ipldbindcode.Epoch, error) {
file, err := os.Open(carPath)
if err != nil {
return nil, nil, err
}
defer file.Close()
rd, err := carreader.New(file)
if err != nil {
return nil, nil, fmt.Errorf("failed to open car file: %w", err)
}
numTotalItems := uint64(0)
counts := make(map[byte]uint64)
startedCountAt := time.Now()
var epochObject *ipldbindcode.Epoch
for {
_, _, block, err := rd.NextNodeBytes()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return nil, nil, err
}
// the first data byte is the block type (after the CBOR tag)
firstDataByte := block[1]
counts[firstDataByte]++
numTotalItems++
if numTotalItems%1_000_000 == 0 {
printToStderr(
fmt.Sprintf("\rCounted %s items", humanize.Comma(int64(numTotalItems))),
)
}
if iplddecoders.Kind(firstDataByte) == iplddecoders.KindEpoch {
epochObject, err = iplddecoders.DecodeEpoch(block)
if err != nil {
return nil, nil, fmt.Errorf("failed to decode Epoch node: %w", err)
}
}
}
printToStderr(
fmt.Sprintf("\rCounted %s items in %s\n", humanize.Comma(int64(numTotalItems)), time.Since(startedCountAt).Truncate(time.Second)),
)
return counts, epochObject, err
}
func printToStderr(msg string) {
fmt.Fprint(os.Stderr, msg)
}