From 4062ed19d438fb7cc5e34ff109cf4066bb79040f Mon Sep 17 00:00:00 2001 From: Gui Iribarren Date: Wed, 11 Sep 2024 13:46:30 +0200 Subject: [PATCH] indexer: quick hack to merge old blocks into db --- vochain/indexer/indexer.go | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/vochain/indexer/indexer.go b/vochain/indexer/indexer.go index 37fea31a2..c3458b2c2 100644 --- a/vochain/indexer/indexer.go +++ b/vochain/indexer/indexer.go @@ -1,6 +1,7 @@ package indexer import ( + "bufio" "bytes" "context" "database/sql" @@ -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) } @@ -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.