Exemplo n.º 1
0
            public void ApplyBitXorTo(UnmanagedSliceBuilder value)
            {
                uint size = checked ((uint)this.Value.Count);

                // truncate the value if larger, or pad it with zeroes if shorter
                value.Resize(size, 0);

                if (size > 0)
                {
                    unsafe
                    {
                        fixed(byte *ptr = this.Value.Array)
                        {
                            byte *left  = value.Data;
                            byte *right = ptr + this.Value.Offset;

                            //TODO: find a way to optimize this for common sizes like 4 or 8 bytes!
                            while (size-- > 0)
                            {
                                *left++ ^= *right++;
                            }
                        }
                    }
                }
            }
        /// <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);
        }
Exemplo n.º 3
0
            public void ApplyAddTo(UnmanagedSliceBuilder value)
            {
                uint size = checked ((uint)this.Value.Count);

                // if the value is empty, then this is the same thing as adding to 0
                if (value.Count == 0)
                {
                    value.Append(this.Value);
                    return;
                }

                // truncate the value if larger, or pad it with zeroes if shorter
                value.Resize(size, 0);

                if (size > 0)
                {
                    unsafe
                    {
                        fixed(byte *ptr = this.Value.Array)
                        {
                            byte *left  = value.Data;
                            byte *right = ptr + this.Value.Offset;

                            //TODO: find a way to optimize this for common sizes like 4 or 8 bytes!
                            int carry = 0;

                            while (size-- > 0)
                            {
                                carry += *left + *right++;
                                *left++ = (byte)carry;
                                carry >>= 8;
                            }
                        }
                    }
                }
            }