Skip to content

Commit

Permalink
[PSL-1245] implement meta-tables migration
Browse files Browse the repository at this point in the history
  • Loading branch information
j-rafique committed Jul 31, 2024
1 parent 82ba0eb commit 0cce5e1
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions p2p/kademlia/store/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ func NewStore(ctx context.Context, dataDir string) (*Store, error) {
log.P2P().WithContext(ctx).Errorf("cannot create to-del-keys table in sqlite database: %s", err.Error())
}

if err := s.migrateMeta(); err != nil {
log.P2P().WithContext(ctx).Errorf("cannot create meta table in sqlite database: %s", err.Error())
}

if err := s.migrateMetaMigration(); err != nil {
log.P2P().WithContext(ctx).Errorf("cannot create meta-migration table in sqlite database: %s", err.Error())
}

if err := s.migrateMigration(); err != nil {
log.P2P().WithContext(ctx).Errorf("cannot create migration table in sqlite database: %s", err.Error())
}

pragmas := []string{
"PRAGMA journal_mode=WAL;",
"PRAGMA synchronous=NORMAL;",
Expand Down Expand Up @@ -160,6 +172,58 @@ func (s *Store) migrateToDelKeys() error {
return nil
}

func (s *Store) migrateMeta() error {
query := `
CREATE TABLE IF NOT EXISTS meta (
key TEXT PRIMARY KEY,
last_accessed TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
access_count INTEGER DEFAULT 0,
data_size INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
); `
if _, err := s.db.Exec(query); err != nil {
return fmt.Errorf("failed to create table 'del_keys': %w", err)
}

return nil
}

func (s *Store) migrateMetaMigration() error {
query := `
CREATE TABLE IF NOT EXISTS meta_migration (
key TEXT,
migration_id INTEGER,
score INTEGER,
is_migrated BOOLEAN,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (key, migration_id)
);`
if _, err := s.db.Exec(query); err != nil {
return fmt.Errorf("failed to create table 'del_keys': %w", err)
}

return nil
}

func (s *Store) migrateMigration() error {
query := `
CREATE TABLE IF NOT EXISTS migration (
id INTEGER PRIMARY KEY AUTOINCREMENT,
total_data_size INTEGER,
migration_started_at TIMESTAMP,
migration_finished_at TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);`
if _, err := s.db.Exec(query); err != nil {
return fmt.Errorf("failed to create table 'del_keys': %w", err)
}

return nil
}

func (s *Store) startCheckpointWorker(ctx context.Context) {
b := backoff.NewExponentialBackOff()
b.MaxElapsedTime = 1 * time.Minute
Expand Down

0 comments on commit 0cce5e1

Please sign in to comment.