3939
4040public abstract class AbstractMongoDatabase <P > implements MongoDatabase {
4141
42- private static final String NAMESPACES_COLLECTION_NAME = "system.namespaces" ;
42+ protected static final String NAMESPACES_COLLECTION_NAME = "system.namespaces" ;
4343
44- private static final String INDEXES_COLLECTION_NAME = "system.indexes" ;
44+ protected static final String INDEXES_COLLECTION_NAME = "system.indexes" ;
4545
4646 private static final Logger log = LoggerFactory .getLogger (AbstractMongoDatabase .class );
4747
@@ -94,17 +94,17 @@ public String toString() {
9494 return getClass ().getSimpleName () + "(" + getDatabaseName () + ")" ;
9595 }
9696
97- @ Override
98- public Document handleCommand (Channel channel , String command , Document query , Oplog oplog ) {
97+ protected Document commandError (Channel channel , String command , Document query ) {
9998 // getlasterror must not clear the last error
10099 if (command .equalsIgnoreCase ("getlasterror" )) {
101100 return commandGetLastError (channel , command , query );
102101 } else if (command .equalsIgnoreCase ("reseterror" )) {
103102 return commandResetError (channel );
104103 }
104+ return null ;
105+ }
105106
106- clearLastStatus (channel );
107-
107+ protected Document handleSupportedCommand (Channel channel , String command , Document query , Oplog oplog ) {
108108 if (command .equalsIgnoreCase ("find" )) {
109109 return commandFind (command , query );
110110 } else if (command .equalsIgnoreCase ("insert" )) {
@@ -163,6 +163,18 @@ public Document handleCommand(Channel channel, String command, Document query, O
163163 throw new NoSuchCommandException (command );
164164 }
165165
166+ @ Override
167+ public Document handleCommand (Channel channel , String command , Document query , Oplog oplog ) {
168+ Document commandErrorDocument = commandError (channel , command , query );
169+ if (commandErrorDocument != null ) {
170+ return commandErrorDocument ;
171+ }
172+
173+ clearLastStatus (channel );
174+
175+ return handleSupportedCommand (channel , command , query , oplog );
176+ }
177+
166178 private Document listCollections () {
167179 List <Document > firstBatch = new ArrayList <>();
168180 for (String namespace : listCollectionNamespaces ()) {
@@ -176,8 +188,7 @@ private Document listCollections() {
176188 collectionDescription .put ("options" , collectionOptions );
177189 collectionDescription .put ("info" , new Document ("readOnly" , false ));
178190 collectionDescription .put ("type" , "collection" );
179- collectionDescription .put ("idIndex" , getPrimaryKeyIndexDescription (namespace )
180- );
191+ collectionDescription .put ("idIndex" , getPrimaryKeyIndexDescription (namespace ));
181192 firstBatch .add (collectionDescription );
182193 }
183194
@@ -237,7 +248,7 @@ private static QueryParameters toQueryParameters(Document query) {
237248 return new QueryParameters (querySelector , numberToSkip , numberToReturn , batchSize , projection );
238249 }
239250
240- private QueryParameters toQueryParameters (MongoQuery query , int numberToSkip , int batchSize ) {
251+ private static QueryParameters toQueryParameters (MongoQuery query , int numberToSkip , int batchSize ) {
241252 return new QueryParameters (query .getQuery (), numberToSkip , 0 , batchSize , query .getReturnFieldSelector ());
242253 }
243254
@@ -851,12 +862,12 @@ private Document updateDocuments(String collectionName, Document selector,
851862 return collection .updateDocuments (selector , update , arrayFilters , multi , upsert , oplog );
852863 }
853864
854- private void putLastError (Channel channel , MongoServerException ex ) {
865+ protected void putLastError (Channel channel , MongoServerException ex ) {
855866 Document error = toError (channel , ex );
856867 putLastResult (channel , error );
857868 }
858869
859- private Document toWriteError (int index , MongoServerException e ) {
870+ protected Document toWriteError (int index , MongoServerException e ) {
860871 Document error = new Document ();
861872 error .put ("index" , index );
862873 error .put ("errmsg" , e .getMessageWithoutErrorCode ());
@@ -976,7 +987,7 @@ protected String getFullCollectionNamespace(String collectionName) {
976987 return getDatabaseName () + "." + collectionName ;
977988 }
978989
979- static boolean isSystemCollection (String collectionName ) {
990+ protected static boolean isSystemCollection (String collectionName ) {
980991 return collectionName .startsWith ("system." );
981992 }
982993
0 commit comments