/* Copyright 2010-2011 10gen Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.IO; namespace MongoDB.Driver { /// /// Represents the result of GetIndexes. /// public class GetIndexesResult : IEnumerable { #region private fields private BsonDocument[] documents; private IndexInfo[] indexes; #endregion #region constructors /// /// Initializes a new instance of the GetIndexesResult class. /// public GetIndexesResult( BsonDocument[] documents ) { this.documents = documents; this.indexes = this.documents.Select(d => new IndexInfo(d)).ToArray(); } #endregion #region public operators /// /// Gets the IndexInfo at the specified index. /// /// The zero-based index of the IndexInfo to get. /// An IndexInfo. public IndexInfo this[int index] { get { return indexes[index]; } } #endregion #region public properties /// /// Gets the count of indexes. /// public int Count { get { return indexes.Length; } } /// /// Gets the raw BSON documents containing the information about the indexes. /// public IEnumerable RawDocuments { get { return documents; } } #endregion #region public methods #endregion #region explicit interface implementations IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable) indexes).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return indexes.GetEnumerator(); } #endregion } /// /// Represents information about an index. /// public class IndexInfo { #region private fields private BsonDocument document; #endregion #region constructors /// /// Creates a new instance of the IndexInfo class. /// /// The BSON document that contains information about the index. public IndexInfo( BsonDocument document ) { this.document = document; } #endregion #region public properties /// /// Gets whether the dups were dropped when the index was created. /// public bool DroppedDups { get { BsonValue value; if (document.TryGetValue("dropDups", out value)) { return value.ToBoolean(); } else { return false; } } } /// /// Gets whether the index was created in the background. /// public bool IsBackground { get { BsonValue value; if (document.TryGetValue("background", out value)) { return value.ToBoolean(); } else { return false; } } } /// /// Gets whether the index is sparse. /// public bool IsSparse { get { BsonValue value; if (document.TryGetValue("sparse", out value)) { return value.ToBoolean(); } else { return false; } } } /// /// Gets whether the index is unique. /// public bool IsUnique { get { BsonValue value; if (document.TryGetValue("unique", out value)) { return value.ToBoolean(); } else { return false; } } } /// /// Gets the key of the index. /// public IndexKeysDocument Key { get { return new IndexKeysDocument(document["key"].AsBsonDocument.Elements); } } /// /// Gets the name of the index. /// public string Name { get { return document["name"].AsString; } } /// /// Gets the namespace of the collection that the index is for. /// public string Namespace { get { return document["ns"].AsString; } } /// /// Gets the raw BSON document containing the index information. /// public BsonDocument RawDocument { get { return document; } } /// /// Gets the version of the index. /// public int Version { get { BsonValue value; if (document.TryGetValue("v", out value)) { return value.ToInt32(); } else { return 0; } } } #endregion } }