Skip to content

Commit

Permalink
removed unnecessary batching
Browse files Browse the repository at this point in the history
  • Loading branch information
karjo24 committed Oct 10, 2024
1 parent 1ad8a16 commit 1bc9f8e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
21 changes: 8 additions & 13 deletions dao/courses.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type CoursesDao interface {
GetAvailableSemesters(c context.Context) []model.Semester
GetCourseByShortLink(link string) (model.Course, error)
GetCourseAdmins(courseID uint) ([]model.User, error)
// ExecAllCourses executes f on all courses from database without batching
ExecAllCourses(f func([]Course))

UpdateCourse(ctx context.Context, course model.Course) error
Expand Down Expand Up @@ -289,23 +290,17 @@ type Course struct {
}

// ExecAllCourses executes f on all courses.
//
// loads every course into memory
func (d coursesDao) ExecAllCourses(f func([]Course)) {
var res []Course
batchNum := 0
batchSize := 100
var numCourses int64
DB.Model(&model.Course{}).Count(&numCourses)
for batchSize*batchNum < int(numCourses) {
err := DB.Raw(`SELECT id, name, slug, year, teaching_term, visibility
err := DB.Raw(`SELECT id, name, slug, year, teaching_term, visibility
FROM courses
WHERE deleted_at IS NULL
LIMIT ? OFFSET ?`, batchSize, batchNum*batchSize).Scan(&res).Error
if err != nil {
fmt.Println(err)
}
f(res)
batchNum++
WHERE deleted_at IS NULL ORDER BY id`).Scan(&res).Error
if err != nil {
fmt.Println(err)
}
f(res)
}

func (d coursesDao) UpdateCourse(ctx context.Context, course model.Course) error {
Expand Down
2 changes: 1 addition & 1 deletion tools/meiliExporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (m *MeiliExporter) Export() {
Visibility: course.Visibility,
}
}
_, err := coursesIndex.AddDocuments(&meilicourses, "ID")
_, err := coursesIndex.AddDocumentsInBatches(&meilicourses, 500, "ID")
if err != nil {
logger.Error("issue adding courses to meili", "err", err)
}
Expand Down

0 comments on commit 1bc9f8e

Please sign in to comment.