Skip to content

Commit

Permalink
Added support for the new bitwise-and and bitwise-or update modifiers…
Browse files Browse the repository at this point in the history
… to the Update builder.
  • Loading branch information
rstam committed Mar 18, 2011
1 parent 3258c50 commit 832dba8
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 18 deletions.
167 changes: 149 additions & 18 deletions Driver/Builders/UpdateBuilder.cs
Expand Up @@ -107,6 +107,58 @@ T value
return new UpdateBuilder().AddToSetWrapped<T>(name, value);
}

/// <summary>
/// Adds a bitwise and update modifier.
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be and-ed with the element.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder BitwiseAnd(
string name,
int value
) {
return new UpdateBuilder().BitwiseAnd(name, value);
}

/// <summary>
/// Adds a bitwise and update modifier.
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be and-ed with the element.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder BitwiseAnd(
string name,
long value
) {
return new UpdateBuilder().BitwiseAnd(name, value);
}

/// <summary>
/// Adds a bitwise or update modifier.
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be or-ed with the element.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder BitwiseOr(
string name,
int value
) {
return new UpdateBuilder().BitwiseOr(name, value);
}

/// <summary>
/// Adds a bitwise or update modifier.
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be or-ed with the element.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public static UpdateBuilder BitwiseOr(
string name,
long value
) {
return new UpdateBuilder().BitwiseOr(name, value);
}

/// <summary>
/// Adds a $inc update modifier.
/// </summary>
Expand Down Expand Up @@ -525,6 +577,62 @@ T value
return AddToSet(name, wrappedValue);
}

/// <summary>
/// Adds a bitwise and update modifier.
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be and-ed with the element.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder BitwiseAnd(
string name,
int value
) {
BitwiseOperation(name, "and", value);
return this;
}

/// <summary>
/// Adds a bitwise and update modifier.
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be and-ed with the element.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder BitwiseAnd(
string name,
long value
) {
BitwiseOperation(name, "and", value);
return this;
}

/// <summary>
/// Adds a bitwise or update modifier.
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be or-ed with the element.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder BitwiseOr(
string name,
int value
) {
BitwiseOperation(name, "or", value);
return this;
}

/// <summary>
/// Adds a bitwise or update modifier.
/// </summary>
/// <param name="name">The name of the element to be modified.</param>
/// <param name="value">The value to be or-ed with the element.</param>
/// <returns>The builder (so method calls can be chained).</returns>
public UpdateBuilder BitwiseOr(
string name,
long value
) {
BitwiseOperation(name, "or", value);
return this;
}

/// <summary>
/// Adds a $inc update modifier.
/// </summary>
Expand All @@ -535,12 +643,7 @@ T value
string name,
double value
) {
BsonElement element;
if (document.TryGetElement("$inc", out element)) {
element.Value.AsBsonDocument.Add(name, value);
} else {
document.Add("$inc", new BsonDocument(name, value));
}
Inc(name, BsonValue.Create(value));
return this;
}

Expand All @@ -554,12 +657,7 @@ T value
string name,
int value
) {
BsonElement element;
if (document.TryGetElement("$inc", out element)) {
element.Value.AsBsonDocument.Add(name, value);
} else {
document.Add("$inc", new BsonDocument(name, value));
}
Inc(name, BsonValue.Create(value));
return this;
}

Expand All @@ -573,12 +671,7 @@ int value
string name,
long value
) {
BsonElement element;
if (document.TryGetElement("$inc", out element)) {
element.Value.AsBsonDocument.Add(name, value);
} else {
document.Add("$inc", new BsonDocument(name, value));
}
Inc(name, BsonValue.Create(value));
return this;
}

Expand Down Expand Up @@ -926,5 +1019,43 @@ IBsonSerializationOptions options
document.Serialize(bsonWriter, nominalType, options);
}
#endregion

