예제 #1
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;
                }
            }
        }
예제 #2
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);
                    }
                }
            }
        }