Пример #1
0
        protected Connector.Query MakeQuery(Connector.Database db, Query query, out Connector.Collection collection)
        {
            if (m_Source.ModifyTarget.IsNullOrWhiteSpace())
            {
                throw new MongoDBDataAccessException(StringConsts.QUERY_MODIFY_TARGET_MISSING_ERROR + "\n" + m_Source.OriginalSource);
            }

            collection = db[m_Source.ModifyTarget];

            return(MakeQuery(query));
        }
Пример #2
0
        internal BSONDocument FindOne(int requestID, Collection collection, Query query, BSONDocument selector)
        {
          EnsureObjectNotDisposed();

          m_BufferStream.Position = 0;
          var total = Protocol.Write_QUERY(m_BufferStream, 
                                           requestID, 
                                           collection.Database,
                                           collection, 
                                           Protocol.QueryFlags.None,
                                           0,
                                           -1,// If the number is negative, then the database will return that number and close the cursor. 
                                              // No futher results for that query can be fetched. If numberToReturn is 1 the server will treat it as -1 
                                              //(closing the cursor automatically).
                                           query, 
                                           selector);
          writeSocket(total);
       
          var got = readSocket();
          var reply = Protocol.Read_REPLY(got);
          Protocol.CheckReplyDataForErrors(reply);

          var result = reply.Documents!=null && reply.Documents.Length>0 ? reply.Documents[0] : null;

          return result;
        }
Пример #3
0
        internal void Drop(int requestID, Collection collection)
        {
          EnsureObjectNotDisposed();

          m_BufferStream.Position = 0;
          var total = Protocol.Write_DROP(m_BufferStream, requestID, collection);
          writeSocket(total);
          var got = readSocket();
          var reply = Protocol.Read_REPLY(got);
          Protocol.CheckReplyDataForErrors(reply);
        }
Пример #4
0
        internal long Count(int requestID, Collection collection, Query query = null, int limit = -1, int skip = -1, object hint = null)
        {
          EnsureObjectNotDisposed();

          m_BufferStream.Position = 0;
          var total = Protocol.Write_COUNT(m_BufferStream, 
                                           requestID, 
                                           collection, 
                                           query,
                                           limit,
                                           skip,
                                           hint);
          writeSocket(total);
       
          var got = readSocket();
          var reply = Protocol.Read_REPLY(got);
          Protocol.CheckReplyDataForErrors(reply);

           if (Protocol.IsOKReplyDoc(reply))
          {
            return reply.Documents[0]["n"].AsLong();
          }
          throw new MongoDBConnectorProtocolException(StringConsts.PROTO_COUNT_REPLY_ERROR); 
        }
Пример #5
0
        internal CRUDResult Insert(int requestID, Collection collection, BSONDocument[] data)
        {
          EnsureObjectNotDisposed();

          m_BufferStream.Position = 0;
          var total = Protocol.Write_INSERT(m_BufferStream, requestID, collection, data);
          writeSocket(total);
          var got = readSocket();
          return Protocol.Read_CRUD_Response("Insert", got);
        }
Пример #6
0
        internal CRUDResult Delete(int requestID, Collection collection, DeleteEntry[] deletes)
        {
          EnsureObjectNotDisposed();

          m_BufferStream.Position = 0;
          var total = Protocol.Write_DELETE(m_BufferStream, requestID, collection, deletes);
          writeSocket(total);
          var got = readSocket();
          return Protocol.Read_CRUD_Response("Delete", got);
        }
Пример #7
0
        //http://docs.mongodb.org/manual/reference/write-concern/
        private static BSONDocument getWriteConcern(Collection collection)
        {
            var concern = collection.Server.WriteConcern;

            if (concern<WriteConcern.Acknowledged) return null;//none

            var result = new BSONDocument();

            result.Set( new BSONInt32Element("w", concern >= WriteConcern.Acknowledged ? 1 : 0));

            if (concern == WriteConcern.Journaled)
             result.Set( new BSONBooleanElement("j", true));

            return result;
        }
Пример #8
0
        internal Cursor Find(int requestID, Collection collection, Query query, BSONDocument selector, int skipCount, int fetchBy)
        {
          EnsureObjectNotDisposed();

          if (fetchBy<=0) fetchBy = Cursor.DEFAULT_FETCH_BY;

          m_BufferStream.Position = 0;
          var total = Protocol.Write_QUERY(m_BufferStream, 
                                           requestID, 
                                           collection.Database,
                                           collection, 
                                           Protocol.QueryFlags.None,
                                           skipCount,
                                           fetchBy,
                                           query, 
                                           selector);
          writeSocket(total);
       
          var got = readSocket();
          var reply = Protocol.Read_REPLY(got);
          Protocol.CheckReplyDataForErrors(reply);

          var result = new Cursor(reply.CursorID, collection, query, selector, reply.Documents){ FetchBy = fetchBy };

          return result;
        }
Пример #9
0
        public static Int32 Write_QUERY(Stream stream,
            Int32 requestID,
            Database db,
            Collection collection, //may be null for $CMD
            QueryFlags flags,
            Int32 numberToSkip,
            Int32 numberToReturn,
            BSONDocument query,
            BSONDocument selector//may be null
            )
        {
            stream.Position = STD_HDR_LEN;//skip the header

                          BinUtils.WriteInt32(stream, (Int32)flags);

                          //if collection==null then query the $CMD collection
                          var fullNameBuffer = collection!=null ? collection.m_FullNameCStringBuffer : db.m_CMD_NameCStringBuffer;
                          stream.Write(fullNameBuffer, 0, fullNameBuffer.Length);

                          BinUtils.WriteInt32(stream, numberToSkip);
                          BinUtils.WriteInt32(stream, numberToReturn);

                          query.WriteAsBSON(stream);

                          if (selector!=null)
                           selector.WriteAsBSON(stream);

                          var total = (Int32)stream.Position;
                          stream.Position = 0;
                          writeStandardHeader(stream, total, requestID, 0, OP_QUERY);
                          return total;
        }
