public void TestTempFileEviction()
        {
            DefaultDiskStorage storage = GetStorageSupplier(1).Get();

            string    resourceId1 = "resource1";
            IInserter inserter    = storage.Insert(resourceId1, null);
            FileInfo  tempFile    = ((DefaultDiskStorage.InserterImpl)inserter)._temporaryFile;

            // Make sure that we don't evict a recent temp file
            PurgeUnexpectedFiles(storage);
            Assert.IsTrue(tempFile.Exists);

            // Mark it old, then try eviction again. It should be gone.
            try
            {
                tempFile.LastWriteTime = _clock.Now.Subtract(
                    TimeSpan.FromMilliseconds(DefaultDiskStorage.TEMP_FILE_LIFETIME_MS + 1000));
            }
            catch (Exception)
            {
                throw new IOException("Unable to update timestamp of file: " + tempFile);
            }

            PurgeUnexpectedFiles(storage);
            Assert.IsFalse(tempFile.Exists);
        }
        public void TestDirectoryIsNotNuked()
        {
            Assert.AreEqual(0, _directory.ListFiles().Length);

            DefaultDiskStorage storage    = GetStorageSupplier(1).Get();
            string             resourceId = "file1";

            byte[] CONTENT = Encoding.UTF8.GetBytes("content");

            // Create a file so we know version directory really exists
            IInserter inserter = storage.Insert(resourceId, null);

            WriteToResource(inserter, CONTENT);
            inserter.Commit(null);

            // Assign some previous date to the "now" used for file creation
            long lastModified = _directory.LastWriteTime.Ticks / TimeSpan.TicksPerMillisecond - 1000;

            _directory.LastWriteTime = new DateTime(lastModified * TimeSpan.TicksPerMillisecond);

            // Check it was changed
            Assert.AreEqual(lastModified * TimeSpan.TicksPerMillisecond, _directory.LastWriteTime.Ticks);

            // Create again, it shouldn't delete the directory
            GetStorageSupplier(1).Get();
            _directory.Refresh();

            // _directory exists...
            Assert.IsTrue(_directory.Exists);

            // And it's the same as before
            Assert.AreEqual(lastModified * TimeSpan.TicksPerMillisecond, _directory.LastWriteTime.Ticks);
        }
Esempio n. 3
0
 public static IInserter GetRootInserter(IInserter inserter)
 {
     while (inserter is IReturnParentInserter)
     {
         inserter = (inserter as IReturnParentInserter).GetParentInserter();
     }
     return(inserter);
 }
Esempio n. 4
0
 public static IInserter GetRootInserter(IInserter inserter)
 {
     while (inserter is IReturnParentInserter)
     {
         inserter = (inserter as IReturnParentInserter).GetParentInserter();
     }
     return inserter;
 }