#region private methods
private void BitwiseOperation(
string name,
string operation,
BsonValue value
) {
BsonElement bitElement;
if (!document.TryGetElement("$bit", out bitElement)) {
bitElement = new BsonElement("$bit", new BsonDocument());
document.Add(bitElement);
}
var bitDocument = bitElement.Value.AsBsonDocument;

BsonElement fieldElement;
if (!bitDocument.TryGetElement(name, out fieldElement)) {
fieldElement = new BsonElement(name, new BsonDocument());
bitDocument.Add(fieldElement);
}
var fieldDocument = fieldElement.Value.AsBsonDocument;

fieldDocument.Add(operation, value);
}

private void Inc(
string name,
BsonValue value
) {
BsonElement incElement;
if (!document.TryGetElement("$inc", out incElement)) {
incElement = new BsonElement("$inc", new BsonDocument());
document.Add(incElement);
}
var incDocument = incElement.Value.AsBsonDocument;

incDocument.Add(name, value);
}
#endregion
}
}
63 changes: 63 additions & 0 deletions DriverUnitTests/Builders/UpdateBuilderTests.cs
Expand Up @@ -61,6 +61,69 @@ private class C {
Assert.AreEqual(expected, update.ToJson());
}

[Test]
public void TestBitwiseAndInt() {
var update = Update.BitwiseAnd("name", 1);
var expected = "{ '$bit' : { 'name' : { 'and' : 1 } } }".Replace("'", "\"");
Assert.AreEqual(expected, update.ToJson());
}

[Test]
public void TestBitwiseAndIntTwice() {
var update = Update.BitwiseAnd("x", 1).BitwiseAnd("y", 2);
var expected = "{ '$bit' : { 'x' : { 'and' : 1 }, 'y' : { 'and' : 2 } } }".Replace("'", "\"");
Assert.AreEqual(expected, update.ToJson());
}

[Test]
public void TestBitwiseAndLong() {
var update = Update.BitwiseAnd("name", 1L);
var expected = "{ '$bit' : { 'name' : { 'and' : 1 } } }".Replace("'", "\"");
Assert.AreEqual(expected, update.ToJson());
}

[Test]
public void TestBitwiseAndLongTwice() {
var update = Update.BitwiseAnd("x", 1L).BitwiseAnd("y", 2L);
var expected = "{ '$bit' : { 'x' : { 'and' : 1 }, 'y' : { 'and' : 2 } } }".Replace("'", "\"");
Assert.AreEqual(expected, update.ToJson());
}

[Test]
public void TestBitwiseAndOrInt() {
var update = Update.BitwiseAnd("x", 1L).BitwiseOr("x", 2L);
var expected = "{ '$bit' : { 'x' : { 'and' : 1, 'or' : 2 } } }".Replace("'", "\"");
Assert.AreEqual(expected, update.ToJson());
}

[Test]
public void TestBitwiseOrInt() {
var update = Update.BitwiseOr("name", 1);
var expected = "{ '$bit' : { 'name' : { 'or' : 1 } } }".Replace("'", "\"");
Assert.AreEqual(expected, update.ToJson());
}

[Test]
public void TestBitwiseOrIntTwice() {
var update = Update.BitwiseOr("x", 1).BitwiseOr("y", 2);
var expected = "{ '$bit' : { 'x' : { 'or' : 1 }, 'y' : { 'or' : 2 } } }".Replace("'", "\"");
Assert.AreEqual(expected, update.ToJson());
}

[Test]
public void TestBitwiseOrLong() {
var update = Update.BitwiseOr("name", 1L);
var expected = "{ '$bit' : { 'name' : { 'or' : 1 } } }".Replace("'", "\"");
Assert.AreEqual(expected, update.ToJson());
}

[Test]
public void TestBitwiseOrLongTwice() {
var update = Update.BitwiseOr("x", 1L).BitwiseOr("y", 2L);
var expected = "{ '$bit' : { 'x' : { 'or' : 1 }, 'y' : { 'or' : 2 } } }".Replace("'", "\"");
Assert.AreEqual(expected, update.ToJson());
}

[Test]
public void TestIncDouble() {
var update = Update.Inc("name", 1.1);
Expand Down

0 comments on commit 832dba8

Please sign in to comment.