|
| 1 | +package adapter |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/bootjp/elastickv/distribution" |
| 7 | + pb "github.com/bootjp/elastickv/proto" |
| 8 | +) |
| 9 | + |
| 10 | +// DistributionServer serves distribution related gRPC APIs. |
| 11 | +type DistributionServer struct { |
| 12 | + engine *distribution.Engine |
| 13 | + pb.UnimplementedDistributionServer |
| 14 | +} |
| 15 | + |
| 16 | +// NewDistributionServer creates a new server. |
| 17 | +func NewDistributionServer(e *distribution.Engine) *DistributionServer { |
| 18 | + return &DistributionServer{engine: e} |
| 19 | +} |
| 20 | + |
| 21 | +// UpdateRoute allows updating route information. |
| 22 | +func (s *DistributionServer) UpdateRoute(start, end []byte, group uint64) { |
| 23 | + s.engine.UpdateRoute(start, end, group) |
| 24 | +} |
| 25 | + |
| 26 | +// GetRoute returns route for a key. |
| 27 | +func (s *DistributionServer) GetRoute(ctx context.Context, req *pb.GetRouteRequest) (*pb.GetRouteResponse, error) { |
| 28 | + r, ok := s.engine.GetRoute(req.Key) |
| 29 | + if !ok { |
| 30 | + return &pb.GetRouteResponse{}, nil |
| 31 | + } |
| 32 | + return &pb.GetRouteResponse{ |
| 33 | + Start: r.Start, |
| 34 | + End: r.End, |
| 35 | + RaftGroupId: r.GroupID, |
| 36 | + }, nil |
| 37 | +} |
| 38 | + |
| 39 | +// GetTimestamp returns monotonically increasing timestamp. |
| 40 | +func (s *DistributionServer) GetTimestamp(ctx context.Context, req *pb.GetTimestampRequest) (*pb.GetTimestampResponse, error) { |
| 41 | + ts := s.engine.NextTimestamp() |
| 42 | + return &pb.GetTimestampResponse{Timestamp: ts}, nil |
| 43 | +} |
0 commit comments