Пример #10
0
        public static Int32 Write_UPDATE(Stream stream,
            Int32 requestID,
            Collection collection,
            UpdateEntry[] updates)
        {
            var body = new BSONDocument();
                          body.Set( new BSONStringElement("update", collection.Name) );

                          var writeConcern = getWriteConcern(collection);
                          if (writeConcern!=null)
                            body.Set( new BSONDocumentElement("writeConcern", writeConcern) );

                          var arr = updates.Select( one =>
                                                    {
                                                      var doc = new BSONDocument();
                                                      doc.Set( new BSONDocumentElement("q", one.Query) );
                                                      doc.Set( new BSONDocumentElement("u", one.Update) );

                                                      if (one.Multi)
                                                        doc.Set( new BSONBooleanElement("multi", true) );

                                                      if (one.Upsert)
                                                        doc.Set( new BSONBooleanElement("upsert", true) );

                                                      return new BSONDocumentElement(doc);
                                                    }
                          ).ToArray();

                          body.Set( new BSONArrayElement("updates", arr) );

                          return Write_QUERY(stream, requestID, collection.Database, null, QueryFlags.None, 0, -1, body, null);
        }
Пример #11
0
        public static Int32 Write_INSERT(Stream stream,
            Int32 requestID,
            Collection collection,
            BSONDocument[] data)
        {
            var body = new BSONDocument();
                          body.Set( new BSONStringElement("insert", collection.Name) );

                          var writeConcern = getWriteConcern(collection);
                          if (writeConcern!=null)
                            body.Set( new BSONDocumentElement("writeConcern", writeConcern) );

                          var arr = data.Select( elm => new BSONDocumentElement(elm) ).ToArray();
                          body.Set( new BSONArrayElement("documents", arr) );

                          return Write_QUERY(stream, requestID, collection.Database, null, QueryFlags.None, 0, -1, body, null);
        }
Пример #12
0
        /*
                        struct {
                            MsgHeader header;             // standard message header
                            int32     ZERO;               // 0 - reserved for future use
                            cstring   fullCollectionName; // "dbname.collectionname"
                            int32     numberToReturn;     // number of documents to return
                            int64     cursorID;           // cursorID from the OP_REPLY
                        }

                        */
        public static Int32 Write_GET_MORE(Stream stream,
            Int32 requestID,
            Collection collection,
            Cursor cursor)
        {
            stream.Position = STD_HDR_LEN;//skip the header

                          BinUtils.WriteInt32(stream, 0);//ZERO

                           //Collection name
                          var fullNameBuffer = collection.m_FullNameCStringBuffer;
                          stream.Write(fullNameBuffer, 0, fullNameBuffer.Length);

                          BinUtils.WriteInt32(stream, cursor.FetchBy);

                          BinUtils.WriteInt64(stream, cursor.ID);

                          var total = (Int32)stream.Position;
                          stream.Position = 0;
                          writeStandardHeader(stream, total, requestID, 0, OP_GET_MORE);
                          return total;
        }
Пример #13
0
        public static Int32 Write_DROP(Stream stream,
            Int32 requestID,
            Collection collection)
        {
            var body = new BSONDocument();
                          body.Set( new BSONStringElement("drop", collection.Name) );

                          return Write_QUERY(stream, requestID, collection.Database, null, QueryFlags.None, 0, -1, body, null);
        }
Пример #14
0
        public static Int32 Write_DELETE(Stream stream,
            Int32 requestID,
            Collection collection,
            DeleteEntry[] deletes)
        {
            var body = new BSONDocument();
                          body.Set( new BSONStringElement("delete", collection.Name) );

                          var writeConcern = getWriteConcern(collection);
                          if (writeConcern!=null)
                            body.Set( new BSONDocumentElement("writeConcern", writeConcern) );

                          var arr = deletes.Select( one =>
                                                    {
                                                      var doc = new BSONDocument();
                                                      doc.Set( new BSONDocumentElement("q", one.Query) );
                                                      doc.Set( new BSONInt32Element("limit", one.Limit==DeleteLimit.None ? 0 : 1) );
                                                      return new BSONDocumentElement(doc);
                                                    }
                          ).ToArray();

                          body.Set( new BSONArrayElement("deletes", arr) );

                          return Write_QUERY(stream, requestID, collection.Database, null, QueryFlags.None, 0, -1, body, null);
        }
Пример #15
0
        public static Int32 Write_COUNT(Stream stream,
            Int32 requestID,
            Collection collection,
            Query query,
            Int32 limit,
            Int32 skip,
            object hint)
        {
            var body = new BSONDocument();
                          body.Set( new BSONStringElement("count", collection.Name) );

                          if (query!=null)  body.Set( new BSONDocumentElement("query", query) );
                          if (limit>0)      body.Set( new BSONInt32Element("limit", limit) );
                          if (skip>0)       body.Set( new BSONInt32Element("skip", limit) );

                          if (hint is string) body.Set( new BSONStringElement("hint", (string)hint) );
                          else
                          if (hint is BSONDocument) body.Set( new BSONDocumentElement("hint", (BSONDocument)hint) );

                          return Write_QUERY(stream, requestID, collection.Database, null, QueryFlags.None, 0, -1, body, null);
        }