Skip to content

Commit

Permalink
feat(database/metadata): define metadata DB for sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
jinghancc committed Dec 21, 2021
1 parent 135a5f8 commit 35530ea
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions internal/database/metadata/sqlite/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package sqlite

import (
"context"
"time"

"github.com/oom-ai/oomstore/internal/database/metadata/informer"
"github.com/oom-ai/oomstore/internal/database/metadata/sqlutil"

"github.com/oom-ai/oomstore/internal/database/metadata"

"github.com/jmoiron/sqlx"

"github.com/oom-ai/oomstore/internal/database/dbutil"
"github.com/oom-ai/oomstore/pkg/oomstore/types"
)

const SQLiteBatchSize = 20

var _ metadata.Store = &DB{}

type DB struct {
*sqlx.DB
*informer.Informer
}

func Open(ctx context.Context, opt *types.SQLiteOpt) (*DB, error) {
db, err := dbutil.OpenSQLite(opt.DBFile)
if err != nil {
return nil, err
}
informer, err := informer.New(time.Second, func() (*informer.Cache, error) {
return sqlutil.ListMetaData(ctx, db)
})
if err != nil {
db.Close()
return nil, err
}
return &DB{
DB: db,
Informer: informer,
}, err
}

func (db *DB) Ping(ctx context.Context) error {
return db.PingContext(ctx)
}

func (db *DB) Close() error {
if err := db.Informer.Close(); err != nil {
return err
}
if err := db.DB.Close(); err != nil {
return err
}
return nil
}

0 comments on commit 35530ea

Please sign in to comment.