This repository has been archived by the owner on Oct 13, 2021. It is now read-only.
/
grpcc.go
138 lines (121 loc) · 3.46 KB
/
grpcc.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
// Copyright 2016 ego authors
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package grpc
import (
"log"
// "os"
// "time"
"golang.org/x/net/context"
"google.golang.org/grpc"
"github.com/go-ego/riot/net/com"
pb "github.com/go-ego/riot/net/grpc/riot-pb"
zlog "github.com/go-vgo/zlog/zlog"
)
// Doc grpc response
type Doc struct {
DocId uint64
Content string
// Attri interface{}
Attri []byte
Tokens []*pb.TokenData
// Tokens []types.TokenData
Labels []string
Fields interface{}
ForceUpdate bool
}
// InitGrpcc init grpc client
func InitGrpcc(address string, doc Doc) int32 {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
zlog.Error("did not connect: ", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
// Contact the server and print out its response.
r, err := c.DocInx(context.Background(), &pb.DocReq{
Content: doc.Content,
Attri: doc.Attri,
Tokens: doc.Tokens,
})
if err != nil {
log.Fatalf("init docinx grpcc could not greet: %v", err)
zlog.Error("init docinx grpcc could not greet: ", err)
return r.Result
}
// log.Printf("Greeting: %s", r.Message)
return r.Result
}
// InitSearchRpc init search grpc
func InitSearchRpc(address string, sea com.SearchArgs) (*pb.SearchReply, error) {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
zlog.Error("did not connect: ", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
logicExp := &pb.Expr{
Must: sea.Logic.Expr.Must,
Should: sea.Logic.Expr.Should,
NotIn: sea.Logic.Expr.NotIn,
}
logic := &pb.Logic{
Must: sea.Logic.Must,
Should: sea.Logic.Should,
NotIn: sea.Logic.NotIn,
Expr: logicExp,
}
// Contact the server and print out its response.
r, err := c.Search(context.Background(), &pb.SearchReq{
Id: sea.Id,
Query: sea.Query,
OutputOffset: int32(sea.OutputOffset),
MaxOutputs: int32(sea.MaxOutputs),
Time: sea.Time,
DocIds: sea.DocIds,
Logic: logic,
})
if err != nil {
log.Fatalf("init searchRpc could not greet: %v", err)
zlog.Error("init searchRpc could not greet: ", err)
return nil, err
}
log.Printf("Greeting: %s", r)
return r, nil
}
// InitDelGrpcc init delete grpcc
func InitDelGrpcc(address string, docid string) int32 {
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
zlog.Error("did not connect: ", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
// Contact the server and print out its response.
r, err := c.Delete(context.Background(), &pb.DeleteReq{
DocId: docid,
})
if err != nil {
log.Fatalf("could not greet: %v", err)
zlog.Error("could not greet: ", err)
return r.Result
}
// log.Printf("Greeting: %d", r.Result)
return r.Result
}