Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Implemented CSHARP-249. GetIndexes now returns GetIndexesResult. Use …
Browse files Browse the repository at this point in the history
…GetIndexes().RawDocuments if you still want the raw IEnumerable<BsonDocument> result.
  • Loading branch information
rstam committed Oct 11, 2011
1 parent 4ba580c commit 1ef39f0
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 26 deletions.
209 changes: 209 additions & 0 deletions Driver/Core/GetIndexesResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/* 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 {
/// <summary>
/// Represents the result of GetIndexes.
/// </summary>
public class GetIndexesResult : IEnumerable<IndexInfo> {
#region private fields
private BsonDocument[] documents;
private IndexInfo[] indexes;
#endregion

#region constructors
/// <summary>
/// Initializes a new instance of the GetIndexesResult class.
/// </summary>
public GetIndexesResult(
BsonDocument[] documents
) {
this.documents = documents;
this.indexes = this.documents.Select(d => new IndexInfo(d)).ToArray();
}
#endregion

#region public operators
/// <summary>
/// Gets the IndexInfo at the specified index.
/// </summary>
/// <param name="index">The zero-based index of the IndexInfo to get.</param>
/// <returns>An IndexInfo.</returns>
public IndexInfo this[int index] {
get { return indexes[index]; }
}
#endregion

#region public properties
/// <summary>
/// Gets the count of indexes.
/// </summary>
public int Count {
get { return indexes.Length; }
}

/// <summary>
/// Gets the raw BSON documents containing the information about the indexes.
/// </summary>
public IEnumerable<BsonDocument> RawDocuments {
get { return documents; }
}
#endregion

#region public methods
#endregion

#region explicit interface implementations
IEnumerator<IndexInfo> IEnumerable<IndexInfo>.GetEnumerator() {
return ((IEnumerable<IndexInfo>) indexes).GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator() {
return indexes.GetEnumerator();
}
#endregion
}

/// <summary>
/// Represents information about an index.
/// </summary>
public class IndexInfo {
#region private fields
private BsonDocument document;
#endregion

#region constructors
/// <summary>
/// Creates a new instance of the IndexInfo class.
/// </summary>
/// <param name="document">The BSON document that contains information about the index.</param>
public IndexInfo(
BsonDocument document
) {
this.document = document;
}
#endregion

#region public properties
/// <summary>
/// Gets whether the dups were dropped when the index was created.
/// </summary>
public bool DroppedDups {
get {
BsonValue value;
if (document.TryGetValue("dropDups", out value)) {
return value.ToBoolean();
} else {
return false;
}
}
}

/// <summary>
/// Gets whether the index was created in the background.
/// </summary>
public bool IsBackground {
get {
BsonValue value;
if (document.TryGetValue("background", out value)) {
return value.ToBoolean();
} else {
return false;
}
}
}

/// <summary>
/// Gets whether the index is sparse.
/// </summary>
public bool IsSparse {
get {
BsonValue value;
if (document.TryGetValue("sparse", out value)) {
return value.ToBoolean();
} else {
return false;
}
}
}

/// <summary>
/// Gets whether the index is unique.
/// </summary>
public bool IsUnique {
get {
BsonValue value;
if (document.TryGetValue("unique", out value)) {
return value.ToBoolean();
} else {
return false;
}
}
}

/// <summary>
/// Gets the key of the index.
/// </summary>
public IndexKeysDocument Key {
get {
return new IndexKeysDocument(document["key"].AsBsonDocument.Elements);
}
}

/// <summary>
/// Gets the name of the index.
/// </summary>
public string Name {
get {
return document["name"].AsString;
}
}

/// <summary>
/// Gets the namespace of the collection that the index is for.
/// </summary>
public string Namespace {
get {
return document["ns"].AsString;
}
}

/// <summary>
/// Gets the version of the index.
/// </summary>
public int Version {
get {
BsonValue value;
if (document.TryGetValue("v", out value)) {
return value.ToInt32();
} else {
return 0;
}
}
}
#endregion
}
}
16 changes: 6 additions & 10 deletions Driver/Core/MongoCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -683,10 +683,10 @@ IMongoGeoNearOptions options
/// Gets the indexes for this collection.
/// </summary>
/// <returns>A list of BsonDocuments that describe the indexes.</returns>
public virtual IEnumerable<BsonDocument> GetIndexes() {
public virtual GetIndexesResult GetIndexes() {
var indexes = database.GetCollection("system.indexes");
var query = Query.EQ("ns", FullName);
return indexes.Find(query).ToList(); // force query to execute before returning
return new GetIndexesResult(indexes.Find(query).ToArray()); // ToArray forces execution of the query
}

/// <summary>
Expand All @@ -704,10 +704,8 @@ public virtual CollectionStatsResult GetStats() {
/// <returns>The total data size.</returns>
public virtual long GetTotalDataSize() {
var totalSize = GetStats().DataSize;
var indexes = GetIndexes();
foreach (var index in indexes) {
var indexName = index["name"].AsString;
var indexCollectionName = string.Format("{0}.${1}", name, indexName);
foreach (var index in GetIndexes()) {
var indexCollectionName = string.Format("{0}.${1}", name, index.Name);
var indexCollection = database.GetCollection(indexCollectionName);
totalSize += indexCollection.GetStats().DataSize;
}
Expand All @@ -720,10 +718,8 @@ public virtual long GetTotalDataSize() {
/// <returns>The total storage size.</returns>
public virtual long GetTotalStorageSize() {
var totalSize = GetStats().StorageSize;
var indexes = GetIndexes();
foreach (var index in indexes) {
var indexName = index["name"].AsString;
var indexCollectionName = string.Format("{0}.${1}", name, indexName);
foreach (var index in GetIndexes()) {
var indexCollectionName = string.Format("{0}.${1}", name, index.Name);
var indexCollection = database.GetCollection(indexCollectionName);
totalSize += indexCollection.GetStats().StorageSize;
}
Expand Down
3 changes: 3 additions & 0 deletions Driver/Driver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<Compile Include="Core\GetIndexesResult.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
68 changes: 52 additions & 16 deletions DriverOnlineTests/Core/MongoCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,59 @@ public void TestCreateCollectionSetCappedSetMaxSize() {
[Test]
public void TestCreateIndex() {
collection.DropAllIndexes();
var indexes = collection.GetIndexes().ToArray();
Assert.AreEqual(1, indexes.Length);
Assert.AreEqual("_id_", indexes[0]["name"].AsString);
var indexes = collection.GetIndexes();
Assert.AreEqual(1, indexes.Count);
Assert.AreEqual(false, indexes[0].DroppedDups);
Assert.AreEqual(false, indexes[0].IsBackground);
Assert.AreEqual(false, indexes[0].IsSparse);
Assert.AreEqual(false, indexes[0].IsUnique);
Assert.AreEqual(new BsonDocument("_id", 1), indexes[0].Key);
Assert.AreEqual("_id_", indexes[0].Name);
Assert.AreEqual("onlinetests.testcollection", indexes[0].Namespace);
Assert.AreEqual(1, indexes[0].Version);

collection.DropAllIndexes();
collection.CreateIndex("x");
indexes = collection.GetIndexes().ToArray();
Assert.AreEqual(2, indexes.Length);
Assert.AreEqual("_id_", indexes[0]["name"].AsString);
Assert.AreEqual("x_1", indexes[1]["name"].AsString);
indexes = collection.GetIndexes();
Assert.AreEqual(2, indexes.Count);
Assert.AreEqual(false, indexes[0].DroppedDups);
Assert.AreEqual(false, indexes[0].IsBackground);
Assert.AreEqual(false, indexes[0].IsSparse);
Assert.AreEqual(false, indexes[0].IsUnique);
Assert.AreEqual(new BsonDocument("_id", 1), indexes[0].Key);
Assert.AreEqual("_id_", indexes[0].Name);
Assert.AreEqual("onlinetests.testcollection", indexes[0].Namespace);
Assert.AreEqual(1, indexes[0].Version);
Assert.AreEqual(false, indexes[1].DroppedDups);
Assert.AreEqual(false, indexes[1].IsBackground);
Assert.AreEqual(false, indexes[1].IsSparse);
Assert.AreEqual(false, indexes[1].IsUnique);
Assert.AreEqual(new BsonDocument("x", 1), indexes[1].Key);
Assert.AreEqual("x_1", indexes[1].Name);
Assert.AreEqual("onlinetests.testcollection", indexes[1].Namespace);
Assert.AreEqual(1, indexes[1].Version);

collection.DropAllIndexes();
collection.CreateIndex(IndexKeys.Ascending("x").Descending("y"), IndexOptions.SetUnique(true));
indexes = collection.GetIndexes().ToArray();
Assert.AreEqual(2, indexes.Length);
Assert.AreEqual("_id_", indexes[0]["name"].AsString);
Assert.AreEqual("x_1_y_-1", indexes[1]["name"].AsString);
Assert.AreEqual(true, indexes[1]["unique"].ToBoolean());
var options = IndexOptions.SetBackground(true).SetDropDups(true).SetSparse(true).SetUnique(true);
collection.CreateIndex(IndexKeys.Ascending("x").Descending("y"), options);
indexes = collection.GetIndexes();
Assert.AreEqual(2, indexes.Count);
Assert.AreEqual(false, indexes[0].DroppedDups);
Assert.AreEqual(false, indexes[0].IsBackground);
Assert.AreEqual(false, indexes[0].IsSparse);
Assert.AreEqual(false, indexes[0].IsUnique);
Assert.AreEqual(new BsonDocument("_id", 1), indexes[0].Key);
Assert.AreEqual("_id_", indexes[0].Name);
Assert.AreEqual("onlinetests.testcollection", indexes[0].Namespace);
Assert.AreEqual(1, indexes[0].Version);
Assert.AreEqual(true, indexes[1].DroppedDups);
Assert.AreEqual(true, indexes[1].IsBackground);
Assert.AreEqual(true, indexes[1].IsSparse);
Assert.AreEqual(true, indexes[1].IsUnique);
Assert.AreEqual(new BsonDocument { { "x", 1 }, { "y", -1 } }, indexes[1].Key);
Assert.AreEqual("x_1_y_-1", indexes[1].Name);
Assert.AreEqual("onlinetests.testcollection", indexes[1].Namespace);
Assert.AreEqual(1, indexes[1].Version);
}

[Test]
Expand Down Expand Up @@ -705,9 +740,10 @@ public void TestGeoNearSphericalTrue() {
[Test]
public void TestGetIndexes() {
collection.DropAllIndexes();
var indexes = collection.GetIndexes().ToArray();
Assert.AreEqual(1, indexes.Length);
Assert.AreEqual("_id_", indexes[0]["name"].AsString);
var indexes = collection.GetIndexes();
Assert.AreEqual(1, indexes.Count);
Assert.AreEqual("_id_", indexes[0].Name);
// see additional tests in TestCreateIndex
}

[Test]
Expand Down

0 comments on commit 1ef39f0

Please sign in to comment.