/// <summary>
            /// Helper to convert from legacy persisted data format to the current. The key array is a set of kvp's where the value
            /// is an offset in the value buffer. These all need to be recalculated with new value buffer VLE format
            /// </summary>
            private void TranscodeBuffers(RecyclableMemoryStreamManager memoryStreamManager, Stream destinationStream)
            {
                using (var intermediateValueStream = memoryStreamManager.GetStream("transcoding value-buffer"))
                {
                    uint currentOffset = 0;
                    var  buffer        = new byte[sizeof(uint)];
                    foreach (var kvp in this.keys)
                    {
                        // write the current key and then the offset in fixed length
                        kvp.Key.Serialize(destinationStream);
                        ByteConverter.FixedLengthEncoding.Encode(currentOffset, buffer, destinationStream);

                        var dict       = new Dictionary <long, uint>();
                        var newEntries = this.values.ReadValuesInto(dict, kvp.Value);

                        currentOffset += BufferedValueArray.WriteVariableLengthEncodedDictionary(
                            dict,
                            newEntries,
                            intermediateValueStream);
                    }

                    // now dump the values at the end of the key array
                    destinationStream.Write(intermediateValueStream.GetBuffer(), 0, (int)intermediateValueStream.Length);
                }
            }
            public QueryableMultiValueData(MemoryStream keyStream, MemoryStream valueStream, DimensionSet dimensionSet)
                : base(dimensionSet)
            {
                if (keyStream.Length == 0)
                {
                    this.keys = new BufferedKeyedData <uint>(null, 0, 0, dimensionSet);
                    keyStream.Dispose();
                    valueStream.Dispose();
                }
                else
                {
                    this.keyStream = keyStream;
                    this.keys      = new BufferedKeyedData <uint>(keyStream.GetBuffer(), 0, (int)keyStream.Length,
                                                                  dimensionSet);

                    this.values      = BufferedValueArray.Create(valueStream.GetBuffer(), 0, (int)valueStream.Length);
                    this.valueStream = valueStream;
                }
            }
            public QueryableMultiValueData(PersistedDataType type, MemoryStream source, DimensionSet dimensionSet, int keyCount)
                : base(dimensionSet)
            {
                if (source.Length == 0)
                {
                    this.keys = new BufferedKeyedData <uint>(null, 0, 0, dimensionSet);
                    source.Dispose();
                }
                else
                {
                    this.keyStream   = source;
                    this.valueStream = null;
                    var keyPortionLength = (int)BufferedKeyedData <uint> .GetBufferSizeForKeyCount(keyCount, dimensionSet);

                    var sourceBuffer = source.GetBuffer();
                    var sourceLength = (int)source.Length;

                    this.keys   = new BufferedKeyedData <uint>(sourceBuffer, 0, keyPortionLength, dimensionSet);
                    this.values = BufferedValueArray.Create(type, sourceBuffer,
                                                            keyPortionLength, sourceLength - keyPortionLength);
                }
            }
 public uint WriteToPersistedData(MemoryStream writeStream)
 {
     return(BufferedValueArray.WriteVariableLengthEncodedDictionary(this.samples, (uint)this.SampleCount,
                                                                    writeStream));
 }