/
golr.go
180 lines (155 loc) · 3.98 KB
/
golr.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 golr
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"sync"
)
type ConnectorBase struct {
url string
}
type SolrConnector struct {
ConnectorBase
}
type ESConnector struct {
ConnectorBase
}
type SolrAddOption struct {
Concurrency int
addressTemplate string
}
func (base *ConnectorBase) Connect (url string){
base.url = url
}
type Connector interface {
Connect (url string)
PostUpdate(payload []byte) ([]byte, error)
AddDocuments(container interface{}, opt *SolrAddOption) <-chan []byte
}
func NewSolrConnect(host string,port int) *SolrConnector {
c := &SolrConnector{}
c.Connect(fmt.Sprintf("http://%s:%d/solr/items1/update", host, port))
return c;
}
func NewESConnect ( host string, port int, collection string) *ESConnector {
e := &ESConnector{}
e.Connect(fmt.Sprintf("http://%s:%d/%s/_bulk", host, port, collection))
return e
}
// Assumes it'll get arrays of some data structure
func (sc *SolrConnector) AddDocuments(container interface{}, opt *SolrAddOption) <-chan []byte {
recvChan := make(chan []byte)
var err error
// todo: size constrain should be placed here
defer func() {
if err != nil {
log.Printf("Error occured, uploading document failed")
}
}()
go func(rC chan []byte) {
b, err := json.Marshal(container)
if err != nil {
log.Println("Failed at marshaling json structure, ", err)
}
respB, err := sc.PostUpdate(b)
if err != nil {
log.Println(err)
}
rC <- respB
}(recvChan)
return recvChan
}
func (sc *SolrConnector) PostUpdate(payload []byte) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest("POST", sc.url, bytes.NewReader(payload))
req.Header.Add("Content-type", "application/json")
//dump, _ := httputil.DumpRequestOut(req, true)
//fmt.Printf("%s", dump)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
//log.Printf("Recieved %d bytes.\n", len(body))
//log.Printf("%s\n", body)
return body, nil
}
// Assumes it'll get arrays of some data structure
func (sc *ESConnector) AddDocuments(container interface{}, opt *SolrAddOption) <-chan []byte {
recvChan := make(chan []byte)
var err error
// todo: size constrain should be placed here
defer func() {
if err != nil {
log.Printf("Error occured, uploading document failed")
}
}()
go func(rC chan []byte) {
b, err := json.Marshal(container)
if err != nil {
log.Println("Failed at marshaling json structure, ", err)
}
respB, err := sc.PostUpdate(b)
if err != nil {
log.Println(err)
}
rC <- respB
}(recvChan)
return recvChan
}
func (sc *ESConnector) PostUpdate(payload []byte) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest("POST", sc.url, bytes.NewReader(payload))
req.Header.Add("Content-type", "application/json")
//dump, _ := httputil.DumpRequestOut(req, true)
//fmt.Printf("%s", dump)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
//log.Printf("Recieved %d bytes.\n", len(body))
//log.Printf("%s\n", body)
return body, nil
}
//TODO: Fix argument type
type JSONProcessor interface {
Process(inputChan chan interface{}, opt *SolrAddOption, jsonReader io.Reader)
}
func (sc *SolrConnector) UploadJSONFile(path string, processor JSONProcessor, opt *SolrAddOption) {
jsonFile, err := os.Open(path)
if err != nil {
log.Println("Error opening file: ", err)
return
}
defer jsonFile.Close()
wg := new(sync.WaitGroup)
inputChan := make(chan interface{})
for i := 0; i < opt.Concurrency; i++ {
wg.Add(1)
go golrworker(sc, inputChan, opt, wg)
}
processor.Process(inputChan, opt, jsonFile)
close(inputChan)
wg.Wait()
}
func golrworker(c Connector, inputChan chan interface{}, opt *SolrAddOption, wg *sync.WaitGroup) {
defer wg.Done()
for pages := range inputChan {
msg := <-c.AddDocuments(pages, opt)
fmt.Println(string(msg[:]))
}
}