public void TestTransactBlock()
        {
            SharedMemoryStream shared = new SharedMemoryStream();

            FragmentedFile.CreateNew(shared, 512, 100, 2).Dispose();

            using (FragmentedFile ff = new FragmentedFile(shared, 512, 100, 2))
            {
                long   id;
                byte[] orig = MakeBytes(255);
                using (Stream write = ff.Create(out id))
                    write.Write(orig, 0, orig.Length);

                Assert.AreEqual(orig, IOStream.ReadAllBytes(ff.Open(id, FileAccess.Read)));

                byte[] change = MakeBytes(800);
                using (Stream write = ff.Open(id, FileAccess.Write))
                    using (ITransactable trans = (ITransactable)write) //the Fragmented File Streams are ITransactable
                    {
                        write.Write(change, 0, change.Length);
                        Assert.AreEqual(orig, IOStream.ReadAllBytes(ff.Open(id, FileAccess.Read)));

                        trans.Commit(); //commit changes so that readers can read
                        Assert.AreEqual(change, IOStream.ReadAllBytes(ff.Open(id, FileAccess.Read)));

                        trans.Rollback(); //rollback even after commit to 'undo' the changes
                        Assert.AreEqual(orig, IOStream.ReadAllBytes(ff.Open(id, FileAccess.Read)));
                    }                     //once disposed you can no longer rollback, if rollback has not been called commit is implied.

                Assert.AreEqual(orig, IOStream.ReadAllBytes(ff.Open(id, FileAccess.Read)));
            }
        }
Ejemplo n.º 2
0
        public void SetContents(string path, HttpStatusCode status, string contentType, string etag, DateTime?modified, byte[] contents)
        {
            if (contents.Length == 0)
            {
                Console.Error.WriteLine("{0} - {1}  (Content is empty)", (int)status, path);
            }

            ContentRecord rec           = _data[path];
            ITransactable pendingUpdate = null;

            try
            {
                ContentRecord.Builder builder = rec.ToBuilder()
                                                .SetContentUri(path)
                                                .SetLastCrawled(CrawlTime)
                                                .SetLastValid(CrawlTime)
                                                .SetHttpStatus((uint)status)
                ;

                builder.ClearContentRedirect();
                builder.SetContentType(contentType);
                builder.SetContentLength((uint)contents.Length);
                if (!String.IsNullOrEmpty(etag))
                {
                    builder.SetETag(etag);
                }

                string hash = Hash.SHA256(contents).ToString();
                if (hash != builder.HashOriginal)
                {
                    Modified = true;
                    builder.SetHashOriginal(hash);
                    builder.SetDateModified(CrawlTime);
                    pendingUpdate = _data.WriteContent(builder, contents);
                }

                if (_data.AddOrUpdate(path, rec = builder.Build()))
                {
                    if (pendingUpdate != null)
                    {
                        pendingUpdate.Commit();
                        pendingUpdate.Dispose();
                        pendingUpdate = null;
                    }
                }
            }
            finally
            {
                if (pendingUpdate != null)
                {
                    pendingUpdate.Rollback();
                    pendingUpdate.Dispose();
                }
            }

            ProcessFileContent(rec, contents);
        }
Ejemplo n.º 3
0
            public void Rollback()
            {
                ITransactable tstore = _store as ITransactable;

                if (tstore != null)
                {
                    _serializer = null;
                    ClearCache();
                    tstore.Rollback();
                }
            }
            public void Rollback()
            {
                ITransactable tstore = _store as ITransactable;

                if (tstore != null)
                {
                    lock (_flushSync) // disallow concurrent async flush
                    {
                        _serializer = null;
                        ClearCache();
                        tstore.Rollback();
                    }
                }
            }
            public void Rollback()
            {
                ITransactable tstore = _store as ITransactable;

                if (tstore != null)
                {
                    using (_lock.Write())
                    {
                        _ordered.Clear();
                        _cache.Clear();
                        tstore.Rollback();
                    }
                }
            }
        public void TestRollbackCreate()
        {
            SharedMemoryStream shared = new SharedMemoryStream();

            FragmentedFile.CreateNew(shared, 512, 100, 2).Dispose();

            using (FragmentedFile ff = new FragmentedFile(shared, 512, 100, 2))
            {
                long   id;
                byte[] bytes = MakeBytes(255);
                using (Stream write = ff.Create(out id))
                    using (ITransactable trans = (ITransactable)write)
                    {
                        write.Write(bytes, 0, bytes.Length);
                        trans.Commit();
                        Assert.AreEqual(bytes, IOStream.ReadAllBytes(ff.Open(id, FileAccess.Read)));
                        trans.Rollback();
                    }

                AssertThrows <InvalidDataException>(delegate() { ff.Open(id, FileAccess.Read); });
            }
        }