/
warehouse.go
180 lines (158 loc) · 4.68 KB
/
warehouse.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
package api
import (
"context"
"io"
"sync"
pb "github.com/airbloc/airbloc-go/proto/rpc/v1/server"
"github.com/airbloc/airbloc-go/shared/service"
"github.com/airbloc/airbloc-go/shared/service/api"
"github.com/airbloc/airbloc-go/shared/types"
"github.com/airbloc/airbloc-go/warehouse"
"github.com/airbloc/logger"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type WarehouseAPI struct {
warehouse *warehouse.Manager
log *logger.Logger
}
func NewWarehouseAPI(backend service.Backend) (_ api.API, err error) {
svc, ok := backend.GetService("warehouse").(*warehouse.Service)
if !ok {
return nil, errors.New("warehouse service is not registered")
}
return &WarehouseAPI{
warehouse: svc.GetManager(),
log: logger.New("warehouse"),
}, nil
}
func (api *WarehouseAPI) StoreBundle(stream pb.Warehouse_StoreBundleServer) error {
total := 0
successful := 0
timer := api.log.Timer()
var bundleStream *warehouse.BundleStream
var wg sync.WaitGroup
for {
request, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
return err
}
if bundleStream == nil {
collectionId, err := types.HexToID(request.GetCollectionId())
if err != nil {
return status.Errorf(codes.InvalidArgument, "Invalid collection ID: %s", request.GetCollectionId())
}
bundleStream, err = api.warehouse.CreateBundle(stream.Context(), collectionId)
if err != nil {
return err
}
}
userId, err := types.HexToID(request.GetUserId())
if err != nil {
return status.Errorf(codes.InvalidArgument, "Invalid user ANID: %s", request.GetUserId())
}
total += 1
wg.Add(1)
datum := &types.Data{
Payload: request.GetPayload(),
UserId: userId,
CollectedAt: types.ParseTimestamp(request.GetCollectedAt()),
}
go func() {
if err := bundleStream.Add(datum); err != nil {
api.log.Error("failed to add data: {}", err.Error())
} else {
successful += 1
}
wg.Done()
}()
}
wg.Wait()
bundle, err := api.warehouse.Store(stream.Context(), bundleStream)
if err != nil {
return errors.Wrap(err, "failed to store a bundle")
}
timer.End("Successfully Ingested {} of {} data.", successful, total)
return stream.SendAndClose(&pb.StoreResult{
BundleId: bundle.Id,
Uri: bundle.Uri,
DataCount: uint64(bundle.DataCount),
GasUsed: 0,
})
}
func (api *WarehouseAPI) StoreEncryptedBundle(stream pb.Warehouse_StoreEncryptedBundleServer) error {
var bundleStream *warehouse.BundleStream
for {
request, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
return err
}
if bundleStream == nil {
collectionId, err := types.HexToID(request.GetCollectionId())
if err != nil {
return status.Errorf(codes.InvalidArgument, "Invalid collection ID: %s", request.GetCollectionId())
}
bundleStream, err = api.warehouse.CreateBundle(stream.Context(), collectionId)
if err != nil {
return err
}
}
userId, err := types.HexToID(request.GetUserId())
if err != nil {
return errors.Wrapf(err, "failed to parse ANID %s", request.GetUserId())
}
datum := &types.EncryptedData{
Payload: request.GetEncryptedPayload(),
UserId: userId,
Capsule: request.GetCapsule(),
CollectedAt: types.ParseTimestamp(request.GetCollectedAt()),
}
bundleStream.AddEncrypted(datum)
}
bundle, err := api.warehouse.Store(stream.Context(), bundleStream)
if err != nil {
return errors.Wrap(err, "failed to store a bundle")
}
return stream.SendAndClose(&pb.StoreResult{
BundleId: bundle.Id,
Uri: bundle.Uri,
DataCount: uint64(bundle.DataCount),
GasUsed: 0,
})
}
func (api *WarehouseAPI) DeleteBundle(ctx context.Context, request *pb.DeleteBundleRequest) (*pb.DeleteBundleResult, error) {
return nil, status.Error(codes.Unimplemented, "unimplemented")
}
func (api *WarehouseAPI) ListBundle(ctx context.Context, req *pb.ListBundleRequest) (*pb.ListBundleResult, error) {
providerId, err := types.HexToID(req.GetProviderId())
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid Provider ID: %s", req.GetProviderId())
}
bundles, err := api.warehouse.List(ctx, providerId)
if err != nil {
return nil, err
}
bundleResults := make([]*pb.ListBundleResult_Bundle, len(bundles))
for i, bundle := range bundles {
bundleResults[i] = &pb.ListBundleResult_Bundle{
CollectionId: bundle.Collection.Hex(),
Index: 1010,
CreatedAt: uint64(bundle.IngestedAt.Unix()),
DataCount: uint64(bundle.DataCount),
Uri: bundle.Uri,
}
}
return &pb.ListBundleResult{
Bundles: bundleResults,
}, nil
}
func (api *WarehouseAPI) AttachToAPI(service *api.Service) {
pb.RegisterWarehouseServer(service.GrpcServer, api)
}