public void MergeWrites(ColaRangeSet<Slice> writes, ulong version)
		{
			Contract.Requires(!m_closed && writes != null && version >= m_minVersion && (!m_closed || version <= m_maxVersion));

			if (m_disposed) ThrowDisposed();
			if (m_closed) throw new InvalidOperationException("This transaction has already been closed");

			//Debug.WriteLine("* Merging writes conflicts for version " + version + ": " + String.Join(", ", writes));

			foreach (var range in writes)
			{
				var begin = range.Begin;
				var end = range.End;

				USlice beginKey, endKey;
				if (begin.Offset == end.Offset && object.ReferenceEquals(begin.Array, end.Array) && end.Count >= begin.Count)
				{ // overlapping keys
					endKey = Store(end);
					beginKey = endKey.Substring(0, (uint)begin.Count);
				}
				else
				{
					beginKey = Store(begin);
					endKey = Store(end);
				}

				m_writeConflicts.Mark(beginKey, endKey, version);
			}

			++m_commitCount;
			if (version > m_maxVersion)
			{
				m_maxVersion = version;
			}
		}
        public void Test_RangeSet_Insert_Partially_Overlapping()
        {
            var cola = new ColaRangeSet <int>();

            Assert.That(cola.Count, Is.EqualTo(0));

            cola.Mark(0, 1);
            cola.Debug_Dump();
            Assert.That(cola.Count, Is.EqualTo(1));

            cola.Mark(0, 2);
            cola.Debug_Dump();
            Assert.That(cola.Count, Is.EqualTo(1));

            cola.Mark(1, 3);
            cola.Debug_Dump();
            Assert.That(cola.Count, Is.EqualTo(1));

            cola.Mark(-1, 2);
            cola.Debug_Dump();
            Assert.That(cola.Count, Is.EqualTo(1));

            Console.WriteLine("Result = { " + String.Join(", ", cola) + " }");
            Console.WriteLine("Bounds = " + cola.Bounds);
        }
		public void Test_Empty_RangeSet()
		{
			var cola = new ColaRangeSet<int>(0, Comparer<int>.Default);
			Assert.That(cola.Count, Is.EqualTo(0));
			Assert.That(cola.Comparer, Is.SameAs(Comparer<int>.Default));
			Assert.That(cola.Capacity, Is.EqualTo(15), "Capacity should be the next power of 2, minus 1");
			Assert.That(cola.Bounds, Is.Not.Null);
			Assert.That(cola.Bounds.Begin, Is.EqualTo(0));
			Assert.That(cola.Bounds.End, Is.EqualTo(0));
		}
        public void Test_Empty_RangeSet()
        {
            var cola = new ColaRangeSet <int>(0, Comparer <int> .Default);

            Assert.That(cola.Count, Is.EqualTo(0));
            Assert.That(cola.Comparer, Is.SameAs(Comparer <int> .Default));
            Assert.That(cola.Capacity, Is.EqualTo(15), "Capacity should be the next power of 2, minus 1");
            Assert.That(cola.Bounds, Is.Not.Null);
            Assert.That(cola.Bounds.Begin, Is.EqualTo(0));
            Assert.That(cola.Bounds.End, Is.EqualTo(0));
        }
        public void Test_RangeSet_Insert_That_Join_Two_Ranges()
        {
            var cola = new ColaRangeSet <int>();

            cola.Mark(0, 1);
            cola.Mark(2, 3);
            cola.Debug_Dump();
            Assert.That(cola.Count, Is.EqualTo(2));

            cola.Mark(1, 2);
            cola.Debug_Dump();
            Assert.That(cola.Count, Is.EqualTo(1));

            Console.WriteLine("Result = { " + String.Join(", ", cola) + " }");
            Console.WriteLine("Bounds = " + cola.Bounds);
        }
        public void Test_RangeSet_Insert_Backwards()
        {
            const int N = 100;

            var cola = new ColaRangeSet <int>();

            for (int i = N; i > 0; i--)
            {
                int x = i << 1;
                cola.Mark(x - 1, x);
            }

            Assert.That(cola.Count, Is.EqualTo(N));

            Console.WriteLine("Result = { " + String.Join(", ", cola) + " }");
            Console.WriteLine("Bounds = " + cola.Bounds);
        }
