本文整理汇总了C#中ReadPreference类的典型用法代码示例。如果您正苦于以下问题:C# ReadPreference类的具体用法?C# ReadPreference怎么用?C# ReadPreference使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ReadPreference类属于命名空间,在下文中一共展示了ReadPreference类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Equals_should_return_false_when_any_field_is_not_equal
public void Equals_should_return_false_when_any_field_is_not_equal(
[Values("mode", "tagSets")]
string notEqualFieldName)
{
var mode = ReadPreferenceMode.Secondary;
var tagSets = new[] { new TagSet(new[] { new Tag("name", "value1") }) };
var subject1 = new ReadPreference(mode, tagSets);
switch (notEqualFieldName)
{
case "mode": mode = ReadPreferenceMode.SecondaryPreferred; break;
case "tagSets": tagSets = new[] { new TagSet(new[] { new Tag("name", "value2") }) }; break;
default: throw new ArgumentException("notEqualFieldName");
}
var subject2 = new ReadPreference(mode, tagSets);
var result1 = subject1.Equals(subject2);
var result2 = subject1.Equals((object)subject2);
var hashCode1 = subject1.GetHashCode();
var hashCode2 = subject2.GetHashCode();
result1.Should().BeFalse();
result2.Should().BeFalse();
hashCode1.Should().NotBe(hashCode2);
}
示例2: CreateReadPreferenceDocument
public static BsonDocument CreateReadPreferenceDocument(ServerType serverType, ReadPreference readPreference)
{
if (serverType != ServerType.ShardRouter || readPreference == null)
{
return null;
}
BsonArray tagSets = null;
if (readPreference.TagSets != null && readPreference.TagSets.Count > 0)
{
tagSets = new BsonArray(readPreference.TagSets.Select(ts => new BsonDocument(ts.Tags.Select(t => new BsonElement(t.Name, t.Value)))));
}
// simple ReadPreferences of Primary and SecondaryPreferred are encoded in the slaveOk bit
if (readPreference.ReadPreferenceMode == ReadPreferenceMode.Primary || readPreference.ReadPreferenceMode == ReadPreferenceMode.SecondaryPreferred)
{
if (tagSets == null && !readPreference.MaxStaleness.HasValue)
{
return null;
}
}
var modeString = readPreference.ReadPreferenceMode.ToString();
modeString = Char.ToLowerInvariant(modeString[0]) + modeString.Substring(1);
return new BsonDocument
{
{ "mode", modeString },
{ "tags", tagSets, tagSets != null },
{ "maxStalenessSeconds", () => (int)readPreference.MaxStaleness.Value.TotalSeconds, readPreference.MaxStaleness.HasValue }
};
}
示例3: MongoDatabaseSettings
/// <summary>
/// Creates a new instance of MongoDatabaseSettings.
/// </summary>
/// <param name="databaseName">The name of the database.</param>
/// <param name="credentials">The credentials to access the database.</param>
/// <param name="guidRepresentation">The representation for Guids.</param>
/// <param name="readPreference">The read preference.</param>
/// <param name="writeConcern">The write concern to use.</param>
public MongoDatabaseSettings(
string databaseName,
MongoCredentials credentials,
GuidRepresentation guidRepresentation,
ReadPreference readPreference,
WriteConcern writeConcern)
{
if (databaseName == null)
{
throw new ArgumentNullException("databaseName");
}
if (databaseName == "admin" && credentials != null && !credentials.Admin)
{
throw new ArgumentOutOfRangeException("credentials", "Credentials for the admin database must have the admin flag set to true.");
}
if (readPreference == null)
{
throw new ArgumentNullException("readPreference");
}
if (writeConcern == null)
{
throw new ArgumentNullException("writeConcern");
}
_databaseName = databaseName;
_credentials = credentials;
_guidRepresentation = guidRepresentation;
_readPreference = readPreference;
_writeConcern = writeConcern;
}
示例4: TestCopyConstructor
public void TestCopyConstructor()
{
var other = new ReadPreference(ReadPreferenceMode.Nearest);
var subject = new ReadPreference(other);
Assert.AreEqual(subject, other);
}
示例5: GridFSBucketOptions
/// <summary>
/// Initializes a new instance of the <see cref="GridFSBucketOptions"/> class.
/// </summary>
/// <param name="other">The other <see cref="GridFSBucketOptions.Immutable"/> from which to copy the values.</param>
public GridFSBucketOptions(GridFSBucketOptions.Immutable other)
{
_bucketName = other.BucketName;
_chunkSizeBytes = other.ChunkSizeBytes;
_readPreference = other.ReadPreference;
_writeConcern = other.WriteConcern;
}
示例6: MongoServerSettings
// constructors
/// <summary>
/// Creates a new instance of MongoServerSettings. Usually you would use a connection string instead.
/// </summary>
public MongoServerSettings()
{
_connectionMode = ConnectionMode.Automatic;
_connectTimeout = MongoDefaults.ConnectTimeout;
_credentialsStore = new MongoCredentialsStore();
_defaultCredentials = null;
_guidRepresentation = MongoDefaults.GuidRepresentation;
_ipv6 = false;
_maxConnectionIdleTime = MongoDefaults.MaxConnectionIdleTime;
_maxConnectionLifeTime = MongoDefaults.MaxConnectionLifeTime;
_maxConnectionPoolSize = MongoDefaults.MaxConnectionPoolSize;
_minConnectionPoolSize = MongoDefaults.MinConnectionPoolSize;
_readPreference = ReadPreference.Primary;
_replicaSetName = null;
_secondaryAcceptableLatency = MongoDefaults.SecondaryAcceptableLatency;
_servers = new List<MongoServerAddress> { new MongoServerAddress("localhost") };
_socketTimeout = MongoDefaults.SocketTimeout;
_useSsl = false;
_verifySslCertificate = true;
_waitQueueSize = MongoDefaults.ComputedWaitQueueSize;
_waitQueueTimeout = MongoDefaults.WaitQueueTimeout;
#pragma warning disable 612, 618
_writeConcern = MongoDefaults.SafeMode.WriteConcern;
#pragma warning restore
}
示例7: constructor_should_initialize_instance_when_tagSets_is_null
public void constructor_should_initialize_instance_when_tagSets_is_null()
{
var result = new ReadPreference(ReadPreferenceMode.Secondary, null);
result.ReadPreferenceMode.Should().Be(ReadPreferenceMode.Secondary);
result.TagSets.Should().BeEmpty();
}
示例8: CreateReadPreferenceDocument
public static BsonDocument CreateReadPreferenceDocument(ServerType serverType, ReadPreference readPreference)
{
if (readPreference == null)
{
return null;
}
if (serverType != ServerType.ShardRouter)
{
return null;
}
BsonArray tagSets = null;
if (readPreference.TagSets != null && readPreference.TagSets.Any())
{
tagSets = new BsonArray(readPreference.TagSets.Select(ts => new BsonDocument(ts.Tags.Select(t => new BsonElement(t.Name, t.Value)))));
}
else if (readPreference.ReadPreferenceMode == ReadPreferenceMode.Primary || readPreference.ReadPreferenceMode == ReadPreferenceMode.SecondaryPreferred)
{
return null;
}
var readPreferenceString = readPreference.ReadPreferenceMode.ToString();
readPreferenceString = Char.ToLowerInvariant(readPreferenceString[0]) + readPreferenceString.Substring(1);
return new BsonDocument
{
{ "mode", readPreferenceString },
{ "tags", tagSets, tagSets != null }
};
}
示例9: MongoDatabaseSettings
/// <summary>
/// Creates a new instance of MongoDatabaseSettings.
/// </summary>
/// <param name="databaseName">The name of the database.</param>
/// <param name="credentials">The credentials to access the database.</param>
/// <param name="guidRepresentation">The representation for Guids.</param>
/// <param name="safeMode">The safe mode to use.</param>
/// <param name="slaveOk">Whether queries should be sent to secondary servers.</param>
public MongoDatabaseSettings(string databaseName, MongoCredentials credentials, GuidRepresentation guidRepresentation, SafeMode safeMode, ReadPreference readPreference)
{
this.databaseName = databaseName;
this.credentials = credentials;
this.guidRepresentation = guidRepresentation;
this.safeMode = safeMode;
this.readPreference = readPreference;
}
示例10: GridFSBucketOptions
/// <summary>
/// Initializes a new instance of the <see cref="GridFSBucketOptions"/> class.
/// </summary>
/// <param name="other">The other <see cref="ImmutableGridFSBucketOptions"/> from which to copy the values.</param>
public GridFSBucketOptions(ImmutableGridFSBucketOptions other)
{
Ensure.IsNotNull(other, nameof(other));
_bucketName = other.BucketName;
_chunkSizeBytes = other.ChunkSizeBytes;
_readConcern = other.ReadConcern;
_readPreference = other.ReadPreference;
_writeConcern = other.WriteConcern;
}
示例11: constructor_with_mode_should_initialize_instance
public void constructor_with_mode_should_initialize_instance()
{
var mode = ReadPreferenceMode.Secondary; // use a value that is not the default
var result = new ReadPreference(mode: mode);
result.ReadPreferenceMode.Should().Be(mode);
result.TagSets.Should().BeEmpty();
}
示例12: GetMongoClientSettings
/// <summary>
/// Returns a client settings object representing a connection
/// to the current MongoDB replica set. You should cache these connection
/// settings and re-obtain them only if there is a connection exception.
/// </summary>
/// <param name="readPreference">The required read preference.</param>
/// <param name="mongoWorkerRoleName">Name of the mongo worker role.</param>
/// <param name="replicaSetName">Name of the mongo replica set.</param>
/// <returns>
/// A MongoClientSettings object representing a replica set connection.
/// </returns>
/// <example>
/// <code>
/// var client = ConnectionUtilities.GetMongoClientSettings(ReadPreference.SecondaryPreferred);
/// var server = client.GetServer();
/// </code>
/// </example>
public static MongoClientSettings GetMongoClientSettings(ReadPreference readPreference, string mongoWorkerRoleName, string replicaSetName)
{
return new MongoClientSettings
{
ReadPreference = readPreference,
ReplicaSetName = replicaSetName,
Servers = GetServerAddresses(mongoWorkerRoleName, replicaSetName)
};
}
示例13: constructor_with_tagSets_should_initialize_instance
public void constructor_with_tagSets_should_initialize_instance()
{
var mode = ReadPreferenceMode.Secondary; // can't use tagSets with mode Primary
var tagSets = new[] { new TagSet(new[] { new Tag("name", "value") }) };
var result = new ReadPreference(mode: mode, tagSets: tagSets);
result.ReadPreferenceMode.Should().Be(mode);
result.TagSets.Should().NotBeSameAs(tagSets);
result.TagSets.Should().Equal(tagSets);
}
示例14: TestEquals
public void TestEquals()
{
ReadPreference primary = ReadPreference.Primary;
ReadPreference secondary = ReadPreference.Secondary;
ReadPreference voidTag = new ReadPreference(new HashSet<string>());
ReadPreference someTags = new ReadPreference(new HashSet<string>());
someTags.Tags.Add("new york");
someTags.Tags.Add("rack 1");
ReadPreference someTagsBis = new ReadPreference(new HashSet<string>());
someTagsBis.Tags.Add("rack 1");
someTagsBis.Tags.Add("new york");
ReadPreference someTagsAgain = new ReadPreference(new HashSet<string>());
someTagsAgain.Tags.Add("palo alto");
someTagsAgain.Tags.Add("rack 1");
//Equals to self
Assert.AreEqual(true, primary.Equals(primary));
Assert.AreEqual(true, secondary.Equals(secondary));
Assert.AreEqual(true, voidTag.Equals(voidTag));
Assert.AreEqual(true, someTags.Equals(someTags));
Assert.AreEqual(true, someTagsAgain.Equals(someTagsAgain));
//Basic stuff are not equal
Assert.AreEqual(false, primary.Equals(secondary));
Assert.AreEqual(false, secondary.Equals(primary));
Assert.AreEqual(false, primary.Equals(voidTag));
Assert.AreEqual(false, voidTag.Equals(primary));
Assert.AreEqual(false, primary.Equals(secondary));
Assert.AreEqual(false, secondary.Equals(primary));
Assert.AreEqual(false, primary.Equals(someTags));
Assert.AreEqual(false, someTags.Equals(primary));
Assert.AreEqual(false, primary.Equals(someTagsAgain));
Assert.AreEqual(false, someTagsAgain.Equals(primary));
Assert.AreEqual(false, someTags.Equals(secondary));
Assert.AreEqual(false, secondary.Equals(someTags));
Assert.AreEqual(true, someTags.Equals(someTagsBis));
Assert.AreEqual(true, someTagsBis.Equals(someTags));
//two object equals constructed separately are equals
Assert.AreEqual(true, someTags.Equals(someTagsBis));
Assert.AreEqual(true, someTagsBis.Equals(someTags));
//default tagged is not tagged but defaulted to secondary ok
Assert.AreEqual(true, voidTag.Equals(secondary));
Assert.AreEqual(true, secondary.Equals(voidTag));
Assert.AreEqual(false, someTagsAgain.Equals(secondary));
Assert.AreEqual(false, secondary.Equals(someTagsAgain));
}
示例15: ReadPreferenceServerSelector
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="ReadPreferenceServerSelector"/> class.
/// </summary>
/// <param name="readPreference">The read preference.</param>
public ReadPreferenceServerSelector(ReadPreference readPreference)
{
_readPreference = Ensure.IsNotNull(readPreference, nameof(readPreference));
if (readPreference.MaxStaleness == Timeout.InfiniteTimeSpan)
{
_maxStaleness = null;
}
else
{
_maxStaleness = readPreference.MaxStaleness;
}
}