Ejemplo n.º 1
0
        public void Add(string key, Stream data)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException();
            }

            if (data == null)
            {
                throw new ArgumentNullException();
            }

            try {
                if (!currentWritings.TryAdd(key, null))
                {
                    throw new ArgumentException();
                }

                if (index.ContainsKey(key))
                {
                    throw new ArgumentException();
                }

                Int64 positionToWrite;
                lock (seekLock) {
                    positionToWrite = storageStream.Length;
                    storageStream.SetLength(storageStream.Length + data.Length);
                }

                byte[] md5;
                using (var writeStream = new FileStream(storageFile, FileMode.Open, FileAccess.Write, FileShare.ReadWrite, writeBufferSize, FileOptions.Asynchronous | FileOptions.SequentialScan | FileOptions.WriteThrough)) {
                    writeStream.Seek(positionToWrite, SeekOrigin.Begin);
                    md5 = data.CopyToWithMD5(writeStream, writeBufferSize);
                    writeStream.Flush(true);
                }

                index.Add(key, new Data {
                    Position = positionToWrite, Length = data.Length, MD5 = md5
                });
            }
            finally {
                object value;
                currentWritings.TryRemove(key, out value);
            }
        }
Ejemplo n.º 2
0
        public void TestCommonConfiguration()
        {
            BPlusTree <KeyInfo, DataValue> .Options options =
                new BPlusTree <KeyInfo, DataValue> .Options(new KeyInfoSerializer(), new DataValueSerializer(), new KeyInfoComparer());

            options.CalcBTreeOrder(32, 300);          //we can simply just guess close
            options.FileName   = TempFile.TempPath;
            options.CreateFile = CreatePolicy.Always; //obviously this is just for testing
            Assert.AreEqual(FileVersion.Version1, options.FileVersion);

            Random  rand = new Random();
            KeyInfo k1 = new KeyInfo(), k2 = new KeyInfo();

            using (BPlusTree <KeyInfo, DataValue> tree = new BPlusTree <KeyInfo, DataValue>(options))
            {
                byte[] data = new byte[255];

                rand.NextBytes(data);
                tree.Add(k1, new DataValue(k1, data));

                Assert.IsTrue(tree.ContainsKey(k1));
                Assert.IsFalse(tree.ContainsKey(k1.Next()));
                Assert.AreEqual(data, tree[k1].Bytes);

                rand.NextBytes(data);
                tree.Add(k2, new DataValue(k2, data));

                Assert.IsTrue(tree.ContainsKey(k2));
                Assert.IsFalse(tree.ContainsKey(k2.Next()));
                Assert.AreEqual(data, tree[k2].Bytes);
            }
            options.CreateFile = CreatePolicy.Never;
            using (BPlusTree <KeyInfo, DataValue> tree = new BPlusTree <KeyInfo, DataValue>(options))
            {
                Assert.IsTrue(tree.ContainsKey(k1));
                Assert.IsTrue(tree.ContainsKey(k2));
            }
        }
 void UpdateStuff(BPlusTree<Guid, TestInfo> tree)
 {
     while (!mreStop.WaitOne(0, false))
     {
         foreach (var pair in tree)
         {
             bool updated = tree.TryUpdate(pair.Key, (k, v) => { v.UpdateCount++; return v; });
             if (!updated && tree.ContainsKey(pair.Key))
                 throw new ApplicationException();
         }
     }
 }
Ejemplo n.º 4
0
 public bool ContainsKey(string key)
 {
     return(_index.ContainsKey(key));
 }