Ejemplo n.º 7
0
        public void Dispose()
        {
            if (m_disposed)
            {
                //TODO: locking ?
                m_disposed = true;

                //TODO!
                m_buffer         = null;
                m_readConflicts  = null;
                m_writeConflicts = null;
                m_clears         = null;
                m_writes         = null;
            }

            GC.SuppressFinalize(this);
        }
		public void Test_RangeSet_Insert_Completly_Overlapping()
		{
			var cola = new ColaRangeSet<int>();
			cola.Mark(1, 2);
			cola.Mark(4, 5);
			cola.Debug_Dump();
			Assert.That(cola.Count, Is.EqualTo(2));
			Assert.That(cola.Bounds.Begin, Is.EqualTo(1));
			Assert.That(cola.Bounds.End, Is.EqualTo(5));

			// overlaps the first range completely
			cola.Mark(0, 3);
			cola.Debug_Dump();
			Assert.That(cola.Count, Is.EqualTo(2));
			Assert.That(cola.Bounds.Begin, Is.EqualTo(0));
			Assert.That(cola.Bounds.End, Is.EqualTo(5));

			Console.WriteLine("Result = { " + String.Join(", ", cola) + " }");
			Console.WriteLine("Bounds = " + cola.Bounds);

		}
        public void Test_RangeSet_Insert_Completly_Overlapping()
        {
            var cola = new ColaRangeSet <int>();

            cola.Mark(1, 2);
            cola.Mark(4, 5);
            cola.Debug_Dump();
            Assert.That(cola.Count, Is.EqualTo(2));
            Assert.That(cola.Bounds.Begin, Is.EqualTo(1));
            Assert.That(cola.Bounds.End, Is.EqualTo(5));

            // overlaps the first range completely
            cola.Mark(0, 3);
            cola.Debug_Dump();
            Assert.That(cola.Count, Is.EqualTo(2));
            Assert.That(cola.Bounds.Begin, Is.EqualTo(0));
            Assert.That(cola.Bounds.End, Is.EqualTo(5));

            Console.WriteLine("Result = { " + String.Join(", ", cola) + " }");
            Console.WriteLine("Bounds = " + cola.Bounds);
        }
        public void Test_RangeSet_Insert_That_Replace_All_Ranges()
        {
            var cola = new ColaRangeSet <int>();

            cola.Mark(0, 1);
            cola.Mark(2, 3);
            cola.Mark(4, 5);
            cola.Mark(6, 7);
            cola.Debug_Dump();
            Assert.That(cola.Count, Is.EqualTo(4));
            Assert.That(cola.Bounds.Begin, Is.EqualTo(0));
            Assert.That(cola.Bounds.End, Is.EqualTo(7));

            cola.Mark(-1, 10);
            cola.Debug_Dump();
            Assert.That(cola.Count, Is.EqualTo(1));
            Assert.That(cola.Bounds.Begin, Is.EqualTo(-1));
            Assert.That(cola.Bounds.End, Is.EqualTo(10));

            Console.WriteLine("Result = { " + String.Join(", ", cola) + " }");
            Console.WriteLine("Bounds = " + cola.Bounds);
        }
		public void Test_RangeSet_Insert_Non_Overlapping()
		{
			var cola = new ColaRangeSet<int>();
			Assert.That(cola.Count, Is.EqualTo(0));

			cola.Mark(0, 1);
			cola.Debug_Dump();
			Assert.That(cola.Count, Is.EqualTo(1));

			cola.Mark(2, 3);
			cola.Debug_Dump();
			Assert.That(cola.Count, Is.EqualTo(2));

			cola.Mark(4, 5);
			cola.Debug_Dump();
			Assert.That(cola.Count, Is.EqualTo(3));

			Assert.That(cola.Bounds.Begin, Is.EqualTo(0));
			Assert.That(cola.Bounds.End, Is.EqualTo(5));

			Console.WriteLine("Result = { " + String.Join(", ", cola) + " }");
			Console.WriteLine("Bounds = " + cola.Bounds);
		}
		public void Test_RangeSet_Insert_Partially_Overlapping()
		{
			var cola = new ColaRangeSet<int>();
			Assert.That(cola.Count, Is.EqualTo(0));

			cola.Mark(0, 1);
			cola.Debug_Dump();
			Assert.That(cola.Count, Is.EqualTo(1));

			cola.Mark(0, 2);
			cola.Debug_Dump();
			Assert.That(cola.Count, Is.EqualTo(1));

			cola.Mark(1, 3);
			cola.Debug_Dump();
			Assert.That(cola.Count, Is.EqualTo(1));

			cola.Mark(-1, 2);
			cola.Debug_Dump();
			Assert.That(cola.Count, Is.EqualTo(1));

			Console.WriteLine("Result = { " + String.Join(", ", cola) + " }");
			Console.WriteLine("Bounds = " + cola.Bounds);
		}
        public void Test_RangeSet_Insert_Non_Overlapping()
        {
            var cola = new ColaRangeSet <int>();

            Assert.That(cola.Count, Is.EqualTo(0));

            cola.Mark(0, 1);
            cola.Debug_Dump();
            Assert.That(cola.Count, Is.EqualTo(1));

            cola.Mark(2, 3);
            cola.Debug_Dump();
            Assert.That(cola.Count, Is.EqualTo(2));

            cola.Mark(4, 5);
            cola.Debug_Dump();
            Assert.That(cola.Count, Is.EqualTo(3));

            Assert.That(cola.Bounds.Begin, Is.EqualTo(0));
            Assert.That(cola.Bounds.End, Is.EqualTo(5));

            Console.WriteLine("Result = { " + String.Join(", ", cola) + " }");
            Console.WriteLine("Bounds = " + cola.Bounds);
        }
