-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
380 lines (326 loc) · 8.14 KB
/
db.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package hl
import (
"bufio"
"bytes"
"github.com/ravlio/highloadcup2018/account"
"github.com/ravlio/highloadcup2018/errors"
"github.com/ravlio/highloadcup2018/idx"
"github.com/ravlio/highloadcup2018/indexer"
"github.com/ravlio/highloadcup2018/metrics"
"github.com/ravlio/highloadcup2018/querier"
"github.com/ravlio/highloadcup2018/requests"
"github.com/ravlio/highloadcup2018/requests/likes"
"github.com/ravlio/highloadcup2018/skiplist"
"github.com/ravlio/highloadcup2018/skiplist/int32t"
"github.com/ravlio/highloadcup2018/skiplist/stringt"
// "github.com/ravlio/highloadcup2018/gojay"
"github.com/valyala/fastjson"
// "github.com/ravlio/highloadcup2018/mmon"
"github.com/ravlio/highloadcup2018/trie"
"github.com/rs/zerolog/log"
// "runtime/debug"
// "archive/zip"
"fmt"
"io/ioutil"
"os"
"runtime"
"sync"
// "fmt"
"time"
)
type Group string
type Order int8
const (
StartBirthday = -631152000
GroupBySex Group = "sex"
GroupByStatus Group = "status"
GroupByInterests Group = "interests"
GroupByCountry Group = "country"
GroupByCity Group = "city"
OrderAsc = 1
OrderDesc = -1
)
type opType uint8
const (
AddAccount opType = iota + 1
UpdateAccount
AddLikes
)
type opMsg struct {
opType opType
msg *requests.AccountRequest
cur *account.Account
id uint32
}
type OptionsIndexer struct {
Enabled bool
BufferSize int
WorkerCount int
}
type Options struct {
Indexer OptionsIndexer
Debug bool
}
type dbBitmap struct {
sex *idx.HashBitmap
emailDomain *idx.HashBitmap
status *idx.HashBitmap
statusNeq *idx.HashBitmap
fname *idx.HashBitmap
fnameY *idx.Bitmap
fnameN *idx.Bitmap
sname *idx.HashBitmap
snameY *idx.Bitmap
snameN *idx.Bitmap
phoneY *idx.Bitmap
phoneN *idx.Bitmap
phoneCode *idx.HashBitmap
country *idx.HashBitmap
countryY *idx.Bitmap
countryN *idx.Bitmap
city *idx.HashBitmap
cityY *idx.Bitmap
cityN *idx.Bitmap
birthYear *idx.HashBitmap
joinedYear *idx.HashBitmap
interest *idx.HashBitmap
premium *idx.Bitmap
premiumY *idx.Bitmap
premiumN *idx.Bitmap
like *idx.HashBitmap // по вертикали — кого лайкали, по горизонтали — кто лайкал
}
type dbTrie struct {
sname *trie.Trie
}
type dbHash struct {
email *idx.Hash
phone *idx.Hash
}
type dbRbTree struct {
premium *idx.RBTree
}
type dbSkiplist struct {
emailLt *skiplist.Skiplist
emailGt *skiplist.Skiplist
birthLt *skiplist.Skiplist
birthGt *skiplist.Skiplist
}
type DB struct {
curTime time.Time
accChan chan opMsg
likesChan chan likes.Likes
likes map[uint32][]uint32
bitmap dbBitmap
hash dbHash
rbtree dbRbTree
trie dbTrie
skiplist dbSkiplist
querier *querier.Querier
indexer *indexer.Indexer
accounts *account.Store
accMx sync.RWMutex
lmx sync.RWMutex
opts *Options
}
func NewDB(opts *Options) *DB {
r := &DB{
opts: opts,
accChan: make(chan opMsg, opts.Indexer.BufferSize),
likesChan: make(chan likes.Likes, opts.Indexer.BufferSize),
likes: make(map[uint32][]uint32),
bitmap: dbBitmap{
sex: idx.NewSexHashBitmap(),
emailDomain: idx.NewUint32HashBitmap(),
status: idx.NewStatusHashBitmap(),
statusNeq: idx.NewStatusHashBitmap(),
fname: idx.NewUint32HashBitmap(),
fnameY: idx.NewBitmap(),
fnameN: idx.NewBitmap(),
sname: idx.NewUint32HashBitmap(),
snameY: idx.NewBitmap(),
snameN: idx.NewBitmap(),
phoneY: idx.NewBitmap(),
phoneN: idx.NewBitmap(),
phoneCode: idx.NewUint32HashBitmap(),
country: idx.NewUint32HashBitmap(),
countryY: idx.NewBitmap(),
countryN: idx.NewBitmap(),
city: idx.NewUint32HashBitmap(),
cityY: idx.NewBitmap(),
cityN: idx.NewBitmap(),
birthYear: idx.NewUint32HashBitmap(),
joinedYear: idx.NewUint32HashBitmap(),
interest: idx.NewUint32HashBitmap(),
premium: idx.NewBitmap(),
premiumY: idx.NewBitmap(),
premiumN: idx.NewBitmap(),
like: idx.NewUint32HashBitmap(),
},
hash: dbHash{
email: idx.NewUint32Hash(),
phone: idx.NewInt64Hash(),
},
trie: dbTrie{
sname: trie.NewTrie(),
},
skiplist: dbSkiplist{
emailLt: skiplist.NewStringSkiplist(stringt.BuiltinLessThan),
emailGt: skiplist.NewStringSkiplist(stringt.BuiltinGreaterThan),
birthLt: skiplist.NewInt32Skiplist(int32t.BuiltinLessThan),
birthGt: skiplist.NewInt32Skiplist(int32t.BuiltinGreaterThan),
},
indexer: indexer.NewIndexer(),
accounts: account.NewAccounts(),
}
if opts.Debug {
errors.Debug = true
} else {
errors.Debug = false
}
return r
}
func (d *DB) Start() error {
mtr := metrics.NewDurarion("index")
if d.opts.Indexer.Enabled {
wg := &sync.WaitGroup{}
for i := 0; i < d.opts.Indexer.WorkerCount; i++ {
wg.Add(1)
go d.runIndexWorker(wg, mtr)
}
wg.Wait()
}
return nil
}
func (d *DB) Stop() error {
return nil
}
func (d *DB) LoadFromJSON(path string) error {
op, err := os.OpenFile(path+"options.txt", os.O_RDONLY, 0666)
if err != nil {
return errors.Wrap(err, "cant open options.txt file")
}
var t int64
var p int
_, err = fmt.Fscanln(op, &t)
if err != nil {
return errors.Wrap(err, "cant scan timestamp")
}
_, err = fmt.Fscanln(op, &p)
if err != nil {
return errors.Wrap(err, "cant scan isRating")
}
op.Close()
d.curTime = time.Unix(t, 0)
var m runtime.MemStats
runtime.ReadMemStats(&m)
log.Info().Str("path", path).Msg("Starting importing data")
start := time.Now()
files, err := ioutil.ReadDir(path + "data/")
if err != nil {
return errors.Wrap(err, "cant read data folder")
}
// dr, err := zip.OpenReader(path+"data.zip")
if err != nil {
return errors.Wrap(err, "cant read data.zip")
}
var accs int
// go mmon.NewMonitor(1)
// var trie map[string]*roaring.Bitmap
// for fn, f := range dr.File {
for fn, f := range files {
/*if fn>20 {
break
}*/
log.Info().Str("file", f.Name()).Int("#", fn).Msg("Reading file ...")
// fd,err:=ioutil.ReadFile(path+"data/"+f.Name())
fd, err := os.Open(path + "data/" + f.Name())
// fd,err:=f.Open()
if err != nil {
return errors.Wrap(err, "cant open file")
}
// bb:=
// rdr := bufio.NewReaderSize(bytes.NewReader(fd),100000)
rdr := bufio.NewReader(fd)
_, err = rdr.Discard(14) // прыгаем на начало массива
if err != nil {
return err
}
var buf bytes.Buffer
var c int
var el int
// var bm = make(map[uint32][]uint32)
var fp fastjson.Parser
for {
b, err := rdr.ReadByte()
if err != nil {
return err
}
buf.WriteByte(b)
if b == '{' {
c++
}
if b == '}' {
c--
if c != 0 {
continue
}
// req, err := jsonp.Parse(buf.String())
// if err != nil {
// return err
// }
// req:=&requests.AccountRequest{}
req := requests.AccountRequestPoolGet()
fv, err := fp.ParseBytes(buf.Bytes())
if err != nil {
req.ReleaseToPool()
return err
}
err = req.FromFastJson(fv)
// err := gojay.UnmarshalJSONObject(buf.Bytes(), req)
if err != nil {
req.ReleaseToPool()
return err
}
/*if len(req.Likes)>0 {
for _,l:=range req.Likes {
m,ok:=bm[l.ID]
}
}*/
if d.opts.Indexer.Enabled {
err = d.CreateAccount(req)
if err != nil {
req.ReleaseToPool()
return err
}
}
// req.ReleaseToPool()
el++
accs++
buf.Reset()
n, err := rdr.ReadByte()
if err != nil {
return err
}
// скипаем ', '
if n == ',' {
_, err := rdr.Discard(1)
if err != nil {
return err
}
} else if n == ']' { // конец
break
}
}
}
fd.Close()
}
// runtime.GC()
// debug.FreeOSMemory()
d.accounts.SortSlice() // на всякий случай сортируем слайс, так как порядок аккаунтов никто не гарантирует
var m2 runtime.MemStats
runtime.ReadMemStats(&m2)
log.Info().TimeDiff("spent", time.Now(), start).Int("Accounts count", accs).Uint64("mb_alloc",
(m2.Sys-m.Sys)/1024/1024).Msg("Importing data finished")
d.PrintIndexStats()
return nil
}