Пример #1
0
 public byte[] EncodeSnapshotV2()
 {
     using var encoder = new DSEncoderV2();
     DeleteSet.Write(encoder);
     EncodingUtils.WriteStateVector(encoder, StateVector);
     return(encoder.ToArray());
 }
Пример #2
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())));
            }
        }