From 3d71db8702f58182f1f7eda3446a2c474443c7f2 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sat, 7 Mar 2026 19:06:11 +0800 Subject: refstore: Add non-transactional store and rw store --- refstore/batch.go | 38 ++++++++++++++++++++++++++++++++++++++ refstore/batch_store.go | 7 +++++++ refstore/read_write.go | 7 +++++++ refstore/read_write_store.go | 8 ++++++++ refstore/transactional.go | 11 ----------- refstore/transactional_store.go | 11 +++++++++++ repository/refs.go | 4 ++-- repository/repository.go | 2 +- 8 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 refstore/batch.go create mode 100644 refstore/batch_store.go create mode 100644 refstore/read_write.go create mode 100644 refstore/read_write_store.go delete mode 100644 refstore/transactional.go create mode 100644 refstore/transactional_store.go diff --git a/refstore/batch.go b/refstore/batch.go new file mode 100644 index 00000000..05e0f1cb --- /dev/null +++ b/refstore/batch.go @@ -0,0 +1,38 @@ +package refstore + +import "codeberg.org/lindenii/furgit/objectid" + +// Batch applies reference operations immediately, one operation at a time. +// +// Unlike Transaction, Batch does not stage changes for one atomic commit. +// Each method attempts its update immediately and returns that operation's +// error, if any. +type Batch interface { + // Create creates one detached reference, requiring that the logical + // reference does not already exist. + Create(name string, newID objectid.ObjectID) error + // Update updates one detached reference, requiring that the current logical + // reference value matches oldID. + Update(name string, newID, oldID objectid.ObjectID) error + // Delete deletes one detached reference, requiring that the current logical + // reference value matches oldID. + Delete(name string, oldID objectid.ObjectID) error + // Verify verifies that the current logical reference value matches oldID. + Verify(name string, oldID objectid.ObjectID) error + + // CreateSymbolic creates one symbolic reference, requiring that the named + // reference does not already exist. + CreateSymbolic(name, newTarget string) error + // UpdateSymbolic updates one symbolic reference directly, requiring that its + // current target matches oldTarget. + UpdateSymbolic(name, newTarget, oldTarget string) error + // DeleteSymbolic deletes one symbolic reference directly, requiring that its + // current target matches oldTarget. + DeleteSymbolic(name, oldTarget string) error + // VerifySymbolic verifies that the named symbolic reference currently points + // at oldTarget. + VerifySymbolic(name, oldTarget string) error + + // Close releases any resources associated with the batch. + Close() error +} diff --git a/refstore/batch_store.go b/refstore/batch_store.go new file mode 100644 index 00000000..8e821eb6 --- /dev/null +++ b/refstore/batch_store.go @@ -0,0 +1,7 @@ +package refstore + +// BatchStore begins non-atomic reference batches. +type BatchStore interface { + // BeginBatch creates one new immediate-apply batch. + BeginBatch() (Batch, error) +} diff --git a/refstore/read_write.go b/refstore/read_write.go new file mode 100644 index 00000000..8ac592bb --- /dev/null +++ b/refstore/read_write.go @@ -0,0 +1,7 @@ +package refstore + +// ReadWriteStore supports both reading and atomic transactional updates. +type ReadWriteStore interface { + ReadingStore + TransactionalStore +} diff --git a/refstore/read_write_store.go b/refstore/read_write_store.go new file mode 100644 index 00000000..506a9aae --- /dev/null +++ b/refstore/read_write_store.go @@ -0,0 +1,8 @@ +package refstore + +// ReadWriteStore supports reading, atomic transactions, and immediate batches. +type ReadWriteStore interface { + ReadingStore + TransactionalStore + BatchStore +} diff --git a/refstore/transactional.go b/refstore/transactional.go deleted file mode 100644 index 8f5c32cd..00000000 --- a/refstore/transactional.go +++ /dev/null @@ -1,11 +0,0 @@ -package refstore - -// TransactionalStore begins atomic reference transactions. -// -// Not every readable reference store is writable. Implementations should only -// satisfy TransactionalStore when they can stage and commit reference updates -// atomically within that backend. -type TransactionalStore interface { - // BeginTransaction creates one new mutable transaction. - BeginTransaction() (Transaction, error) -} diff --git a/refstore/transactional_store.go b/refstore/transactional_store.go new file mode 100644 index 00000000..8f5c32cd --- /dev/null +++ b/refstore/transactional_store.go @@ -0,0 +1,11 @@ +package refstore + +// TransactionalStore begins atomic reference transactions. +// +// Not every readable reference store is writable. Implementations should only +// satisfy TransactionalStore when they can stage and commit reference updates +// atomically within that backend. +type TransactionalStore interface { + // BeginTransaction creates one new mutable transaction. + BeginTransaction() (Transaction, error) +} diff --git a/repository/refs.go b/repository/refs.go index a695252d..0af7c462 100644 --- a/repository/refs.go +++ b/repository/refs.go @@ -11,7 +11,7 @@ import ( ) //nolint:ireturn -func openRefStore(root *os.Root, algo objectid.Algorithm, packedRefsTimeout time.Duration) (out refstore.ReadingStore, err error) { +func openRefStore(root *os.Root, algo objectid.Algorithm, packedRefsTimeout time.Duration) (out refstore.ReadWriteStore, err error) { refRoot, err := root.OpenRoot(".") if err != nil { return nil, fmt.Errorf("repository: open root for refs: %w", err) @@ -30,6 +30,6 @@ func openRefStore(root *os.Root, algo objectid.Algorithm, packedRefsTimeout time // Refs returns the configured ref store. // //nolint:ireturn -func (repo *Repository) Refs() refstore.ReadingStore { +func (repo *Repository) Refs() refstore.ReadWriteStore { return repo.refs } diff --git a/repository/repository.go b/repository/repository.go index 7948f2ee..f120ea72 100644 --- a/repository/repository.go +++ b/repository/repository.go @@ -19,5 +19,5 @@ type Repository struct { objects objectstore.Store objectsLooseForWritingOnly *objectloose.Store - refs refstore.ReadingStore + refs refstore.ReadWriteStore } -- cgit v1.3.1-10-gc9f91