From fe8c96f0f064b244448a878c0a4cb7c12a5dfb5e Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Thu, 12 Jun 2025 14:39:23 +0100 Subject: [PATCH] knotclient: introduce sqlite3 based cursor store Signed-off-by: oppiliappan --- knotclient/cursor/sqlite.go | 83 +++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 knotclient/cursor/sqlite.go diff --git a/knotclient/cursor/sqlite.go b/knotclient/cursor/sqlite.go new file mode 100644 index 00000000..a6b815ab --- /dev/null +++ b/knotclient/cursor/sqlite.go @@ -0,0 +1,83 @@ +package cursor + +import ( + "database/sql" + "fmt" + + _ "github.com/mattn/go-sqlite3" +) + +type SqliteStore struct { + db *sql.DB + tableName string +} + +type SqliteStoreOpt func(*SqliteStore) + +func WithTableName(name string) SqliteStoreOpt { + return func(s *SqliteStore) { + s.tableName = name + } +} + +func NewSQLiteStore(dbPath string, opts ...SqliteStoreOpt) (*SqliteStore, error) { + db, err := sql.Open("sqlite3", dbPath) + if err != nil { + return nil, fmt.Errorf("failed to open sqlite database: %w", err) + } + + store := &SqliteStore{ + db: db, + tableName: "cursors", + } + + for _, o := range opts { + o(store) + } + + if err := store.init(); err != nil { + return nil, err + } + + return store, nil +} + +func (s *SqliteStore) init() error { + createTable := fmt.Sprintf(` + create table if not exists %s ( + knot text primary key, + cursor text + );`, s.tableName) + _, err := s.db.Exec(createTable) + return err +} + +func (s *SqliteStore) Set(knot string, cursor int64) { + query := fmt.Sprintf(` + insert into %s (knot, cursor) + values (?, ?) + on conflict(knot) do update set cursor=excluded.cursor; + `, s.tableName) + + _, err := s.db.Exec(query, knot, cursor) + + if err != nil { + // TODO: log here + } +} + +func (s *SqliteStore) Get(knot string) (cursor int64) { + query := fmt.Sprintf(` + select cursor from %s where knot = ?; + `, s.tableName) + err := s.db.QueryRow(query, knot).Scan(&cursor) + + if err != nil { + if err != sql.ErrNoRows { + // TODO: log here + } + return 0 + } + + return cursor +} -- 2.51.2