Skip to content

Commit

Permalink
indexer: quick hack to merge old blocks into db
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Sep 11, 2024
1 parent fed10d4 commit 4062ed1
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions vochain/indexer/indexer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package indexer

import (
"bufio"
"bytes"
"context"
"database/sql"
Expand Down Expand Up @@ -179,6 +180,12 @@ func (idx *Indexer) startDB() error {
defer func() { go idx.ReindexBlocks(false) }()
}

if file, err := os.Open(idx.dbPath + ".import"); err == nil {
defer func() { go idx.insertSQLstatements(file) }()
} else {
log.Errorf("couldn't open %s: %v", idx.dbPath+".import", err)
}

if err := goose.Up(idx.readWriteDB, "migrations"); err != nil {
return fmt.Errorf("goose up: %w", err)
}
Expand Down Expand Up @@ -287,6 +294,43 @@ func (idx *Indexer) SaveBackup(ctx context.Context, path string) error {
return err
}

func (idx *Indexer) insertSQLstatements(r io.Reader) {
log.Infof("starting to import SQL statements")

if idx.readWriteDB == nil {
log.Error("database is not yet initialized")
return
}
scanner := bufio.NewScanner(r)
var statement strings.Builder
l, n := 0, 0
for scanner.Scan() {
l++
line := scanner.Text()
statement.WriteString(line)
statement.WriteString("\n")

if strings.HasSuffix(line, ";") {
n++
if n%10000 == 1 {
log.Infof("executing %q", statement.String())
}
_, err := idx.readWriteDB.Exec(statement.String())
if err != nil {
log.Errorf("failed to execute statement: %s (error: %v)", statement.String(), err)
return
}
statement.Reset()
}
}

if err := scanner.Err(); err != nil {
log.Errorf("error during restore: %s", err)
}

log.Infof("finished parsing %d lines, executed %d SQL statements", l, n)
}

// ExportBackupAsBytes backs up the database, and returns the contents as []byte.
//
// Note that writes to the database may be blocked until the backup finishes.
Expand Down

0 comments on commit 4062ed1

Please sign in to comment.