Ejemplo n.º 14
0
        private void Initialize(bool first)
        {
            if (m_disposed)
            {
                ThrowDisposed();
            }

            lock (m_lock)
            {
                m_readVersion = null;
                if (first)
                {
                    m_committedVersion = -1;                        // note: current fdb_c client does not reset commited version to -1 when calling fdb_transaction_reset()
                }
                m_buffer = new SliceBuffer(InitialBufferSize);
                if (first)
                {
                    m_clears         = new ColaRangeSet <Slice>(SliceComparer.Default);
                    m_writes         = new ColaOrderedDictionary <Slice, WriteCommand[]>(SliceComparer.Default);
                    m_readConflicts  = new ColaRangeSet <Slice>(SliceComparer.Default);
                    m_writeConflicts = new ColaRangeSet <Slice>(SliceComparer.Default);
                }
                else
                {
                    m_clears.Clear();
                    m_writes.Clear();
                    m_readConflicts.Clear();
                    m_writeConflicts.Clear();
                }

                m_retryCount          = 0;
                this.AccessSystemKeys = NO_ACCESS;
                this.NextWriteNoWriteConflictRange = false;
                this.ReadYourWritesDisable         = false;
            }
        }
		public void Test_RangeSet_Insert_Backwards()
		{
			const int N = 100;

			var cola = new ColaRangeSet<int>();

			for(int i = N; i > 0; i--)
			{
				int x = i << 1;
				cola.Mark(x - 1, x);
			}

			Assert.That(cola.Count, Is.EqualTo(N));

			Console.WriteLine("Result = { " + String.Join(", ", cola) + " }");
			Console.WriteLine("Bounds = " + cola.Bounds);
		}
		public void Test_RangeSet_Insert_That_Replace_All_Ranges()
		{
			var cola = new ColaRangeSet<int>();
			cola.Mark(0, 1);
			cola.Mark(2, 3);
			cola.Mark(4, 5);
			cola.Mark(6, 7);
			cola.Debug_Dump();
			Assert.That(cola.Count, Is.EqualTo(4));
			Assert.That(cola.Bounds.Begin, Is.EqualTo(0));
			Assert.That(cola.Bounds.End, Is.EqualTo(7));

			cola.Mark(-1, 10);
			cola.Debug_Dump();
			Assert.That(cola.Count, Is.EqualTo(1));
			Assert.That(cola.Bounds.Begin, Is.EqualTo(-1));
			Assert.That(cola.Bounds.End, Is.EqualTo(10));

			Console.WriteLine("Result = { " + String.Join(", ", cola) + " }");
			Console.WriteLine("Bounds = " + cola.Bounds);
		}
		public void Test_RangeSet_Insert_That_Join_Two_Ranges()
		{
			var cola = new ColaRangeSet<int>();
			cola.Mark(0, 1);
			cola.Mark(2, 3);
			cola.Debug_Dump();
			Assert.That(cola.Count, Is.EqualTo(2));

			cola.Mark(1, 2);
			cola.Debug_Dump();
			Assert.That(cola.Count, Is.EqualTo(1));

			Console.WriteLine("Result = { " + String.Join(", ", cola) + " }");
			Console.WriteLine("Bounds = " + cola.Bounds);
		}
		/// <summary>Checks if a list of reads conflicts with at least one write performed in this transaction window</summary>
		/// <param name="reads">List of reads to check for conflicts</param>
		/// <param name="version">Sequence number of the transaction that performed the reads</param>
		/// <returns>True if at least one read is conflicting with a write with a higher sequence number; otherwise, false.</returns>
		public bool Conflicts(ColaRangeSet<Slice> reads, ulong version)
		{
			Contract.Requires(reads != null);

			//Debug.WriteLine("* Testing for conflicts for: " + String.Join(", ", reads));

			if (version > m_maxVersion)
			{ // all the writes are before the reads, so no possible conflict!
				//Debug.WriteLine(" > cannot conflict");
				return false;
			}

			using (var scratch = new UnmanagedSliceBuilder())
			{
				//TODO: do a single-pass version of intersection checking !
				foreach (var read in reads)
				{
					scratch.Clear();
					scratch.Append(read.Begin);
					var p = scratch.Count;
					scratch.Append(read.End);
					var begin = scratch.ToUSlice(p);
					var end = scratch.ToUSlice(p, scratch.Count - p);

					if (m_writeConflicts.Intersect(begin, end, version, (v, min) => v > min))
					{
						Debug.WriteLine(" > Conflicting read: " + read);
						return true;
					}
				}
			}

			//Debug.WriteLine("  > No conflicts found");
			return false;
		}