-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
280 lines (236 loc) · 7.19 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
package main
import (
"fmt"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"log"
"math"
"time"
)
type DBCreds struct {
User string
Password string
DBName string
Host string
Port string
}
func getConnection(creds DBCreds) (*gorm.DB, error) {
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s", creds.Host, creds.User, creds.Password, creds.DBName, creds.Port)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
return nil, err
}
return db, nil
}
type ResolvedItemModel struct {
gorm.Model
ID uint
}
type TokenModel struct {
Token string `gorm:"primaryKey"`
Documents []DocumentModel `gorm:"many2many:document_token_frequency_models;joinForeignKey:token;joinReferences:document_id"`
}
type DocumentModel struct {
gorm.Model
ID uint
URL string
Score int
Title string
Tokens []TokenModel `gorm:"many2many:document_token_frequency_models;joinForeignKey:document_id;joinReferences:token"`
Comments []CommentModel
}
type CommentModel struct {
gorm.Model
ID uint
Text string
DocumentModelID uint `gorm:"index"`
}
type CommentTokenFrequencyModel struct {
Token string `gorm:"primaryKey;index"`
CommentID uint `gorm:"primaryKey;index"`
CommentModel CommentModel `gorm:"foreignKey:CommentID"`
DocumentID uint `gorm:"primaryKey;index"`
Frequency int
}
type DocumentTokenFrequencyModel struct {
Token string `gorm:"primaryKey;index"`
DocumentID uint `gorm:"primaryKey"`
Frequency int
}
type DocumentTokenCount struct {
DocumentID uint `gorm:"primaryKey"`
TotalTokens int
}
func (DocumentTokenCount) TableName() string {
return "document_token_counts_view"
}
func loadTokensToIndex(db *gorm.DB) (Index, error) {
tokenToDocs := make(map[string][]uint)
var tokenModels []TokenModel
if err := db.Preload("Documents").Find(&tokenModels).Error; err != nil {
return nil, fmt.Errorf("failed to load token models: %w", err)
}
for _, tokenModel := range tokenModels {
var docIDs []uint
for _, document := range tokenModel.Documents {
docIDs = append(docIDs, document.ID)
}
tokenToDocs[tokenModel.Token] = docIDs
}
return tokenToDocs, nil
}
func (doc *Document) toDocumentModel() *DocumentModel {
comments := make([]CommentModel, 0)
for _, comment := range doc.Comments {
comments = append(comments, CommentModel{ID: comment.ID, DocumentModelID: doc.Id, Text: safeRemoveHtml(comment.Text)})
}
return &DocumentModel{
ID: doc.Id,
URL: doc.Story.URL,
Title: safeRemoveHtml(doc.Story.Title),
Comments: comments,
Score: doc.Story.Score,
}
}
func (doc *DocumentModel) toDocument() Document {
comments := make([]Comment, 0)
for _, comment := range doc.Comments {
comments = append(comments, Comment{Text: comment.Text})
}
return Document{
Id: doc.ID,
Story: Story{Title: doc.Title, URL: doc.URL, Score: doc.Score},
Comments: comments,
}
}
func addDocumentToDbIndex(db *gorm.DB, doc *Document) error {
return db.Transaction(func(tx *gorm.DB) error {
documentModel := *doc.toDocumentModel()
tx.Create(&documentModel)
tokens := doc.getTokens()
if _, err := addTokensWithFrequency(tx, doc.Id, tokens); err != nil {
return err
}
if _, err := addCommentTokensWithFrequency(tx, documentModel); err != nil {
return err
}
resolvedItems := []*ResolvedItemModel{
{ID: doc.Id},
}
for _, comment := range documentModel.Comments {
resolvedItems = append(resolvedItems, &ResolvedItemModel{ID: comment.ID})
}
tx.Create(resolvedItems)
return nil
})
}
func addTokensWithFrequency(db *gorm.DB, docID uint, tokens map[string]int) (tokensModel []TokenModel, err error) {
for token, freq := range tokens {
tokenModel := TokenModel{Token: token}
if err := db.Where("token = ?", token).FirstOrCreate(&tokenModel).Error; err != nil {
return nil, err
}
if err := db.Create(&DocumentTokenFrequencyModel{
Token: token,
DocumentID: docID,
Frequency: freq,
}).Error; err != nil {
return nil, err
}
tokensModel = append(tokensModel, tokenModel)
}
return tokensModel, nil
}
func addCommentTokensWithFrequency(db *gorm.DB, doc DocumentModel) (tokensModel []TokenModel, err error) {
for _, commentModel := range doc.Comments {
tokens := commentModel.getCommentsTokens()
for token, freq := range tokens {
tokenModel := TokenModel{Token: token}
if err := db.Where("token = ?", token).FirstOrCreate(&tokenModel).Error; err != nil {
return nil, err
}
if err := db.Create(&CommentTokenFrequencyModel{
Token: token,
CommentID: commentModel.ID,
DocumentID: doc.ID,
Frequency: freq,
}).Error; err != nil {
return nil, err
}
tokensModel = append(tokensModel, tokenModel)
}
}
return tokensModel, nil
}
func getInverseDocumentFrequencies(db *gorm.DB, tokens []string) (map[string]float64, error) {
start := time.Now()
var totalDocs int64
if err := db.Model(&DocumentModel{}).Count(&totalDocs).Error; err != nil {
return nil, err
}
type TokenCount struct {
Token string
DocFreq int64
}
var tokenCounts []TokenCount
dtfStart := time.Now()
err := db.Model(&DocumentTokenFrequencyModel{}).
Select("token, COUNT(DISTINCT document_id) as doc_freq").
Where("token IN ?", tokens).
Group("token").
Scan(&tokenCounts).Error
log.Printf("document token freq took %s", time.Since(dtfStart))
if err != nil {
return nil, err
}
idfs := make(map[string]float64)
for _, tc := range tokenCounts {
if tc.DocFreq > 0 {
idfs[tc.Token] = math.Log(float64(totalDocs) / (1 + float64(tc.DocFreq)))
}
}
elapsed := time.Since(start)
log.Printf("get inverse freq took %s", elapsed)
return idfs, nil
}
func getNormalizedTokenFrequencies(db *gorm.DB, tokens []string, docTotals []DocumentTokenCount) (map[uint]map[string]float64, error) {
type TokenFreq struct {
DocumentID uint
Token string
Frequency int
}
var tokenFreqs []TokenFreq
dtfModelStart := time.Now()
err := db.Debug().Table("document_token_frequency_models").
Select("document_token_frequency_models.document_id, document_token_frequency_models.token, document_token_frequency_models.frequency").
Where("token IN ?", tokens).
Scan(&tokenFreqs).Error
log.Printf("document token frequency took %s", time.Since(dtfModelStart))
if err != nil {
return nil, err
}
// Get total frequencies per document in one query
//var docTotals []DocumentTokenCount
//documentTotalStart := time.Now()
//err = db.Debug().Find(&docTotals).Error
//err = db.Debug().Table("document_token_frequency_models").
// Select("document_id, SUM(frequency) as total_tokens").
// Where("tokens IN ?", tokens).
// Group("document_id").
// Scan(&docTotals).Error
//log.Printf("documentTotalStart took %s", time.Since(documentTotalStart))
totalTokensMap := make(map[uint]int)
for _, dt := range docTotals {
totalTokensMap[dt.DocumentID] = dt.TotalTokens
}
result := make(map[uint]map[string]float64)
for _, tf := range tokenFreqs {
if totalTokensMap[tf.DocumentID] > 0 {
if result[tf.DocumentID] == nil {
result[tf.DocumentID] = make(map[string]float64)
}
result[tf.DocumentID][tf.Token] = float64(tf.Frequency) / (1 + float64(totalTokensMap[tf.DocumentID]))
}
}
return result, nil
}