Esempio n. 5
0
 protected void Remove(IInserter inserter)
 {
     foreach (var entry in m_insertersByKey.Where(kvp => kvp.Value == inserter).ToList())
     {
         m_insertersByKey.Remove(entry);
     }
     m_insertersInOrderAdded.Remove(inserter);
 }
        private static FileBinaryResource WriteToStorage(
            DefaultDiskStorage storage,
            string resourceId,
            byte[] value)
        {
            IInserter inserter = storage.Insert(resourceId, null);

            WriteToResource(inserter, value);
            return((FileBinaryResource)inserter.Commit(null));
        }
        /// <summary>
        /// Inserts resource into file with key.
        /// </summary>
        /// <param name="key">Cache key.</param>
        /// <param name="callback">
        /// Callback that writes to an output stream.
        /// </param>
        /// <returns>A sequence of bytes.</returns>
        public IBinaryResource Insert(ICacheKey key, IWriterCallback callback)
        {
            // Write to a temp file, then move it into place.
            // This allows more parallelism when writing files.
            SettableCacheEvent cacheEvent = SettableCacheEvent.Obtain().SetCacheKey(key);

            _cacheEventListener.OnWriteAttempt(cacheEvent);
            string resourceId;

            lock (_lock)
            {
                // For multiple resource ids associated with the same image,
                // we only write one file
                resourceId = CacheKeyUtil.GetFirstResourceId(key);
            }

            cacheEvent.SetResourceId(resourceId);

            try
            {
                // Getting the file is synchronized
                IInserter inserter = StartInsert(resourceId, key);

                try
                {
                    inserter.WriteData(callback, key);

                    // Committing the file is synchronized
                    IBinaryResource resource = EndInsert(inserter, key, resourceId);
                    cacheEvent.SetItemSize(resource.GetSize())
                    .SetCacheSize(_cacheStats.Size);

                    _cacheEventListener.OnWriteSuccess(cacheEvent);
                    return(resource);
                }
                finally
                {
                    if (!inserter.CleanUp())
                    {
                        Debug.WriteLine("Failed to delete temp file");
                    }
                }
            }
            catch (IOException ioe)
            {
                cacheEvent.SetException(ioe);
                _cacheEventListener.OnWriteException(cacheEvent);
                Debug.WriteLine("Failed inserting a file into the cache");
                throw;
            }
            finally
            {
                cacheEvent.Recycle();
            }
        }
 private static void WriteToResource(
     IInserter inserter,
     byte[] content)
 {
     inserter.WriteData(
         new WriterCallbackImpl(
             os =>
     {
         os.Write(content, 0, content.Length);
     }),
         null);
 }
 /// <summary>
 /// Commits the provided temp file to the cache, renaming it to match
 /// the cache's hashing convention.
 /// </summary>
 private IBinaryResource EndInsert(
     IInserter inserter,
     ICacheKey key,
     string resourceId)
 {
     lock (_lock)
     {
         IBinaryResource resource = inserter.Commit(key);
         _resourceIndex.Add(resourceId);
         _cacheStats.Increment(resource.GetSize(), 1);
         return(resource);
     }
 }
Esempio n. 10
0
        public Table(string name, ISchema schema, int startRowId)
        {
            SetTableName(name);
            mSchema = schema;
            Schema.SetTable(this, mSchema);

            if (mSchema != null)
            {
                hasErrors = false;

                indexer   = GetIndexer(schema);
                cache     = GetTableCache(startRowId);
                validator = GetValidator(schema);
                inserter  = GetInserter(schema, cache);
            }
        }
Esempio n. 11
0
            internal static GivenStep Given(Scenario scenario, IInserter inserter)
            {
                var rootInserter = InserterUtil.GetRootInserter(inserter);

                if (!Object.ReferenceEquals(rootInserter, inserter))
                {
                    scenario.Step("Given(IInserter({0})) rootInserter -to-> IInserter({1})", inserter.GetType().Name, rootInserter.GetType().Name);
                }
                else
                {
                    scenario.Step("Given(IInserter({0}))", rootInserter.GetType().Name);
                }
                scenario.InjectDependencies(rootInserter);

                scenario.InvokeOrFail(rootInserter.Insert);
                return(new GivenStep(scenario));
            }
        private static FileInfo Write(
            DefaultDiskStorage storage,
            string resourceId,
            byte[] content)
        {
            IInserter  inserter = storage.Insert(resourceId, null);
            FileInfo   file     = ((DefaultDiskStorage.InserterImpl)inserter)._temporaryFile;
            FileStream fos      = file.Create();

            try
            {
                fos.Write(content, 0, content.Length);
            }
            finally
            {
                fos.Dispose();
            }

            return(((FileBinaryResource)inserter.Commit(null)).File);
        }
Esempio n. 13
0
 /// <summary>
 /// Invoke an inserter
 /// </summary>
 /// <returns>the next step</returns>
 public GivenStep Given(IInserter inserter)
 {
     return(Givens.Given(this, inserter));
 }
Esempio n. 14
0
 /// <summary>
 /// Invoke an inserter
 /// </summary>
 /// <returns>the next step</returns>
 public GivenStep Given(IInserter inserter)
 {
     return(Givens.Given(Scenario, inserter));
 }