|
|
|
internal/gitstore/store.go raw
| 1 | package gitstore | | 2 | | | 3 | import ( | | 4 | "context" | | 5 | "database/sql" | | 6 | "errors" | | 7 | "path/filepath" | | 8 | | | 9 | "github.com/FelineStateMachine/tinyrelay/internal/storage" | | 10 | "github.com/FelineStateMachine/tinyrelay/protocol/nostr" | | 11 | ) | | 12 | | | 13 | // ErrDuplicate indicates that an event was already persisted. | | 14 | var ErrDuplicate = errors.New("duplicate event" ) | | 15 | | | 16 | // Store adapts the shared SQLite event store to the Git engine contract. | | 17 | type Store struct{ store *storage.Store } | | 18 | | | 19 | // Open creates a caller-owned SQLite store. | | 20 | func Open(ctx context.Context, path string) (*Store, error) { | | 21 | path, err := filepath.Abs(path) | | 22 | if err != nil { | | 23 | return nil, err | | 24 | } | | 25 | store, err := storage.Open(ctx, path) | | 26 | if err != nil { | | 27 | return nil, err | | 28 | } | | 29 | return &Store{store: store}, nil | | 30 | } | | 31 | | | 32 | // Wrap adapts an existing host store without taking lifecycle ownership. | | 33 | func Wrap(store *storage.Store) *Store { return &Store{store: store} } | | 34 | | | 35 | func (s *Store) Query(ctx context.Context, f nostr.Filter, now int64, limit int) ([]nostr.Event, error) { | | 36 | result, err := s.store.Query(ctx, f, storage.QueryOptions{Now: now, Limit: limit, Access: storage.Access{All: true}}) | | 37 | return result.Events, err | | 38 | } | | 39 | func (s *Store) Save(ctx context.Context, e nostr.Event, now int64) error { | | 40 | _, err := s.store.Save(ctx, e, storage.SaveOptions{Now: now}) | | 41 | if errors.Is(err, storage.ErrDuplicate) { | | 42 | return ErrDuplicate | | 43 | } | | 44 | return err | | 45 | } | | 46 | func (s *Store) Latest(ctx context.Context, kind int, pubkey, identifier string) (nostr.Event, bool, error) { | | 47 | var raw string | | 48 | err := s.store.DB().QueryRowContext(ctx, "SELECT raw FROM events WHERE kind=? AND pubkey=? AND d=? ORDER BY created_at DESC,id ASC LIMIT 1" , kind, pubkey, identifier).Scan(&raw) | | 49 | if errors.Is(err, sql.ErrNoRows) { | | 50 | return nostr.Event{}, false, nil | | 51 | } | | 52 | if err != nil { | | 53 | return nostr.Event{}, false, err | | 54 | } | | 55 | event, err := nostr.Parse([]byte(raw)) | | 56 | if err != nil { | | 57 | return nostr.Event{}, false, err | | 58 | } | | 59 | return event, true, nil | | 60 | } | | 61 | func (s *Store) Exists(ctx context.Context, id string, kind int) (bool, error) { | | 62 | var found int | | 63 | err := s.store.DB().QueryRowContext(ctx, "SELECT 1 FROM events WHERE id=? AND kind=?" , id, kind).Scan(&found) | | 64 | if errors.Is(err, sql.ErrNoRows) { | | 65 | return false, nil | | 66 | } | | 67 | return err == nil, err | | 68 | } | | 69 | func (s *Store) Close() error { return s.store.Close() } |
|
|
About| owner | c01503e8035f |
|---|
| visibility | public |
|---|
| head | d730679639bf |
|---|
Maintainers- c01503e8035f owner
- ba1b5beed5b5 agent hermes
Clonehttps://012.run/npub1cq2s86qrtadf36eqq3epm38wmhdynrrcwxf84eldk3f94dkv7ttsrulrz5/tinyrelay.git
|