kevo/pkg/engine/compat.go
Jeremy Tregunna 7e226825df
All checks were successful
Go Tests / Run Tests (1.24.2) (push) Successful in 9m48s
fix: engine refactor bugfix fest, go fmt
2025-04-25 23:36:08 -06:00

81 lines
2.4 KiB
Go

package engine
import (
"errors"
"sync"
"github.com/KevoDB/kevo/pkg/common/iterator"
)
// Compatibility layer for the legacy engine API
// LegacyTransaction interface is kept for backward compatibility
type LegacyTransaction interface {
Get(key []byte) ([]byte, error)
Put(key, value []byte) error
Delete(key []byte) error
NewIterator() iterator.Iterator
NewRangeIterator(startKey, endKey []byte) iterator.Iterator
Commit() error
Rollback() error
IsReadOnly() bool
}
// LegacyTransactionCreator is kept for backward compatibility
type LegacyTransactionCreator interface {
CreateTransaction(engine interface{}, readOnly bool) (LegacyTransaction, error)
}
var (
// legacyTransactionCreatorFunc holds the function that creates transactions
legacyTransactionCreatorFunc LegacyTransactionCreator
transactionCreatorMu sync.RWMutex
)
// RegisterTransactionCreator registers a function that can create transactions
// This is kept for backward compatibility
func RegisterTransactionCreator(creator LegacyTransactionCreator) {
transactionCreatorMu.Lock()
defer transactionCreatorMu.Unlock()
legacyTransactionCreatorFunc = creator
}
// GetRegisteredTransactionCreator returns the registered transaction creator
// This is for internal use by the engine facade
func GetRegisteredTransactionCreator() LegacyTransactionCreator {
transactionCreatorMu.RLock()
defer transactionCreatorMu.RUnlock()
return legacyTransactionCreatorFunc
}
// CreateTransactionWithCreator creates a transaction using the registered creator
// This is for internal use by the engine facade
func CreateTransactionWithCreator(engine interface{}, readOnly bool) (LegacyTransaction, error) {
transactionCreatorMu.RLock()
creator := legacyTransactionCreatorFunc
transactionCreatorMu.RUnlock()
if creator == nil {
return nil, errors.New("no transaction creator registered")
}
return creator.CreateTransaction(engine, readOnly)
}
// GetRWLock is a compatibility method for the engine facade
// It returns a sync.RWMutex for use by the legacy transaction code
func (e *EngineFacade) GetRWLock() *sync.RWMutex {
// Forward to the transaction manager's lock
return e.txManager.GetRWLock()
}
// IncrementTxCompleted is a compatibility method for the engine facade
func (e *EngineFacade) IncrementTxCompleted() {
e.txManager.IncrementTxCompleted()
}
// IncrementTxAborted is a compatibility method for the engine facade
func (e *EngineFacade) IncrementTxAborted() {
e.txManager.IncrementTxAborted()
}