mirror of
https://github.com/get-drexa/drive.git
synced 2025-11-30 21:41:39 +00:00
refactor: replace KeyMode with ShouldPersistKey
This commit is contained in:
@@ -2,13 +2,6 @@ package blob
|
|||||||
|
|
||||||
type Key string
|
type Key string
|
||||||
|
|
||||||
type KeyMode int
|
|
||||||
|
|
||||||
const (
|
|
||||||
KeyModeStable KeyMode = iota
|
|
||||||
KeyModeDerived
|
|
||||||
)
|
|
||||||
|
|
||||||
func (k Key) IsNil() bool {
|
func (k Key) IsNil() bool {
|
||||||
return k == ""
|
return k == ""
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ func NewFlatKeyResolver() *FlatKeyResolver {
|
|||||||
return &FlatKeyResolver{}
|
return &FlatKeyResolver{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *FlatKeyResolver) KeyMode() blob.KeyMode {
|
func (r *FlatKeyResolver) ShouldPersistKey() bool {
|
||||||
return blob.KeyModeStable
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *FlatKeyResolver) Resolve(ctx context.Context, node *Node) (blob.Key, error) {
|
func (r *FlatKeyResolver) Resolve(ctx context.Context, node *Node) (blob.Key, error) {
|
||||||
|
|||||||
@@ -17,8 +17,8 @@ func NewHierarchicalKeyResolver(db *bun.DB) *HierarchicalKeyResolver {
|
|||||||
return &HierarchicalKeyResolver{db: db}
|
return &HierarchicalKeyResolver{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *HierarchicalKeyResolver) KeyMode() blob.KeyMode {
|
func (r *HierarchicalKeyResolver) ShouldPersistKey() bool {
|
||||||
return blob.KeyModeDerived
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *HierarchicalKeyResolver) Resolve(ctx context.Context, node *Node) (blob.Key, error) {
|
func (r *HierarchicalKeyResolver) Resolve(ctx context.Context, node *Node) (blob.Key, error) {
|
||||||
|
|||||||
@@ -7,7 +7,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type BlobKeyResolver interface {
|
type BlobKeyResolver interface {
|
||||||
KeyMode() blob.KeyMode
|
// ShouldPersistKey returns true if the resolved key should be stored in node.BlobKey.
|
||||||
|
// Flat keys (e.g. UUIDs) return true - key is generated once and stored.
|
||||||
|
// Hierarchical keys return false - key is derived from path each time.
|
||||||
|
ShouldPersistKey() bool
|
||||||
Resolve(ctx context.Context, node *Node) (blob.Key, error)
|
Resolve(ctx context.Context, node *Node) (blob.Key, error)
|
||||||
ResolveDeletionKeys(ctx context.Context, node *Node, allKeys []blob.Key) (*DeletionPlan, error)
|
ResolveDeletionKeys(ctx context.Context, node *Node, allKeys []blob.Key) (*DeletionPlan, error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ func (vfs *VirtualFS) CreateFile(ctx context.Context, userID uuid.UUID, opts Cre
|
|||||||
Name: opts.Name,
|
Name: opts.Name,
|
||||||
}
|
}
|
||||||
|
|
||||||
if vfs.keyResolver.KeyMode() == blob.KeyModeStable {
|
if vfs.keyResolver.ShouldPersistKey() {
|
||||||
node.BlobKey, err = vfs.keyResolver.Resolve(ctx, &node)
|
node.BlobKey, err = vfs.keyResolver.Resolve(ctx, &node)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -184,7 +184,7 @@ func (vfs *VirtualFS) WriteFile(ctx context.Context, node *Node, content FileCon
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if vfs.keyResolver.KeyMode() == blob.KeyModeStable {
|
if vfs.keyResolver.ShouldPersistKey() {
|
||||||
node.BlobKey = key
|
node.BlobKey = key
|
||||||
setCols = append(setCols, "blob_key")
|
setCols = append(setCols, "blob_key")
|
||||||
}
|
}
|
||||||
@@ -360,7 +360,7 @@ func (vfs *VirtualFS) MoveNode(ctx context.Context, node *Node, parentID uuid.UU
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if vfs.keyResolver.KeyMode() == blob.KeyModeStable {
|
if vfs.keyResolver.ShouldPersistKey() {
|
||||||
node.BlobKey = newKey
|
node.BlobKey = newKey
|
||||||
_, err = vfs.db.NewUpdate().Model(node).
|
_, err = vfs.db.NewUpdate().Model(node).
|
||||||
WherePK().
|
WherePK().
|
||||||
|
|||||||
Reference in New Issue
Block a user