-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk.go
109 lines (91 loc) · 2.81 KB
/
chunk.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
package sidetree
import (
"encoding/json"
"errors"
"fmt"
"github.com/13x-tech/ion-sdk-go/pkg/did"
"github.com/13x-tech/ion-sdk-go/pkg/operations"
)
var (
ErrInvalidDeltaCount = errors.New("invalid delta count")
)
type ChunkOption func(c *ChunkFile)
func WithMappingArrays(
createMappingArray []string,
recoverMappingArray []string,
updateMappingArray []string,
) ChunkOption {
return func(c *ChunkFile) {
c.createMappingArray = createMappingArray
c.recoverMappingArray = recoverMappingArray
c.updateMappingArray = updateMappingArray
}
}
func WithOperations(
createOps map[string]operations.CreateInterface,
recoverOps map[string]operations.RecoverInterface,
updateOps map[string]operations.UpdateInterface,
) ChunkOption {
return func(c *ChunkFile) {
c.createOps = createOps
c.recoverOps = recoverOps
c.updateOps = updateOps
}
}
func NewChunkFile(data []byte, opts ...ChunkOption) (*ChunkFile, error) {
var c ChunkFile
if err := json.Unmarshal(data, &c); err != nil {
return nil, fmt.Errorf("unable to unmarshal: %w", err)
}
for _, opt := range opts {
opt(&c)
}
return &c, nil
}
type ChunkFile struct {
Deltas []did.Delta `json:"deltas"`
createMappingArray []string
recoverMappingArray []string
updateMappingArray []string
createOps map[string]operations.CreateInterface
recoverOps map[string]operations.RecoverInterface
updateOps map[string]operations.UpdateInterface
}
func (c *ChunkFile) Process() error {
// c.processor.log.Infof("Processing chunk file %s", c.processor.ChunkFileURI)
// TODO Check Max Chunk File Size
// In order to process Chunk File Delta Entries in relation to the DIDs they
// are bound to, they must be mapped back to the Create, Recovery,
// and Update operation entries present in the Core Index File and
// Provisional Index File. To create this mapping, concatenate the
// Core Index File Create Entries, Core Index File Recovery Entries,
// Provisional Index File Update Entries into a single array, in that order,
// herein referred to as the Operation Delta Mapping Array
var mappingArray []string
if len(c.createMappingArray) > 0 {
mappingArray = append(mappingArray, c.createMappingArray...)
}
if len(c.recoverMappingArray) > 0 {
mappingArray = append(mappingArray, c.recoverMappingArray...)
}
if len(c.updateMappingArray) > 0 {
mappingArray = append(mappingArray, c.updateMappingArray...)
}
if len(mappingArray) != len(c.Deltas) {
return ErrInvalidDeltaCount
}
for i, delta := range c.Deltas {
id := mappingArray[i]
c.setDelta(id, delta)
}
return nil
}
func (c *ChunkFile) setDelta(id string, delta did.Delta) {
if createOp, ok := c.createOps[id]; ok {
createOp.SetDelta(delta)
} else if recoverOp, ok := c.recoverOps[id]; ok {
recoverOp.SetDelta(delta)
} else if updateOp, ok := c.updateOps[id]; ok {
updateOp.SetDelta(delta)
}
}