2020-05-22 19:12:26 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package testblobs
|
|
|
|
|
|
|
|
import (
|
2021-09-10 14:05:29 +01:00
|
|
|
"context"
|
|
|
|
|
2020-05-22 19:12:26 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/storj/storagenode"
|
2023-04-05 18:03:06 +01:00
|
|
|
"storj.io/storj/storagenode/blobstore"
|
2020-05-22 19:12:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ensures that limitedSpaceDB implements storagenode.DB.
|
|
|
|
var _ storagenode.DB = (*limitedSpaceDB)(nil)
|
|
|
|
|
|
|
|
// limitedSpaceDB implements storage node DB with limited free space.
|
|
|
|
type limitedSpaceDB struct {
|
|
|
|
storagenode.DB
|
|
|
|
log *zap.Logger
|
|
|
|
blobs *LimitedSpaceBlobs
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLimitedSpaceDB creates a new storage node DB with limited free space.
|
|
|
|
func NewLimitedSpaceDB(log *zap.Logger, db storagenode.DB, freeSpace int64) storagenode.DB {
|
|
|
|
return &limitedSpaceDB{
|
|
|
|
DB: db,
|
|
|
|
blobs: newLimitedSpaceBlobs(log, db.Pieces(), freeSpace),
|
|
|
|
log: log,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pieces returns the blob store.
|
2023-04-05 18:03:06 +01:00
|
|
|
func (lim *limitedSpaceDB) Pieces() blobstore.Blobs {
|
2020-05-22 19:12:26 +01:00
|
|
|
return lim.blobs
|
|
|
|
}
|
|
|
|
|
|
|
|
// LimitedSpaceBlobs implements a limited space blob store.
|
|
|
|
type LimitedSpaceBlobs struct {
|
2023-04-05 18:03:06 +01:00
|
|
|
blobstore.Blobs
|
2020-05-22 19:12:26 +01:00
|
|
|
log *zap.Logger
|
|
|
|
freeSpace int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// newLimitedSpaceBlobs creates a new limited space blob store wrapping the provided blobs.
|
2023-04-05 18:03:06 +01:00
|
|
|
func newLimitedSpaceBlobs(log *zap.Logger, blobs blobstore.Blobs, freeSpace int64) *LimitedSpaceBlobs {
|
2020-05-22 19:12:26 +01:00
|
|
|
return &LimitedSpaceBlobs{
|
|
|
|
log: log,
|
|
|
|
Blobs: blobs,
|
|
|
|
freeSpace: freeSpace,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FreeSpace returns how much free space left for writing.
|
2021-09-10 14:05:29 +01:00
|
|
|
func (limspace *LimitedSpaceBlobs) FreeSpace(ctx context.Context) (int64, error) {
|
2020-05-22 19:12:26 +01:00
|
|
|
return limspace.freeSpace, nil
|
|
|
|
}
|