-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_grpc.go
More file actions
244 lines (205 loc) · 5.55 KB
/
api_grpc.go
File metadata and controls
244 lines (205 loc) · 5.55 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package blocktree
import (
"context"
v1 "github.com/emrgen/blocktree/apis/v1"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
var _ v1.BlocktreeServer = (*grpcApi)(nil)
// grpcApi is the gRPC server implementation for the blocktree service
// It implements the v1.BlocktreeServer interface
type grpcApi struct {
v1.BlocktreeServer
api *Api
}
func newGrpcApi(api *Api) *grpcApi {
return &grpcApi{
api: api,
}
}
// ApplyTransactions applies a list of transactions to the blocktree store
func (a *grpcApi) ApplyTransactions(ctx context.Context, req *v1.TransactionsRequest) (*v1.TransactionsResponse, error) {
txs := req.GetTransactions()
transactions := make([]*Transaction, 0, len(txs))
for _, tx := range txs {
//logrus.Info("Parsing transaction: ", tx)
transaction, err := transactionFromProtoV1(tx)
if err != nil {
return nil, err
}
//logrus.Info("Parsed transaction: ", transaction)
transactions = append(transactions, transaction)
}
res := &v1.TransactionsResponse{
Transactions: make([]*v1.ApplyTransactionResult, len(transactions)),
}
_, err := a.api.Apply(transactions...)
if err != nil {
return nil, err
}
//logrus.Info("Applied transactions: ", transactions)
return res, nil
}
// CreateSpace creates a new space in the blocktree store
func (a *grpcApi) CreateSpace(ctx context.Context, req *v1.CreateSpaceRequest) (*v1.CreateSpaceResponse, error) {
spaceID, _ := uuid.Parse(req.GetSpaceId())
err := a.api.CreateSpace(spaceID, req.GetName())
if err != nil {
return nil, err
}
return &v1.CreateSpaceResponse{
SpaceId: spaceID.String(),
}, nil
}
func (a *grpcApi) GetBlock(ctx context.Context, req *v1.GetBlockRequest) (*v1.GetBlockResponse, error) {
blockID, _ := uuid.Parse(req.GetBlockId())
var spaceID *uuid.UUID
var err error
if req.GetSpaceId() == "" {
// get space id from block id
spaceID, err = a.api.GetBlockSpaceID(blockID)
if err != nil {
return nil, err
}
} else {
sid, err := uuid.Parse(req.GetSpaceId())
if err != nil {
return nil, err
}
spaceID = &sid
}
block, err := a.api.GetBlock(*spaceID, blockID)
if err != nil {
return nil, err
}
logrus.Infof("block %v", block)
return &v1.GetBlockResponse{
Block: BlockToProtoV1(block),
}, nil
}
func (a *grpcApi) GetChildren(ctx context.Context, req *v1.GetBlockChildrenRequest) (*v1.GetBlockChildrenResponse, error) {
var err error
var spaceID *uuid.UUID
blockID, err := uuid.Parse(req.GetBlockId())
if err != nil {
return nil, err
}
if req.GetSpaceId() == "" {
// get space id from block id
spaceID, err = a.api.GetBlockSpaceID(blockID)
if err != nil {
return nil, err
}
} else {
sid, err := uuid.Parse(req.GetSpaceId())
if err != nil {
return nil, err
}
spaceID = &sid
}
blocks, err := a.api.GetChildrenBlocks(*spaceID, blockID)
if err != nil {
return nil, err
}
v1blocks := make([]*v1.Block, len(blocks))
for i, block := range blocks {
logrus.Infof("block %v index: %s", block.ID.String(), block.Index.String())
v1blocks[i] = BlockToProtoV1(block)
}
return &v1.GetBlockChildrenResponse{
Blocks: v1blocks,
}, nil
}
func (a *grpcApi) GetDescendants(ctx context.Context, req *v1.GetBlockDescendantsRequest) (*v1.GetBlockDescendantsResponse, error) {
logrus.Infof("Getting descendant blocks for block: %s", req.GetBlockId())
var err error
blockID, err := uuid.Parse(req.GetBlockId())
if err != nil {
return nil, err
}
var spaceID uuid.UUID
if req.SpaceId == nil {
// get space id from block id
sid, err := a.api.GetBlockSpaceID(blockID)
if err != nil {
return nil, err
}
spaceID = *sid
} else {
sid, err := uuid.Parse(req.GetSpaceId())
if err != nil {
return nil, err
}
spaceID = sid
}
blocks, err := a.api.GetDescendantBlocks(spaceID, blockID)
if err != nil {
return nil, err
}
view, err := blockViewFromBlocks(blockID, blocks)
if err != nil {
return nil, err
}
return &v1.GetBlockDescendantsResponse{
Block: BlockViewToProtoV1(view),
}, nil
}
func (a *grpcApi) GetPage(ctx context.Context, req *v1.GetBlockPageRequest) (*v1.GetBlockPageResponse, error) {
//TODO implement me
panic("implement me")
}
func (a *grpcApi) GetBackLinks(ctx context.Context, req *v1.GetBackLinksRequest) (*v1.GetBackLinksResponse, error) {
var err error
blockID, err := uuid.Parse(req.GetBlockId())
if err != nil {
return nil, err
}
spaceID, err := uuid.Parse(req.GetSpaceId())
if err != nil {
return nil, err
}
links, err := a.api.GetBackLinks(blockID, spaceID)
if err != nil {
return nil, err
}
blocks := make([]*v1.Block, 0)
for _, block := range links {
blocks = append(blocks, BlockToProtoV1(block))
}
return &v1.GetBackLinksResponse{
Blocks: blocks,
}, nil
}
func (a *grpcApi) GetUpdates(ctx context.Context, req *v1.GetUpdatesRequest) (*v1.GetUpdatesResponse, error) {
var err error
spaceID, err := uuid.Parse(req.GetSpaceId())
if err != nil {
return nil, err
}
txID, err := uuid.Parse(req.GetTransactionId())
if err != nil {
return nil, err
}
updates, err := a.api.GetUpdates(spaceID, txID)
if err != nil {
return nil, err
}
res := &v1.GetUpdatesResponse{
Updates: make(map[string]*v1.ChildIds),
Blocks: make([]*v1.Block, 0),
}
// convert updates to proto format
for _, block := range updates.Blocks {
res.Blocks = append(res.Blocks, BlockToProtoV1(block))
}
for parentID, childrenIDs := range updates.Children {
childIds := &v1.ChildIds{
BlockIds: make([]string, 0),
}
for _, id := range childrenIDs {
childIds.BlockIds = append(childIds.BlockIds, id.String())
}
res.Updates[parentID.String()] = childIds
}
return res, nil
}