Exemplo n.º 1
0
        public void HeaderPage_Savepoint()
        {
            var data   = new byte[Constants.PAGE_SIZE];
            var buffer = new PageBuffer(data, 0, 1);

            // mark buffer as writable (debug propose)
            buffer.ShareCounter = Constants.BUFFER_WRITABLE;

            // create new header page
            var header = new HeaderPage(buffer, 0);

            header.InsertCollection("my-col1", 1);
            header.InsertCollection("my-col2", 2);

            header.GetCollections().Count().Should().Be(2);

            // savepoint alse execute UpdateBuffer
            var sp = header.Savepoint();

            header.DeleteCollection("my-col1");
            header.DeleteCollection("my-col2");

            header.UpdateBuffer();

            header.GetCollections().Count().Should().Be(0);

            // now, restore header
            header.Restore(sp);

            header.GetCollections().Count().Should().Be(2);

            buffer.ShareCounter = 0;
        }
Exemplo n.º 2
0
        public void HeaderPage_Collections()
        {
            var data   = new byte[Constants.PAGE_SIZE];
            var buffer = new PageBuffer(data, 0, 1);

            // mark buffer as writable (debug propose)
            buffer.ShareCounter = Constants.BUFFER_WRITABLE;

            // create new header page
            var header = new HeaderPage(buffer, 0);

            header.InsertCollection("my-col1", 1);
            header.InsertCollection("my-col2", 2);

            header.GetCollections().Count().Should().Be(2);
            ((int)header.GetCollectionPageID("my-col1")).Should().Be(1);
            ((int)header.GetCollectionPageID("my-col2")).Should().Be(2);
            header.GetAvaiableCollectionSpace().Should().Be(7981);

            header.UpdateBuffer();

            // read header
            var h2 = new HeaderPage(buffer);

            h2.GetCollections().Count().Should().Be(2);
            ((int)h2.GetCollectionPageID("my-col1")).Should().Be(1);
            ((int)h2.GetCollectionPageID("my-col2")).Should().Be(2);
            h2.GetAvaiableCollectionSpace().Should().Be(7981);

            buffer.ShareCounter = 0;
        }
Exemplo n.º 3
0
        public void CacheAsync_Thread_ShareCounter()
        {
            // Set()   - Seta true - Se estiver bloqueado, vai liberar
            // Reset() - Seta false - Quando chegar no proximo Wait() vai aguardar
            // Wait()  - Trava a thread SE estiver false (Reset) - Passa reto se estiver true (Set)
            var wa = new ManualResetEventSlim(true);
            var wb = new ManualResetEventSlim(false);

            // serialize 2 threads
            void serialize(ManualResetEventSlim toBlock, ManualResetEventSlim toFree)
            {
                toBlock?.Reset();
                toFree.Set();
                toBlock?.Wait();
            }

            ;

            var disk = new DiskService(new EngineSettings {
                DataStream = new MemoryStream(), MemorySegmentSize = 10
            });

            var ta = new Task(() =>
            {
                var r = disk.GetReader();
                wa.Wait();

                // test starts here!!!
                var p0 = new HeaderPage(r.NewPage(), 0);

                p0.UserVersion = 25;

                disk.WriteAsync(new PageBuffer[] { p0.UpdateBuffer() });

                // (1 ->) jump to thread B
                serialize(wa, wb);
                // (2 <-) continue from thread B

                disk.Queue.Wait();

                // (3 ->) jump to thread B
                serialize(wa, wb);
            });

            var tb = new Task(() =>
            {
                var r = disk.GetReader();
                wb.Wait();

                // (1 <-) continue from thread A
                var p0 = r.ReadPage(0, false, FileOrigin.Log);

                // share counter can be 2 or 3
                // - if 2, page was not persisted yet on disk (async)
                // - if 1, page already persisted on disk
                var share = p0.ShareCounter;

                (share >= 1 && share <= 2).Should().BeTrue();

                // (2 ->) jump to thread A
                serialize(wb, wa);
                // (3 <-) continue from thread B

                // but now, I'm sure this page was saved and thread A release
                p0.ShareCounter.Should().Be(1);

                // let's release my page
                p0.Release();

                p0.ShareCounter.Should().Be(0);

                // release thread A
                serialize(null, wa);
            });

            ta.Start();
            tb.Start();

            Task.WaitAll(ta, tb);
        }