2018-05-15 01:31:26 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
2018-05-30 03:47:40 +01:00
|
|
|
"fmt"
|
2018-05-15 01:31:26 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
2018-05-30 03:47:40 +01:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2018-05-15 01:31:26 +01:00
|
|
|
|
2018-07-06 20:43:53 +01:00
|
|
|
proto "storj.io/storj/protos/pointerdb"
|
2018-05-15 01:31:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-06-29 21:06:25 +01:00
|
|
|
port string
|
|
|
|
apiKey = []byte("abc123")
|
2018-05-15 01:31:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func initializeFlags() {
|
|
|
|
flag.StringVar(&port, "port", ":8080", "port")
|
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
initializeFlags()
|
|
|
|
|
|
|
|
logger, _ := zap.NewDevelopment()
|
|
|
|
defer logger.Sync()
|
|
|
|
|
|
|
|
conn, err := grpc.Dial(port, grpc.WithInsecure())
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Failed to dial: ", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2018-07-06 20:43:53 +01:00
|
|
|
client := proto.NewPointerDBClient(conn)
|
2018-05-15 01:31:26 +01:00
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
logger.Debug(fmt.Sprintf("client dialed port %s", port))
|
|
|
|
|
2018-05-15 01:31:26 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
// Example pointer paths to put
|
|
|
|
pr1 := proto.PutRequest{
|
2018-06-29 21:06:25 +01:00
|
|
|
Path: []byte("test/path/1"),
|
2018-05-30 03:47:40 +01:00
|
|
|
Pointer: &proto.Pointer{
|
2018-06-26 16:18:05 +01:00
|
|
|
Type: proto.Pointer_INLINE,
|
2018-06-29 21:06:25 +01:00
|
|
|
InlineSegment: []byte("inline1"),
|
2018-05-30 03:47:40 +01:00
|
|
|
},
|
2018-06-29 21:06:25 +01:00
|
|
|
APIKey: apiKey,
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
2018-05-30 03:47:40 +01:00
|
|
|
pr2 := proto.PutRequest{
|
2018-06-29 21:06:25 +01:00
|
|
|
Path: []byte("test/path/2"),
|
2018-05-30 03:47:40 +01:00
|
|
|
Pointer: &proto.Pointer{
|
2018-06-26 16:18:05 +01:00
|
|
|
Type: proto.Pointer_INLINE,
|
2018-06-29 21:06:25 +01:00
|
|
|
InlineSegment: []byte("inline2"),
|
2018-05-30 03:47:40 +01:00
|
|
|
},
|
2018-06-29 21:06:25 +01:00
|
|
|
APIKey: apiKey,
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
2018-05-30 03:47:40 +01:00
|
|
|
pr3 := proto.PutRequest{
|
2018-06-29 21:06:25 +01:00
|
|
|
Path: []byte("test/path/3"),
|
2018-05-30 03:47:40 +01:00
|
|
|
Pointer: &proto.Pointer{
|
2018-06-26 16:18:05 +01:00
|
|
|
Type: proto.Pointer_INLINE,
|
2018-06-29 21:06:25 +01:00
|
|
|
InlineSegment: []byte("inline3"),
|
2018-05-30 03:47:40 +01:00
|
|
|
},
|
2018-06-29 21:06:25 +01:00
|
|
|
APIKey: apiKey,
|
|
|
|
}
|
|
|
|
// rps is an example slice of RemotePieces, which is passed into
|
|
|
|
// this example Pointer of type REMOTE.
|
|
|
|
var rps []*proto.RemotePiece
|
|
|
|
rps = append(rps, &proto.RemotePiece{
|
|
|
|
PieceNum: int64(1),
|
|
|
|
NodeId: "testId",
|
|
|
|
})
|
|
|
|
pr4 := proto.PutRequest{
|
|
|
|
Path: []byte("test/path/4"),
|
|
|
|
Pointer: &proto.Pointer{
|
|
|
|
Type: proto.Pointer_REMOTE,
|
|
|
|
Remote: &proto.RemoteSegment{
|
|
|
|
Redundancy: &proto.RedundancyScheme{
|
|
|
|
Type: proto.RedundancyScheme_RS,
|
|
|
|
MinReq: int64(1),
|
|
|
|
Total: int64(3),
|
|
|
|
RepairThreshold: int64(2),
|
|
|
|
SuccessThreshold: int64(3),
|
|
|
|
},
|
|
|
|
PieceId: "testId",
|
|
|
|
RemotePieces: rps,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
APIKey: apiKey,
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Example Puts
|
2018-05-30 03:47:40 +01:00
|
|
|
_, err = client.Put(ctx, &pr1)
|
|
|
|
if err != nil || status.Code(err) == codes.Internal {
|
2018-05-15 01:31:26 +01:00
|
|
|
logger.Error("failed to put", zap.Error(err))
|
|
|
|
}
|
2018-05-30 03:47:40 +01:00
|
|
|
_, err = client.Put(ctx, &pr2)
|
|
|
|
if err != nil || status.Code(err) == codes.Internal {
|
2018-05-15 01:31:26 +01:00
|
|
|
logger.Error("failed to put", zap.Error(err))
|
|
|
|
}
|
2018-05-30 03:47:40 +01:00
|
|
|
_, err = client.Put(ctx, &pr3)
|
|
|
|
if err != nil || status.Code(err) == codes.Internal {
|
2018-05-15 01:31:26 +01:00
|
|
|
logger.Error("failed to put", zap.Error(err))
|
|
|
|
}
|
2018-06-29 21:06:25 +01:00
|
|
|
_, err = client.Put(ctx, &pr4)
|
|
|
|
if err != nil || status.Code(err) == codes.Internal {
|
|
|
|
logger.Error("failed to put", zap.Error(err))
|
|
|
|
}
|
2018-05-15 01:31:26 +01:00
|
|
|
|
|
|
|
// Example Get
|
|
|
|
getReq := proto.GetRequest{
|
2018-06-29 21:06:25 +01:00
|
|
|
Path: []byte("test/path/1"),
|
|
|
|
APIKey: apiKey,
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
getRes, err := client.Get(ctx, &getReq)
|
2018-05-30 03:47:40 +01:00
|
|
|
if err != nil || status.Code(err) == codes.Internal {
|
2018-05-15 01:31:26 +01:00
|
|
|
logger.Error("failed to get", zap.Error(err))
|
2018-06-04 17:45:07 +01:00
|
|
|
} else {
|
|
|
|
pointer := string(getRes.Pointer)
|
|
|
|
logger.Debug("get response: " + pointer)
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Example List
|
|
|
|
listReq := proto.ListRequest{
|
2018-06-29 21:06:25 +01:00
|
|
|
StartingPathKey: []byte("test/path/2"),
|
2018-05-30 03:47:40 +01:00
|
|
|
Limit: 5,
|
2018-06-29 21:06:25 +01:00
|
|
|
APIKey: apiKey,
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
listRes, err := client.List(ctx, &listReq)
|
2018-05-30 03:47:40 +01:00
|
|
|
if err != nil || status.Code(err) == codes.Internal {
|
2018-05-15 01:31:26 +01:00
|
|
|
logger.Error("failed to list file paths")
|
2018-06-04 17:45:07 +01:00
|
|
|
} else {
|
|
|
|
var stringList []string
|
|
|
|
for _, pathByte := range listRes.Paths {
|
|
|
|
stringList = append(stringList, string(pathByte))
|
|
|
|
}
|
2018-06-29 21:06:25 +01:00
|
|
|
logger.Debug("listed paths: " + strings.Join(stringList, ", ") + "; truncated: " + fmt.Sprintf("%t", listRes.Truncated))
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Example Delete
|
|
|
|
delReq := proto.DeleteRequest{
|
2018-06-29 21:06:25 +01:00
|
|
|
Path: []byte("test/path/1"),
|
|
|
|
APIKey: apiKey,
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
2018-05-30 03:47:40 +01:00
|
|
|
_, err = client.Delete(ctx, &delReq)
|
|
|
|
if err != nil || status.Code(err) == codes.Internal {
|
|
|
|
logger.Error("failed to delete: " + string(delReq.Path))
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
}
|