Skip to content

Commit

Permalink
fix(logger): make a new buffer slice and fix logging
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-stevan-andjelkovic committed Jan 26, 2021
1 parent 4b952c4 commit a025d5f
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/logger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ func main() {
}
}

func newBuffer() [][]byte {
return make([][]byte, 0, BUFFER_LEN)
}

func worker(db *sql.DB, queue chan []byte) {
buffer := make([][]byte, 0, BUFFER_LEN)
buffer := newBuffer()
for {
if len(buffer) >= BUFFER_LEN {
commit(db, buffer)
buffer = buffer[:0]
buffer = newBuffer()
} else {
if len(buffer) == 0 {
entry := <-queue // Blocking.
Expand All @@ -52,7 +56,7 @@ func worker(db *sql.DB, queue chan []byte) {
buffer = append(buffer, entry)
} else {
commit(db, buffer)
buffer = buffer[:0]
buffer = newBuffer()
}
}
}
Expand Down Expand Up @@ -109,14 +113,14 @@ func commit(db *sql.DB, buffer [][]byte) {
panic(err)
}
duration := time.Since(start)
log.Println("The worker thread commited %d entries in %v!", BUFFER_LEN, duration)
log.Printf("The worker thread committed %d entries in %v!\n", len(buffer), duration)
}

func parse(entry []byte) ([]byte, []byte, []byte) {
// NOTE: Tab characters are not allowed to appear unescaped in JSON.
split := bytes.Split(entry, []byte("\t"))
if len(split) != 3 {
panic(fmt.Sprintf("parse: failed to split entry: %v", entry))
panic(fmt.Sprintf("parse: failed to split entry: %s", string(entry)))
}
return split[0], split[1], split[2]
}
Expand Down

0 comments on commit a025d5f

Please sign in to comment.