Ejemplo n.º 1
0
        /// <summary>
        /// 构造函数
        /// </summary>
        public MemoryIndex(string strFilePath, Key key)
            : base(strFilePath, key)
        {
            BTree = new List<BTNode>();

            fs.Seek(RootNodeStart, SeekOrigin.Begin);
            //加载根节点

            if (Deep == 1)
            {
                //树只有一层
                for (int i = 0; i < Setp; i++)
                {
                    BTNode Node = ReadFoliageNode();
                    if (Node == null || Node.Value == -1)
                    {
                        break;
                    }
                    //id
                    Node.ID = i;
                    //节点深度
                    Node.Depth = Deep;
                    BTree.Add(Node);
                }

            }
            else
            {
                //2曾或者三层
                for (int i = 0; i < Setp; i++)
                {
                    //每次设置文件指针到 根节点起始位置+NodeLength*i
                    fs.Seek(RootNodeStart + (NodeLength* i), SeekOrigin.Begin);
                    BTNode Node = ReadTreeNode();
                    if (Node == null || Node.Value == -1)
                    {
                        break;
                    }
                    //id
                    Node.ID = i;
                    //节点深度
                    Node.Depth = Deep;
                    //位移
                    Node.Value = i * 1000;
                    //加载子节点
                    Node.ChildrenNodes = GetChildNodes(Node);
                    BTree.Add(Node);
                }

            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 构造函数
 /// </summary>
 public FileIndex(string strFilePath, Key key)
     : base(strFilePath, key)
 {
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 得到配置文件下的所有索引入口
        /// </summary>
        /// <returns></returns>
        public List<Key> GetSearchEnterKey()
        {
            List<Key> results = new List<Key>();
            List<SearchEnter> lstEnter = SearchEnters;
            foreach (SearchEnter se in lstEnter)
            {
                if (se.Index == 0)
                {
                    continue;
                }
                if (se.SingleFile == true)
                {
                    Key key = new Key()
                    {
                        Name = se.Name,
                        Length = se.Length,
                        Encoder = getKeyType(se.WordSplit),
                    };
                    if (se.WordSplit == WordSplitType.None)
                    {
                        if (se.Name == "TX" || se.Name == "KW")
                        {
                            key.Encoder = "gb2312";
                        }
                    }
                    //索引的值类型
                    Value value = new Value();
                    value.Type = ValType.Com;

                    if (se.WordLocation == true)
                    {
                        value.Length = 8;
                    }
                    else
                    {
                        value.Length = 4;
                    }
                    key.value = value;
                    key.ToString();
                    results.Add(key);
                    if (se.SubKey.Count > 0)
                    {
                        //添加子索引
                        foreach (SubSearchEnter subse in se.SubKey)
                        {

                            //索引的键字类型
                            Key subkey = new Key()
                            {
                                Name = subse.Name,
                                Length = subse.Length,
                                Encoder = getKeyType(se.WordSplit)
                            };
                            //索引的值类型
                            Value subvalue = new Value();
                            subvalue.Length = 4;
                            subvalue.Type = ValType.Com;
                            subkey.value = subvalue;

                            results.Add(subkey);
                        }
                    }

                }
                else
                {
                    if (se.WordSplit == WordSplitType.Cn)
                    {
                        //中文1-3字词索引
                        for (int i = 1; i <= 3; i++)
                        {
                            //索引的键字类型
                            Key wordkey = new Key()
                            {
                                Name = se.Name + i.ToString(),
                                Length = i * 2,
                                Encoder = getKeyType(se.WordSplit)
                            };

                            //索引的值类型
                            Value wordvalue = new Value();
                            if (se.WordLocation == true)
                            {
                                wordvalue.Length = 8;
                            }
                            else
                            {
                                wordvalue.Length = 4;
                            }

                            wordvalue.Type = ValType.Com;
                            wordkey.value = wordvalue;
                            results.Add(wordkey);
                        }

                    }
                    else
                    {
                        //英文 的数字索引
                        Key key = new Key()
                        {
                            Name = se.Name + "_0",
                            Length = 16,
                            Encoder = "utf-8"
                        };
                        //索引的值类型
                        Value value = new Value();
                        value.Length = 8;
                        value.Type = ValType.EnglishWord;
                        key.value = value;
                        results.Add(key);
                        //A-Z 索引
                        for (int i = 1; i <= 26; i++)
                        {
                            //索引的键字类型
                            Key wordkey = new Key()
                            {
                                Name = se.Name + "_" + (char)(i + 64),
                                Length = 16,
                                Encoder = "utf-8"
                            };
                            //索引的值类型
                            Value wordvalue = new Value();
                            wordvalue.Length = 8;
                            wordvalue.Type = ValType.EnglishWord;
                            wordkey.value = wordvalue;
                            results.Add(wordkey);
                        }

                    }
                }

            }
            return results;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 构造函数
        /// </summary>
        public Index(string strFilePath, Key key)
        {
            this.FilePath = strFilePath;
            this.Key = key;
            NodeLength = Key.Length + 4;
            FoliageNodeLength = Key.Length + 8;

            by4 = new byte[4];
            byTreeNode = new byte[NodeLength];
            byFoliage = new byte[FoliageNodeLength];

            ///打开索引文件
            _fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read);

            //读叶子节点起始位置
            readLength = _fs.Read(by4, 0, 4);
            _FoliageNodeStart = BitConverter.ToInt32(by4, 0);

            //读叶子节点的结束位置
            readLength = _fs.Read(by4, 0, 4);
            _FoliageNodeEnd = BitConverter.ToInt32(by4, 0);

            //得到树的深度
            _fs.Seek(_FoliageNodeEnd, SeekOrigin.Begin);
            readLength = _fs.Read(by4, 0, 4);
            Deep = BitConverter.ToInt32(by4, 0);

            //根据树的深度判断树的根节点的位置
            switch (Deep)
            {
                case 3:

                    //中间节点起始位置
                    readLength = _fs.Read(by4, 0, 4);
                    _MidNodeStart = _FoliageNodeEnd + BitConverter.ToInt32(by4, 0);

                    //跟节点的其实位置
                    readLength = _fs.Read(by4, 0, 4);
                    _RootNodeStart = _FoliageNodeEnd + BitConverter.ToInt32(by4, 0);
                    break;
                case 2:
                    //中间节点
                    _fs.Seek(_FoliageNodeEnd + 4, SeekOrigin.Begin);
                    readLength = _fs.Read(by4, 0, 4);
                    _RootNodeStart = _FoliageNodeEnd + BitConverter.ToInt32(by4, 0);
                    _MidNodeStart = _RootNodeStart;
                    break;
                case 1:
                    _RootNodeStart = _FoliageNodeStart;
                    break;
                default:
                    throw new Exception("索引树深度数据异常");
            }
        }