From 35530eaa8785a783549d88ce515248cd1983c8d2 Mon Sep 17 00:00:00 2001 From: jinghan Date: Tue, 21 Dec 2021 17:06:07 +0800 Subject: [PATCH] feat(database/metadata): define metadata DB for sqlite --- internal/database/metadata/sqlite/store.go | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 internal/database/metadata/sqlite/store.go diff --git a/internal/database/metadata/sqlite/store.go b/internal/database/metadata/sqlite/store.go new file mode 100644 index 000000000..82684cdcf --- /dev/null +++ b/internal/database/metadata/sqlite/store.go @@ -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 +}