Class MongoDBConnection
- java.lang.Object
-
- org.nuxeo.ecm.core.storage.dbs.DBSConnectionBase
-
- org.nuxeo.ecm.core.storage.mongodb.MongoDBConnection
-
- All Implemented Interfaces:
AutoCloseable,LockManager,DBSConnection
public class MongoDBConnection extends DBSConnectionBase
MongoDB implementation of aDBSConnection.- Since:
- 11.1 (introduce in 5.9.4 as MongoDBRepository)
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.nuxeo.ecm.core.storage.dbs.DBSConnection
DBSConnection.DBSQueryOperator
-
-
Field Summary
Fields Modifier and Type Field Description protected com.mongodb.client.ClientSessionclientSessionprotected com.mongodb.client.MongoCollection<org.bson.Document>collprotected MongoDBConverterconverterprotected StringidKeyThe key to use to store the id in the database.protected static org.bson.conversions.BsonLOCK_FIELDSprotected MongoDBRepositorymongoDBRepositoryprotected static RandomRANDOMprotected static DurationRETRY_THRESHOLDprotected static DurationRETRY_TIMESLOTprotected longsequenceLastValueLast value or randomized value used from the in-memory sequence.protected longsequenceLeftNumber of values still available in the in-memory sequence.protected booleantransactionStartedprotected static intTRY_COUNTprotected static org.bson.conversions.BsonUNSET_LOCK_UPDATEprotected booleanuseCustomIdTrue if we don't use MongoDB's native "_id" key to store the id.-
Fields inherited from class org.nuxeo.ecm.core.storage.dbs.DBSConnectionBase
repository
-
-
Constructor Summary
Constructors Constructor Description MongoDBConnection(MongoDBRepository repository)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddIgnoredIds(org.bson.Document filter, Set<String> ignored)protected voidaddPrincipals(org.bson.Document query, Set<String> principals)voidbegin()Starts a new transaction.voidclose()Closes this connection.voidcommit()Commits the current transaction.protected longcountDocuments(org.bson.conversions.Bson filter)protected longcountDocuments(org.bson.conversions.Bson filter, com.mongodb.client.model.CountOptions options)voidcreateState(State state)Creates a document.voidcreateStates(List<State> states)Creates documents.protected com.mongodb.client.result.DeleteResultdeleteMany(org.bson.conversions.Bson filter)voiddeleteStates(Set<String> ids)Deletes a set of document.protected booleanexists(org.bson.conversions.Bson filter)protected booleanexists(org.bson.conversions.Bson filter, org.bson.conversions.Bson projection)protected com.mongodb.client.FindIterable<org.bson.Document>find(org.bson.conversions.Bson filter)protected List<State>findAll(org.bson.conversions.Bson filter)protected StatefindOne(org.bson.conversions.Bson filter)protected StatefindOne(org.bson.conversions.Bson filter, org.bson.conversions.Bson projection)StringgenerateNewId()Generates a new id for a document.protected org.bson.DocumentgetChildQuery(String parentId, String name, Set<String> ignored)Stream<State>getDescendants(String rootId, Set<String> keys)Returns a stream of descendants from a given root document, in no particular order.Stream<State>getDescendants(String rootId, Set<String> keys, int limit)Returns a stream of descendants from a given root document, in no particular order.LockgetLock(String id)Gets the lock on a document.protected longgetMaxTimeMs()protected longgetNextSequenceId()booleanhasChild(String parentId, String name, Set<String> ignored)Checks if a document has a child with the given nameprotected voidinitRepository(MongoDBRepositoryDescriptor descriptor)Initializes the MongoDB repositoryprotected voidinitRepositoryIndexes(MongoDBRepositoryDescriptor descriptor)protected voidinsertMany(List<org.bson.Document> documents)protected voidinsertOne(org.bson.Document document)protected org.bson.DocumentjustPresenceField()protected voidlogQuery(String id, org.bson.conversions.Bson fields)protected voidlogQuery(org.bson.conversions.Bson filter, org.bson.conversions.Bson fields)protected voidlogQuery(org.bson.conversions.Bson query, org.bson.conversions.Bson fields, org.bson.conversions.Bson orderBy, int limit, int offset)protected NuxeoExceptionnewQueryException(String message, com.mongodb.MongoException cause, org.bson.conversions.Bson filter)protected NuxeoExceptionnewQueryFailure(com.mongodb.MongoException cause, org.bson.conversions.Bson filter)protected NuxeoExceptionnewQueryTimeout(com.mongodb.MongoException cause, org.bson.conversions.Bson filter)protected NuxeoExceptionnewQueryTimeoutClient(com.mongodb.MongoException cause, org.bson.conversions.Bson filter)PartialList<Map<String,Serializable>>queryAndFetch(DBSExpressionEvaluator evaluator, OrderByClause orderByClause, boolean distinctDocuments, int limit, int offset, int countUpTo)Queries the repository for documents matching a NXQL query, and returns a projection of the documents.List<State>queryKeyValue(String key1, Object value1, String key2, Object value2, Set<String> ignored)Queries the repository for documents having key1 = value1 and key2 = value2.List<State>queryKeyValue(String key, Object value, Set<String> ignored)Queries the repository for documents having key = value.booleanqueryKeyValuePresence(String key, String value, Set<String> ignored)Queries the repository to check if there are documents having key = value.List<State>queryKeyValueWithOperator(String key1, Object value1, String key2, DBSConnection.DBSQueryOperator operator, Object value2, Set<String> ignored)Queries the repository for documents having key1 = value1 and key2 ${operator} value2.protected longrandomInitialSeed()Initial seed generation.StatereadChildState(String parentId, String name, Set<String> ignored)Reads the state of a child document.StatereadPartialState(String id, Collection<String> keys)Reads the partial state of a document.StatereadState(String id)Reads the state of a document.List<State>readStates(List<String> ids)Reads the states of several documents.LockremoveLock(String id, String owner)Removes the lock from a document.voidrollback()Rolls back the current transaction.ScrollResult<String>scroll(String scrollId)Get the next batch of results containing id of documents, thescrollIdis part of the previousScrollResultresponse.ScrollResult<String>scroll(DBSExpressionEvaluator evaluator, int batchSize, int keepAliveSeconds)Executes the given query and returns the first batch of results containing id of documents, next batch must be requested within thekeepAliveSecondsdelay.LocksetLock(String id, Lock lock)Sets a lock on a document.protected Stream<State>stream(org.bson.conversions.Bson filter)protected Stream<State>stream(org.bson.conversions.Bson filter, org.bson.conversions.Bson projection)protected Stream<State>stream(org.bson.conversions.Bson filter, org.bson.conversions.Bson projection, int limit)Logs, runs request and constructs a closeableStreamon top ofMongoCursor.protected LongtryUpdateRandomizedSequence()Updates the randomized sequence, using xorshift.protected com.mongodb.client.result.UpdateResultupdateMany(org.bson.conversions.Bson filter, org.bson.conversions.Bson update)protected longupdateRandomizedSequence()protected longupdateSequence()Allocates a new sequence block.voidupdateState(String id, State.StateDiff diff, DBSTransactionState.ConditionalUpdates conditionalUpdates)Updates a document.protected longxorshift(long n)xorshift algorithm from George Marsaglia, with period 2^64 - 1.protected longxorshift(long n, long times)Iterated version of xorshift.-
Methods inherited from class org.nuxeo.ecm.core.storage.dbs.DBSConnectionBase
getRootACP, getRootId, initRoot
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.nuxeo.ecm.core.api.lock.LockManager
clearLockManagerCaches, closeLockManager
-
-
-
-
Field Detail
-
RANDOM
protected static final Random RANDOM
-
mongoDBRepository
protected final MongoDBRepository mongoDBRepository
-
coll
protected final com.mongodb.client.MongoCollection<org.bson.Document> coll
-
idKey
protected final String idKey
The key to use to store the id in the database.
-
useCustomId
protected final boolean useCustomId
True if we don't use MongoDB's native "_id" key to store the id.
-
sequenceLeft
protected long sequenceLeft
Number of values still available in the in-memory sequence.
-
sequenceLastValue
protected long sequenceLastValue
Last value or randomized value used from the in-memory sequence.When used as a randomized sequence, this value (and the rest of the next block) may only be used after a successful update of the in-database version for the next task needing a randomized value.
-
converter
protected final MongoDBConverter converter
-
clientSession
protected com.mongodb.client.ClientSession clientSession
-
transactionStarted
protected boolean transactionStarted
-
TRY_COUNT
protected static final int TRY_COUNT
- See Also:
- Constant Field Values
-
RETRY_TIMESLOT
protected static final Duration RETRY_TIMESLOT
-
RETRY_THRESHOLD
protected static final Duration RETRY_THRESHOLD
-
LOCK_FIELDS
protected static final org.bson.conversions.Bson LOCK_FIELDS
-
UNSET_LOCK_UPDATE
protected static final org.bson.conversions.Bson UNSET_LOCK_UPDATE
-
-
Constructor Detail
-
MongoDBConnection
public MongoDBConnection(MongoDBRepository repository)
-
-
Method Detail
-
close
public void close()
Description copied from interface:DBSConnectionCloses this connection.
-
begin
public void begin()
Description copied from interface:DBSConnectionStarts a new transaction.- See Also:
DBSRepository.supportsTransactions()
-
commit
public void commit()
Description copied from interface:DBSConnectionCommits the current transaction.- See Also:
DBSRepository.supportsTransactions()
-
rollback
public void rollback()
Description copied from interface:DBSConnectionRolls back the current transaction.- See Also:
DBSRepository.supportsTransactions()
-
initRepository
protected void initRepository(MongoDBRepositoryDescriptor descriptor)
Initializes the MongoDB repository- Parameters:
descriptor- the MongoDB repository descriptor- Since:
- 11.1
-
initRepositoryIndexes
protected void initRepositoryIndexes(MongoDBRepositoryDescriptor descriptor)
-
getNextSequenceId
protected long getNextSequenceId()
-
updateSequence
protected long updateSequence()
Allocates a new sequence block. The database contains the last value from the last block.
-
tryUpdateRandomizedSequence
protected Long tryUpdateRandomizedSequence()
Updates the randomized sequence, using xorshift.
-
updateRandomizedSequence
protected long updateRandomizedSequence()
-
randomInitialSeed
protected long randomInitialSeed()
Initial seed generation.
-
xorshift
protected long xorshift(long n, long times)Iterated version of xorshift.
-
xorshift
protected long xorshift(long n)
xorshift algorithm from George Marsaglia, with period 2^64 - 1.- See Also:
- xorshift algorithm from George Marsaglia
-
generateNewId
public String generateNewId()
Description copied from interface:DBSConnectionGenerates a new id for a document.- Returns:
- the new id
-
createState
public void createState(State state)
Description copied from interface:DBSConnectionCreates a document.- Parameters:
state- the document state
-
createStates
public void createStates(List<State> states)
Description copied from interface:DBSConnectionCreates documents.- Parameters:
states- the document states
-
readState
public State readState(String id)
Description copied from interface:DBSConnectionReads the state of a document.- Parameters:
id- the document id- Returns:
- the document state, or
nullif not found
-
readPartialState
public State readPartialState(String id, Collection<String> keys)
Description copied from interface:DBSConnectionReads the partial state of a document.- Parameters:
id- the document idkeys- the keys to read- Returns:
- the document partial state, or
nullif not found
-
readStates
public List<State> readStates(List<String> ids)
Description copied from interface:DBSConnectionReads the states of several documents.The returned states may be in a different order than the ids.
- Parameters:
ids- the document ids- Returns:
- the document states, an element by be
nullif not found
-
updateState
public void updateState(String id, State.StateDiff diff, DBSTransactionState.ConditionalUpdates conditionalUpdates)
Description copied from interface:DBSConnectionUpdates a document.- Parameters:
id- the document iddiff- the diff to applyconditionalUpdates- the conditional updates, ornull
-
deleteStates
public void deleteStates(Set<String> ids)
Description copied from interface:DBSConnectionDeletes a set of document.- Parameters:
ids- the document ids
-
readChildState
public State readChildState(String parentId, String name, Set<String> ignored)
Description copied from interface:DBSConnectionReads the state of a child document.- Parameters:
parentId- the parent document idname- the name of the childignored- a set of document ids that should not be considered- Returns:
- the state of the child document, or
nullif not found
-
newQueryException
protected NuxeoException newQueryException(String message, com.mongodb.MongoException cause, org.bson.conversions.Bson filter)
-
newQueryTimeout
protected NuxeoException newQueryTimeout(com.mongodb.MongoException cause, org.bson.conversions.Bson filter)
-
newQueryTimeoutClient
protected NuxeoException newQueryTimeoutClient(com.mongodb.MongoException cause, org.bson.conversions.Bson filter)
-
newQueryFailure
protected NuxeoException newQueryFailure(com.mongodb.MongoException cause, org.bson.conversions.Bson filter)
-
logQuery
protected void logQuery(String id, org.bson.conversions.Bson fields)
-
logQuery
protected void logQuery(org.bson.conversions.Bson filter, org.bson.conversions.Bson fields)
-
logQuery
protected void logQuery(org.bson.conversions.Bson query, org.bson.conversions.Bson fields, org.bson.conversions.Bson orderBy, int limit, int offset)
-
hasChild
public boolean hasChild(String parentId, String name, Set<String> ignored)
Description copied from interface:DBSConnectionChecks if a document has a child with the given name- Parameters:
parentId- the parent document idname- the name of the childignored- a set of document ids that should not be considered- Returns:
trueif the child exists,falseif not
-
getChildQuery
protected org.bson.Document getChildQuery(String parentId, String name, Set<String> ignored)
-
queryKeyValue
public List<State> queryKeyValue(String key, Object value, Set<String> ignored)
Description copied from interface:DBSConnectionQueries the repository for documents having key = value.- Parameters:
key- the keyvalue- the valueignored- a set of document ids that should not be considered- Returns:
- the document states matching the query
-
queryKeyValue
public List<State> queryKeyValue(String key1, Object value1, String key2, Object value2, Set<String> ignored)
Description copied from interface:DBSConnectionQueries the repository for documents having key1 = value1 and key2 = value2.- Parameters:
key1- the first keyvalue1- the first valuekey2- the second keyvalue2- the second valueignored- a set of document ids that should not be considered- Returns:
- the document states matching the query
-
queryKeyValueWithOperator
public List<State> queryKeyValueWithOperator(String key1, Object value1, String key2, DBSConnection.DBSQueryOperator operator, Object value2, Set<String> ignored)
Description copied from interface:DBSConnectionQueries the repository for documents having key1 = value1 and key2 ${operator} value2.- Parameters:
key1- the first keyvalue1- the first valuekey2- the second keyoperator- the operator to apply between key2 and value2value2- the second valueignored- a set of document ids that should not be considered- Returns:
- the document states matching the query
-
getDescendants
public Stream<State> getDescendants(String rootId, Set<String> keys)
Description copied from interface:DBSConnectionReturns a stream of descendants from a given root document, in no particular order. This does not include information about the root document itself.THE STREAM MUST BE CLOSED WHEN DONE to release resources.
- Parameters:
rootId- the root document idkeys- what to collect about the descendants in addition to their ids- Returns:
- a stream of
States; THE STREAM MUST BE CLOSED WHEN DONE
-
getDescendants
public Stream<State> getDescendants(String rootId, Set<String> keys, int limit)
Description copied from interface:DBSConnectionReturns a stream of descendants from a given root document, in no particular order. This does not include information about the root document itself.THE STREAM MUST BE CLOSED WHEN DONE to release resources.
- Parameters:
rootId- the root document idkeys- what to collect about the descendants in addition to their idslimit- the maximum number of descendants to return- Returns:
- a stream of
States; THE STREAM MUST BE CLOSED WHEN DONE
-
queryKeyValuePresence
public boolean queryKeyValuePresence(String key, String value, Set<String> ignored)
Description copied from interface:DBSConnectionQueries the repository to check if there are documents having key = value.- Parameters:
key- the keyvalue- the valueignored- a set of document ids that should not be considered- Returns:
trueif the query matches at least one document,falseif the query matches nothing
-
exists
protected boolean exists(org.bson.conversions.Bson filter)
-
exists
protected boolean exists(org.bson.conversions.Bson filter, org.bson.conversions.Bson projection)
-
findOne
protected State findOne(org.bson.conversions.Bson filter)
-
findOne
protected State findOne(org.bson.conversions.Bson filter, org.bson.conversions.Bson projection)
-
stream
protected Stream<State> stream(org.bson.conversions.Bson filter, org.bson.conversions.Bson projection)
-
stream
protected Stream<State> stream(org.bson.conversions.Bson filter, org.bson.conversions.Bson projection, int limit)
Logs, runs request and constructs a closeableStreamon top ofMongoCursor.We should rely on this method, because it correctly handles cursor closed state.
Note: Looping on
FindIterableorMongoIterablecould lead to cursor leaks. This is also the case on some call toMongoIterable.first().- Returns:
- a closeable
Streaminstance linked toMongoCursor
-
justPresenceField
protected org.bson.Document justPresenceField()
-
queryAndFetch
public PartialList<Map<String,Serializable>> queryAndFetch(DBSExpressionEvaluator evaluator, OrderByClause orderByClause, boolean distinctDocuments, int limit, int offset, int countUpTo)
Description copied from interface:DBSConnectionQueries the repository for documents matching a NXQL query, and returns a projection of the documents.- Parameters:
evaluator- the map-based evaluator for the queryorderByClause- an ORDER BY clausedistinctDocuments-trueif the projection should return a maximum of one row per documentlimit- the limit on the number of documents to returnoffset- the offset in the list of documents to returncountUpTo- if-1, count the total size without offset/limit.
If0, don't count the total size, set it to-1.
Ifn, count the total number if there are less than n documents otherwise set the total size to-2.- Returns:
- a partial list of maps containing the NXQL projections requested, and the total size according to countUpTo
-
scroll
public ScrollResult<String> scroll(DBSExpressionEvaluator evaluator, int batchSize, int keepAliveSeconds)
Description copied from interface:DBSConnectionExecutes the given query and returns the first batch of results containing id of documents, next batch must be requested within thekeepAliveSecondsdelay.
-
scroll
public ScrollResult<String> scroll(String scrollId)
Description copied from interface:DBSConnectionGet the next batch of results containing id of documents, thescrollIdis part of the previousScrollResultresponse.
-
getLock
public Lock getLock(String id)
Description copied from interface:LockManagerGets the lock on a document.If the document does not exist,
nullis returned.- Parameters:
id- the document id- Returns:
- the existing lock, or
nullwhen there is no lock
-
setLock
public Lock setLock(String id, Lock lock)
Description copied from interface:LockManagerSets a lock on a document.If the document is already locked, returns its existing lock status (there is no re-locking,
LockManager.removeLock(java.lang.String, java.lang.String)must be called first).- Parameters:
id- the document idlock- the lock to set- Returns:
nullif locking succeeded, or the existing lock if locking failed
-
removeLock
public Lock removeLock(String id, String owner)
Description copied from interface:LockManagerRemoves the lock from a document.The previous lock is returned.
If
ownerisnullthen the lock is unconditionally removed.If
owneris notnull, it must match the existing lock owner for the lock to be removed. If it doesn't match, the returned lock will returntrueforLock.getFailed().- Parameters:
id- the document idowner- the owner to check, ornullfor no check- Returns:
- the previous lock (may be
null), with a failed flag if locking failed
-
insertOne
protected void insertOne(org.bson.Document document)
-
insertMany
protected void insertMany(List<org.bson.Document> documents)
-
updateMany
protected com.mongodb.client.result.UpdateResult updateMany(org.bson.conversions.Bson filter, org.bson.conversions.Bson update)
-
deleteMany
protected com.mongodb.client.result.DeleteResult deleteMany(org.bson.conversions.Bson filter)
-
find
protected com.mongodb.client.FindIterable<org.bson.Document> find(org.bson.conversions.Bson filter)
-
getMaxTimeMs
protected long getMaxTimeMs()
-
countDocuments
protected long countDocuments(org.bson.conversions.Bson filter)
-
countDocuments
protected long countDocuments(org.bson.conversions.Bson filter, com.mongodb.client.model.CountOptions options)
-
-