Making a file or folder, or a server-side copy, forgot the cached listing of its folder, so the next upload into that folder - which checks the name is free - listed the whole folder again. Copying M new files into a folder of N cost on the order of M*N/100 extra requests. The new node is now added to the cached listing. A failed call still forgets it, as the call may have taken effect. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
148 lines
3.3 KiB
Go
148 lines
3.3 KiB
Go
package protondrive
|
|
|
|
import (
|
|
"slices"
|
|
"sync"
|
|
)
|
|
|
|
// nodeCache holds decrypted nodes and folder listings in memory.
|
|
//
|
|
// A nil nodeCache, as newNodeCache returns when caching is disabled,
|
|
// holds nothing: its lookups miss and its updates do nothing. Cached
|
|
// nodes are shared and must not be modified.
|
|
type nodeCache struct {
|
|
mu sync.Mutex
|
|
nodes map[string]*node // by ID
|
|
listings map[string][]string // IDs of the complete listing of a folder, by folder ID
|
|
}
|
|
|
|
// newNodeCache returns an empty cache, or nil if enabled is false.
|
|
func newNodeCache(enabled bool) *nodeCache {
|
|
if !enabled {
|
|
return nil
|
|
}
|
|
return &nodeCache{
|
|
nodes: make(map[string]*node),
|
|
listings: make(map[string][]string),
|
|
}
|
|
}
|
|
|
|
// get returns the node id, or nil if it isn't cached.
|
|
func (c *nodeCache) get(id string) *node {
|
|
if c == nil {
|
|
return nil
|
|
}
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
return c.nodes[id]
|
|
}
|
|
|
|
// put caches n, replacing any node with its ID. If the cached node had
|
|
// another parent, the listings of both parents are forgotten.
|
|
func (c *nodeCache) put(n *node) {
|
|
if c == nil {
|
|
return
|
|
}
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
if old := c.nodes[n.ID]; old != nil && old.ParentID != n.ParentID {
|
|
delete(c.listings, old.ParentID)
|
|
delete(c.listings, n.ParentID)
|
|
}
|
|
c.nodes[n.ID] = n
|
|
}
|
|
|
|
// children returns the cached listing of folder parentID. It reports
|
|
// false if the listing, or any node in it, isn't cached.
|
|
func (c *nodeCache) children(parentID string) ([]*node, bool) {
|
|
if c == nil {
|
|
return nil, false
|
|
}
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
ids, ok := c.listings[parentID]
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
out := make([]*node, 0, len(ids))
|
|
for _, id := range ids {
|
|
n := c.nodes[id]
|
|
if n == nil {
|
|
return nil, false
|
|
}
|
|
out = append(out, n)
|
|
}
|
|
return out, true
|
|
}
|
|
|
|
// setChildren caches nodes as the complete listing of folder parentID,
|
|
// and each of the nodes.
|
|
func (c *nodeCache) setChildren(parentID string, nodes []*node) {
|
|
if c == nil {
|
|
return
|
|
}
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
ids := make([]string, len(nodes))
|
|
for i, n := range nodes {
|
|
ids[i] = n.ID
|
|
c.nodes[n.ID] = n
|
|
}
|
|
c.listings[parentID] = ids
|
|
}
|
|
|
|
// add caches n, a node new to its parent, and adds it to the cached
|
|
// listing of its parent, if there is one.
|
|
func (c *nodeCache) add(n *node) {
|
|
if c == nil {
|
|
return
|
|
}
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
if old := c.nodes[n.ID]; old != nil && old.ParentID != n.ParentID {
|
|
delete(c.listings, old.ParentID)
|
|
delete(c.listings, n.ParentID)
|
|
}
|
|
c.nodes[n.ID] = n
|
|
if ids, ok := c.listings[n.ParentID]; ok && !slices.Contains(ids, n.ID) {
|
|
c.listings[n.ParentID] = append(ids, n.ID)
|
|
}
|
|
}
|
|
|
|
// invalidate forgets the node id, its listing if it is a folder, and
|
|
// the listing of its parent.
|
|
func (c *nodeCache) invalidate(id string) {
|
|
if c == nil {
|
|
return
|
|
}
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
if n := c.nodes[id]; n != nil {
|
|
delete(c.listings, n.ParentID)
|
|
}
|
|
delete(c.nodes, id)
|
|
delete(c.listings, id)
|
|
}
|
|
|
|
// invalidateChildren forgets the listing of folder parentID, but not
|
|
// the nodes in it.
|
|
func (c *nodeCache) invalidateChildren(parentID string) {
|
|
if c == nil {
|
|
return
|
|
}
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
delete(c.listings, parentID)
|
|
}
|
|
|
|
// clear forgets everything.
|
|
func (c *nodeCache) clear() {
|
|
if c == nil {
|
|
return
|
|
}
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
clear(c.nodes)
|
|
clear(c.listings)
|
|
}
|