Beispiel #1
0
        private async Task <bool> VerifyCollectionModel(IDocumentDatabase db, CollectionModel model)
        {
            (await db.CollectionExist(_workContext, _collectionName)).Should().BeTrue();
            IDocumentCollection <TestDocument> collection = db.GetCollection <TestDocument>(_collectionName);

            foreach (var indexDetail in model.Indexes)
            {
                IndexDetailV1 detail = await collection.Index.GetIndexDetail(_workContext, indexDetail.Name);

                detail.Should().NotBeNull();

                if (!indexDetail.IsEquals(detail))
                {
                    return(false);
                }
            }

            IEnumerable <IndexDetailV1> dbIndex = await collection.Index.ListIndexes(_workContext);

            dbIndex.Should().NotBeNull();
            dbIndex.Count().Should().Be(model.Indexes.Count + 1);

            return(dbIndex
                   .Where(x => x.Name != "_id_")
                   .OrderBy(x => x.Name)
                   .Zip(model.Indexes.OrderBy(x => x.Name), (d, m) => new { Db = d, Model = m })
                   .All(x => x.Model.IsEquals(x.Db)));
        }
Beispiel #2
0
        public async Task CreateSingleIndexTest()
        {
            await _documentServer.DropDatabase(_workContext, _dbName);

            IDocumentDatabase db = _documentServer.GetDatabase(_workContext, _dbName);
            await db.CreateCollection(_workContext, _collectionName);

            IDocumentCollection <TestDocument> collection = db.GetCollection <TestDocument>(_collectionName);

            IEnumerable <IndexDetailV1> list = await collection.Index.ListIndexes(_workContext);

            list.Should().NotBeNull();
            list.Count().Should().Be(1);

            list.First().Name.Should().Be("_id_");

            var model = new CollectionIndex
            {
                Name   = "TestIndex_1",
                Unique = true,
                Sparse = false,
                Keys   = new List <IndexKey>
                {
                    new IndexKey {
                        FieldName = "FirstName"
                    },
                    new IndexKey {
                        FieldName = "Last Name"
                    },
                }
            };

            await collection.Index.CreateIndex(_workContext, model);

            list = await collection.Index.ListIndexes(_workContext);

            list.Should().NotBeNull();
            list.Count().Should().Be(2);

            list.First().Name.Should().Be("_id_");

            IndexDetailV1 detail = list.Skip(1).First();

            detail.Name.Should().Be(model.Name);
            detail.Unique.Should().Be(model.Unique);
            detail.Sparse.Should().Be(model.Sparse);
            detail.Keys.Count.Should().Be(model.Keys.Count);
            detail.Keys[0].FieldName.Should().Be(model.Keys[0].FieldName);
            detail.Keys[0].Descending.Should().Be(model.Keys[0].Descending);
            detail.Keys[1].FieldName.Should().Be(model.Keys[1].FieldName);
            detail.Keys[1].Descending.Should().Be(model.Keys[1].Descending);

            await db.DropCollection(_workContext, _collectionName);

            await _documentServer.DropDatabase(_workContext, _dbName);
        }
Beispiel #3
0
        public async Task ApplyCappedCollectionModelWithIndex()
        {
            const int maxDocuments   = 100;
            const int maxSizeInBytes = maxDocuments * 1000;

            var model = new CappedCollectionModel
            {
                CollectionName       = _collectionName,
                MaxSizeInBytes       = maxSizeInBytes,
                MaxNumberOfDocuments = maxDocuments,
                Indexes = new List <CollectionIndex>
                {
                    new CollectionIndex
                    {
                        Name   = "TestIndex_1",
                        Unique = true,
                        Keys   = new List <IndexKey>
                        {
                            new IndexKey {
                                FieldName = "Field1", Descending = false
                            },
                        }
                    }
                }
            };

            var package = new CollectionModelPackage(_documentDatabase, model, new CollectionModelSettings {
                ReCreate = true
            });

            bool result = await package.Apply(_workContext);

            result.Should().BeTrue();

            (await _documentDatabase.CollectionExist(_workContext, _collectionName)).Should().BeTrue();
            CollectionDetailV1 detail = await _documentDatabase.GetCollectionDetail(_workContext, _collectionName);

            detail.Should().NotBeNull();
            detail.Name.Should().Be(_collectionName);
            detail.Type.Should().Be("collection");
            detail.Readonly.Should().BeFalse();
            detail.Capped.Should().BeTrue();
            detail.MaxDocuments.Should().HaveValue();
            detail.MaxDocuments.Should().Be(maxDocuments);
            detail.MaxSizeInBytes.Should().HaveValue();
            detail.MaxSizeInBytes.Should().BeGreaterOrEqualTo(maxSizeInBytes);

            IDocumentCollection <TestDocument> collection = _documentDatabase.GetCollection <TestDocument>(_collectionName);
            IndexDetailV1 indexDetail = await collection.Index.GetIndexDetail(_workContext, model.Indexes[0].Name);

            indexDetail.Should().NotBeNull();
            indexDetail.Keys.Count.Should().Be(1);

            model.Indexes[0].IsEquals(indexDetail).Should().BeTrue();
        }
