Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
Change the Cursor to auto close the database cursor after a documents…
Browse files Browse the repository at this point in the history
… where enumerated. This should prevent a lot open database cursors when people are forgetting to dispose the cursor.

Add KeepCursor method to the Cursor which can disable the above behavior.
  • Loading branch information
lanwin committed May 20, 2010
1 parent 148d713 commit 64c782d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
23 changes: 21 additions & 2 deletions source/MongoDB/Cursor_1.cs
Expand Up @@ -22,6 +22,7 @@ public class Cursor<T> : ICursor<T> where T : class
private QueryOptions _options;
private ReplyMessage<T> _reply;
private int _skip;
private bool _keepCursor;
private readonly ISerializationFactory _serializationFactory;

/// <summary>
Expand Down Expand Up @@ -164,7 +165,22 @@ public Cursor(ISerializationFactory serializationFactory, Connection connection,
TryModify();
AddOrRemoveSpecOpt("$hint", index);
return this;
}
}

/// <summary>
/// Keeps the cursor open.
/// </summary>
/// <param name="value">if set to <c>true</c> [value].</param>
/// <returns></returns>
/// <remarks>
/// By default cursors are closed automaticly after documents
/// are Enumerated.
/// </remarks>
public ICursor<T> KeepCursor(bool value)
{
_keepCursor = value;
return this;
}

/// <summary>
/// Snapshots the specified index.
Expand All @@ -173,7 +189,7 @@ public Cursor(ISerializationFactory serializationFactory, Connection connection,
TryModify();
AddOrRemoveSpecOpt("$snapshot", true);
return this;
}
}

/// <summary>
/// Explains this instance.
Expand Down Expand Up @@ -223,6 +239,9 @@ public Cursor(ISerializationFactory serializationFactory, Connection connection,
yield return document;
}
while(Id > 0 && _limit<CursorPosition);

if(!_keepCursor)
Dispose(true);
}
}

Expand Down
11 changes: 11 additions & 0 deletions source/MongoDB/ICursor_1.cs
Expand Up @@ -78,6 +78,17 @@ public interface ICursor<T> : IDisposable
/// <returns></returns>
ICursor<T> Hint(object index);

/// <summary>
/// Keeps the cursor open.
/// </summary>
/// <param name="value">if set to <c>true</c> [value].</param>
/// <returns></returns>
/// <remarks>
/// By default cursors are closed automaticly after documents
/// are Enumerated.
/// </remarks>
ICursor<T> KeepCursor(bool value);

/// <summary>
/// Snapshots this instance.
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions source/MongoDB/Obsolete/Cursor.cs
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;

namespace MongoDB
Expand Down Expand Up @@ -116,6 +117,21 @@ public long Id
return this;
}

/// <summary>
/// Keeps the cursor open.
/// </summary>
/// <param name="value">if set to <c>true</c> [value].</param>
/// <returns></returns>
/// <remarks>
/// By default cursors are closed automaticly after documents
/// are Enumerated.
/// </remarks>
public ICursor KeepCursor(bool value)
{
_cursor.KeepCursor(value);
return this;
}

/// <summary>
/// Snapshots this instance.
/// </summary>
Expand Down
15 changes: 13 additions & 2 deletions source/MongoDB/Obsolete/ICursor.cs
Expand Up @@ -74,9 +74,20 @@ public interface ICursor : IDisposable
/// Hints the specified index.
/// </summary>
/// <param name="index">The index.</param>
/// <returns></returns>
/// <returns></returns>
ICursor Hint(Document index);


/// <summary>
/// Keeps the cursor open.
/// </summary>
/// <param name="value">if set to <c>true</c> [value].</param>
/// <returns></returns>
/// <remarks>
/// By default cursors are closed automaticly after documents
/// are Enumerated.
/// </remarks>
ICursor KeepCursor(bool value);

/// <summary>
/// Snapshots this instance.
/// </summary>
Expand Down

0 comments on commit 64c782d

Please sign in to comment.