本文整理匯總了Java中com.mongodb.ReadPreference.primary方法的典型用法代碼示例。如果您正苦於以下問題:Java ReadPreference.primary方法的具體用法?Java ReadPreference.primary怎麽用?Java ReadPreference.primary使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.mongodb.ReadPreference
的用法示例。
在下文中一共展示了ReadPreference.primary方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: readPreference
import com.mongodb.ReadPreference; //導入方法依賴的package包/類
/**
* Prepares {@link ReadPreference} from given {@link ReadPreferenceEnum}
*
* @param readPreferenceEnum Read preference enum value provided in config
* @return Read preference for mongo client options
*/
public static ReadPreference readPreference(ReadPreferenceEnum readPreferenceEnum) {
switch (readPreferenceEnum) {
case NEAREST:
return ReadPreference.nearest();
case PRIMARY:
return ReadPreference.primary();
case SECONDARY:
return ReadPreference.secondary();
case PRIMARY_PREFERRED:
return ReadPreference.primaryPreferred();
case SECONDARY_PREFERRED:
return ReadPreference.secondaryPreferred();
default:
return null;
}
}
示例2: getBlob
import com.mongodb.ReadPreference; //導入方法依賴的package包/類
private MongoBlob getBlob(String id, long lastMod) {
DBObject query = getBlobQuery(id, lastMod);
// try the secondary first
// TODO add a configuration option for whether to try reading from secondary
ReadPreference pref = ReadPreference.secondaryPreferred();
DBObject fields = new BasicDBObject();
fields.put(MongoBlob.KEY_DATA, 1);
MongoBlob blob = (MongoBlob) getBlobCollection().findOne(query, fields, pref);
if (blob == null) {
// not found in the secondary: try the primary
pref = ReadPreference.primary();
blob = (MongoBlob) getBlobCollection().findOne(query, fields, pref);
}
return blob;
}
示例3: getMongoReadPreference
import com.mongodb.ReadPreference; //導入方法依賴的package包/類
<T extends Document> ReadPreference getMongoReadPreference(Collection<T> collection,
String parentId,
DocumentReadPreference preference) {
switch(preference){
case PRIMARY:
return ReadPreference.primary();
case PREFER_PRIMARY :
return ReadPreference.primaryPreferred();
case PREFER_SECONDARY :
return getConfiguredReadPreference(collection);
case PREFER_SECONDARY_IF_OLD_ENOUGH:
if(collection != Collection.NODES){
return ReadPreference.primary();
}
//Default to primary preferred such that in case primary is being elected
//we can still read from secondary
//TODO REVIEW Would that be safe
ReadPreference readPreference = ReadPreference.primaryPreferred();
if (parentId != null) {
long replicationSafeLimit = getTime() - maxReplicationLagMillis;
NodeDocument cachedDoc = (NodeDocument) getIfCached(collection, parentId);
if (cachedDoc != null && !cachedDoc.hasBeenModifiedSince(replicationSafeLimit)) {
//If parent has been modified loooong time back then there children
//would also have not be modified. In that case we can read from secondary
readPreference = getConfiguredReadPreference(collection);
}
}
return readPreference;
default:
throw new IllegalArgumentException("Unsupported usage " + preference);
}
}
示例4: parse
import com.mongodb.ReadPreference; //導入方法依賴的package包/類
public static ReadPreference parse(String value) {
value = value.trim();
int paren = value.indexOf('(');
String pref;
List<TagSet> tags;
if (paren != -1) {
pref = value.substring(0, paren).trim();
String argsStr = value.substring(paren + 1).trim();
if (!argsStr.endsWith(")")) {
throw new InvalidReadPreference(value);
}
tags = parseArgs(argsStr.substring(0, argsStr.length() - 1));
} else {
pref = value;
tags = null;
}
switch (pref) {
case READ_PREFERENCE_NEAREST:
if (tags == null) {
return ReadPreference.nearest();
} else {
return ReadPreference.nearest(tags);
}
case READ_PREFERENCE_PRIMARY:
return ReadPreference.primary();
case READ_PREFERENCE_PRIMARY_PREFERRED:
if (tags == null) {
return ReadPreference.primaryPreferred();
} else {
return ReadPreference.primaryPreferred(tags);
}
case READ_PREFERENCE_SECONDARY:
if (tags == null) {
return ReadPreference.secondary();
} else {
return ReadPreference.secondary(tags);
}
case READ_PREFERENCE_SECONDARY_PREFERRED:
if (tags == null) {
return ReadPreference.secondaryPreferred();
} else {
return ReadPreference.secondaryPreferred(tags);
}
default:
throw new InvalidReadPreference(value);
}
}
示例5: getTaggableReadPreference
import com.mongodb.ReadPreference; //導入方法依賴的package包/類
ReadPreference getTaggableReadPreference()
{
ReadPreference readPref = getQueryReadPreference();
if( readPref == ReadPreference.primary() )
return readPref; // primary read preference mode does not apply tags
DBObject tagObjects = getReadPreferenceTagsAsParsedObject();
if( tagObjects == null )
return readPref; // no tags in read preference
List<TagSet> tagsList = new ArrayList<TagSet>( );
if ( tagObjects instanceof BasicDBList )
{
BasicDBList tagObjectList = (BasicDBList) tagObjects;
for ( Object obj : tagObjectList )
{
if ( obj instanceof DBObject )
{
tagsList.add( toTags( (DBObject) obj ) );
}
else
{ // ignore elements that are not DBObject
logInvalidTagValue( obj );
}
}
}
else
{
tagsList.add( toTags( tagObjects ) );
}
if ( tagsList.size( ) == 0 )
{
return readPref;
}
try
{
return ReadPreference.valueOf( readPref.getName( ), tagsList );
}
catch( RuntimeException ex )
{
// log and ignore tags
getLogger().info( ex.getLocalizedMessage() );
}
return readPref;
}