public class ReadPreference extends Object implements Serializable, DocumentAssignable
ReadPreference.Mode
and a set of tag matching
documents. The ReadPreference.Mode
specified if primary and/or secondary servers in
a replica set can be used and which should be tried first. The tag matching
documents control which secondary servers can be used.
Each tag matching document specified the minimum set of key/values that the
secondary server must be tagged with. As an example a tag matching document
of { a : 1, b : 2 }
would match a server with the tags
{ a : 1, b : 2, c : 3 }
but would not match a server with tags
{ a : 1, c : 3, d : 4 }
.
The tag matching documents and server tags must match exactly so a server
with tags { a: true, b : 2 }
would not match the
{ a : 1, b : 2 }
tag matching document. Neither would a server
with the tags { c: 1, b : 2 }
.
{ a : 1, b : 2 }
and { a : 1, c : 3 }
are
provided then both the server { a : 1, b : 2, c : 3 }
and
{ a : 1, c : 3, d : 4 }
would match.
If no tag matching documents are specified then all secondary servers may be used.
Modifier and Type | Class and Description |
---|---|
static class |
ReadPreference.Mode
Enumeration of the basic
ReadPreference modes of service. |
Modifier and Type | Field and Description |
---|---|
static ReadPreference |
CLOSEST
ReadPreference to read from the closest/ReadPreference.Mode.NEAREST
primary of secondary server. |
static String |
FIELD_NAME
The name of the field in the query document for the read preferences.
|
static ReadPreference |
PREFER_PRIMARY
ReadPreference to prefer reading from the primary but to fallback
to a secondary if the primary is not available. |
static ReadPreference |
PREFER_SECONDARY
ReadPreference to prefer reading from a secondary but to
'fallback' to a primary if a secondary is not available. |
static ReadPreference |
PRIMARY
The default
ReadPreference to read from the primary only. |
static ReadPreference |
SECONDARY
ReadPreference to read only from a secondary but using any
secondary. |
Modifier | Constructor and Description |
---|---|
protected |
ReadPreference(ReadPreference.Mode mode,
DocumentAssignable... tagMatchDocuments)
Creates a new ReadPreference.
|
protected |
ReadPreference(ReadPreference.Mode mode,
String address)
Creates a new ReadPreference.
|
Modifier and Type | Method and Description |
---|---|
Document |
asDocument()
Converts the object into a document.
|
static ReadPreference |
closest(DocumentAssignable... tagMatchDocuments)
Creates a
ReadPreference to read from the closest/
ReadPreference.Mode.NEAREST primary of secondary server. |
boolean |
equals(Object object)
Determines if the passed object is of this same type as this object and
if so that its fields are equal.
|
ReadPreference.Mode |
getMode()
Returns the read preference mode controlling if primary or secondary
servers can be used and which to prefer.
|
String |
getServer()
Returns the server to read from.
|
List<Document> |
getTagMatchingDocuments()
Returns the list of tag matching documents to control the secondaries
used.
|
int |
hashCode()
Computes a reasonable hash code.
|
boolean |
isLegacy()
Returns true if the read preference is compatible with the legacy
"slaveOk", e.g., is one of
ReadPreference.Mode.PRIMARY_ONLY ,
ReadPreference.Mode.SECONDARY_ONLY , or ReadPreference.Mode.SERVER and has no tag
matching documents. |
boolean |
isSecondaryOk()
Returns true if the mode allows reading from secondaries, false
otherwise.
|
boolean |
matches(Document tags)
Returns true if this
ReadPreference matches the tags
document. |
protected boolean |
nullSafeEquals(Object rhs,
Object lhs)
Does a null safe equals comparison.
|
static ReadPreference |
preferPrimary(DocumentAssignable... tagMatchDocuments)
Creates a
ReadPreference to prefer reading from the primary but
to fallback to a secondary if the primary is not available. |
static ReadPreference |
preferSecondary(DocumentAssignable... tagMatchDocuments)
Creates a
ReadPreference to prefer reading from a secondary but
to 'fallback' to a primary if a secondary is not available. |
static ReadPreference |
primary()
Returns the default
ReadPreference to read from the primary only:
PRIMARY . |
static ReadPreference |
secondary(DocumentAssignable... tagMatchDocuments)
Creates a
ReadPreference to read only from a secondary. |
static ReadPreference |
server(String address)
Creates a
ReadPreference to read only from a specific server. |
String |
toString() |
public static final ReadPreference CLOSEST
ReadPreference
to read from the closest/ReadPreference.Mode.NEAREST
primary of secondary server.public static final String FIELD_NAME
public static final ReadPreference PREFER_PRIMARY
ReadPreference
to prefer reading from the primary but to fallback
to a secondary if the primary is not available.public static final ReadPreference PREFER_SECONDARY
ReadPreference
to prefer reading from a secondary but to
'fallback' to a primary if a secondary is not available.public static final ReadPreference PRIMARY
ReadPreference
to read from the primary only.public static final ReadPreference SECONDARY
ReadPreference
to read only from a secondary but using any
secondary.protected ReadPreference(ReadPreference.Mode mode, DocumentAssignable... tagMatchDocuments)
mode
- The read preference mode controlling if primary or secondary
servers can be used and which to prefer.tagMatchDocuments
- Set of tag matching "documents" controlling which secondary
servers are used.protected ReadPreference(ReadPreference.Mode mode, String address)
mode
- The read preference mode controlling if primary or secondary
servers can be used and which to prefer.address
- The server to read from.public static ReadPreference closest(DocumentAssignable... tagMatchDocuments)
ReadPreference
to read from the closest/
ReadPreference.Mode.NEAREST
primary of secondary server.
If tag matching documents are specified then only servers matching the specified tag matching documents would be used.
If no tag matching documents are specified then returns CLOSEST
.
tagMatchDocuments
- Set of tag matching "documents" controlling which servers are
used.ReadPreference
.public static ReadPreference preferPrimary(DocumentAssignable... tagMatchDocuments)
ReadPreference
to prefer reading from the primary but
to fallback to a secondary if the primary is not available.
If tag matching documents are specified then only secondary servers matching the specified tag matching documents would be used.
If no tag matching documents are specified then returns
PREFER_PRIMARY
.
tagMatchDocuments
- Set of tag matching "documents" controlling which secondary
servers are used.ReadPreference
.public static ReadPreference preferSecondary(DocumentAssignable... tagMatchDocuments)
ReadPreference
to prefer reading from a secondary but
to 'fallback' to a primary if a secondary is not available.
If tag matching documents are specified then only secondary servers matching the specified tag matching documents would be used.
If no tag matching documents are specified then returns
PREFER_SECONDARY
.
tagMatchDocuments
- Set of tag matching "documents" controlling which secondary
servers are used.ReadPreference
.public static ReadPreference primary()
ReadPreference
to read from the primary only:
PRIMARY
.PRIMARY
ReadPreference
.public static ReadPreference secondary(DocumentAssignable... tagMatchDocuments)
ReadPreference
to read only from a secondary.
If tag matching documents are specified then only secondary servers matching the specified tag matching documents would be used.
If no tag matching documents are specified then returns
PREFER_SECONDARY
.
tagMatchDocuments
- Set of tag matching "documents" controlling which secondary
servers are used.ReadPreference
.public static ReadPreference server(String address)
ReadPreference
to read only from a specific server.
Used by the MongoIterator
to ensure cursor fetch and terminate
requests use the originating server.
Note: Use this form of ReadPreference
with caution. If the
specified server fails all requests will fail.
address
- The server to read from.ReadPreference
.public Document asDocument()
Overridden to return the read preference document.
The document contains:
mode
string value as returned by ReadPreference.Mode.getToken()
.
tags
array containing the
tag matching documents
server
containing the server to read from. This
should only used for cursors to ensure the correct server is used to
request more documents.asDocument
in interface DocumentAssignable
public boolean equals(Object object)
equals
in class Object
object
- The object to compare to.Object.equals(java.lang.Object)
public ReadPreference.Mode getMode()
public String getServer()
MongoIterator
to
ensure cursor fetch and terminate requests use the originating server.public List<Document> getTagMatchingDocuments()
public int hashCode()
public boolean isLegacy()
ReadPreference.Mode.PRIMARY_ONLY
,
ReadPreference.Mode.SECONDARY_ONLY
, or ReadPreference.Mode.SERVER
and has no tag
matching documents.public boolean isSecondaryOk()
public boolean matches(Document tags)
ReadPreference
matches the tags
document.tags
- The tags to be matched against.ReadPreference
matches the tags, false
otherwise.public String toString()
Overridden to return a string representation of the read preference..
Copyright © 2011–2014 Allanbank Consulting, Inc.. All rights reserved.