Ejemplo n.º 1
0
        public void Put_and_Replace(ILRUCache <SimpleLRUCacheItem, int> c)
        {
            c.Put(new SimpleLRUCacheItem(10, "Dog"));
            var numItems = c.Count;

            Assert.AreEqual("Dog", c.Get(10).Value, "Cache did not contain key 10");
            c.Put(new SimpleLRUCacheItem(10, "Cat")); // Dog should be replaced with "Cat"
            Assert.AreEqual("Cat", c.Get(10).Value, "Cache did not contain key 10");
            Assert.AreEqual(numItems, c.Count, 0, "Cache size changed");
        }
Ejemplo n.º 2
0
 /// <param name="c">Expected to be empty, capacity constraint may impact performance</param>
 /// <param name="NumPuts"></param>
 public void ManyPuts(ILRUCache <SimpleLRUCacheItem, int> c, int NumPuts = 1000)
 {
     for (int i = 0; i < NumPuts; i++)
     {
         c.Put(new SimpleLRUCacheItem(i, i.ToString()));
     }
 }
Ejemplo n.º 3
0
        public void ParallelOperations(ILRUCache <SimpleLRUCacheItem, int> c, int NumThreads = 10)
        {
            int MaxKey  = NumThreads * 100;
            int NumGets = 1000;

            if (c.Capacity < MaxKey)
            {
                throw new ArgumentOutOfRangeException();
            }

            Parallel.For(0, NumThreads, t =>
            {
                var maxIndex = (t * 100) + 100;
                for (int i = (t * 100); i < maxIndex; i++)
                {
                    c.Put(new SimpleLRUCacheItem(i, i.ToString()));
                }
            });

            Assert.AreEqual(MaxKey, c.Count, 0, "Cache size incorrect");

            Parallel.For(0, NumThreads * 10, t =>
            {
                for (int i = 0; i < NumGets; i++)
                {
                    var k = this.random.Next(0, MaxKey);
                    var n = c.Get(k);
                    Assert.AreEqual(k.ToString(), n.Value, "Cache did not contain key.");
                }
            });
        }
Ejemplo n.º 4
0
 public void Optimize(TFChunk.TFChunk chunk)
 {
     if (!chunk.ChunkHeader.IsScavenged)
     {
         return;
     }
     _cache.Put(chunk.FileName, chunk);
 }
Ejemplo n.º 5
0
 static public void AddRainbowItems(ILRUCache <SimpleLRUCacheItem, int> c)
 {
     c.Put(new SimpleLRUCacheItem(0, "Red"));
     c.Put(new SimpleLRUCacheItem(1, "Orange"));
     c.Put(new SimpleLRUCacheItem(2, "Yellow"));
     c.Put(new SimpleLRUCacheItem(3, "Green"));
     c.Put(new SimpleLRUCacheItem(4, "Blue"));
     c.Put(new SimpleLRUCacheItem(5, "Indigo"));
     c.Put(new SimpleLRUCacheItem(6, "Violet"));
 }
Ejemplo n.º 6
0
 /// <param name="c">Expect the cache to be empty with a capacity of at least 10</param>
 public void ExpirationTest(ILRUCache <SimpleLRUCacheItem, int> c)
 {
     c.Put(new SimpleLRUCacheItem(1, "Cat", new TimeSpan(0, 0, 5))); // 5 second timespan
     Assert.AreEqual(1, c.Count);
     Thread.Sleep(1 * 1000);                                         // Sleep for 5 seconds.
     Assert.AreEqual(1, c.Count);
     Assert.AreEqual("Cat", c.Get(1).Value);
     Thread.Sleep(4 * 1000); // Sleep for 5 seconds.
     Assert.AreEqual(1, c.Count);
     Thread.Sleep(1 * 1000); // Sleep for 5 seconds.
     Assert.AreEqual(0, c.Count);
 }
