Exemple #1
0
        public static void Serialize(this IDictionary <short, Field> fields, Compression compression, Stream stream)
        {
            foreach (var field in fields)
            {
                byte[] keyBytes = BitConverter.GetBytes(field.Key);
                byte[] valBytes;
                string toStore = field.Value.Store ? field.Value.Value : string.Empty;

                if (compression == Compression.GZip)
                {
                    valBytes = Deflator.Compress(Encoding.GetBytes(toStore));
                }
                else if (compression == Compression.Lz)
                {
                    valBytes = QuickLZ.compress(Encoding.GetBytes(toStore), 1);
                }
                else
                {
                    valBytes = Encoding.GetBytes(toStore);
                }

                byte[] valLengthBytes = BitConverter.GetBytes(valBytes.Length);

                if (!BitConverter.IsLittleEndian)
                {
                    Array.Reverse(keyBytes);
                    Array.Reverse(valBytes);
                    Array.Reverse(valLengthBytes);
                }

                stream.Write(keyBytes, 0, sizeof(short));
                stream.Write(valLengthBytes, 0, sizeof(int));
                stream.Write(valBytes, 0, valBytes.Length);
            }
        }
Exemple #2
0
        public static byte[] Serialize(this IList <Field> fields, Compression compression)
        {
            using (var stream = new MemoryStream())
            {
                foreach (var field in fields)
                {
                    byte[] keyBytes       = Encoding.GetBytes(field.Key);
                    byte[] keyLengthBytes = BitConverter.GetBytes((short)keyBytes.Length);
                    byte[] valBytes;
                    string toStore = field.Store ? field.Value : string.Empty;

                    if (compression == Compression.GZip)
                    {
                        valBytes = Deflator.Compress(Encoding.GetBytes(toStore));
                    }
                    else if (compression == Compression.QuickLz)
                    {
                        valBytes = QuickLZ.compress(Encoding.GetBytes(toStore), 1);
                    }
                    else
                    {
                        valBytes = Encoding.GetBytes(toStore);
                    }

                    byte[] valLengthBytes = BitConverter.GetBytes(valBytes.Length);

                    if (!BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(keyLengthBytes);
                        Array.Reverse(keyBytes);
                        Array.Reverse(valBytes);
                        Array.Reverse(valLengthBytes);
                    }

                    stream.Write(keyLengthBytes, 0, sizeof(short));
                    stream.Write(keyBytes, 0, keyBytes.Length);
                    stream.Write(valLengthBytes, 0, sizeof(int));
                    stream.Write(valBytes, 0, valBytes.Length);
                }
                return(stream.ToArray());
            }
        }
Exemple #3
0
        public static IEnumerable <Field> DeserializeFields(Stream stream, int documentId, Compression compression)
        {
            while (true)
            {
                var keyLengthBytes = new byte[sizeof(short)];

                var read = stream.Read(keyLengthBytes, 0, sizeof(short));

                if (read == 0)
                {
                    break;
                }

                short keyLength = BitConverter.ToInt16(keyLengthBytes, 0);

                if (!BitConverter.IsLittleEndian)
                {
                    Array.Reverse(keyLengthBytes);
                }

                byte[] keyBytes = new byte[keyLength];

                stream.Read(keyBytes, 0, keyLength);

                if (!BitConverter.IsLittleEndian)
                {
                    Array.Reverse(keyBytes);
                }

                string key = Encoding.GetString(keyBytes);

                var valLengthBytes = new byte[sizeof(int)];

                stream.Read(valLengthBytes, 0, sizeof(int));

                if (!BitConverter.IsLittleEndian)
                {
                    Array.Reverse(valLengthBytes);
                }

                int valLength = BitConverter.ToInt32(valLengthBytes, 0);

                byte[] valBytes = new byte[valLength];

                stream.Read(valBytes, 0, valLength);

                if (!BitConverter.IsLittleEndian)
                {
                    Array.Reverse(valBytes);
                }


                string value;

                if (compression == Compression.GZip)
                {
                    value = Encoding.GetString(Deflator.Deflate(valBytes));
                }
                else if (compression == Compression.Lz)
                {
                    value = Encoding.GetString(QuickLZ.decompress(valBytes));
                }
                else
                {
                    value = Encoding.GetString(valBytes);
                }


                yield return(new Field(documentId, key, value));
            }
        }