Exemplo n.º 1
0
        public void Deduplication()
        {
            _driver.Write(Arg.Any <string>()).Returns(x => new MemoryStream());

            var index = new XmlStorageIndex(_driver);

            _driver.Exists(Arg.Any <string>()).Returns(true);
            _driver.GetLength(Arg.Is("f1")).Returns(3);
            _driver.GetLength(Arg.Is("f3")).Returns(3);
            _driver.GetLength(Arg.Is("f4")).Returns(3);
            _driver.Read(Arg.Is("f1")).Returns(new MemoryStream(new byte[] { 1, 2, 3 }));
            _driver.Read(Arg.Is("f3")).Returns(new MemoryStream(new byte[] { 1, 2, 3 }));
            _driver.Read(Arg.Is("f4")).Returns(new MemoryStream(new byte[] { 1, 2, 3 })
            {
                Position = 1
            });

            var storage = new XmlStorage(_driver, index);

            index.Add(new StorageItem {
                FileName = "f1", Url = "url1"
            });
            index.Add(new StorageItem {
                FileName = "f2", Url = "url2"
            });
            index.Add(new StorageItem {
                FileName = "f3", Url = "url3"
            });
            index.Add(new StorageItem {
                FileName = "f4", Url = "url4"
            });
            storage.Deduplication();

            Assert.IsTrue(index.Items.Any(x => x.FileName == "f2"));
            Assert.IsTrue(
                index.Items.Count(x => x.FileName == "f1") == 3 ||
                index.Items.Count(x => x.FileName == "f3") == 3 ||
                index.Items.Count(x => x.FileName == "f4") == 3
                );
        }
Exemplo n.º 2
0
        public void Deduplication()
        {
            var changedItems = new List <int>();
            var items        = _index.Items
                               .Where(item => _driver.Exists(item.FileName))
                               .ToDictionary(item => item, item => _driver.GetLength(item.FileName));

            var length = items.Count;

            for (int i = 0; i < length; i++)
            {
                if (changedItems.Contains(i))
                {
                    continue;
                }
                var item1 = items.ElementAt(i);

                using (var s1 = _driver.Read(item1.Key.FileName))
                {
                    for (int j = i + 1; j < length; j++)
                    {
                        if (changedItems.Contains(j))
                        {
                            continue;
                        }

                        var item2 = items.ElementAt(j);

                        if (item1.Value != item2.Value)
                        {
                            continue;
                        }

                        using (var s2 = _driver.Read(item2.Key.FileName))
                        {
                            if (!FileStreamEquals.Equals(s1, s2))
                            {
                                continue;
                            }
                        }

                        _driver.Remove(item2.Key.FileName);
                        item2.Key.FileName = item1.Key.FileName;

                        changedItems.Add(j);
                    }
                }
                _index.Save();
            }
            _index.Save();
        }
 public long GetLength(string name)
 {
     return(_driver.GetLength(GetNameWithExtention(name)));
 }