-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.go
More file actions
126 lines (112 loc) · 3.41 KB
/
Copy pathdb.go
File metadata and controls
126 lines (112 loc) · 3.41 KB
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
package main
import (
"fmt"
"log"
"os"
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type Word struct {
gorm.Model
Name string `gorm:"index:word_name_idx,unique"`
}
type Url struct {
gorm.Model
Name string `gorm:"index:url_name_idx,unique"`
Title, Description string
Count int
}
type WordFrequencyRecord struct {
gorm.Model
Count int
WordID uint
Word Word
UrlID uint
Url Url
IdxWordUrl string `gorm:"index:idx_word_url,unique"`
}
// migrateTables migrates the Word, Url, and WordFrequencyRecord tables using autoMigrate
func migrateTables(db *gorm.DB) {
err := db.AutoMigrate(&Word{}, &Url{}, &WordFrequencyRecord{})
if err != nil {
log.Fatalf("Error creating tables: %v\n", err)
}
}
// dropDatabase to drop the database file
func dropDatabase(dbName string) {
if err := os.Remove(dbName); err != nil && !os.IsNotExist(err) {
log.Fatalf("Failed to drop the database: %v", err)
}
log.Println("Database dropped and will be recreated.")
}
// connectToDB connects to a mysql DB given its name, migrates the tables, and then
// returns a pointer to the gorm.DB struct
func connectToDB(dsn string, useSqlite bool) (*gorm.DB, error) {
if useSqlite {
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
migrateTables(db)
return db, err
}
db, err := gorm.Open(mysql.New(mysql.Config{
DSN: dsn,
}), &gorm.Config{})
if err != nil {
fmt.Errorf("[ERROR] Could not connect to the database: %s", err)
return nil, err
}
fmt.Println("[INFO] Successfully connected to the database!")
migrateTables(db)
return db, err
}
// getItem takes in a pointer to a struct, and fills the
// struct with data from the first entry of the respective table that matches the filter
func getItem[K *Word | *WordFrequencyRecord | *Url](db *gorm.DB, object K) error {
result := db.Where(object).First(object)
if result.Error != nil {
fmt.Printf("Error fetching %v: %v\n", object, result.Error)
}
return result.Error
}
// create takes in a pointer to a struct and inserts the data from the struct into the database
func create[K *Word | *WordFrequencyRecord | *Url](db *gorm.DB, object K) error {
if err := db.Create(object).Error; err != nil {
log.Printf("Error creating object: %v\n", err)
}
return nil
}
// getItemOrCreate takes in a pointer to an object and attempts to fetch the object
// from the database. If it is unsuccessful then it inserts a new object into the database.
func getItemOrCreate[K *Word | *WordFrequencyRecord | *Url](db *gorm.DB, object K) error {
err := getItem(db, object)
if err != nil {
err = create(db, object)
}
return err
}
func batchInsertWordFrequencyRecords(db *gorm.DB, wordFrequencyRecords []*WordFrequencyRecord, batchSize int) error {
if len(wordFrequencyRecords) == 0 {
return nil
}
// Helper function to execute a batch insert
insertBatch := func(batch []*WordFrequencyRecord) error {
// Perform a batch insert using Gorm's CreateInBatches
// TODO: creating in batches still not working here
if err := db.CreateInBatches(batch, batchSize).Error; err != nil {
return err
}
return nil
}
// Process the records in batches
for i := 0; i < len(wordFrequencyRecords); i += batchSize {
end := i + batchSize
if end > len(wordFrequencyRecords) {
end = len(wordFrequencyRecords)
}
// Insert the current batch
if err := insertBatch(wordFrequencyRecords[i:end]); err != nil {
return err
}
}
return nil
}