public async Task ReplaceRelativePathAndUpdateLastDeletion(ICacheDatabaseRecord record,
                                                                   string movedRelativePath,
                                                                   DateTime lastDeletionAttempt)
        {
            var newRecord = new CacheDatabaseRecord()
            {
                AccessCountKey      = record.AccessCountKey,
                ContentType         = record.ContentType,
                CreatedAt           = record.CreatedAt,
                DiskSize            = record.DiskSize,
                LastDeletionAttempt = lastDeletionAttempt,
                RelativePath        = movedRelativePath
            };

            var loadedDict = await GetLoadedDict();

            if (loadedDict.TryAdd(movedRelativePath, newRecord))
            {
                await writeLog.LogCreated(newRecord);
            }
            else
            {
                throw new InvalidOperationException("Record already moved in database");
            }
            await DeleteRecord(record);
        }
        public async Task <bool> CreateRecordIfSpace(string relativePath, string contentType, long recordDiskSpace,
                                                     DateTime createdDate,
                                                     int accessCountKey, long diskSpaceLimit)
        {
            // Lock so multiple writers can't get past the capacity check simultaneously
            using (await createLock.LockAsync())
            {
                var loadedDict = await GetLoadedDict();

                var extraLogBytes = GetLogBytesOverhead(relativePath.Length + (contentType?.Length ?? 0));

                var existingDiskUsage = await GetShardSize();

                if (existingDiskUsage + recordDiskSpace + extraLogBytes > diskSpaceLimit)
                {
                    //logger?.LogInformation("Refusing new {RecordDiskSpace} byte record in shard {ShardId} of MetaStore",
                    //    recordDiskSpace, shardId);
                    return(false);
                }

                var newRecord = new CacheDatabaseRecord()
                {
                    AccessCountKey      = accessCountKey,
                    ContentType         = contentType,
                    CreatedAt           = createdDate,
                    DiskSize            = recordDiskSpace,
                    LastDeletionAttempt = DateTime.MinValue,
                    RelativePath        = relativePath
                };

                if (loadedDict.TryAdd(relativePath, newRecord))
                {
                    await writeLog.LogCreated(newRecord);
                }
                else
                {
                    logger?.LogWarning("CreateRecordIfSpace did nothing - database entry already exists - {Path}", relativePath);
                }
            }

            return(true);
        }
 public Task LogUpdated(CacheDatabaseRecord updatedRecord)
 {
     return(WriteLogEntry(new LogEntry(LogEntryType.Update, updatedRecord), false));
 }
 public Task LogCreated(CacheDatabaseRecord newRecord)
 {
     // We flush created entries through to disk right away so we don't orphan disk blobs
     return(WriteLogEntry(new LogEntry(LogEntryType.Create, newRecord), true));
 }