forked from varnamproject/varnamd
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sync.go
310 lines (267 loc) · 7.82 KB
/
sync.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/varnamproject/libvarnam-golang"
)
type syncDispatcher struct {
quit chan struct{}
force chan bool // Send a TRUE message so that execution begins immediatly
ticker *time.Ticker
}
func newSyncDispatcher(intervalInSeconds time.Duration) *syncDispatcher {
return &syncDispatcher{ticker: time.NewTicker(intervalInSeconds), force: make(chan bool), quit: make(chan struct{})}
}
func (s *syncDispatcher) start() {
err := createSyncMetadataDir()
if err != nil {
fmt.Printf("Failed to create sync metadata directory. Sync will be disabled.\nActual error: %s\n", err.Error())
return
}
for s := range varnamdConfig.schemesToDownload {
// download cache directory for each of the languages
err = createLearnQueueDir(s)
if err != nil {
fmt.Printf("Failed to create learn queue directory for '%s'. Sync will be disabled.\nActual error: %s\n", err.Error())
return
}
}
go func() {
for {
select {
case <-s.ticker.C:
performSync()
case <-s.force:
performSync()
case <-s.quit:
s.ticker.Stop()
return
}
}
}()
}
func (s *syncDispatcher) stop() {
close(s.quit)
}
func (s *syncDispatcher) runNow() {
s.force <- true
}
func performSync() {
log.Println("---SYNC BEGIN---")
log.Printf("Config: %v\n", varnamdConfig)
syncWordsFromUpstream()
log.Println("---SYNC DONE---")
}
func syncWordsFromUpstream() {
for langCode := range varnamdConfig.schemesToDownload {
log.Printf("Sync: %s\n", langCode)
syncWordsFromUpstreamFor(langCode)
}
}
func syncWordsFromUpstreamFor(langCode string) {
corpusDetails, err := getCorpusDetails(langCode)
if err != nil {
log.Printf("Error getting corpus details for '%s'. %s\n", langCode, err.Error())
return
}
localFilesToLearn := make(chan string, 100)
downloadedFilesToLearn := make(chan string, 100)
done := make(chan bool)
// adding files which are remaining to learn in the local learn queue
remainingFilesFromLastDownload := getFilesFromLearnQueue(langCode)
go addFilesFromLocalLearnQueue(langCode, remainingFilesFromLastDownload, localFilesToLearn)
go downloadAllWords(langCode, corpusDetails.WordsCount, downloadedFilesToLearn)
go func() {
learnAll(langCode, localFilesToLearn)
learnAll(langCode, downloadedFilesToLearn)
done <- true
}()
<-done
}
func addFilesFromLocalLearnQueue(langCode string, files []string, filesToLearn chan string) {
if files != nil {
log.Printf("Adding %d files to learn from local learn queue\n", len(files))
for _, f := range files {
filesToLearn <- f
}
} else {
log.Printf("Local learn queue for '%s' is empty", langCode)
}
close(filesToLearn)
}
func downloadAllWords(langCode string, corpusSize int, output chan string) {
for {
offset := getDownloadOffset(langCode)
log.Printf("Offset: %d\n", offset)
if offset >= corpusSize {
break
}
filePath, err := downloadWordsAndUpdateOffset(langCode, offset)
if err != nil {
break
}
output <- filePath
}
log.Println("Local copy is upto date. No need to download from upstream")
close(output)
}
func learnAll(langCode string, filesToLearn chan string) {
for fileToLearn := range filesToLearn {
learnFromFile(langCode, fileToLearn)
}
}
func learnFromFile(langCode, fileToLearn string) {
log.Printf("Learning from %s\n", fileToLearn)
start := time.Now()
getOrCreateHandler(langCode, func(handle *libvarnam.Varnam) (data interface{}, err error) {
learnStatus, err := handle.LearnFromFile(fileToLearn)
end := time.Now()
if err != nil {
log.Printf("Error learning from '%s'\n", err.Error())
} else {
log.Printf("Learned from '%s'. TotalWords: %d, Failed: %d. Took %s\n", fileToLearn, learnStatus.TotalWords, learnStatus.Failed, end.Sub(start))
}
err = os.Remove(fileToLearn)
if err != nil {
log.Printf("Error deleting '%s'. %s\n", fileToLearn, err.Error())
}
return
})
}
func downloadWordsAndUpdateOffset(langCode string, offset int) (string, error) {
count, filePath, err := downloadWords(langCode, offset)
if err != nil {
log.Printf("Error downloading words for '%s'. %s\n", langCode, err.Error())
return "", err
}
err = setDownloadOffset(langCode, offset+count)
if err != nil {
log.Printf("Error setting download offset for '%s'. %s\n", langCode, err.Error())
return "", err
}
return filePath, nil
}
func getCorpusDetails(langCode string) (*libvarnam.CorpusDetails, error) {
url := fmt.Sprintf("%s/meta/%s", varnamdConfig.upstream, langCode)
log.Printf("Fetching corpus details for '%s'\n", langCode)
var m metaResponse
err := getJSONResponse(url, &m)
if err != nil {
return nil, err
}
log.Printf("Corpus size: %d\n", m.Result.WordsCount)
return m.Result, nil
}
// Downloads words from upstream starting from the specified offset and stores it locally in the learn queue
// Returns the number of words downloaded, local file path and error if any
func downloadWords(langCode string, offset int) (totalWordsDownloaded int, downloadedFilePath string, err error) {
url := fmt.Sprintf("%s/download/%s/%d", varnamdConfig.upstream, langCode, offset)
var response downloadResponse
err = getJSONResponse(url, &response)
if err != nil {
return 0, "", err
}
downloadedFilePath, err = transformAndPersistWords(langCode, offset, &response)
if err != nil {
log.Printf("Download was successful, but failed to persist to local learn queue. %s\n", err.Error())
return 0, "", err
}
return response.Count, downloadedFilePath, nil
}
func transformAndPersistWords(langCode string, offset int, dresp *downloadResponse) (string, error) {
learnQueueDir := getLearnQueueDir(langCode)
targetFile, err := os.Create(path.Join(learnQueueDir, fmt.Sprintf("%s.%d", langCode, offset)))
if err != nil {
return "", err
}
defer targetFile.Close()
for _, word := range dresp.Words {
_, err = targetFile.WriteString(fmt.Sprintf("%s %d\n", word.Word, word.Confidence))
if err != nil {
return "", err
}
}
return targetFile.Name(), nil
}
func getFilesFromLearnQueue(langCode string) []string {
var files []string
learnQueueDir := getLearnQueueDir(langCode)
queueContents, err := ioutil.ReadDir(learnQueueDir)
if err != nil {
return nil
}
for _, c := range queueContents {
if !c.IsDir() {
files = append(files, path.Join(learnQueueDir, c.Name()))
}
}
return files
}
func getJSONResponse(url string, output interface{}) error {
log.Printf("GET: '%s'\n", url)
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
jsonDecoder := json.NewDecoder(resp.Body)
err = jsonDecoder.Decode(output)
if err != nil {
return err
}
return nil
}
func getDownloadOffset(langCode string) int {
filePath := getDownloadOffsetMetadataFile(langCode)
content, err := ioutil.ReadFile(filePath)
if err != nil {
return 0
}
offset, err := strconv.Atoi(strings.TrimSpace(string(content)))
if err != nil {
return 0
}
return offset
}
func setDownloadOffset(langCode string, offset int) error {
filePath := getDownloadOffsetMetadataFile(langCode)
return ioutil.WriteFile(filePath, []byte(fmt.Sprintf("%d", offset)), 0666)
}
func getDownloadOffsetMetadataFile(langCode string) string {
syncDir := getSyncMetadataDir()
return path.Join(syncDir, fmt.Sprintf("%s.download.offset", langCode))
}
func createLearnQueueDir(langCode string) error {
queueDir := getLearnQueueDir(langCode)
err := os.MkdirAll(queueDir, 0777)
if err != nil {
return err
}
return nil
}
func getLearnQueueDir(langCode string) string {
syncDir := getSyncMetadataDir()
queueDir := path.Join(syncDir, fmt.Sprintf("%s.learn.queue", langCode))
return queueDir
}
func createSyncMetadataDir() error {
syncDir := getSyncMetadataDir()
err := os.MkdirAll(syncDir, 0777)
if err != nil {
return err
}
return nil
}
func getSyncMetadataDir() string {
configDir := getConfigDir()
syncDir := path.Join(configDir, "sync")
return syncDir
}