コード例 #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);
        }
コード例 #2
0
ファイル: Serializer.cs プロジェクト: CodeFork/resin
        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());
            }
        }
コード例 #3
0
ファイル: Serializer.cs プロジェクト: ksec/resin
        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");
            }
        }
コード例 #4
0
ファイル: Serializer.cs プロジェクト: mpvyard/resin
        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;
                }
            }
        }
コード例 #5
0
ファイル: Serializer.cs プロジェクト: CodeFork/resin
        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);
            }
        }
コード例 #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);
                    }
                }
            }
        }