Merge branch '12483-writable-fs'
[arvados.git] / services / keep-web / cache.go
index c29ae7d43c270b129c0bf459aab25df927dc0407..9ee99903c8d1e537d487a67d1c77d848fc93c807 100644 (file)
@@ -16,6 +16,7 @@ import (
 
 type cache struct {
        TTL                  arvados.Duration
+       UUIDTTL              arvados.Duration
        MaxCollectionEntries int
        MaxCollectionBytes   int64
        MaxPermissionEntries int
@@ -85,6 +86,29 @@ func (c *cache) Stats() cacheStats {
        }
 }
 
+// Update saves a modified version (fs) to an existing collection
+// (coll) and, if successful, updates the relevant cache entries so
+// subsequent calls to Get() reflect the modifications.
+func (c *cache) Update(client *arvados.Client, coll arvados.Collection, fs arvados.CollectionFileSystem) error {
+       c.setupOnce.Do(c.setup)
+
+       if m, err := fs.MarshalManifest("."); err != nil || m == coll.ManifestText {
+               return err
+       } else {
+               coll.ManifestText = m
+       }
+       var updated arvados.Collection
+       defer c.pdhs.Remove(coll.UUID)
+       err := client.RequestAndDecode(&updated, "PATCH", "/arvados/v1/collections/"+coll.UUID, client.UpdateBody(coll), nil)
+       if err == nil {
+               c.collections.Add(client.AuthToken+"\000"+coll.PortableDataHash, &cachedCollection{
+                       expire:     time.Now().Add(time.Duration(c.TTL)),
+                       collection: &updated,
+               })
+       }
+       return err
+}
+
 func (c *cache) Get(arv *arvadosclient.ArvadosClient, targetID string, forceReload bool) (*arvados.Collection, error) {
        c.setupOnce.Do(c.setup)
 
@@ -135,13 +159,12 @@ func (c *cache) Get(arv *arvadosclient.ArvadosClient, targetID string, forceRelo
                        return nil, err
                }
                if current.PortableDataHash == pdh {
-                       exp := time.Now().Add(time.Duration(c.TTL))
                        c.permissions.Add(permKey, &cachedPermission{
-                               expire: exp,
+                               expire: time.Now().Add(time.Duration(c.TTL)),
                        })
                        if pdh != targetID {
                                c.pdhs.Add(targetID, &cachedPDH{
-                                       expire: exp,
+                                       expire: time.Now().Add(time.Duration(c.UUIDTTL)),
                                        pdh:    pdh,
                                })
                        }
@@ -167,7 +190,7 @@ func (c *cache) Get(arv *arvadosclient.ArvadosClient, targetID string, forceRelo
                expire: exp,
        })
        c.pdhs.Add(targetID, &cachedPDH{
-               expire: exp,
+               expire: time.Now().Add(time.Duration(c.UUIDTTL)),
                pdh:    collection.PortableDataHash,
        })
        c.collections.Add(arv.ApiToken+"\000"+collection.PortableDataHash, &cachedCollection{