From ab151d6708cb29bed525989ddfe6177838e63dfd Mon Sep 17 00:00:00 2001 From: i-norden Date: Thu, 30 Nov 2023 13:00:34 -0600 Subject: [PATCH 1/2] add index to event_entry.key --- chain/events/filter/index.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/chain/events/filter/index.go b/chain/events/filter/index.go index 2b1890c73..b1cc2ea5f 100644 --- a/chain/events/filter/index.go +++ b/chain/events/filter/index.go @@ -56,6 +56,8 @@ var ddls = []string{ value BLOB NOT NULL )`, + `CREATE INDEX IF NOT EXISTS event_entry_key_index ON event_entry (key)`, + // metadata containing version of schema `CREATE TABLE IF NOT EXISTS _meta ( version UINT64 NOT NULL UNIQUE @@ -63,6 +65,7 @@ var ddls = []string{ `INSERT OR IGNORE INTO _meta (version) VALUES (1)`, `INSERT OR IGNORE INTO _meta (version) VALUES (2)`, + `INSERT OR IGNORE INTO _meta (version) VALUES (3)`, } var ( @@ -70,7 +73,7 @@ var ( ) const ( - schemaVersion = 2 + schemaVersion = 3 eventExists = `SELECT MAX(id) FROM event WHERE height=? AND tipset_key=? AND tipset_key_cid=? AND emitter_addr=? AND event_index=? AND message_cid=? AND message_index=?` insertEvent = `INSERT OR IGNORE INTO event(height, tipset_key, tipset_key_cid, emitter_addr, event_index, message_cid, message_index, reverted) VALUES(?, ?, ?, ?, ?, ?, ?, ?)` @@ -321,6 +324,21 @@ func NewEventIndex(ctx context.Context, path string, chainStore *store.ChainStor version = 2 } + if version == 2 { + log.Infof("upgrading event index from version 1 to version 2") + + // to upgrade to version 3 we only need to create an index on the event entries table (key) column + // which means we can just reapply the schema (it will not have any effect on existing data) + for _, ddl := range ddls { + if _, err := db.Exec(ddl); err != nil { + _ = db.Close() + return nil, xerrors.Errorf("could not upgrade index to version 3, exec ddl %q: %w", ddl, err) + } + } + + version = 3 + } + if version != schemaVersion { _ = db.Close() return nil, xerrors.Errorf("invalid database version: got %d, expected %d", version, schemaVersion) From f1017b18d8b9d6f7ca3421b2c38ada1c9285c203 Mon Sep 17 00:00:00 2001 From: i-norden Date: Thu, 30 Nov 2023 13:03:36 -0600 Subject: [PATCH 2/2] add index to event.emitter_addr --- chain/events/filter/index.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/chain/events/filter/index.go b/chain/events/filter/index.go index b1cc2ea5f..793bb6b47 100644 --- a/chain/events/filter/index.go +++ b/chain/events/filter/index.go @@ -46,6 +46,7 @@ var ddls = []string{ )`, `CREATE INDEX IF NOT EXISTS height_tipset_key ON event (height,tipset_key)`, + `CREATE INDEX IF NOT EXISTS event_emitter_addr ON event (emitter_addr)`, `CREATE TABLE IF NOT EXISTS event_entry ( event_id INTEGER, @@ -327,7 +328,8 @@ func NewEventIndex(ctx context.Context, path string, chainStore *store.ChainStor if version == 2 { log.Infof("upgrading event index from version 1 to version 2") - // to upgrade to version 3 we only need to create an index on the event entries table (key) column + // to upgrade to version 3 we only need to create an index on the event_entry.key column + // and on the event.emitter_addr column // which means we can just reapply the schema (it will not have any effect on existing data) for _, ddl := range ddls { if _, err := db.Exec(ddl); err != nil {