Example #1
0
        public async Task PutAsync(string node, string key, JToken value, VersionVector context)
        {
            var requestUri = GetRequestUri(node, key);

            using (var client = new HttpClient())
            {
                var req = new HttpRequestMessage(HttpMethod.Put, requestUri);
                req.Headers.Add("X-Context", context.ToString());
                req.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(JsonMediaType));

                // TODO: Serialize JSON to stream, not string
                //MemoryStream stream = new MemoryStream();
                //StreamReader streamWriter = new StreamReader(stream);
                //using (JsonWriter jsonWriter = new JsonTextWriter(streamWriter))
                //{
                //    JsonSerializer serializer = new JsonSerializer();
                //    serializer.Serialize(jsonWriter, value);
                //}
                //req.Content = new StreamContent();

                string json = JsonConvert.SerializeObject(value);
                req.Content = new StringContent(json, Encoding.UTF8, JsonMediaType);

                HttpResponseMessage response = await client.SendAsync(req);

                response.EnsureSuccessStatusCode();
            }
        }
Example #2
0
        public DottedVersionVector(CausalEvent dot, VersionVector v)
        {
            Ensure.That(dot, "dot").IsNotNull();
            Ensure.That(v, "v").IsNotNull();

            _dot = dot;
            _v   = v;
        }
Example #3
0
        /// <summary>
        /// Remove obsolete versions.
        /// </summary>
        public Siblings Discard(Siblings s, VersionVector context)
        {
            Ensure.That(s, "s").IsNotNull();
            Ensure.That(context, "context").IsNotNull();

            // discard all siblings that are obsolete because they are included
            // in the context.
            IEnumerable <VersionedObject> concurrent = s.Where(sibling => !sibling.Clock.HappensBefore(context));

            return(new Siblings(concurrent));
        }
Example #4
0
        /// <summary>
        /// Generates a new clock.
        /// </summary>
        public DottedVersionVector Event(VersionVector context, Siblings s, string i)
        {
            Ensure.That(s, "s").IsNotNull();
            Ensure.That(context, "context").IsNotNull();
            Ensure.That(i, "i").IsNotNullOrEmpty();

            long maxDot           = s.MaxDot(i);
            long maxCausalHistory = context[i];

            long maxCounter = Math.Max(maxDot, maxCausalHistory);
            var  dot        = new CausalEvent(i, maxCounter + 1);

            return(new DottedVersionVector(dot, context));
        }
Example #5
0
        public static VersionVector Parse(string value)
        {
            VersionVector parsed;

            if (string.IsNullOrWhiteSpace(value))
            {
                parsed = new VersionVector();
            }
            else
            {
                parsed = (VersionVector)Parser.Value.Parse(value);
            }

            return(parsed);
        }
Example #6
0
        internal async Task PutAsync(string key, JToken value, VersionVector context = null)
        {
            Ensure.That(key, "key").IsNotNullOrWhiteSpace();

            context = context ?? new VersionVector();

            string coordinatingNode = _ring.Node(key);

            if (_ring.PreferenceList(key, N).Contains(_name))
            {
                Log.DebugFormat("put k={0}", key);

                BigInteger hash     = _ring.Hash(key);
                Siblings   siblings = _data.GetValueOrDefault(hash);
                if (siblings != null)
                {
                    // discard obsolete versions
                    siblings = _kernel.Discard(siblings, context);
                }
                else
                {
                    siblings = new Siblings();
                }

                DottedVersionVector dvv = _kernel.Event(context, siblings, _name);
                var versionedObject     = new VersionedObject(value, dvv);

                siblings.Add(versionedObject);

                _data[hash] = siblings;

                await ReplicatePutAsync(key, siblings);
            }
            else
            {
                Log.DebugFormat("forward to coordinating node {0}", coordinatingNode);
                await _remote.PutAsync(coordinatingNode, key, value, context);
            }
        }
Example #7
0
        public bool HappensBefore(VersionVector vv)
        {
            Ensure.That(vv, "vv").IsNotNull();

            return(_dot.N <= vv[_dot.I]);
        }