Ejemplo n.º 7
0
        public void RemoveOneTest(ILRUCache <SimpleLRUCacheItem, int> c)
        {
            var dog = new SimpleLRUCacheItem(10, "Dog");

            c.Put(dog);
            var numItems = c.Count;

            Assert.AreEqual("Dog", c.Get(dog.Key).Value, "Cache did not contain dog");
            var wasRemoved = c.Remove(dog.Key);

            Assert.IsTrue(wasRemoved); // Was it successfully removed?
            try
            {
                var dog2 = c.Get(10); // Try to get dogs Key
                Assert.IsTrue(true, "Dog was found but not expected");
            } catch (KeyNotFoundException kex)
            {
                // OK!
            }
        }
Ejemplo n.º 8
0
        public void Commit(CommitLogRecord commit)
        {
            var lastCommitPosition = Interlocked.Read(ref _lastCommitPosition);

            if (commit.LogPosition < lastCommitPosition || (commit.LogPosition == lastCommitPosition && !_indexRebuild))
            {
                return;  // already committed
            }
            bool   first       = true;
            int    eventNumber = -1;
            uint   streamHash  = 0;
            string streamId    = null;

            foreach (var prepare in GetTransactionPrepares(commit.TransactionPosition))
            {
                if (first)
                {
                    streamHash = _hasher.Hash(prepare.EventStreamId);
                    streamId   = prepare.EventStreamId;
                    first      = false;
                }
                else
                {
                    Debug.Assert(prepare.EventStreamId == streamId);
                }

                bool addToIndex = false;
                if ((prepare.Flags & PrepareFlags.StreamDelete) != 0)
                {
                    eventNumber = EventNumber.DeletedStream;
                    _committedEvents.PutRecord(prepare.EventId, Tuple.Create(streamId, eventNumber), throwOnDuplicate: false);
                    addToIndex = commit.LogPosition > _persistedCommitCheckpoint ||
                                 commit.LogPosition == _persistedCommitCheckpoint && prepare.LogPosition > _persistedPrepareCheckpoint;
                }
                else if ((prepare.Flags & PrepareFlags.Data) != 0)
                {
                    eventNumber = commit.EventNumber + prepare.TransactionOffset;
                    _committedEvents.PutRecord(prepare.EventId, Tuple.Create(streamId, eventNumber), throwOnDuplicate: false);
                    addToIndex = commit.LogPosition > _persistedCommitCheckpoint ||
                                 commit.LogPosition == _persistedCommitCheckpoint && prepare.LogPosition > _persistedPrepareCheckpoint;
                }

                // could be just empty prepares for TransactionBegin and TransactionEnd, for instance
                // or records which are rebuilt but are already in PTables
                if (addToIndex)
                {
#if CHECK_COMMIT_DUPLICATES
                    long pos;
                    if (_tableIndex.TryGetOneValue(streamHash, eventNumber, out pos))
                    {
                        ReadEventResult res = ((IReadIndex)this).ReadEvent(eventStreamId, eventNumber);
                        if (res.Result == SingleReadResult.Success)
                        {
                            Debugger.Break();
                            throw new Exception(
                                      string.Format(
                                          "Trying to add duplicate event #{0} for stream {1}(hash {2})\nCommit: {3}\nPrepare: {4}.",
                                          eventNumber,
                                          eventStreamId,
                                          streamHash,
                                          commit,
                                          prepare));
                        }
                    }
#endif
                    _tableIndex.Add(commit.LogPosition, streamHash, eventNumber, prepare.LogPosition);
                    _bus.Publish(new StorageMessage.EventCommited(commit.LogPosition, eventNumber, prepare));
                }
            }

            if (first)
            {
                throw new Exception("No prepares for commit found!");
            }

            var newLastCommitPosition = commit.LogPosition > lastCommitPosition ? commit.LogPosition : lastCommitPosition;
            if (Interlocked.CompareExchange(ref _lastCommitPosition, newLastCommitPosition, lastCommitPosition) != lastCommitPosition)
            {
                throw new Exception("Concurrency error in ReadIndex.Commit: _lastCommitPosition was modified during Commit execution!");
            }

            _streamInfoCache.Put(streamId,
                                 key => new StreamCacheInfo(eventNumber, null),
                                 (key, old) => new StreamCacheInfo(eventNumber, old.Metadata));
        }