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 KeyMode int
|
||||
|
||||
const (
|
||||
KeyModeStable KeyMode = iota
|
||||
KeyModeDerived
|
||||
)
|
||||
|
||||
func (k Key) IsNil() bool {
|
||||
return k == ""
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ func NewFlatKeyResolver() *FlatKeyResolver {
|
||||
return &FlatKeyResolver{}
|
||||
}
|
||||
|
||||
func (r *FlatKeyResolver) KeyMode() blob.KeyMode {
|
||||
return blob.KeyModeStable
|
||||
func (r *FlatKeyResolver) ShouldPersistKey() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
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}
|
||||
}
|
||||
|
||||
func (r *HierarchicalKeyResolver) KeyMode() blob.KeyMode {
|
||||
return blob.KeyModeDerived
|
||||
func (r *HierarchicalKeyResolver) ShouldPersistKey() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *HierarchicalKeyResolver) Resolve(ctx context.Context, node *Node) (blob.Key, error) {
|
||||
|
||||
@@ -7,7 +7,10 @@ import (
|
||||
)
|
||||
|
||||
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)
|
||||
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,
|
||||
}
|
||||
|
||||
if vfs.keyResolver.KeyMode() == blob.KeyModeStable {
|
||||
if vfs.keyResolver.ShouldPersistKey() {
|
||||
node.BlobKey, err = vfs.keyResolver.Resolve(ctx, &node)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -184,7 +184,7 @@ func (vfs *VirtualFS) WriteFile(ctx context.Context, node *Node, content FileCon
|
||||
return err
|
||||
}
|
||||
|
||||
if vfs.keyResolver.KeyMode() == blob.KeyModeStable {
|
||||
if vfs.keyResolver.ShouldPersistKey() {
|
||||
node.BlobKey = key
|
||||
setCols = append(setCols, "blob_key")
|
||||
}
|
||||
@@ -360,7 +360,7 @@ func (vfs *VirtualFS) MoveNode(ctx context.Context, node *Node, parentID uuid.UU
|
||||
return err
|
||||
}
|
||||
|
||||
if vfs.keyResolver.KeyMode() == blob.KeyModeStable {
|
||||
if vfs.keyResolver.ShouldPersistKey() {
|
||||
node.BlobKey = newKey
|
||||
_, err = vfs.db.NewUpdate().Model(node).
|
||||
WherePK().
|
||||
|
||||
Reference in New Issue
Block a user