frostfs-node/pkg/local_object_storage/writecache/writecachebitcask/api.go
Alejandro Lopez 42e74d6aab [#610] Add bitcask-inspired writecache implementation
Signed-off-by: Alejandro Lopez <a.lopez@yadro.com>
2023-08-31 14:17:10 +03:00

48 lines
1.5 KiB
Go

package writecachebitcask
import (
"context"
"encoding/binary"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/writecache"
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
)
func (c *cache) Get(_ context.Context, addr oid.Address) (*objectSDK.Object, error) {
region := c.locateRegion(addr)
return c.regions[region].get(addr)
}
func (c *cache) Head(_ context.Context, addr oid.Address) (*objectSDK.Object, error) {
region := c.locateRegion(addr)
return c.regions[region].get(addr)
}
func (c *cache) Put(ctx context.Context, req common.PutPrm) (common.PutRes, error) {
if uint64(len(req.RawData)) > c.maxObjectSize {
return common.PutRes{}, writecache.ErrBigObject
}
if mode.Mode(c.mode.Load()).ReadOnly() {
return common.PutRes{}, writecache.ErrReadOnly
}
region := c.locateRegion(req.Address)
return common.PutRes{}, c.regions[region].put(ctx, req.Address, req.RawData)
}
func (c *cache) Delete(ctx context.Context, addr oid.Address) error {
if mode.Mode(c.mode.Load()).ReadOnly() {
return writecache.ErrReadOnly
}
region := c.locateRegion(addr)
return c.regions[region].delete(ctx, addr)
}
func (c *cache) locateRegion(addr oid.Address) int {
id := addr.Object()
h := binary.LittleEndian.Uint32(id[:4])
region := h & (uint32(c.regionCount) - 1)
return int(region)
}