Esempio n. 1
0
        /// <summary>
        /// 根据单词获取一个倒排列表
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        public async Task <IndexList> GetOrAddIndexList(WordAtom word)
        {
            HashIndexList hashIndex = null;

            if (CurrentIndexList.TryGetValue(word.Word, out hashIndex))
            {
                return(hashIndex.List);
            }
            int          hashcode = word.GetHashCode();
            DictAndState dict;
            MongoClient  Client = Mongo.GetNode(hashcode.ToString());

            if (!DictList.TryGetValue(hashcode, out dict))
            {
                dict = new DictAndState()
                {
                    IsNew = true,
                    Dict  = new HashDict()
                    {
                        Hashcode = hashcode,
                        Words    = new ConcurrentDictionary <string, string>()
                    }
                };
                hashIndex = new HashIndexList()
                {
                    Hashcode = hashcode, List = CreateEmptyIndexList(word), IsNew = true
                };
                dict.Dict.Words.TryAdd(word.Word, hashIndex.List.Id);
                if (!DictList.TryAdd(hashcode, dict))
                {
                    return(null);
                }
            }
            string listId;

            if (dict.Dict.Words.TryGetValue(word.Word, out listId))
            {
                if (hashIndex == null || listId != hashIndex.List.Id)
                {
                    var indexListDb = Client.GetDatabase(Config.IndexListDbName).GetCollection <BsonDocument>(ListHash.GetNode(hashcode.ToString()));
                    var index       = await indexListDb.Find(Builders <BsonDocument> .Filter.Eq("_id", listId)).SingleOrDefaultAsync();

                    hashIndex = new HashIndexList()
                    {
                        Hashcode = hashcode,
                        IsNew    = false,
                        List     = new IndexList()
                        {
                            Id         = listId,
                            IndexAtoms = index != null ? new ConcurrentDictionary <string, IndexAtom>(AtomDerializer.UnpackSingleObject(index["IndexAtoms"].AsByteArray)) : new ConcurrentDictionary <string, IndexAtom>()
                        }
                    };
                }
            }
            if (!CurrentIndexList.TryAdd(word.Word, hashIndex))
            {
                return(null);
            }
            return(hashIndex.List);
        }
Esempio n. 2
0
        /// <summary>
        /// 创建空倒排列表
        /// </summary>
        /// <param name="word">单词</param>
        /// <returns></returns>
        private IndexList CreateEmptyIndexList(WordAtom word)
        {
            var posList = new IndexList();

            posList.Id         = ObjectId.GenerateNewId().ToString();
            posList.IndexAtoms = new ConcurrentDictionary <string, IndexAtom>();
            return(posList);
        }