From 1aa6f72085f577ce9af70086eae50ab5ab59343e Mon Sep 17 00:00:00 2001 From: Guenter Obiltschnig Date: Mon, 13 Feb 2017 15:53:08 +0100 Subject: [PATCH] MongoDB: fixes for style and consistency --- MongoDB/include/Poco/MongoDB/Array.h | 40 +++++---- MongoDB/include/Poco/MongoDB/BSONReader.h | 15 ++-- MongoDB/include/Poco/MongoDB/BSONWriter.h | 16 ++-- MongoDB/include/Poco/MongoDB/Binary.h | 31 ++++--- MongoDB/include/Poco/MongoDB/Connection.h | 62 +++++++++----- MongoDB/include/Poco/MongoDB/Cursor.h | 28 ++++--- MongoDB/include/Poco/MongoDB/Database.h | 35 +++++--- MongoDB/include/Poco/MongoDB/DeleteRequest.h | 49 ++++++----- MongoDB/include/Poco/MongoDB/Document.h | 49 ++++++----- MongoDB/include/Poco/MongoDB/Element.h | 25 +++--- MongoDB/include/Poco/MongoDB/GetMoreRequest.h | 22 ++--- MongoDB/include/Poco/MongoDB/InsertRequest.h | 35 +++++--- MongoDB/include/Poco/MongoDB/JavaScriptCode.h | 22 +++-- .../include/Poco/MongoDB/KillCursorsRequest.h | 13 +-- MongoDB/include/Poco/MongoDB/Message.h | 11 ++- MongoDB/include/Poco/MongoDB/MessageHeader.h | 51 +++++------ MongoDB/include/Poco/MongoDB/ObjectId.h | 37 ++++---- .../Poco/MongoDB/PoolableConnectionFactory.h | 13 ++- MongoDB/include/Poco/MongoDB/QueryRequest.h | 81 ++++++++++++------ .../include/Poco/MongoDB/RegularExpression.h | 23 ++--- MongoDB/include/Poco/MongoDB/ReplicaSet.h | 15 ++-- MongoDB/include/Poco/MongoDB/RequestMessage.h | 14 ++-- .../include/Poco/MongoDB/ResponseMessage.h | 32 +++---- MongoDB/include/Poco/MongoDB/UpdateRequest.h | 48 ++++++----- MongoDB/src/Array.cpp | 23 +++-- MongoDB/src/Binary.cpp | 6 +- MongoDB/src/Connection.cpp | 46 +++++++--- MongoDB/src/Cursor.cpp | 15 ++-- MongoDB/src/Database.cpp | 84 ++++++------------- MongoDB/src/DeleteRequest.cpp | 12 ++- MongoDB/src/Document.cpp | 64 +++++++------- MongoDB/src/Element.cpp | 2 - MongoDB/src/GetMoreRequest.cpp | 6 +- MongoDB/src/InsertRequest.cpp | 10 +-- MongoDB/src/JavaScriptCode.cpp | 2 - MongoDB/src/KillCursorsRequest.cpp | 8 +- MongoDB/src/Message.cpp | 5 +- MongoDB/src/MessageHeader.cpp | 10 +-- MongoDB/src/ObjectId.cpp | 19 +++-- MongoDB/src/QueryRequest.cpp | 8 +- MongoDB/src/RegularExpression.cpp | 10 +-- MongoDB/src/ReplicaSet.cpp | 22 ++--- MongoDB/src/RequestMessage.cpp | 5 +- MongoDB/src/ResponseMessage.cpp | 11 ++- MongoDB/src/UpdateRequest.cpp | 6 +- MongoDB/testsuite/src/MongoDBTest.cpp | 11 ++- MongoDB/testsuite/src/MongoDBTest.h | 4 - 47 files changed, 642 insertions(+), 514 deletions(-) diff --git a/MongoDB/include/Poco/MongoDB/Array.h b/MongoDB/include/Poco/MongoDB/Array.h index 4e139a1f6..bc2b588c6 100644 --- a/MongoDB/include/Poco/MongoDB/Array.h +++ b/MongoDB/include/Poco/MongoDB/Array.h @@ -15,62 +15,68 @@ // SPDX-License-Identifier: BSL-1.0 // + #ifndef MongoDB_Array_INCLUDED #define MongoDB_Array_INCLUDED -#include "Poco/NumberFormatter.h" +#include "Poco/NumberFormatter.h" #include "Poco/MongoDB/MongoDB.h" #include "Poco/MongoDB/Document.h" + namespace Poco { namespace MongoDB { -class MongoDB_API Array : public Document - /// Implements the BSON Array + +class MongoDB_API Array: public Document + /// This class represents a BSON Array. { public: typedef SharedPtr Ptr; Array(); - /// Constructor + /// Creates an empty Array. virtual ~Array(); - /// Destructor + /// Destroys the Array. template T get(int pos) const - /// Returns the element on the given index and tries to convert - /// it to the template type. When the element is not found, a - /// NotFoundException will be thrown. When the element can't be + /// Returns the element at the given index and tries to convert + /// it to the template type. If the element is not found, a + /// Poco::NotFoundException will be thrown. If the element cannot be /// converted a BadCastException will be thrown. { return Document::get(Poco::NumberFormatter::format(pos)); } template - T get(int pos, const T& def) const - /// Returns the element on the given index and tries to convert - /// it to the template type. When the element is not found, or - /// has the wrong type, the def argument will be returned. + T get(int pos, const T& deflt) const + /// Returns the element at the given index and tries to convert + /// it to the template type. If the element is not found, or + /// has the wrong type, the deflt argument will be returned. { - return Document::get(Poco::NumberFormatter::format(pos), def); + return Document::get(Poco::NumberFormatter::format(pos), deflt); } Element::Ptr get(int pos) const; - /// Returns the element on the given index. - /// An empty element will be returned when the element is not found. + /// Returns the element at the given index. + /// An empty element will be returned if the element is not found. template bool isType(int pos) const - /// Returns true when the type of the element equals the TypeId of ElementTrait + /// Returns true if the type of the element equals the TypeId of ElementTrait, + /// otherwise false. { return Document::isType(Poco::NumberFormatter::format(pos)); } std::string toString(int indent = 0) const; + /// Returns a string representation of the Array. }; + // BSON Embedded Array // spec: document template<> @@ -103,4 +109,4 @@ inline void BSONWriter::write(Array::Ptr& from) } } // namespace Poco::MongoDB -#endif //MongoDB_Array_INCLUDED +#endif // MongoDB_Array_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/BSONReader.h b/MongoDB/include/Poco/MongoDB/BSONReader.h index ce915f3a8..e8567cdb6 100644 --- a/MongoDB/include/Poco/MongoDB/BSONReader.h +++ b/MongoDB/include/Poco/MongoDB/BSONReader.h @@ -29,16 +29,17 @@ namespace MongoDB { class MongoDB_API BSONReader - /// Class for reading BSON from a Poco::BinaryReader + /// Class for reading BSON using a Poco::BinaryReader { public: - BSONReader(const Poco::BinaryReader& reader) : _reader(reader) - /// Constructor + BSONReader(const Poco::BinaryReader& reader): + _reader(reader) + /// Creates the BSONReader using the given BinaryWriter. { } virtual ~BSONReader() - /// Destructor + /// Destroys the BSONReader. { } @@ -55,11 +56,13 @@ public: /// A cstring is a string terminated with a 0x00. private: - Poco::BinaryReader _reader; }; +// +// inlines +// inline std::string BSONReader::readCString() { std::string val; @@ -80,4 +83,4 @@ inline std::string BSONReader::readCString() } } // namespace Poco::MongoDB -#endif // MongoDB_BSONReader_INCLUDED +#endif // MongoDB_BSONReader_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/BSONWriter.h b/MongoDB/include/Poco/MongoDB/BSONWriter.h index 80e553d4e..92b6e1db3 100644 --- a/MongoDB/include/Poco/MongoDB/BSONWriter.h +++ b/MongoDB/include/Poco/MongoDB/BSONWriter.h @@ -29,16 +29,17 @@ namespace MongoDB { class MongoDB_API BSONWriter - /// Class for writing BSON to a Poco::BinaryWriter. + /// Class for writing BSON using a Poco::BinaryWriter. { public: - BSONWriter(const Poco::BinaryWriter& writer) : _writer(writer) - /// Constructor + BSONWriter(const Poco::BinaryWriter& writer): + _writer(writer) + /// Creates the BSONWriter. { } virtual ~BSONWriter() - /// Destructor + /// Destroys the BSONWriter. { } @@ -52,13 +53,16 @@ public: void writeCString(const std::string& value); /// Writes a cstring to the writer. A cstring is a string - /// terminated with 0x00 + /// terminated a null character. private: Poco::BinaryWriter _writer; }; +// +// inlines +// inline void BSONWriter::writeCString(const std::string& value) { _writer.writeRaw(value); @@ -69,4 +73,4 @@ inline void BSONWriter::writeCString(const std::string& value) } } // namespace Poco::MongoDB -#endif // MongoDB_BSONWriter_INCLUDED +#endif // MongoDB_BSONWriter_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/Binary.h b/MongoDB/include/Poco/MongoDB/Binary.h index 660530abc..f6fc3bdfc 100644 --- a/MongoDB/include/Poco/MongoDB/Binary.h +++ b/MongoDB/include/Poco/MongoDB/Binary.h @@ -35,47 +35,49 @@ namespace MongoDB { class MongoDB_API Binary - /// Implements BSON Binary. It's a wrapper around a Poco::Buffer. + /// Implements BSON Binary. + /// + /// A Binary stores its data in a Poco::Buffer. { public: typedef SharedPtr Ptr; Binary(); - /// Constructor + /// Creates an empty Binary with subtype 0. Binary(Poco::Int32 size, unsigned char subtype); - /// Constructor + /// Creates a Binary with a buffer of the given size and the given subtype. Binary(const UUID& uuid); - /// Constructor for setting a UUID in a binary element + /// Creates a Binary containing an UUID. Binary(const std::string& data, unsigned char subtype = 0); - /// Constructor for getting binary data from a string. + /// Creates a Binary with the contents of the given string and the given subtype. Binary(const void* data, Poco::Int32 size, unsigned char subtype = 0); - /// Constructor for getting binary data from a buffer. + /// Creates a Binary with the contents of the given buffer and the given subtype. virtual ~Binary(); - /// Destructor + /// Destroys the Binary. Buffer& buffer(); - /// Returns a reference to the buffer + /// Returns a reference to the internal buffer unsigned char subtype() const; - /// Returns the subtype + /// Returns the subtype. void subtype(unsigned char type); - /// Sets the subtype + /// Sets the subtype. std::string toString(int indent = 0) const; - /// Returns the binary encoded in Base64 + /// Returns the contents of the Binary as Base64-encoded string. std::string toRawString() const; - /// Returns the raw content as a string. + /// Returns the raw content of the Binary as a string. UUID uuid() const; /// Returns the UUID when the binary subtype is 0x04. - /// Otherwise BadCastException will be thrown + /// Otherwise, throws a Poco::BadCastException. private: Buffer _buffer; @@ -83,6 +85,9 @@ private: }; +// +// inlines +// inline unsigned char Binary::subtype() const { return _subtype; diff --git a/MongoDB/include/Poco/MongoDB/Connection.h b/MongoDB/include/Poco/MongoDB/Connection.h index 957235e54..825c6e4a4 100644 --- a/MongoDB/include/Poco/MongoDB/Connection.h +++ b/MongoDB/include/Poco/MongoDB/Connection.h @@ -33,60 +33,80 @@ namespace MongoDB { class MongoDB_API Connection /// Represents a connection to a MongoDB server + /// using the MongoDB wire protocol. + /// + /// See https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/ + /// for more information on the wire protocol. { public: typedef Poco::SharedPtr Ptr; Connection(); - /// Default constructor. Use this when you want to - /// connect later on. + /// Creates an unconnected Connection. + /// + /// Use this when you want to connect later on. Connection(const std::string& hostAndPort); - /// Constructor which connects to the given MongoDB host/port. + /// Creates a Connection connected to the given MongoDB instance at host:port. + /// /// The host and port must be separated with a colon. Connection(const std::string& host, int port); - /// Constructor which connects to the given MongoDB host/port. + /// Creates a Connection connected to the given MongoDB instance at host and port. - Connection(const Net::SocketAddress& addrs); - /// Constructor which connects to the given MongoDB host/port. + Connection(const Poco::Net::SocketAddress& addrs); + /// Creates a Connection connected to the given MongoDB instance at the given address. + + Connection(const Poco::Net::StreamSocket& socket); + /// Creates a Connection connected to the given MongoDB instance using the given socket, + /// which must already be connected. virtual ~Connection(); - /// Destructor + /// Destroys the Connection. - Net::SocketAddress address() const; - /// Returns the address of the MongoDB connection + Poco::Net::SocketAddress address() const; + /// Returns the address of the MongoDB server. void connect(const std::string& hostAndPort); - /// Connects to the given MongoDB server. The host and port must be separated - /// with a colon. + /// Connects to the given MongoDB server. + /// + /// The host and port must be separated with a colon. void connect(const std::string& host, int port); /// Connects to the given MongoDB server. - void connect(const Net::SocketAddress& addrs); + void connect(const Poco::Net::SocketAddress& addrs); /// Connects to the given MongoDB server. + + void connect(const Poco::Net::StreamSocket& socket); + /// Connects using an already connected socket. void disconnect(); - /// Disconnects from the MongoDB server + /// Disconnects from the MongoDB server. void sendRequest(RequestMessage& request); - /// Sends a request to the MongoDB server - /// Only use this when the request hasn't a response. + /// Sends a request to the MongoDB server. + /// + /// Used for one-way requests without a response. void sendRequest(RequestMessage& request, ResponseMessage& response); /// Sends a request to the MongoDB server and receives the response. - /// Use this when a response is expected: only a query or getmore + /// + /// Use this when a response is expected: only a "query" or "getmore" /// request will return a response. -private: - Net::SocketAddress _address; - Net::StreamSocket _socket; +protected: void connect(); - /// Connects to the MongoDB server + +private: + Poco::Net::SocketAddress _address; + Poco::Net::StreamSocket _socket; }; +// +// inlines +// inline Net::SocketAddress Connection::address() const { return _address; @@ -96,4 +116,4 @@ inline Net::SocketAddress Connection::address() const } } // namespace Poco::MongoDB -#endif //MongoDB_Connection_INCLUDED +#endif // MongoDB_Connection_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/Cursor.h b/MongoDB/include/Poco/MongoDB/Cursor.h index 9f344f65a..dbf98a5cc 100644 --- a/MongoDB/include/Poco/MongoDB/Cursor.h +++ b/MongoDB/include/Poco/MongoDB/Cursor.h @@ -30,26 +30,27 @@ namespace Poco { namespace MongoDB { -class MongoDB_API Cursor : public Document - /// Cursor is an helper class for querying multiple documents +class MongoDB_API Cursor: public Document + /// Cursor is an helper class for querying multiple documents. { public: - Cursor(const std::string& dbname, const std::string& collectionName, QueryRequest::Flags flags = QueryRequest::QUERY_NONE); - /// Constructor + Cursor(const std::string& dbname, const std::string& collectionName, QueryRequest::Flags flags = QueryRequest::QUERY_DEFAULT); + /// Creates a Cursor for the given database and collection, using the specified flags. - Cursor(const std::string& fullCollectionName, QueryRequest::Flags flags = QueryRequest::QUERY_NONE); - /// Constructor + Cursor(const std::string& fullCollectionName, QueryRequest::Flags flags = QueryRequest::QUERY_DEFAULT); + /// Creates a Cursor for the given database and collection ("database.collection"), using the specified flags. virtual ~Cursor(); - /// Destructor + /// Destroys the Cursor. ResponseMessage& next(Connection& connection); - /// Try to get the next documents. As long as ResponseMessage has a - /// cursor id next can be called to retrieve the next bunch of documents. - /// kill must be called when not all documents are needed. + /// Tries to get the next documents. As long as ResponseMessage has a + /// cursor ID next can be called to retrieve the next bunch of documents. + /// + /// The cursor must be killed (see kill()) when not all documents are needed. QueryRequest& query(); - /// Returns the associated query + /// Returns the associated query. void kill(Connection& connection); /// Kills the cursor and reset it so that it can be reused. @@ -60,6 +61,9 @@ private: }; +// +// inlines +// inline QueryRequest& Cursor::query() { return _query; @@ -69,4 +73,4 @@ inline QueryRequest& Cursor::query() } } // namespace Poco::MongoDB -#endif //MongoDB_Cursor_INCLUDED +#endif // MongoDB_Cursor_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/Database.h b/MongoDB/include/Poco/MongoDB/Database.h index edcbeb2fe..84b81de16 100644 --- a/MongoDB/include/Poco/MongoDB/Database.h +++ b/MongoDB/include/Poco/MongoDB/Database.h @@ -39,13 +39,13 @@ class MongoDB_API Database /// the database. { public: - Database(const std::string& db); - /// Constructor + explicit Database(const std::string& name); + /// Creates a Database for the database with the given name. virtual ~Database(); - /// Destructor + /// Destroys the Database. - bool authenticate(Connection& connection, const std::string& username, const std::string& password, const std::string& method = AUTH_MONGODB_CR); + bool authenticate(Connection& connection, const std::string& username, const std::string& password, const std::string& method = AUTH_SCRAM_SHA1); /// Authenticates against the database using the given connection, /// username and password, as well as authentication method. /// @@ -54,31 +54,37 @@ public: /// authentication methods. /// /// Returns true if authentication was successful, otherwise false. + /// + /// May throw a Poco::ProtocolException if authentication fails for a reason other than + /// invalid credentials. Int64 count(Connection& connection, const std::string& collectionName) const; - /// Sends a count request for the given collection to MongoDB. When - /// the command fails, -1 is returned. + /// Sends a count request for the given collection to MongoDB. + /// + /// If the command fails, -1 is returned. Poco::SharedPtr createCommand() const; /// Creates a QueryRequest for a command. Poco::SharedPtr createCountRequest(const std::string& collectionName) const; - /// Creates a QueryRequest to count the given collection. The collectionname must not contain - /// the database name! + /// Creates a QueryRequest to count the given collection. + /// The collectionname must not contain the database name. Poco::SharedPtr createDeleteRequest(const std::string& collectionName) const; /// Creates a DeleteRequest to delete documents in the given collection. - /// The collectionname must not contain the database name! + /// The collectionname must not contain the database name. Poco::SharedPtr createInsertRequest(const std::string& collectionName) const; /// Creates an InsertRequest to insert new documents in the given collection. - /// The collectionname must not contain the database name! + /// The collectionname must not contain the database name. Poco::SharedPtr createQueryRequest(const std::string& collectionName) const; - /// Creates a QueryRequest. The collectionname must not contain the database name! + /// Creates a QueryRequest. + /// The collectionname must not contain the database name. Poco::SharedPtr createUpdateRequest(const std::string& collectionName) const; - /// Creates an UpdateRequest. The collectionname must not contain the database name! + /// Creates an UpdateRequest. + /// The collectionname must not contain the database name. Poco::MongoDB::Document::Ptr ensureIndex(Connection& connection, const std::string& collection, @@ -92,7 +98,7 @@ public: /// For more info look at the ensureIndex information on the MongoDB website. Document::Ptr getLastErrorDoc(Connection& connection) const; - /// Sends the getLastError command to the database and returns the document + /// Sends the getLastError command to the database and returns the error document. std::string getLastError(Connection& connection) const; /// Sends the getLastError command to the database and returns the err element @@ -113,6 +119,9 @@ private: }; +// +// inlines +// inline Poco::SharedPtr Database::createCommand() const { Poco::SharedPtr cmd = createQueryRequest("$cmd"); diff --git a/MongoDB/include/Poco/MongoDB/DeleteRequest.h b/MongoDB/include/Poco/MongoDB/DeleteRequest.h index 358b30521..d5cecfa45 100644 --- a/MongoDB/include/Poco/MongoDB/DeleteRequest.h +++ b/MongoDB/include/Poco/MongoDB/DeleteRequest.h @@ -29,51 +29,57 @@ namespace Poco { namespace MongoDB { -class MongoDB_API DeleteRequest : public RequestMessage - /// Class for creating an OP_DELETE client request. This request - /// is used to delete one ore more documents from a database. +class MongoDB_API DeleteRequest: public RequestMessage + /// A DeleteRequest is used to delete one ore more documents from a database. /// /// Specific flags for this request - /// - DELETE_NONE - /// No flags - /// - DELETE_SINGLE_REMOVE - /// Delete only the first document + /// - DELETE_DEFAULT: default delete operation + /// - DELETE_SINGLE_REMOVE: delete only the first document { public: - typedef enum + enum Flags { - DELETE_NONE = 0, - DELETE_SINGLE_REMOVE = 1 - } Flags; + DELETE_DEFAULT = 0, + /// Default - DeleteRequest(const std::string& collectionName, Flags flags = DELETE_NONE); - /// Constructor. The full collection name is the concatenation of the database + DELETE_SINGLE_REMOVE = 1 + /// Delete only the first document. + }; + + DeleteRequest(const std::string& collectionName, Flags flags = DELETE_DEFAULT); + /// Creates a DeleteRequest for the given collection using the given flags. + /// + /// The full collection name is the concatenation of the database /// name with the collection name, using a "." for the concatenation. For example, /// for the database "foo" and the collection "bar", the full collection name is /// "foo.bar". DeleteRequest(const std::string& collectionName, bool justOne); - /// Constructor. The full collection name is the concatenation of the database + /// Creates a DeleteRequest for the given collection. + /// + /// The full collection name is the concatenation of the database /// name with the collection name, using a "." for the concatenation. For example, /// for the database "foo" and the collection "bar", the full collection name is - /// "foo.bar". When justOne is true, only the first matching document will + /// "foo.bar". + /// + /// If justOne is true, only the first matching document will /// be removed (the same as using flag DELETE_SINGLE_REMOVE). virtual ~DeleteRequest(); /// Destructor Flags flags() const; - /// Returns flags + /// Returns the flags. void flags(Flags flag); - /// Sets flags + /// Sets the flags. Document& selector(); - /// Returns the selector document + /// Returns the selector document. protected: void buildRequest(BinaryWriter& writer); - /// Writes the OP_DELETE request to the writer + /// Writes the OP_DELETE request to the writer. private: Flags _flags; @@ -82,6 +88,9 @@ private: }; +/// +/// inlines +/// inline DeleteRequest::Flags DeleteRequest::flags() const { return _flags; @@ -103,4 +112,4 @@ inline Document& DeleteRequest::selector() } } // namespace Poco::MongoDB -#endif //MongoDB_DeleteRequest_INCLUDED +#endif // MongoDB_DeleteRequest_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/Document.h b/MongoDB/include/Poco/MongoDB/Document.h index 33d1c1748..77cc451bc 100644 --- a/MongoDB/include/Poco/MongoDB/Document.h +++ b/MongoDB/include/Poco/MongoDB/Document.h @@ -25,6 +25,7 @@ #include "Poco/MongoDB/MongoDB.h" #include "Poco/MongoDB/Element.h" #include +#include namespace Poco { @@ -34,7 +35,8 @@ namespace MongoDB { class ElementFindByName { public: - ElementFindByName(const std::string& name) : _name(name) + ElementFindByName(const std::string& name): + _name(name) { } @@ -49,26 +51,28 @@ private: class MongoDB_API Document - /// Represents a BSON document + /// Represents a MongoDB (BSON) document. { public: typedef SharedPtr Ptr; typedef std::vector Vector; Document(); - /// Constructor + /// Creates an empty Document. virtual ~Document(); - /// Destructor + /// Destroys the Document. Document& addElement(Element::Ptr element); /// Add an element to the document. + /// /// The active document is returned to allow chaining of the add methods. template Document& add(const std::string& name, T value) /// Creates an element with the given name and value and /// adds it to the document. + /// /// The active document is returned to allow chaining of the add methods. { return addElement(new ConcreteElement(name, value)); @@ -77,6 +81,7 @@ public: Document& add(const std::string& name, const char* value) /// Creates an element with the given name and value and /// adds it to the document. + /// /// The active document is returned to allow chaining of the add methods. { return addElement(new ConcreteElement(name, std::string(value))); @@ -94,10 +99,10 @@ public: /// Puts all element names into std::vector. bool empty() const; - /// Returns true when the document doesn't contain any documents. + /// Returns true if the document doesn't contain any documents. bool exists(const std::string& name); - /// Returns true when the document has an element with the given name + /// Returns true if the document has an element with the given name. template T get(const std::string& name) const @@ -107,16 +112,16 @@ public: /// converted a BadCastException will be thrown. { Element::Ptr element = get(name); - if ( element.isNull() ) + if (element.isNull()) { throw NotFoundException(name); } else { - if ( ElementTraits::TypeId == element->type() ) + if (ElementTraits::TypeId == element->type()) { ConcreteElement* concrete = dynamic_cast* >(element.get()); - if ( concrete != NULL ) + if (concrete != 0) { return concrete->value(); } @@ -132,15 +137,15 @@ public: /// has the wrong type, the def argument will be returned. { Element::Ptr element = get(name); - if ( element.isNull() ) + if (element.isNull()) { return def; } - if ( ElementTraits::TypeId == element->type() ) + if (ElementTraits::TypeId == element->type()) { ConcreteElement* concrete = dynamic_cast* >(element.get()); - if ( concrete != NULL ) + if (concrete != 0) { return concrete->value(); } @@ -154,17 +159,17 @@ public: /// An empty element will be returned when the element is not found. Int64 getInteger(const std::string& name) const; - /// Returns an integer. Useful when MongoDB returns int32, int64 + /// Returns an integer. Useful when MongoDB returns Int32, Int64 /// or double for a number (count for example). This method will always /// return an Int64. When the element is not found, a - /// NotFoundException will be thrown. + /// Poco::NotFoundException will be thrown. template bool isType(const std::string& name) const - /// Returns true when the type of the element equals the TypeId of ElementTrait + /// Returns true when the type of the element equals the TypeId of ElementTrait. { Element::Ptr element = get(name); - if ( element.isNull() ) + if (element.isNull()) { return false; } @@ -175,7 +180,7 @@ public: void read(BinaryReader& reader); /// Reads a document from the reader - size_t size() const; + std::size_t size() const; /// Returns the number of elements in the document. virtual std::string toString(int indent = 0) const; @@ -189,6 +194,9 @@ protected: }; +// +// inlines +// inline Document& Document::addElement(Element::Ptr element) { _elements.push_back(element); @@ -218,7 +226,7 @@ inline bool Document::empty() const inline void Document::elementNames(std::vector& keys) const { - for(ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it) + for (ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it) { keys.push_back((*it)->name()); } @@ -231,7 +239,7 @@ inline bool Document::exists(const std::string& name) } -inline size_t Document::size() const +inline std::size_t Document::size() const { return _elements.size(); } @@ -264,7 +272,8 @@ inline void BSONWriter::write(Document::Ptr& from) from->write(_writer); } + } } // namespace Poco::MongoDB -#endif // MongoDB_Document_INCLUDED +#endif // MongoDB_Document_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/Element.h b/MongoDB/include/Poco/MongoDB/Element.h index 4688d185f..e2b468088 100644 --- a/MongoDB/include/Poco/MongoDB/Element.h +++ b/MongoDB/include/Poco/MongoDB/Element.h @@ -42,19 +42,19 @@ namespace MongoDB { class MongoDB_API Element - /// Represents an element of a Document or an Array + /// Represents an Element of a Document or an Array. { public: typedef Poco::SharedPtr Ptr; - Element(const std::string& name); - /// Constructor + explicit Element(const std::string& name); + /// Creates the Element with the given name. virtual ~Element(); /// Destructor - std::string name() const; - /// Returns the name of the element + const std::string& name() const; + /// Returns the name of the element. virtual std::string toString(int indent = 0) const = 0; /// Returns a string representation of the element. @@ -71,7 +71,10 @@ private: }; -inline std::string Element::name() const +// +// inlines +// +inline const std::string& Element::name() const { return _name; } @@ -114,7 +117,7 @@ struct ElementTraits oss << '"'; - for(std::string::const_iterator it = value.begin(); it != value.end(); ++it) + for (std::string::const_iterator it = value.begin(); it != value.end(); ++it) { switch (*it) { @@ -350,10 +353,12 @@ struct ElementTraits template -class ConcreteElement : public Element +class ConcreteElement: public Element { public: - ConcreteElement(const std::string& name, const T& init) : Element(name), _value(init) + ConcreteElement(const std::string& name, const T& init): + Element(name), + _value(init) { } @@ -397,4 +402,4 @@ private: } } // namespace Poco::MongoDB -#endif // MongoDB_Element_INCLUDED +#endif // MongoDB_Element_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/GetMoreRequest.h b/MongoDB/include/Poco/MongoDB/GetMoreRequest.h index e73941e1e..778841ba7 100644 --- a/MongoDB/include/Poco/MongoDB/GetMoreRequest.h +++ b/MongoDB/include/Poco/MongoDB/GetMoreRequest.h @@ -28,31 +28,31 @@ namespace Poco { namespace MongoDB { -class MongoDB_API GetMoreRequest : public RequestMessage - /// Class for creating an OP_GETMORE client request. This request is used - /// to query the database for more documents in a collection after - /// a query request is send. +class MongoDB_API GetMoreRequest: public RequestMessage + /// A GetMoreRequest is used to query the database for more documents in a collection + /// after a query request is send (OP_GETMORE). { public: GetMoreRequest(const std::string& collectionName, Int64 cursorID); - /// Constructor. The full collection name is the concatenation of the database + /// Creates a GetMoreRequest for the give collection and cursor. + /// + /// The full collection name is the concatenation of the database /// name with the collection name, using a "." for the concatenation. For example, /// for the database "foo" and the collection "bar", the full collection name is /// "foo.bar". The cursorID has been returned by the response on the query request. /// By default the numberToReturn is set to 100. - virtual ~GetMoreRequest(); - /// Destructor + /// Destroys the GetMoreRequest. Int32 getNumberToReturn() const; - /// Returns the limit of returned documents + /// Returns the limit of returned documents. void setNumberToReturn(Int32 n); - /// Sets the limit of returned documents + /// Sets the limit of returned documents. Int64 cursorID() const; - /// Returns the cursor id + /// Returns the cursor ID. protected: void buildRequest(BinaryWriter& writer); @@ -88,4 +88,4 @@ inline Int64 GetMoreRequest::cursorID() const } } // namespace Poco::MongoDB -#endif //MongoDB_GetMoreRequest_INCLUDED +#endif // MongoDB_GetMoreRequest_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/InsertRequest.h b/MongoDB/include/Poco/MongoDB/InsertRequest.h index d273f6492..655d583a2 100644 --- a/MongoDB/include/Poco/MongoDB/InsertRequest.h +++ b/MongoDB/include/Poco/MongoDB/InsertRequest.h @@ -29,26 +29,34 @@ namespace Poco { namespace MongoDB { -class MongoDB_API InsertRequest : public RequestMessage - /// Class for creating an OP_INSERT client request. This request is used - /// to insert one or more documents to the database. +class MongoDB_API InsertRequest: public RequestMessage + /// A request for inserting one or more documents to the database + /// (OP_INSERT). { public: - typedef enum + enum Flags { - INSERT_NONE = 0, + INSERT_DEFAULT = 0, + /// If specified, perform a normal insert operation. + INSERT_CONTINUE_ON_ERROR = 1 - } Flags; + /// If set, the database will not stop processing a bulk insert if one + /// fails (e.g. due to duplicate IDs). This makes bulk insert behave similarly + /// to a series of single inserts, except lastError will be set if any insert + /// fails, not just the last one. If multiple errors occur, only the most + /// recent will be reported. + }; - InsertRequest(const std::string& collectionName, Flags flags = INSERT_NONE ); - /// Constructor. + InsertRequest(const std::string& collectionName, Flags flags = INSERT_DEFAULT); + /// Creates an InsertRequest. + /// /// The full collection name is the concatenation of the database /// name with the collection name, using a "." for the concatenation. For example, /// for the database "foo" and the collection "bar", the full collection name is /// "foo.bar". virtual ~InsertRequest(); - /// Destructor + /// Destroys the InsertRequest. Document& addNewDocument(); /// Adds a new document for insertion. A reference to the empty document is @@ -56,20 +64,21 @@ public: /// on destruction. Document::Vector& documents(); - /// Returns the documents to insert into the database + /// Returns the documents to insert into the database. protected: - void buildRequest(BinaryWriter& writer); private: - Int32 _flags; std::string _fullCollectionName; Document::Vector _documents; }; +// +// inlines +// inline Document& InsertRequest::addNewDocument() { Document::Ptr doc = new Document(); @@ -87,4 +96,4 @@ inline Document::Vector& InsertRequest::documents() } } // namespace Poco::MongoDB -#endif //MongoDB_InsertRequest_INCLUDED +#endif // MongoDB_InsertRequest_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/JavaScriptCode.h b/MongoDB/include/Poco/MongoDB/JavaScriptCode.h index 49076d29d..693095ac3 100644 --- a/MongoDB/include/Poco/MongoDB/JavaScriptCode.h +++ b/MongoDB/include/Poco/MongoDB/JavaScriptCode.h @@ -32,31 +32,34 @@ namespace MongoDB { class MongoDB_API JavaScriptCode - /// Represents JavaScript type in BSON + /// Represents JavaScript type in BSON. { public: typedef SharedPtr Ptr; JavaScriptCode(); - /// Constructor + /// Creates an empty JavaScriptCode object. virtual ~JavaScriptCode(); - /// Destructor + /// Destroys the JavaScriptCode. - void setCode(const std::string& s); - /// Set the code + void setCode(const std::string& code); + /// Sets the JavaScript code. std::string getCode() const; - /// Get the code + /// Returns the JavaScript code. private: std::string _code; }; -inline void JavaScriptCode::setCode(const std::string& s) +// +// inlines +// +inline void JavaScriptCode::setCode(const std::string& code) { - _code = s; + _code = code; } @@ -65,6 +68,7 @@ inline std::string JavaScriptCode::getCode() const return _code; } + // BSON JavaScript code // spec: string template<> @@ -100,4 +104,4 @@ inline void BSONWriter::write(JavaScriptCode::Ptr& from) } } // namespace Poco::MongoDB -#endif // MongoDB_JavaScriptCode_INCLUDED +#endif // MongoDB_JavaScriptCode_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/KillCursorsRequest.h b/MongoDB/include/Poco/MongoDB/KillCursorsRequest.h index 7e14ccf82..7fd5d593d 100644 --- a/MongoDB/include/Poco/MongoDB/KillCursorsRequest.h +++ b/MongoDB/include/Poco/MongoDB/KillCursorsRequest.h @@ -28,20 +28,20 @@ namespace Poco { namespace MongoDB { -class MongoDB_API KillCursorsRequest : public RequestMessage +class MongoDB_API KillCursorsRequest: public RequestMessage /// Class for creating an OP_KILL_CURSORS client request. This /// request is used to kill cursors, which are still open, /// returned by query requests. { public: KillCursorsRequest(); - /// Constructor + /// Creates a KillCursorsRequest. virtual ~KillCursorsRequest(); - /// Destructor + /// Destroys the KillCursorsRequest. std::vector& cursors(); - /// Return the cursors + /// The internal list of cursors. protected: void buildRequest(BinaryWriter& writer); @@ -49,6 +49,9 @@ protected: }; +// +// inlines +// inline std::vector& KillCursorsRequest::cursors() { return _cursors; @@ -58,4 +61,4 @@ inline std::vector& KillCursorsRequest::cursors() } } // namespace Poco::MongoDB -#endif //MongoDB_KillCursorsRequest_INCLUDED +#endif // MongoDB_KillCursorsRequest_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/Message.h b/MongoDB/include/Poco/MongoDB/Message.h index 122a3353e..d0ddb2991 100644 --- a/MongoDB/include/Poco/MongoDB/Message.h +++ b/MongoDB/include/Poco/MongoDB/Message.h @@ -33,11 +33,11 @@ namespace MongoDB { class MongoDB_API Message - /// Base class for all messages send or retrieved from MongoDB server + /// Base class for all messages send or retrieved from MongoDB server. { public: - Message(MessageHeader::OpCode opcode); - /// Constructor + explicit Message(MessageHeader::OpCode opcode); + /// Creates a Message using the given OpCode. virtual ~Message(); /// Destructor @@ -53,6 +53,9 @@ protected: }; +// +// inlines +// inline MessageHeader& Message::header() { return _header; @@ -69,4 +72,4 @@ inline void Message::messageLength(Poco::Int32 length) } } // namespace Poco::MongoDB -#endif //MongoDB_Message_INCLUDED +#endif // MongoDB_Message_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/MessageHeader.h b/MongoDB/include/Poco/MongoDB/MessageHeader.h index cd9b5fba6..813f7dc5f 100644 --- a/MongoDB/include/Poco/MongoDB/MessageHeader.h +++ b/MongoDB/include/Poco/MongoDB/MessageHeader.h @@ -29,54 +29,54 @@ namespace MongoDB { class MongoDB_API MessageHeader - /// Represents the header which is always prepended to a request - /// or response of MongoDB + /// Represents the message header which is always prepended to a + /// MongoDB request or response message. { public: static const unsigned int MSG_HEADER_SIZE = 16; - typedef enum + enum OpCode { - Reply = 1 - , Msg = 1000 - , Update = 2001 - , Insert = 2002 - , Query = 2004 - , GetMore = 2005 - , Delete = 2006 - , KillCursors = 2007 - } OpCode; + OP_REPLY = 1, + OP_MSG = 1000, + OP_UPDATE = 2001, + OP_INSERT = 2002, + OP_QUERY = 2004, + OP_GET_MORE = 2005, + OP_DELETE = 2006, + OP_KILL_CURSORS = 2007 + }; + + explicit MessageHeader(OpCode); + /// Creates the MessageHeader using the given OpCode. virtual ~MessageHeader(); - /// Destructor + /// Destroys the MessageHeader. void read(BinaryReader& reader); - /// Reads the header + /// Reads the header using the given BinaryReader. void write(BinaryWriter& writer); - /// Writes the header + /// Writes the header using the given BinaryWriter. Int32 getMessageLength() const; - /// Returns the message length + /// Returns the message length. OpCode opCode() const; - /// Returns the OpCode + /// Returns the OpCode. Int32 getRequestID() const; - /// Returns the request id of the current message + /// Returns the request ID of the current message. void setRequestID(Int32 id); - /// Sets the request id of the current message + /// Sets the request ID of the current message. Int32 responseTo() const; /// Returns the request id from the original request. private: - MessageHeader(OpCode opcode); - /// Constructor. - void setMessageLength(Int32 length); - /// Sets the message length + /// Sets the message length. Int32 _messageLength; Int32 _requestID; @@ -87,6 +87,9 @@ private: }; +// +// inlines +// inline MessageHeader::OpCode MessageHeader::opCode() const { return _opCode; @@ -126,4 +129,4 @@ inline Int32 MessageHeader::responseTo() const } } // namespace Poco::MongoDB -#endif //MongoDB_MessageHeader_INCLUDED +#endif // MongoDB_MessageHeader_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/ObjectId.h b/MongoDB/include/Poco/MongoDB/ObjectId.h index aa85a2907..57157d4c2 100644 --- a/MongoDB/include/Poco/MongoDB/ObjectId.h +++ b/MongoDB/include/Poco/MongoDB/ObjectId.h @@ -32,10 +32,10 @@ namespace MongoDB { class MongoDB_API ObjectId /// ObjectId is a 12-byte BSON type, constructed using: /// - /// - a 4-byte timestamp, - /// - a 3-byte machine identifier, - /// - a 2-byte process id, and - /// - a 3-byte counter, starting with a random value. + /// - a 4-byte timestamp, + /// - a 3-byte machine identifier, + /// - a 2-byte process id, and + /// - a 3-byte counter, starting with a random value. /// /// In MongoDB, documents stored in a collection require a unique _id field that acts /// as a primary key. Because ObjectIds are small, most likely unique, and fast to generate, @@ -46,15 +46,17 @@ class MongoDB_API ObjectId public: typedef SharedPtr Ptr; - ObjectId(const std::string& id); - /// Constructor. The string must contain a hexidecimal representation - /// of an object id. This means a string of 24 characters. + explicit ObjectId(const std::string& id); + /// Creates an ObjectId from a string. + /// + /// The string must contain a hexidecimal representation + /// of an object ID. This means a string of 24 characters. ObjectId(const ObjectId& copy); - /// Copy constructor. + /// Creates an ObjectId by copying another one. virtual ~ObjectId(); - /// Destructor + /// Destroys the ObjectId. Timestamp timestamp() const; /// Returns the timestamp which is stored in the first four bytes of the id @@ -65,22 +67,22 @@ public: /// of the ID char array. private: - ObjectId(); - /// Constructor. Creates an empty Id + static int fromHex(char c); + static char fromHex(const char* c); + unsigned char _id[12]; friend class BSONWriter; friend class BSONReader; friend class Document; - - static int fromHex(char c); - - static char fromHex(const char* c); }; +// +// inlines +// inline Timestamp ObjectId::timestamp() const { int time; @@ -92,6 +94,7 @@ inline Timestamp ObjectId::timestamp() const return Timestamp::fromEpochTime((time_t) time); } + inline int ObjectId::fromHex(char c) { if ( '0' <= c && c <= '9' ) @@ -103,11 +106,13 @@ inline int ObjectId::fromHex(char c) return 0xff; } + inline char ObjectId::fromHex(const char* c) { return (char)((fromHex(c[0]) << 4 ) | fromHex(c[1])); } + // BSON Embedded Document // spec: ObjectId template<> @@ -141,4 +146,4 @@ inline void BSONWriter::write(ObjectId::Ptr& from) } } // namespace Poco::MongoDB -#endif //MongoDB_ObjectId_INCLUDED +#endif // MongoDB_ObjectId_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/PoolableConnectionFactory.h b/MongoDB/include/Poco/MongoDB/PoolableConnectionFactory.h index 2e07fe567..bb92be05a 100644 --- a/MongoDB/include/Poco/MongoDB/PoolableConnectionFactory.h +++ b/MongoDB/include/Poco/MongoDB/PoolableConnectionFactory.h @@ -33,13 +33,13 @@ class PoolableObjectFactory /// are created with the given address. { public: - PoolableObjectFactory(Net::SocketAddress& address) - : _address(address) + PoolableObjectFactory(Net::SocketAddress& address): + _address(address) { } - PoolableObjectFactory(const std::string& address) - : _address(address) + PoolableObjectFactory(const std::string& address): + _address(address) { } @@ -105,8 +105,7 @@ private: }; -} // namespace MongoDB -} // namespace Poco +} } // namespace Poco::MongoDB -#endif //MongoDB_PoolableConnectionFactory_INCLUDED +#endif // MongoDB_PoolableConnectionFactory_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/QueryRequest.h b/MongoDB/include/Poco/MongoDB/QueryRequest.h index 7e452d7b0..cc4039822 100644 --- a/MongoDB/include/Poco/MongoDB/QueryRequest.h +++ b/MongoDB/include/Poco/MongoDB/QueryRequest.h @@ -29,59 +29,89 @@ namespace Poco { namespace MongoDB { -class MongoDB_API QueryRequest : public RequestMessage - /// Class for creating an OP_QUERY client request. This request - /// is used to query documents from the database. +class MongoDB_API QueryRequest: public RequestMessage + /// A request to query documents in a MongoDB database + /// using an OP_QUERY request. { public: - typedef enum + enum Flags { - QUERY_NONE = 0, - QUERY_TAILABLECURSOR = 2, + QUERY_DEFAULT = 0, + /// Do not set any flags. + + QUERY_TAILABLE_CURSOR = 2, + /// Tailable means cursor is not closed when the last data is retrieved. + /// Rather, the cursor marks the final object’s position. + /// You can resume using the cursor later, from where it was located, + /// if more data were received. Like any "latent cursor", the cursor may + /// become invalid at some point (CursorNotFound) – for example if the final + /// object it references were deleted. + QUERY_SLAVE_OK = 4, - //QUERY_OPLOG_REPLAY = 8 (internal replication use only - drivers should not implement) - QUERY_NOCUROSR_TIMEOUT = 16, + /// Allow query of replica slave. Normally these return an error except + /// for namespace "local". + + // QUERY_OPLOG_REPLAY = 8 (internal replication use only - drivers should not implement) + + QUERY_NO_CURSOR_TIMEOUT = 16, + /// The server normally times out idle cursors after an inactivity period + /// (10 minutes) to prevent excess memory use. Set this option to prevent that. + QUERY_AWAIT_DATA = 32, - QUERY_EXHAUST = 64, - QUERY_PARTIAL = 128 - } Flags; + /// Use with QUERY_TAILABLECURSOR. If we are at the end of the data, block for + /// a while rather than returning no data. After a timeout period, we do + /// return as normal. - QueryRequest(const std::string& collectionName, Flags flags = QUERY_NONE); - /// Constructor. + QUERY_EXHAUST = 64, + /// Stream the data down full blast in multiple "more" packages, on the + /// assumption that the client will fully read all data queried. + /// Faster when you are pulling a lot of data and know you want to pull + /// it all down. + /// Note: the client is not allowed to not read all the data unless it + /// closes the connection. + + QUERY_PARTIAL = 128 + /// Get partial results from a mongos if some shards are down + /// (instead of throwing an error). + }; + + QueryRequest(const std::string& collectionName, Flags flags = QUERY_DEFAULT); + /// Creates a QueryRequest. + /// /// The full collection name is the concatenation of the database /// name with the collection name, using a "." for the concatenation. For example, /// for the database "foo" and the collection "bar", the full collection name is /// "foo.bar". virtual ~QueryRequest(); - /// Destructor + /// Destroys the QueryRequest. Flags getFlags() const; - /// Returns the flags + /// Returns the flags. void setFlags(Flags flag); - /// Set the flags + /// Set the flags. std::string fullCollectionName() const; - /// Returns the . used for this query + /// Returns the . used for this query. Int32 getNumberToSkip() const; - /// Returns the number of documents to skip + /// Returns the number of documents to skip. void setNumberToSkip(Int32 n); - /// Sets the number of documents to skip + /// Sets the number of documents to skip. Int32 getNumberToReturn() const; - /// Returns the number to return + /// Returns the number of documents to return. void setNumberToReturn(Int32 n); - /// Sets the number to return (limit) + /// Sets the number of documents to return (limit). Document& selector(); - /// Returns the selector document + /// Returns the selector document. Document& returnFieldSelector(); - /// Returns the field selector document + /// Returns the field selector document. protected: void buildRequest(BinaryWriter& writer); @@ -96,6 +126,9 @@ private: }; +// +// inlines +// inline QueryRequest::Flags QueryRequest::getFlags() const { return _flags; @@ -153,4 +186,4 @@ inline void QueryRequest::setNumberToReturn(Int32 n) } } // namespace Poco::MongoDB -#endif //MongoDB_QueryRequest_INCLUDED +#endif // MongoDB_QueryRequest_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/RegularExpression.h b/MongoDB/include/Poco/MongoDB/RegularExpression.h index ee048790e..f962f41aa 100644 --- a/MongoDB/include/Poco/MongoDB/RegularExpression.h +++ b/MongoDB/include/Poco/MongoDB/RegularExpression.h @@ -30,34 +30,34 @@ namespace MongoDB { class MongoDB_API RegularExpression - /// Represents a regular expression in BSON format + /// Represents a regular expression in BSON format. { public: typedef SharedPtr Ptr; RegularExpression(); - /// Constructor + /// Creates an empty RegularExpression. RegularExpression(const std::string& pattern, const std::string& options); - /// Constructor + /// Creates a RegularExpression using the given pattern and options. virtual ~RegularExpression(); - /// Destructor + /// Destroys the RegularExpression. SharedPtr createRE() const; - /// Tries to create a Poco::RegularExpression + /// Tries to create a Poco::RegularExpression from the MongoDB regular expression. std::string getOptions() const; - /// Returns the options + /// Returns the options string. void setOptions(const std::string& options); - /// Sets the options + /// Sets the options string. std::string getPattern() const; - /// Returns the pattern + /// Returns the pattern. void setPattern(const std::string& pattern); - /// Sets the pattern + /// Sets the pattern. private: std::string _pattern; @@ -65,6 +65,9 @@ private: }; +/// +/// inlines +/// inline std::string RegularExpression::getPattern() const { return _pattern; @@ -125,4 +128,4 @@ inline void BSONWriter::write(RegularExpression::Ptr& fr } } // namespace Poco::MongoDB -#endif // MongoDB_RegularExpression_INCLUDED +#endif // MongoDB_RegularExpression_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/ReplicaSet.h b/MongoDB/include/Poco/MongoDB/ReplicaSet.h index 3269f55d8..16cd7add4 100644 --- a/MongoDB/include/Poco/MongoDB/ReplicaSet.h +++ b/MongoDB/include/Poco/MongoDB/ReplicaSet.h @@ -30,21 +30,26 @@ namespace MongoDB { class MongoDB_API ReplicaSet - /// Class for working with a replicaset + /// Class for working with a MongoDB replica set. { public: - ReplicaSet(const std::vector& addresses); - /// Constructor + explicit ReplicaSet(const std::vector& addresses); + /// Creates the ReplicaSet using the given server addresses. virtual ~ReplicaSet(); - /// Destructor + /// Destroys the ReplicaSet. Connection::Ptr findMaster(); /// Tries to find the master MongoDB instance from the addresses + /// passed to the constructor. + /// + /// Returns the Connection to the master, or null if no master + /// instance was found. -private: +protected: Connection::Ptr isMaster(const Net::SocketAddress& host); +private: std::vector _addresses; }; diff --git a/MongoDB/include/Poco/MongoDB/RequestMessage.h b/MongoDB/include/Poco/MongoDB/RequestMessage.h index d418263aa..09470dc44 100644 --- a/MongoDB/include/Poco/MongoDB/RequestMessage.h +++ b/MongoDB/include/Poco/MongoDB/RequestMessage.h @@ -29,18 +29,18 @@ namespace Poco { namespace MongoDB { -class MongoDB_API RequestMessage : public Message - /// Base class for a request +class MongoDB_API RequestMessage: public Message + /// Base class for a request sent to the MongoDB server. { public: - RequestMessage(MessageHeader::OpCode opcode); - /// Constructor + explicit RequestMessage(MessageHeader::OpCode opcode); + /// Creates a RequestMessage using the given opcode. virtual ~RequestMessage(); - /// Destructor + /// Destroys the RequestMessage. void send(std::ostream& ostr); - /// Sends the request to stream + /// Writes the request to stream. protected: virtual void buildRequest(BinaryWriter& ss) = 0; @@ -50,4 +50,4 @@ protected: } } // namespace Poco::MongoDB -#endif //MongoDB_RequestMessage_INCLUDED +#endif // MongoDB_RequestMessage_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/ResponseMessage.h b/MongoDB/include/Poco/MongoDB/ResponseMessage.h index 675b48c46..5450f41ec 100644 --- a/MongoDB/include/Poco/MongoDB/ResponseMessage.h +++ b/MongoDB/include/Poco/MongoDB/ResponseMessage.h @@ -24,42 +24,43 @@ #include "Poco/MongoDB/Message.h" #include "Poco/MongoDB/Document.h" #include +#include namespace Poco { namespace MongoDB { -class MongoDB_API ResponseMessage : public Message - /// Class that represents a response (OP_REPLY) from MongoDB +class MongoDB_API ResponseMessage: public Message + /// This class represents a response (OP_REPLY) from MongoDB. { public: ResponseMessage(); - /// Constructor + /// Creates an empty ResponseMessage. virtual ~ResponseMessage(); - /// Destructor + /// Destroys the ResponseMessage. Int64 cursorID() const; - /// Returns the cursor id + /// Returns the cursor ID. void clear(); - /// Clears the response + /// Clears the response. - size_t count() const; - /// Returns the number of documents in the response + std::size_t count() const; + /// Returns the number of documents in the response. Document::Vector& documents(); - /// Returns the retrieved documents + /// Returns a vector containing the received documents. bool empty() const; - /// Returns true when the response doesn't contain any documents + /// Returns true if the response does not contain any documents. bool hasDocuments() const; - /// Returns true when there is at least one document + /// Returns true if there is at least one document in the response. void read(std::istream& istr); - /// Reads the response from the stream + /// Reads the response from the stream. private: Int32 _responseFlags; @@ -70,7 +71,10 @@ private: }; -inline size_t ResponseMessage::count() const +// +// inlines +// +inline std::size_t ResponseMessage::count() const { return _documents.size(); } @@ -103,4 +107,4 @@ inline bool ResponseMessage::hasDocuments() const } } // namespace Poco::MongoDB -#endif //MongoDB_ResponseMessage_INCLUDED +#endif // MongoDB_ResponseMessage_INCLUDED diff --git a/MongoDB/include/Poco/MongoDB/UpdateRequest.h b/MongoDB/include/Poco/MongoDB/UpdateRequest.h index 2a729ae71..ec08c810d 100644 --- a/MongoDB/include/Poco/MongoDB/UpdateRequest.h +++ b/MongoDB/include/Poco/MongoDB/UpdateRequest.h @@ -29,39 +29,42 @@ namespace Poco { namespace MongoDB { -class UpdateRequest : public RequestMessage - /// Class for creating an OP_UPDATE client request. This request is used - /// to update a document. +class UpdateRequest: public RequestMessage + /// This request is used to update a document in a database + /// using the OP_UPDATE client request. { public: - typedef enum + enum Flags { - UPDATE_NOFLAGS = 0, - // If set, the database will insert the supplied object into the - // collection if no matching document is found. + UPDATE_DEFAULT = 0, + /// If set, the database will insert the supplied object into the + /// collection if no matching document is found. + UPDATE_UPSERT = 1, - // if set, the database will update all matching objects in the collection. - // Otherwise only updates first matching doc. - UPDATE_MULTIUPDATE = 2 - } Flags; + /// If set, the database will update all matching objects in the collection. + /// Otherwise only updates first matching doc. - UpdateRequest(const std::string& collectionName, Flags flags = UPDATE_NOFLAGS); - /// Constructor. + UPDATE_MULTIUPDATE = 2 + /// If set to, updates multiple documents that meet the query criteria. + /// Otherwise only updates one document. + }; + + UpdateRequest(const std::string& collectionName, Flags flags = UPDATE_DEFAULT); + /// Creates the UpdateRequest. + /// /// The full collection name is the concatenation of the database /// name with the collection name, using a "." for the concatenation. For example, /// for the database "foo" and the collection "bar", the full collection name is /// "foo.bar". virtual ~UpdateRequest(); - /// Destructor - + /// Destroys the UpdateRequest. Document& selector(); - /// Returns the selector document - + /// Returns the selector document. Document& update(); - /// The document to update + /// Returns the document to update. Flags flags() const; /// Returns the flags @@ -72,7 +75,6 @@ public: protected: void buildRequest(BinaryWriter& writer); - private: Flags _flags; std::string _fullCollectionName; @@ -81,21 +83,27 @@ private: }; +// +// inlines +// inline UpdateRequest::Flags UpdateRequest::flags() const { return _flags; } + inline void UpdateRequest::flags(UpdateRequest::Flags flags) { _flags = flags; } + inline Document& UpdateRequest::selector() { return _selector; } + inline Document& UpdateRequest::update() { return _update; @@ -105,4 +113,4 @@ inline Document& UpdateRequest::update() } } // namespace Poco::MongoDB -#endif //MongoDB_UpdateRequest_INCLUDED +#endif // MongoDB_UpdateRequest_INCLUDED diff --git a/MongoDB/src/Array.cpp b/MongoDB/src/Array.cpp index dd0e57782..0ed520b2e 100644 --- a/MongoDB/src/Array.cpp +++ b/MongoDB/src/Array.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: Array // -// Implementation of the Array class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // @@ -24,7 +22,8 @@ namespace Poco { namespace MongoDB { -Array::Array() : Document() +Array::Array(): + Document() { } @@ -47,26 +46,26 @@ std::string Array::toString(int indent) const oss << "["; - if ( indent > 0 ) oss << std::endl; + if (indent > 0) oss << std::endl; - for(ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it) + for (ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it) { - if ( it != _elements.begin() ) + if (it != _elements.begin()) { oss << ","; - if ( indent > 0 ) oss << std::endl; + if (indent > 0) oss << std::endl; } - for(int i = 0; i < indent; ++i) oss << ' '; + for (int i = 0; i < indent; ++i) oss << ' '; oss << (*it)->toString(indent > 0 ? indent + 2 : 0); } - if ( indent > 0 ) + if (indent > 0) { oss << std::endl; - if ( indent >= 2 ) indent -= 2; - for(int i = 0; i < indent; ++i) oss << ' '; + if (indent >= 2) indent -= 2; + for (int i = 0; i < indent; ++i) oss << ' '; } oss << "]"; @@ -75,4 +74,4 @@ std::string Array::toString(int indent) const } -}} // Namespace Poco::Mongo +} } // Namespace Poco::Mongo diff --git a/MongoDB/src/Binary.cpp b/MongoDB/src/Binary.cpp index c70ac507e..98e840722 100644 --- a/MongoDB/src/Binary.cpp +++ b/MongoDB/src/Binary.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: Binary // -// Implementation of the Binary class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // @@ -76,9 +74,10 @@ std::string Binary::toString(int indent) const return oss.str(); } + UUID Binary::uuid() const { - if ( _subtype == 0x04 && _buffer.size() == 16 ) + if (_subtype == 0x04 && _buffer.size() == 16) { UUID uuid; uuid.copyFrom((const char*) _buffer.begin()); @@ -87,4 +86,5 @@ UUID Binary::uuid() const throw BadCastException("Invalid subtype"); } + } } // namespace Poco::MongoDB diff --git a/MongoDB/src/Connection.cpp b/MongoDB/src/Connection.cpp index 93dd27f7e..edca15d1e 100644 --- a/MongoDB/src/Connection.cpp +++ b/MongoDB/src/Connection.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: Connection // -// Implementation of the Connection class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // @@ -18,41 +16,55 @@ #include "Poco/Net/SocketStream.h" #include "Poco/MongoDB/Connection.h" -#include namespace Poco { namespace MongoDB { -Connection::Connection() : _address(), _socket() +Connection::Connection(): + _address(), + _socket() { } -Connection::Connection(const std::string& hostAndPort) : _address(hostAndPort), _socket() +Connection::Connection(const std::string& hostAndPort): + _address(hostAndPort), + _socket() { connect(); } -Connection::Connection(const std::string& host, int port) : _address(host, port), _socket() +Connection::Connection(const std::string& host, int port): + _address(host, port), + _socket() { connect(); } -Connection::Connection(const Net::SocketAddress& addrs) : _address(addrs), _socket() +Connection::Connection(const Poco::Net::SocketAddress& addrs): + _address(addrs), + _socket() { connect(); } +Connection::Connection(const Poco::Net::StreamSocket& socket): + _address(socket.peerAddress()), + _socket(socket) +{ +} + + Connection::~Connection() { try { - _socket.close(); + disconnect(); } catch (...) { @@ -68,25 +80,32 @@ void Connection::connect() void Connection::connect(const std::string& hostAndPort) { - _address = Net::SocketAddress(hostAndPort); + _address = Poco::Net::SocketAddress(hostAndPort); connect(); } void Connection::connect(const std::string& host, int port) { - _address = Net::SocketAddress(host, port); + _address = Poco::Net::SocketAddress(host, port); connect(); } -void Connection::connect(const Net::SocketAddress& addrs) +void Connection::connect(const Poco::Net::SocketAddress& addrs) { _address = addrs; connect(); } +void Connection::connect(const Poco::Net::StreamSocket& socket) +{ + _address = socket.peerAddress(); + _socket = socket; +} + + void Connection::disconnect() { _socket.close(); @@ -95,7 +114,7 @@ void Connection::disconnect() void Connection::sendRequest(RequestMessage& request) { - Net::SocketOutputStream sos(_socket); + Poco::Net::SocketOutputStream sos(_socket); request.send(sos); } @@ -104,8 +123,9 @@ void Connection::sendRequest(RequestMessage& request, ResponseMessage& response) { sendRequest(request); - Net::SocketInputStream sis(_socket); + Poco::Net::SocketInputStream sis(_socket); response.read(sis); } + } } // Poco::MongoDB diff --git a/MongoDB/src/Cursor.cpp b/MongoDB/src/Cursor.cpp index 8ce5b4882..14f77f0e3 100644 --- a/MongoDB/src/Cursor.cpp +++ b/MongoDB/src/Cursor.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: Cursor // -// Implementation of the Cursor class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // @@ -25,14 +23,14 @@ namespace Poco { namespace MongoDB { -Cursor::Cursor(const std::string& db, const std::string& collection, QueryRequest::Flags flags) - : _query(db + '.' + collection, flags) +Cursor::Cursor(const std::string& db, const std::string& collection, QueryRequest::Flags flags): + _query(db + '.' + collection, flags) { } -Cursor::Cursor(const std::string& fullCollectionName, QueryRequest::Flags flags) - : _query(fullCollectionName, flags) +Cursor::Cursor(const std::string& fullCollectionName, QueryRequest::Flags flags): + _query(fullCollectionName, flags) { } @@ -42,7 +40,6 @@ Cursor::~Cursor() try { poco_assert_dbg(!_response.cursorID()); - } catch (...) { @@ -52,7 +49,7 @@ Cursor::~Cursor() ResponseMessage& Cursor::next(Connection& connection) { - if ( _response.cursorID() == 0 ) + if (_response.cursorID() == 0) { connection.sendRequest(_query, _response); } @@ -69,7 +66,7 @@ ResponseMessage& Cursor::next(Connection& connection) void Cursor::kill(Connection& connection) { - if ( _response.cursorID() != 0 ) + if (_response.cursorID() != 0) { KillCursorsRequest killRequest; killRequest.cursors().push_back(_response.cursorID()); diff --git a/MongoDB/src/Database.cpp b/MongoDB/src/Database.cpp index f9c5c003c..6300ecae3 100644 --- a/MongoDB/src/Database.cpp +++ b/MongoDB/src/Database.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: Database // -// Implementation of the Database class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // @@ -158,19 +156,11 @@ bool Database::authCR(Connection& connection, const std::string& username, const if (response.documents().size() > 0) { Document::Ptr pDoc = response.documents()[0]; - try - { - double ok = pDoc->get("ok"); - if (ok != 1) return false; - nonce = pDoc->get("nonce", ""); - if (nonce.empty()) return false; - } - catch (Poco::NotFoundException&) - { - return false; - } + if (pDoc->get("ok") != 1) return false; + nonce = pDoc->get("nonce", ""); + if (nonce.empty()) throw Poco::ProtocolException("no nonce received"); } - else return false; + else throw Poco::ProtocolException("empty response for getnonce"); std::string credsDigest = hashCredentials(username, password); @@ -191,15 +181,9 @@ bool Database::authCR(Connection& connection, const std::string& username, const if (response.documents().size() > 0) { Document::Ptr pDoc = response.documents()[0]; - try - { - return pDoc->get("ok") == 1; - } - catch (Poco::NotFoundException&) - { - } + return pDoc->get("ok") == 1; } - return false; + else throw Poco::ProtocolException("empty response for authenticate"); } @@ -224,22 +208,15 @@ bool Database::authSCRAM(Connection& connection, const std::string& username, co if (response.documents().size() > 0) { Document::Ptr pDoc = response.documents()[0]; - try + if (pDoc->get("ok") == 1) { - if (pDoc->get("ok") == 1) - { - Binary::Ptr pPayload = pDoc->get("payload"); - serverFirstMsg = pPayload->toRawString(); - conversationId = pDoc->get("conversationId"); - } - else return false; - } - catch (Poco::NotFoundException&) - { - return false; + Binary::Ptr pPayload = pDoc->get("payload"); + serverFirstMsg = pPayload->toRawString(); + conversationId = pDoc->get("conversationId"); } + else return false; } - else return false; + else throw Poco::ProtocolException("empty response for saslStart"); std::map kvm = parseKeyValueList(serverFirstMsg); const std::string serverNonce = kvm["r"]; @@ -287,21 +264,14 @@ bool Database::authSCRAM(Connection& connection, const std::string& username, co if (response.documents().size() > 0) { Document::Ptr pDoc = response.documents()[0]; - try + if (pDoc->get("ok") == 1) { - if (pDoc->get("ok") == 1) - { - Binary::Ptr pPayload = pDoc->get("payload"); - serverSecondMsg.assign(reinterpret_cast(pPayload->buffer().begin()), pPayload->buffer().size()); - } - else return false; - } - catch (Poco::NotFoundException&) - { - return false; + Binary::Ptr pPayload = pDoc->get("payload"); + serverSecondMsg.assign(reinterpret_cast(pPayload->buffer().begin()), pPayload->buffer().size()); } + else return false; } - else return false; + else throw Poco::ProtocolException("empty response for saslContinue"); Poco::HMACEngine hmacSKey(saltedPassword); hmacSKey.update(std::string("Server Key")); @@ -314,7 +284,8 @@ bool Database::authSCRAM(Connection& connection, const std::string& username, co kvm = parseKeyValueList(serverSecondMsg); std::string serverSignatureReceived = kvm["v"]; - if (serverSignature != serverSignatureReceived) return false; + if (serverSignature != serverSignatureReceived) + throw Poco::ProtocolException("server signature verification failed"); pCommand = createCommand(); pCommand->selector() @@ -328,8 +299,7 @@ bool Database::authSCRAM(Connection& connection, const std::string& username, co Document::Ptr pDoc = response.documents()[0]; return pDoc->get("ok") == 1; } - - return false; + else throw Poco::ProtocolException("empty response for saslContinue"); } @@ -340,7 +310,7 @@ Int64 Database::count(Connection& connection, const std::string& collectionName) Poco::MongoDB::ResponseMessage response; connection.sendRequest(*countRequest, response); - if ( response.documents().size() > 0 ) + if (response.documents().size() > 0) { Poco::MongoDB::Document::Ptr doc = response.documents()[0]; return doc->getInteger("n"); @@ -357,22 +327,22 @@ Poco::MongoDB::Document::Ptr Database::ensureIndex(Connection& connection, const index->add("name", indexName); index->add("key", keys); - if ( version > 0 ) + if (version > 0) { index->add("version", version); } - if ( unique ) + if (unique) { index->add("unique", true); } - if ( background ) + if (background) { index->add("background", true); } - if ( ttl > 0 ) + if (ttl > 0) { index->add("expireAfterSeconds", ttl); } @@ -396,7 +366,7 @@ Document::Ptr Database::getLastErrorDoc(Connection& connection) const Poco::MongoDB::ResponseMessage response; connection.sendRequest(*request, response); - if ( response.documents().size() > 0 ) + if (response.documents().size() > 0) { errorDoc = response.documents()[0]; } @@ -408,7 +378,7 @@ Document::Ptr Database::getLastErrorDoc(Connection& connection) const std::string Database::getLastError(Connection& connection) const { Document::Ptr errorDoc = getLastErrorDoc(connection); - if ( !errorDoc.isNull() && errorDoc->isType("err") ) + if (!errorDoc.isNull() && errorDoc->isType("err")) { return errorDoc->get("err"); } diff --git a/MongoDB/src/DeleteRequest.cpp b/MongoDB/src/DeleteRequest.cpp index 5662ced92..7215f3c2e 100644 --- a/MongoDB/src/DeleteRequest.cpp +++ b/MongoDB/src/DeleteRequest.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: DeleteRequest // -// Implementation of the DeleteRequest class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // @@ -23,8 +21,8 @@ namespace Poco { namespace MongoDB { -DeleteRequest::DeleteRequest(const std::string& collectionName, DeleteRequest::Flags flags) - : RequestMessage(MessageHeader::Delete), +DeleteRequest::DeleteRequest(const std::string& collectionName, DeleteRequest::Flags flags): + RequestMessage(MessageHeader::OP_DELETE), _flags(flags), _fullCollectionName(collectionName), _selector() @@ -32,9 +30,9 @@ DeleteRequest::DeleteRequest(const std::string& collectionName, DeleteRequest::F } -DeleteRequest::DeleteRequest(const std::string& collectionName, bool justOne) - : RequestMessage(MessageHeader::Delete), - _flags(justOne ? DELETE_SINGLE_REMOVE : DELETE_NONE), +DeleteRequest::DeleteRequest(const std::string& collectionName, bool justOne): + RequestMessage(MessageHeader::OP_DELETE), + _flags(justOne ? DELETE_SINGLE_REMOVE : DELETE_DEFAULT), _fullCollectionName(collectionName), _selector() { diff --git a/MongoDB/src/Document.cpp b/MongoDB/src/Document.cpp index 0e6630ed1..1a153589f 100644 --- a/MongoDB/src/Document.cpp +++ b/MongoDB/src/Document.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: Document // -// Implementation of the Document class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // @@ -44,7 +42,7 @@ Element::Ptr Document::get(const std::string& name) const Element::Ptr element; ElementSet::const_iterator it = std::find_if(_elements.begin(), _elements.end(), ElementFindByName(name)); - if ( it != _elements.end() ) + if (it != _elements.end()) { return *it; } @@ -52,29 +50,31 @@ Element::Ptr Document::get(const std::string& name) const return element; } + Int64 Document::getInteger(const std::string& name) const { Element::Ptr element = get(name); - if ( element.isNull() ) throw NotFoundException(name); + if (element.isNull()) throw Poco::NotFoundException(name); - if ( ElementTraits::TypeId == element->type() ) + if (ElementTraits::TypeId == element->type()) { - ConcreteElement* concrete = dynamic_cast* >(element.get()); - if ( concrete != NULL ) return concrete->value(); + ConcreteElement* concrete = dynamic_cast*>(element.get()); + if (concrete) return concrete->value(); } - else if ( ElementTraits::TypeId == element->type() ) + else if (ElementTraits::TypeId == element->type()) { - ConcreteElement* concrete = dynamic_cast* >(element.get()); - if ( concrete != NULL ) return concrete->value(); + ConcreteElement* concrete = dynamic_cast*>(element.get()); + if (concrete) return concrete->value(); } - else if ( ElementTraits::TypeId == element->type() ) + else if (ElementTraits::TypeId == element->type()) { - ConcreteElement* concrete = dynamic_cast* >(element.get()); - if ( concrete != NULL ) return concrete->value(); + ConcreteElement* concrete = dynamic_cast*>(element.get()); + if (concrete) return concrete->value(); } - throw BadCastException("Invalid type mismatch!"); + throw Poco::BadCastException("Invalid type mismatch!"); } + void Document::read(BinaryReader& reader) { int size; @@ -83,13 +83,13 @@ void Document::read(BinaryReader& reader) unsigned char type; reader >> type; - while( type != '\0' ) + while (type != '\0') { Element::Ptr element; - + std::string name = BSONReader(reader).readCString(); - switch(type) + switch (type) { case ElementTraits::TypeId: element = new ConcreteElement(name, 0); @@ -101,16 +101,16 @@ void Document::read(BinaryReader& reader) element = new ConcreteElement(name, ""); break; case ElementTraits::TypeId: - element = new ConcreteElement(name, new Document()); + element = new ConcreteElement(name, new Document); break; case ElementTraits::TypeId: - element = new ConcreteElement(name, new Array()); + element = new ConcreteElement(name, new Array); break; case ElementTraits::TypeId: - element = new ConcreteElement(name, new Binary()); + element = new ConcreteElement(name, new Binary); break; case ElementTraits::TypeId: - element = new ConcreteElement(name, new ObjectId()); + element = new ConcreteElement(name, new ObjectId); break; case ElementTraits::TypeId: element = new ConcreteElement(name, false); @@ -158,31 +158,31 @@ std::string Document::toString(int indent) const oss << '{'; - if ( indent > 0 ) oss << std::endl; + if (indent > 0) oss << std::endl; - for(ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it) + for (ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it) { - if ( it != _elements.begin() ) + if (it != _elements.begin()) { oss << ','; - if ( indent > 0 ) oss << std::endl; + if (indent > 0) oss << std::endl; } - for(int i = 0; i < indent; ++i) oss << ' '; + for (int i = 0; i < indent; ++i) oss << ' '; oss << '"' << (*it)->name() << '"'; - oss << (( indent > 0 ) ? " : " : ":"); + oss << (indent > 0 ? " : " : ":"); oss << (*it)->toString(indent > 0 ? indent + 2 : 0); } - if ( indent > 0 ) + if (indent > 0) { oss << std::endl; - if ( indent >= 2 ) indent -= 2; + if (indent >= 2) indent -= 2; - for(int i = 0; i < indent; ++i) oss << ' '; + for (int i = 0; i < indent; ++i) oss << ' '; } oss << '}'; @@ -193,7 +193,7 @@ std::string Document::toString(int indent) const void Document::write(BinaryWriter& writer) { - if ( _elements.empty() ) + if (_elements.empty()) { writer << 5; } @@ -201,7 +201,7 @@ void Document::write(BinaryWriter& writer) { std::stringstream sstream; Poco::BinaryWriter tempWriter(sstream); - for(ElementSet::iterator it = _elements.begin(); it != _elements.end(); ++it) + for (ElementSet::iterator it = _elements.begin(); it != _elements.end(); ++it) { tempWriter << static_cast((*it)->type()); BSONWriter(tempWriter).writeCString((*it)->name()); diff --git a/MongoDB/src/Element.cpp b/MongoDB/src/Element.cpp index 8b26d8022..a1e3d91e8 100644 --- a/MongoDB/src/Element.cpp +++ b/MongoDB/src/Element.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: Element // -// Implementation of the Element class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // diff --git a/MongoDB/src/GetMoreRequest.cpp b/MongoDB/src/GetMoreRequest.cpp index 08ffd6f7d..ae877f5c4 100644 --- a/MongoDB/src/GetMoreRequest.cpp +++ b/MongoDB/src/GetMoreRequest.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: GetMoreRequest // -// Implementation of the GetMoreRequest class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // @@ -24,8 +22,8 @@ namespace Poco { namespace MongoDB { -GetMoreRequest::GetMoreRequest(const std::string& collectionName, Int64 cursorID) - : RequestMessage(MessageHeader::GetMore), +GetMoreRequest::GetMoreRequest(const std::string& collectionName, Int64 cursorID): + RequestMessage(MessageHeader::OP_GET_MORE), _fullCollectionName(collectionName), _numberToReturn(100), _cursorID(cursorID) diff --git a/MongoDB/src/InsertRequest.cpp b/MongoDB/src/InsertRequest.cpp index 3ddaa6457..5a940ef9e 100644 --- a/MongoDB/src/InsertRequest.cpp +++ b/MongoDB/src/InsertRequest.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: InsertRequest // -// Implementation of the InsertRequest class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // @@ -23,8 +21,8 @@ namespace Poco { namespace MongoDB { -InsertRequest::InsertRequest(const std::string& collectionName, Flags flags) - : RequestMessage(MessageHeader::Insert), +InsertRequest::InsertRequest(const std::string& collectionName, Flags flags): + RequestMessage(MessageHeader::OP_INSERT), _flags(flags), _fullCollectionName(collectionName) { @@ -38,12 +36,12 @@ InsertRequest::~InsertRequest() void InsertRequest::buildRequest(BinaryWriter& writer) { - //TODO: throw exception when no document is added + poco_assert (!_documents.empty()); writer << _flags; BSONWriter bsonWriter(writer); bsonWriter.writeCString(_fullCollectionName); - for(Document::Vector::iterator it = _documents.begin(); it != _documents.end(); ++it) + for (Document::Vector::iterator it = _documents.begin(); it != _documents.end(); ++it) { bsonWriter.write(*it); } diff --git a/MongoDB/src/JavaScriptCode.cpp b/MongoDB/src/JavaScriptCode.cpp index b2cf9d88a..8ae8ca144 100644 --- a/MongoDB/src/JavaScriptCode.cpp +++ b/MongoDB/src/JavaScriptCode.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: JavaScriptCode // -// Implementation of the JavaScriptCode class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // diff --git a/MongoDB/src/KillCursorsRequest.cpp b/MongoDB/src/KillCursorsRequest.cpp index ffc141973..912d18e98 100644 --- a/MongoDB/src/KillCursorsRequest.cpp +++ b/MongoDB/src/KillCursorsRequest.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: KillCursorsRequest // -// Implementation of the KillCursorsRequest class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // @@ -23,8 +21,8 @@ namespace Poco { namespace MongoDB { -KillCursorsRequest::KillCursorsRequest() - : RequestMessage(MessageHeader::KillCursors) +KillCursorsRequest::KillCursorsRequest(): + RequestMessage(MessageHeader::OP_KILL_CURSORS) { } @@ -38,7 +36,7 @@ void KillCursorsRequest::buildRequest(BinaryWriter& writer) { writer << 0; // 0 - reserved for future use writer << _cursors.size(); - for(std::vector::iterator it = _cursors.begin(); it != _cursors.end(); ++it) + for (std::vector::iterator it = _cursors.begin(); it != _cursors.end(); ++it) { writer << *it; } diff --git a/MongoDB/src/Message.cpp b/MongoDB/src/Message.cpp index efe84929e..fee51e77e 100644 --- a/MongoDB/src/Message.cpp +++ b/MongoDB/src/Message.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: Message // -// Implementation of the Message class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // @@ -23,7 +21,8 @@ namespace Poco { namespace MongoDB { -Message::Message(MessageHeader::OpCode opcode) : _header(opcode) +Message::Message(MessageHeader::OpCode opcode): + _header(opcode) { } diff --git a/MongoDB/src/MessageHeader.cpp b/MongoDB/src/MessageHeader.cpp index 6d12f8ca6..949c6cc4d 100644 --- a/MongoDB/src/MessageHeader.cpp +++ b/MongoDB/src/MessageHeader.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: MessageHeader // -// Implementation of the MessageHeader class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // @@ -18,15 +16,17 @@ #include "Poco/MongoDB/Message.h" #include "Poco/Exception.h" -#include "Poco/Net/SocketStream.h" -#include "Poco/StreamCopier.h" namespace Poco { namespace MongoDB { -MessageHeader::MessageHeader(OpCode opCode) : _messageLength(0), _requestID(0), _responseTo(0), _opCode(opCode) +MessageHeader::MessageHeader(OpCode opCode): + _messageLength(0), + _requestID(0), + _responseTo(0), + _opCode(opCode) { } diff --git a/MongoDB/src/ObjectId.cpp b/MongoDB/src/ObjectId.cpp index 0111c7af7..5a79f6989 100644 --- a/MongoDB/src/ObjectId.cpp +++ b/MongoDB/src/ObjectId.cpp @@ -7,42 +7,47 @@ // Package: MongoDB // Module: ObjectId // -// Implementation of the ObjectId class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // // SPDX-License-Identifier: BSL-1.0 // + #include "Poco/MongoDB/ObjectId.h" #include "Poco/Format.h" +#include namespace Poco { namespace MongoDB { + ObjectId::ObjectId() { - memset(_id, 0, sizeof(_id)); + std::memset(_id, 0, sizeof(_id)); } + ObjectId::ObjectId(const std::string& id) { poco_assert_dbg(id.size() == 24); - const char *p = id.c_str(); - for (std::size_t i = 0; i < 12; ++i) { + const char* p = id.c_str(); + for (std::size_t i = 0; i < 12; ++i) + { _id[i] = fromHex(p); p += 2; } } + ObjectId::ObjectId(const ObjectId& copy) { - memcpy(_id, copy._id, sizeof(_id)); + std::memcpy(_id, copy._id, sizeof(_id)); } + ObjectId::~ObjectId() { } @@ -52,7 +57,7 @@ std::string ObjectId::toString(const std::string& fmt) const { std::string s; - for(int i = 0; i < 12; ++i) + for (int i = 0; i < 12; ++i) { s += format(fmt, (unsigned int) _id[i]); } diff --git a/MongoDB/src/QueryRequest.cpp b/MongoDB/src/QueryRequest.cpp index 49f3e549b..216dfe7e7 100644 --- a/MongoDB/src/QueryRequest.cpp +++ b/MongoDB/src/QueryRequest.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: QueryRequest // -// Implementation of the QueryRequest class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // @@ -23,8 +21,8 @@ namespace Poco { namespace MongoDB { -QueryRequest::QueryRequest(const std::string& collectionName, QueryRequest::Flags flags) - : RequestMessage(MessageHeader::Query), +QueryRequest::QueryRequest(const std::string& collectionName, QueryRequest::Flags flags): + RequestMessage(MessageHeader::OP_QUERY), _flags(flags), _fullCollectionName(collectionName), _numberToSkip(0), @@ -48,7 +46,7 @@ void QueryRequest::buildRequest(BinaryWriter& writer) writer << _numberToReturn; _selector.write(writer); - if ( ! _returnFieldSelector.empty() ) + if (!_returnFieldSelector.empty()) { _returnFieldSelector.write(writer); } diff --git a/MongoDB/src/RegularExpression.cpp b/MongoDB/src/RegularExpression.cpp index c599c5484..4c6ce193e 100644 --- a/MongoDB/src/RegularExpression.cpp +++ b/MongoDB/src/RegularExpression.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: RegularExpression // -// Implementation of the RegularExpression class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // @@ -29,7 +27,9 @@ RegularExpression::RegularExpression() } -RegularExpression::RegularExpression(const std::string& pattern, const std::string& options) : _pattern(pattern), _options(options) +RegularExpression::RegularExpression(const std::string& pattern, const std::string& options): + _pattern(pattern), + _options(options) { } @@ -42,9 +42,9 @@ RegularExpression::~RegularExpression() SharedPtr RegularExpression::createRE() const { int options = 0; - for(std::string::const_iterator optIt = _options.begin(); optIt != _options.end(); ++optIt) + for (std::string::const_iterator optIt = _options.begin(); optIt != _options.end(); ++optIt) { - switch(*optIt) + switch (*optIt) { case 'i': // Case Insensitive options |= Poco::RegularExpression::RE_CASELESS; diff --git a/MongoDB/src/ReplicaSet.cpp b/MongoDB/src/ReplicaSet.cpp index 3f6a0df0f..d00034093 100644 --- a/MongoDB/src/ReplicaSet.cpp +++ b/MongoDB/src/ReplicaSet.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: ReplicaSet // -// Implementation of the ReplicaSet class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // @@ -20,11 +18,13 @@ #include "Poco/MongoDB/QueryRequest.h" #include "Poco/MongoDB/ResponseMessage.h" + namespace Poco { namespace MongoDB { -ReplicaSet::ReplicaSet(const std::vector &addresses) : _addresses(addresses) +ReplicaSet::ReplicaSet(const std::vector &addresses): + _addresses(addresses) { } @@ -38,10 +38,10 @@ Connection::Ptr ReplicaSet::findMaster() { Connection::Ptr master; - for(std::vector::iterator it = _addresses.begin(); it != _addresses.end(); ++it) + for (std::vector::iterator it = _addresses.begin(); it != _addresses.end(); ++it) { master = isMaster(*it); - if ( ! master.isNull() ) + if (!master.isNull()) { break; } @@ -66,25 +66,25 @@ Connection::Ptr ReplicaSet::isMaster(const Net::SocketAddress& address) ResponseMessage response; conn->sendRequest(request, response); - if ( response.documents().size() > 0 ) + if (response.documents().size() > 0) { Document::Ptr doc = response.documents()[0]; - if ( doc->get("ismaster") ) + if (doc->get("ismaster")) { return conn; } - else if ( doc->exists("primary") ) + else if (doc->exists("primary")) { return isMaster(Net::SocketAddress(doc->get("primary"))); } } } - catch(...) + catch (...) { - conn = NULL; + conn = 0; } - return NULL; + return 0; } diff --git a/MongoDB/src/RequestMessage.cpp b/MongoDB/src/RequestMessage.cpp index 9af9c4efe..602a2f839 100644 --- a/MongoDB/src/RequestMessage.cpp +++ b/MongoDB/src/RequestMessage.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: RequestMessage // -// Implementation of the RequestMessage class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // @@ -25,7 +23,8 @@ namespace Poco { namespace MongoDB { -RequestMessage::RequestMessage(MessageHeader::OpCode opcode) : Message(opcode) +RequestMessage::RequestMessage(MessageHeader::OpCode opcode): + Message(opcode) { } diff --git a/MongoDB/src/ResponseMessage.cpp b/MongoDB/src/ResponseMessage.cpp index a46e27161..56b129554 100644 --- a/MongoDB/src/ResponseMessage.cpp +++ b/MongoDB/src/ResponseMessage.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: ResponseMessage // -// Implementation of the ResponseMessage class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // @@ -24,7 +22,12 @@ namespace Poco { namespace MongoDB { -ResponseMessage::ResponseMessage() : Message(MessageHeader::Reply), _responseFlags(0), _cursorID(0), _startingFrom(0), _numberReturned(0) +ResponseMessage::ResponseMessage(): + Message(MessageHeader::OP_REPLY), + _responseFlags(0), + _cursorID(0), + _startingFrom(0), + _numberReturned(0) { } @@ -57,7 +60,7 @@ void ResponseMessage::read(std::istream& istr) reader >> _startingFrom; reader >> _numberReturned; - for(int i = 0; i < _numberReturned; ++i) + for (int i = 0; i < _numberReturned; ++i) { Document::Ptr doc = new Document(); doc->read(reader); diff --git a/MongoDB/src/UpdateRequest.cpp b/MongoDB/src/UpdateRequest.cpp index f8d17fd41..9007ed1d1 100644 --- a/MongoDB/src/UpdateRequest.cpp +++ b/MongoDB/src/UpdateRequest.cpp @@ -7,8 +7,6 @@ // Package: MongoDB // Module: UpdateRequest // -// Implementation of the UpdateRequest class. -// // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. // and Contributors. // @@ -23,8 +21,8 @@ namespace Poco { namespace MongoDB { -UpdateRequest::UpdateRequest(const std::string& collectionName, UpdateRequest::Flags flags) - : RequestMessage(MessageHeader::Update), +UpdateRequest::UpdateRequest(const std::string& collectionName, UpdateRequest::Flags flags): + RequestMessage(MessageHeader::OP_UPDATE), _flags(flags), _fullCollectionName(collectionName), _selector(), diff --git a/MongoDB/testsuite/src/MongoDBTest.cpp b/MongoDB/testsuite/src/MongoDBTest.cpp index cc40b0025..fa42a0715 100644 --- a/MongoDB/testsuite/src/MongoDBTest.cpp +++ b/MongoDB/testsuite/src/MongoDBTest.cpp @@ -8,11 +8,10 @@ // // SPDX-License-Identifier: BSL-1.0 // -#include + #include "Poco/DateTime.h" #include "Poco/ObjectPool.h" - #include "Poco/MongoDB/InsertRequest.h" #include "Poco/MongoDB/QueryRequest.h" #include "Poco/MongoDB/DeleteRequest.h" @@ -22,16 +21,17 @@ #include "Poco/MongoDB/Cursor.h" #include "Poco/MongoDB/ObjectId.h" #include "Poco/MongoDB/Binary.h" - #include "Poco/Net/NetException.h" #include "Poco/UUIDGenerator.h" - #include "MongoDBTest.h" #include "CppUnit/TestCaller.h" #include "CppUnit/TestSuite.h" +#include + using namespace Poco::MongoDB; + Poco::MongoDB::Connection::Ptr MongoDBTest::_mongo; @@ -79,6 +79,7 @@ void MongoDBTest::testInsertRequest() _mongo->sendRequest(request); } + void MongoDBTest::testQueryRequest() { Poco::MongoDB::QueryRequest request("team.players"); @@ -120,6 +121,7 @@ void MongoDBTest::testQueryRequest() } } + void MongoDBTest::testDBQueryRequest() { Database db("team"); @@ -346,6 +348,7 @@ void MongoDBTest::testCommand() { } } + void MongoDBTest::testUUID() { Poco::MongoDB::Document::Ptr club = new Poco::MongoDB::Document(); diff --git a/MongoDB/testsuite/src/MongoDBTest.h b/MongoDB/testsuite/src/MongoDBTest.h index ab2055ec8..bcab44ddc 100644 --- a/MongoDB/testsuite/src/MongoDBTest.h +++ b/MongoDB/testsuite/src/MongoDBTest.h @@ -18,7 +18,6 @@ #include "Poco/MongoDB/MongoDB.h" #include "Poco/MongoDB/Connection.h" - #include "CppUnit/TestCase.h" @@ -27,7 +26,6 @@ class MongoDBTest: public CppUnit::TestCase public: MongoDBTest(const std::string& name); - virtual ~MongoDBTest(); void testInsertRequest(); @@ -49,9 +47,7 @@ public: static CppUnit::Test* suite(); private: - static Poco::MongoDB::Connection::Ptr _mongo; - };