Beispiel #4
0
        public async Task ApplyCollectionModelFull()
        {
            IDocumentDatabase db = _documentServer.GetDatabase(_workContext, _dbName);

            var model = new CollectionModel
            {
                CollectionName = _collectionName,
                Indexes        = new List <CollectionIndex>
                {
                    new CollectionIndex
                    {
                        Name   = "TestIndex_1",
                        Unique = true,
                        Keys   = new List <IndexKey>
                        {
                            new IndexKey {
                                FieldName = "Field1", Descending = false
                            },
                        }
                    }
                }
            };

            var package = new CollectionModelPackage(db, model, new CollectionModelSettings {
                ReCreate = true
            });

            bool result = await package.Apply(_workContext);

            result.Should().BeTrue();

            (await db.CollectionExist(_workContext, _collectionName)).Should().BeTrue();
            IDocumentCollection <TestDocument> collection = db.GetCollection <TestDocument>(_collectionName);

            IndexDetailV1 detail = await collection.Index.GetIndexDetail(_workContext, model.Indexes[0].Name);

            detail.Should().NotBeNull();
            detail.Keys.Count.Should().Be(1);

            model.Indexes[0].IsEquals(detail).Should().BeTrue();

            await db.DropCollection(_workContext, _collectionName);

            await _documentServer.DropDatabase(_workContext, _dbName);
        }
Beispiel #5
0
        /// <summary>
        /// List indexes
        /// </summary>
        /// <param name="context">work context</param>
        /// <returns>list of index details</returns>
        public async Task <IEnumerable <IndexDetailV1> > ListIndexes(IWorkContext context)
        {
            Verify.IsNotNull(nameof(context), context);

            var list = new List <IndexDetailV1>();

            using (var cursor = await Parent.MongoCollection.Indexes.ListAsync())
            {
                List <BsonDocument> resultList = await cursor.ToListAsync();

                foreach (var item in resultList)
                {
                    var indexDetail = new IndexDetailV1
                    {
                        Name      = item.Get <string>("name"),
                        Version   = item.Get <int>("v"),
                        Unique    = item.Get <bool>("unique", required: false),
                        Sparse    = item.Get <bool>("sparse", required: false),
                        Namespace = item.Get <string>("ns"),
                    };

                    list.Add(indexDetail);

                    BsonDocument keyDocument = (BsonDocument)item["key"];
                    var          keyList     = new List <IndexKey>();
                    foreach (var kValue in keyDocument.Elements)
                    {
                        var indexKey = new IndexKey
                        {
                            FieldName  = kValue.Name,
                            Descending = kValue.Value.AsInt32 == 1 ? false : true,
                        };

                        keyList.Add(indexKey);
                    }

                    indexDetail.Keys = keyList;
                }

                return(list);
            }
        }
        public async Task <bool> Test(IWorkContext context)
        {
            Verify.IsNotNull(nameof(context), context);
            context = context.WithTag(_tag);

            IDocumentCollection <BsonDocument> collection = Parent.Database.GetCollection <BsonDocument>(Parent.Model.CollectionName);

            IndexDetailV1 detail = await collection.Index.GetIndexDetail(context, Index.Name);

            if (detail == null)
            {
                MongoDbEventSource.Log.Info(context, $"Index {Index.Name} for collection {Parent.Model.CollectionName} does not exist");
                return(false);
            }

            bool status = Index.IsEquals(detail);

            MongoDbEventSource.Log.Info(context, $"Index {Index.Name} for collection {Parent.Model.CollectionName}, Equals={status}");
            return(status);
        }
Beispiel #7
0
        public static bool IsEquals(this CollectionIndex self, IndexDetailV1 detail)
        {
            bool equal = self.Name.Equals(detail.Name) &&
                         self.Unique == detail.Unique &&
                         self.Sparse == detail.Sparse &&
                         self.Keys?.Count == detail?.Keys.Count;

            if (!equal)
            {
                return(false);
            }

            if (self.Keys?.Count() > 0)
            {
                return(self.Keys
                       .OrderBy(x => x.FieldName)
                       .Zip(detail.Keys.OrderBy(x => x.FieldName), (f, s) => new { F = s, S = s })
                       .All(x => x.F.Equals(x.S)));
            }

            return(true);
        }