/// <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); }
/// <summary> /// 保存数据并返回DocId /// </summary> /// <param name="propertys"></param> /// <returns></returns> public async Task <string> save(List <PropertyInfo> propertys) { var id = ObjectId.GenerateNewId().ToString(); BsonDocument bson = new BsonDocument("_id", id); foreach (var property in propertys) { if (property.Save) { bson.Add(property.Name, property.Value); } } var collection = Mongo.GetNode(id.ToString()).GetDatabase(DocDatabase).GetCollection <BsonDocument>(ListHash.GetNode(id.ToString())); await collection.InsertOneAsync(bson); return(id); }