You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
6.1 KiB

  1. /* Copyright 2010-2011 10gen Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Text.RegularExpressions;
  21. using MongoDB.Bson;
  22. using MongoDB.Bson.Serialization;
  23. using MongoDB.Bson.IO;
  24. namespace MongoDB.Driver {
  25. /// <summary>
  26. /// Represents the result of GetIndexes.
  27. /// </summary>
  28. public class GetIndexesResult : IEnumerable<IndexInfo> {
  29. #region private fields
  30. private BsonDocument[] documents;
  31. private IndexInfo[] indexes;
  32. #endregion
  33. #region constructors
  34. /// <summary>
  35. /// Initializes a new instance of the GetIndexesResult class.
  36. /// </summary>
  37. public GetIndexesResult(
  38. BsonDocument[] documents
  39. ) {
  40. this.documents = documents;
  41. this.indexes = this.documents.Select(d => new IndexInfo(d)).ToArray();
  42. }
  43. #endregion
  44. #region public operators
  45. /// <summary>
  46. /// Gets the IndexInfo at the specified index.
  47. /// </summary>
  48. /// <param name="index">The zero-based index of the IndexInfo to get.</param>
  49. /// <returns>An IndexInfo.</returns>
  50. public IndexInfo this[int index] {
  51. get { return indexes[index]; }
  52. }
  53. #endregion
  54. #region public properties
  55. /// <summary>
  56. /// Gets the count of indexes.
  57. /// </summary>
  58. public int Count {
  59. get { return indexes.Length; }
  60. }
  61. /// <summary>
  62. /// Gets the raw BSON documents containing the information about the indexes.
  63. /// </summary>
  64. public IEnumerable<BsonDocument> RawDocuments {
  65. get { return documents; }
  66. }
  67. #endregion
  68. #region public methods
  69. #endregion
  70. #region explicit interface implementations
  71. IEnumerator<IndexInfo> IEnumerable<IndexInfo>.GetEnumerator() {
  72. return ((IEnumerable<IndexInfo>) indexes).GetEnumerator();
  73. }
  74. IEnumerator IEnumerable.GetEnumerator() {
  75. return indexes.GetEnumerator();
  76. }
  77. #endregion
  78. }
  79. /// <summary>
  80. /// Represents information about an index.
  81. /// </summary>
  82. public class IndexInfo {
  83. #region private fields
  84. private BsonDocument document;
  85. #endregion
  86. #region constructors
  87. /// <summary>
  88. /// Creates a new instance of the IndexInfo class.
  89. /// </summary>
  90. /// <param name="document">The BSON document that contains information about the index.</param>
  91. public IndexInfo(
  92. BsonDocument document
  93. ) {
  94. this.document = document;
  95. }
  96. #endregion
  97. #region public properties
  98. /// <summary>
  99. /// Gets whether the dups were dropped when the index was created.
  100. /// </summary>
  101. public bool DroppedDups {
  102. get {
  103. BsonValue value;
  104. if (document.TryGetValue("dropDups", out value)) {
  105. return value.ToBoolean();
  106. } else {
  107. return false;
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// Gets whether the index was created in the background.
  113. /// </summary>
  114. public bool IsBackground {
  115. get {
  116. BsonValue value;
  117. if (document.TryGetValue("background", out value)) {
  118. return value.ToBoolean();
  119. } else {
  120. return false;
  121. }
  122. }
  123. }
  124. /// <summary>
  125. /// Gets whether the index is sparse.
  126. /// </summary>
  127. public bool IsSparse {
  128. get {
  129. BsonValue value;
  130. if (document.TryGetValue("sparse", out value)) {
  131. return value.ToBoolean();
  132. } else {
  133. return false;
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// Gets whether the index is unique.
  139. /// </summary>
  140. public bool IsUnique {
  141. get {
  142. BsonValue value;
  143. if (document.TryGetValue("unique", out value)) {
  144. return value.ToBoolean();
  145. } else {
  146. return false;
  147. }
  148. }
  149. }
  150. /// <summary>
  151. /// Gets the key of the index.
  152. /// </summary>
  153. public IndexKeysDocument Key {
  154. get {
  155. return new IndexKeysDocument(document["key"].AsBsonDocument.Elements);
  156. }
  157. }
  158. /// <summary>
  159. /// Gets the name of the index.
  160. /// </summary>
  161. public string Name {
  162. get {
  163. return document["name"].AsString;
  164. }
  165. }
  166. /// <summary>
  167. /// Gets the namespace of the collection that the index is for.
  168. /// </summary>
  169. public string Namespace {
  170. get {
  171. return document["ns"].AsString;
  172. }
  173. }
  174. /// <summary>
  175. /// Gets the raw BSON document containing the index information.
  176. /// </summary>
  177. public BsonDocument RawDocument {
  178. get { return document; }
  179. }
  180. /// <summary>
  181. /// Gets the version of the index.
  182. /// </summary>
  183. public int Version {
  184. get {
  185. BsonValue value;
  186. if (document.TryGetValue("v", out value)) {
  187. return value.ToInt32();
  188. } else {
  189. return 0;
  190. }
  191. }
  192. }
  193. #endregion
  194. }
  195. }