Skip to content

Commit

Permalink
FMWK-521 Replace aerospike.Client dependency with interface (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
filkeith authored Aug 5, 2024
1 parent d61aafb commit 55dccba
Show file tree
Hide file tree
Showing 7 changed files with 715 additions and 14 deletions.
30 changes: 25 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ const (
MaxPartitions = 4096
)

// AerospikeClient describes aerospike client interface for easy mocking.
//
// go:generate mockery --name AerospikeClient
type AerospikeClient interface {
GetDefaultScanPolicy() *a.ScanPolicy
GetDefaultInfoPolicy() *a.InfoPolicy
GetDefaultWritePolicy() *a.WritePolicy
Put(policy *a.WritePolicy, key *a.Key, bins a.BinMap) a.Error
CreateComplexIndex(policy *a.WritePolicy, namespace string, set string, indexName string, binName string,
indexType a.IndexType, indexCollectionType a.IndexCollectionType, ctx ...*a.CDTContext,
) (*a.IndexTask, a.Error)
DropIndex(policy *a.WritePolicy, namespace string, set string, indexName string) a.Error
RegisterUDF(policy *a.WritePolicy, udfBody []byte, serverPath string, language a.Language,
) (*a.RegisterTask, a.Error)
BatchOperate(policy *a.BatchPolicy, records []a.BatchRecordIfc) a.Error
Cluster() *a.Cluster
ScanPartitions(scanPolicy *a.ScanPolicy, partitionFilter *a.PartitionFilter, namespace string,
setName string, binNames ...string) (*a.Recordset, a.Error)
}

// Client is the main entry point for the backup package.
// It wraps an aerospike client and provides methods to start backup and restore operations.
// Example usage:
Expand Down Expand Up @@ -68,7 +88,7 @@ const (
// // handle error
// }
type Client struct {
aerospikeClient *a.Client
aerospikeClient AerospikeClient
logger *slog.Logger
id string
}
Expand All @@ -77,7 +97,7 @@ type Client struct {
// - ac is the aerospike client to use for backup and restore operations.
// - id is an identifier for the client.
// - logger is the logger that this client will log to.
func NewClient(ac *a.Client, id string, logger *slog.Logger) (*Client, error) {
func NewClient(ac AerospikeClient, id string, logger *slog.Logger) (*Client, error) {
if ac == nil {
return nil, errors.New("aerospike client pointer is nil")
}
Expand All @@ -101,23 +121,23 @@ func NewClient(ac *a.Client, id string, logger *slog.Logger) (*Client, error) {

func (c *Client) getUsableInfoPolicy(p *a.InfoPolicy) a.InfoPolicy {
if p == nil {
p = c.aerospikeClient.DefaultInfoPolicy
p = c.aerospikeClient.GetDefaultInfoPolicy()
}

return *p
}

func (c *Client) getUsableWritePolicy(p *a.WritePolicy) a.WritePolicy {
if p == nil {
p = c.aerospikeClient.DefaultWritePolicy
p = c.aerospikeClient.GetDefaultWritePolicy()
}

return *p
}

func (c *Client) getUsableScanPolicy(p *a.ScanPolicy) a.ScanPolicy {
if p == nil {
p = c.aerospikeClient.DefaultScanPolicy
p = c.aerospikeClient.GetDefaultScanPolicy()
}

return *p
Expand Down
5 changes: 2 additions & 3 deletions handler_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"log/slog"
"sync/atomic"

a "github.com/aerospike/aerospike-client-go/v7"
"github.com/aerospike/backup-go/internal/asinfo"
"github.com/aerospike/backup-go/internal/logging"
"github.com/aerospike/backup-go/io/aerospike"
Expand Down Expand Up @@ -52,7 +51,7 @@ type BackupHandler struct {
writer Writer
encoder Encoder
config *BackupConfig
aerospikeClient *a.Client
aerospikeClient AerospikeClient
logger *slog.Logger
firstFileHeaderWritten *atomic.Bool
limiter *rate.Limiter
Expand All @@ -65,7 +64,7 @@ type BackupHandler struct {
// newBackupHandler creates a new BackupHandler.
func newBackupHandler(
config *BackupConfig,
ac *a.Client,
ac AerospikeClient,
logger *slog.Logger,
writer Writer,
) *BackupHandler {
Expand Down
4 changes: 2 additions & 2 deletions handler_backup_records.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ import (

type backupRecordsHandler struct {
config *BackupConfig
aerospikeClient *a.Client
aerospikeClient AerospikeClient
logger *slog.Logger
}

func newBackupRecordsHandler(
config *BackupConfig,
ac *a.Client,
ac AerospikeClient,
logger *slog.Logger,
) *backupRecordsHandler {
logger.Debug("created new backup records handler")
Expand Down
5 changes: 2 additions & 3 deletions handler_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"io"
"log/slog"

a "github.com/aerospike/aerospike-client-go/v7"
"github.com/aerospike/backup-go/internal/asinfo"
"github.com/aerospike/backup-go/internal/logging"
"github.com/aerospike/backup-go/internal/processors"
Expand Down Expand Up @@ -48,7 +47,7 @@ type StreamingReader interface {
type RestoreHandler struct {
reader StreamingReader
config *RestoreConfig
aerospikeClient *a.Client
aerospikeClient AerospikeClient
logger *slog.Logger
limiter *rate.Limiter
errors chan error
Expand All @@ -59,7 +58,7 @@ type RestoreHandler struct {
// newRestoreHandler creates a new RestoreHandler.
func newRestoreHandler(
config *RestoreConfig,
ac *a.Client,
ac AerospikeClient,
logger *slog.Logger,
reader StreamingReader,
) *RestoreHandler {
Expand Down
6 changes: 5 additions & 1 deletion internal/asinfo/info_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,16 @@ type infoGetter interface {
RequestInfo(infoPolicy *a.InfoPolicy, commands ...string) (map[string]string, a.Error)
}

type aerospikeClient interface {
Cluster() *a.Cluster
}

type InfoClient struct {
policy *a.InfoPolicy
cluster *a.Cluster
}

func NewInfoClientFromAerospike(aeroClient *a.Client, policy *a.InfoPolicy) *InfoClient {
func NewInfoClientFromAerospike(aeroClient aerospikeClient, policy *a.InfoPolicy) *InfoClient {
return &InfoClient{
cluster: aeroClient.Cluster(),
policy: policy,
Expand Down
83 changes: 83 additions & 0 deletions internal/asinfo/mocks/aerospikeClient_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 55dccba

Please sign in to comment.