Ejemplo n.º 1
0
        public static void Serialize(this LcrsNode node, Stream stream)
        {
            var valBytes    = BitConverter.GetBytes(node.Value);
            var byte0       = EncodedBoolean[node.HaveSibling];
            var byte1       = EncodedBoolean[node.HaveChild];
            var byte2       = EncodedBoolean[node.EndOfWord];
            var depthBytes  = BitConverter.GetBytes(node.Depth);
            var weightBytes = BitConverter.GetBytes(node.Weight);

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

            stream.Write(valBytes, 0, valBytes.Length);
            stream.WriteByte(byte0);
            stream.WriteByte(byte1);
            stream.WriteByte(byte2);
            stream.Write(depthBytes, 0, depthBytes.Length);
            stream.Write(weightBytes, 0, weightBytes.Length);

            Serialize(node.PostingsAddress, stream);
        }
Ejemplo n.º 2
0
        public static byte[] Serialize(this LcrsNode node)
        {
            using (var stream = new MemoryStream())
            {
                var valBytes    = BitConverter.GetBytes(node.Value);
                var byte0       = EncodedBoolean[node.HaveSibling];
                var byte1       = EncodedBoolean[node.HaveChild];
                var byte2       = EncodedBoolean[node.EndOfWord];
                var depthBytes  = BitConverter.GetBytes(node.Depth);
                var weightBytes = BitConverter.GetBytes(node.Weight);
                var addrBytes   = Serialize(node.PostingsAddress);

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

                stream.Write(valBytes, 0, valBytes.Length);
                stream.WriteByte(byte0);
                stream.WriteByte(byte1);
                stream.WriteByte(byte2);
                stream.Write(depthBytes, 0, depthBytes.Length);
                stream.Write(weightBytes, 0, weightBytes.Length);
                stream.Write(addrBytes, 0, addrBytes.Length);

                return(stream.ToArray());
            }
        }
Ejemplo n.º 3
0
        public static void Serialize(this LcrsNode node, Stream stream)
        {
            var valBytes        = BitConverter.GetBytes(node.Value);
            var haveSiblingByte = EncodedBoolean[node.HaveSibling];
            var haveChildByte   = EncodedBoolean[node.HaveChild];
            var eowByte         = EncodedBoolean[node.EndOfWord];
            var depthBytes      = BitConverter.GetBytes(node.Depth);
            var weightBytes     = BitConverter.GetBytes(node.Weight);

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

            stream.Write(valBytes, 0, valBytes.Length);
            stream.WriteByte(haveSiblingByte);
            stream.WriteByte(haveChildByte);
            stream.WriteByte(eowByte);
            stream.Write(depthBytes, 0, depthBytes.Length);
            stream.Write(weightBytes, 0, weightBytes.Length);

            if (!BlockSerializer.Serialize(node.PostingsAddress, stream) && node.EndOfWord)
            {
                throw new InvalidOperationException("cannot store word without posting address");
            }
        }
Ejemplo n.º 4
0
        private static void SerializeDepthFirst(this LcrsTrie trie, Stream stream)
        {
            var stack = new Stack <LcrsNode>();
            var node  = new LcrsNode(trie, 0, trie.Weight, trie.PostingsAddress);

            while (node != null)
            {
                node.Serialize(stream);

                if (node.Tree.RightSibling != null)
                {
                    stack.Push(new LcrsNode(
                                   node.Tree.RightSibling, node.Depth,
                                   node.Tree.RightSibling.Weight, node.Tree.RightSibling.PostingsAddress));
                }

                if (node.Tree.LeftChild != null)
                {
                    node = new LcrsNode(
                        node.Tree.LeftChild, (short)(node.Depth + 1),
                        node.Tree.LeftChild.Weight, node.Tree.LeftChild.PostingsAddress);
                }
                else if (stack.Count > 0)
                {
                    node = stack.Pop();
                }
                else
                {
                    break;
                }
            }
        }
Ejemplo n.º 5
0
        private static void SerializeDepthFirst(this LcrsTrie trie, Stream stream, short depth)
        {
            var bytes = new LcrsNode(trie, depth, trie.Weight, trie.PostingsAddress).Serialize();

            stream.Write(bytes, 0, bytes.Length);

            if (trie.LeftChild != null)
            {
                trie.LeftChild.SerializeDepthFirst(stream, (short)(depth + 1));
            }

            if (trie.RightSibling != null)
            {
                trie.RightSibling.SerializeDepthFirst(stream, depth);
            }
        }
Ejemplo n.º 6
0
        public static void Serialize(this LcrsTrie trie, string fileName)
        {
            var dir         = Path.GetDirectoryName(fileName);
            var version     = Path.GetFileNameWithoutExtension(fileName);
            var sixFileName = Path.Combine(dir, version + ".six");

            using (var sixStream = new FileStream(sixFileName, FileMode.Append, FileAccess.Write, FileShare.Read))
            {
                FileStream treeStream;

                var segmentDelimiter = new LcrsNode(SegmentDelimiter, false, false, false, 0, 1, null);

                if (File.Exists(fileName))
                {
                    treeStream = new FileStream(
                        fileName, FileMode.Append, FileAccess.Write, FileShare.Read);

                    segmentDelimiter.Serialize(treeStream);
                }
                else
                {
                    treeStream = new FileStream(
                        fileName, FileMode.Append, FileAccess.Write, FileShare.None);
                }

                var position = treeStream.Position;
                var posBytes = BitConverter.GetBytes(position);

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

                sixStream.Write(posBytes, 0, sizeof(long));

                using (treeStream)
                {
                    if (trie.LeftChild != null)
                    {
                        trie.LeftChild.SerializeDepthFirst(treeStream, 0);
                    }
                }
            }
        }