Ejemplo n.º 1
0
        public static DeleteSet Read(IDSDecoder decoder)
        {
            var ds = new DeleteSet();

            var numClients = (int)decoder.Reader.ReadVarUint();

            Debug.Assert(numClients >= 0);

            for (int i = 0; i < numClients; i++)
            {
                decoder.ResetDsCurVal();

                var client          = (int)decoder.Reader.ReadVarUint();
                var numberOfDeletes = (int)decoder.Reader.ReadVarUint();

                if (numberOfDeletes > 0)
                {
                    if (!ds.Clients.TryGetValue(client, out var dsField))
                    {
                        dsField            = new List <DeleteItem>(numberOfDeletes);
                        ds.Clients[client] = dsField;
                    }

                    for (int j = 0; j < numberOfDeletes; j++)
                    {
                        var deleteItem = new DeleteItem(decoder.ReadDsClock(), decoder.ReadDsLength());
                        dsField.Add(deleteItem);
                    }
                }
            }

            return(ds);
        }
Ejemplo n.º 2
0
        public static IDictionary <int, int> ReadStateVector(IDSDecoder decoder)
        {
            var ssLength = (int)decoder.Reader.ReadVarUint();
            var ss       = new Dictionary <int, int>(ssLength);

            for (int i = 0; i < ssLength; i++)
            {
                var client = (int)decoder.Reader.ReadVarUint();
                var clock  = (int)decoder.Reader.ReadVarUint();
                ss[client] = clock;
            }

            return(ss);
        }
Ejemplo n.º 3
0
        public void ReadAndApplyDeleteSet(IDSDecoder decoder, Transaction transaction)
        {
            var unappliedDs = new DeleteSet();
            var numClients  = decoder.Reader.ReadVarUint();

            for (int i = 0; i < numClients; i++)
            {
                decoder.ResetDsCurVal();

                var client          = (int)decoder.Reader.ReadVarUint();
                var numberOfDeletes = decoder.Reader.ReadVarUint();

                if (!Clients.TryGetValue(client, out var structs))
                {
                    structs = new List <AbstractStruct>();
                    // NOTE: Clients map is not updated.
                }

                var state = GetState(client);

                for (int deleteIndex = 0; deleteIndex < numberOfDeletes; deleteIndex++)
                {
                    var clock    = decoder.ReadDsClock();
                    var clockEnd = clock + decoder.ReadDsLength();
                    if (clock < state)
                    {
                        if (state < clockEnd)
                        {
                            unappliedDs.Add(client, state, clockEnd - state);
                        }

                        var index = StructStore.FindIndexSS(structs, clock);

                        // We can ignore the case of GC and Delete structs, because we are going to skip them.
                        var str = structs[index];

                        // Split the first item if necessary.
                        if (!str.Deleted && str.Id.Clock < clock)
                        {
                            var splitItem = (str as Item).SplitItem(transaction, clock - str.Id.Clock);
                            structs.Insert(index + 1, splitItem);

                            // Increase, we now want to use the next struct.
                            index++;
                        }

                        while (index < structs.Count)
                        {
                            str = structs[index++];
                            if (str.Id.Clock < clockEnd)
                            {
                                if (!str.Deleted)
                                {
                                    if (clockEnd < str.Id.Clock + str.Length)
                                    {
                                        var splitItem = (str as Item).SplitItem(transaction, clockEnd - str.Id.Clock);
                                        structs.Insert(index, splitItem);
                                    }

                                    str.Delete(transaction);
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        unappliedDs.Add(client, clock, clockEnd - clock);
                    }
                }
            }

            if (unappliedDs.Clients.Count > 0)
            {
                // @TODO: No need for encoding+decoding ds anymore.
                using var unappliedDsEncoder = new DSEncoderV2();
                unappliedDs.Write(unappliedDsEncoder);
                _pendingDeleteReaders.Add(new DSDecoderV2(new MemoryStream(unappliedDsEncoder.ToArray())));
            }
        }