internal KDBRecord <byte[]> GetRaw(string table, byte[] key)
                             {
                                 var db  = MongoClient.DatabaseFromConnectString(EffectiveConnectionString);
                                 var doc = db[table].FindOne(MongoQuery.ID_EQ_BYTE_ARRAY(key));

                                 if (doc == null)
                                 {
                                     return(KDBRecord <byte[]> .Unassigned);
                                 }
                                 var elmValue = doc[FIELD_VALUE] as BSONBinaryElement;

                                 if (elmValue == null)
                                 {
                                     return(KDBRecord <byte[]> .Unassigned);
                                 }

                                 DateTime lastUseDate;
                                 DateTime?absoluteExpirationDateUTC;
                                 int      slidingExpirationDays;

                                 readAttrs(table, doc, out lastUseDate, out absoluteExpirationDateUTC, out slidingExpirationDays);

                                 var value = elmValue.Value;

                                 return(new KDBRecord <byte[]>(value.Data, slidingExpirationDays, lastUseDate, absoluteExpirationDateUTC));
                             }
Beispiel #2
0
        protected virtual int DoUpdate(Connector.Database db, Row row, IDataStoreKey key, FieldFilterFunc filter = null)
        {
            var doc = convertRowToBSONDocumentWith_ID(row, "update", filter);
            var _id = doc[Connector.Protocol._ID];

            doc.Delete(Connector.Protocol._ID);
            if (doc.Count == 0)
            {
                return(0);              // nothing to update
            }
            //20160212 spol
            if (filter != null)
            {
                var wrapDoc = new BSONDocument();
                wrapDoc.Set(new BSONDocumentElement(Connector.Protocol.SET, doc));
                doc = wrapDoc;
            }

            var tname = GetCollectionName(row.Schema);

            var collection = db[tname];

            var qry = new Connector.Query();

            qry.Set(_id);
            var upd = new Connector.UpdateEntry(qry, doc, false, false);

            var result = collection.Update(upd);

            CheckCRUDResult(result, row.Schema.Name, "update");

            return(result.TotalDocumentsAffected);
        }
Beispiel #3
0
    public static Query ID_EQ_String(string id)
    {
      if (id==null)
       throw new MongoDBConnectorException(StringConsts.ARGUMENT_ERROR+"ID_EQ_String(id==null)");

      var result = new Query();
      result.Set( new BSONStringElement(_ID, id) );
      return result;
    }
                             internal void Touch(string table, byte[] key)
                             {
                                 var db   = MongoClient.DatabaseFromConnectString(EffectiveConnectionString);
                                 var udoc = new BSONDocument()
                                            .Set(new BSONDateTimeElement(FIELD_LAST_USE_DATE, App.TimeSource.UTCNow));

                                 // 20170404 spol: update document with $set to prevent document clear
                                 udoc = new BSONDocument()
                                        .Set(new BSONDocumentElement("$set", udoc));

                                 db[table].Update(new UpdateEntry(MongoQuery.ID_EQ_BYTE_ARRAY(key), udoc, multi: false, upsert: false));
                             }
Beispiel #5
0
        private void btnFetchOrderBy_Click(object sender, EventArgs e)
        {
            var c = MongoClient.Instance.DefaultLocalServer["db1"]["t1"];

            var query =  new Query(
            @"{
               '$query': {'_id': {'$lte': '$$ID'}},
               '$orderby': {'age': '$$ORD'}
              }",
               true,

             new TemplateArg("ID", BSONElementType.Int32, 197),
             new TemplateArg("ORD", BSONElementType.Int32, -1)
            );

            var data = c.FindAndFetchAll(query, cursorFetchLimit: 4);
            MessageBox.Show( "Got {0} \n=======================\n {1}".Args(data.Count, data.ToJSON(JSONWritingOptions.PrettyPrint)) );
        }
Beispiel #6
0
        protected virtual int DoDelete(Connector.Database db, Row row, IDataStoreKey key)
        {
            var doc = convertRowToBSONDocumentWith_ID(row, "delete");

            var tname = GetCollectionName(row.Schema);

            var collection = db[tname];

            var qry = new Connector.Query();

            qry.Set(doc[Connector.Protocol._ID]);

            var result = collection.Delete(new Connector.DeleteEntry(qry, Connector.DeleteLimit.OnlyFirstMatch));

            CheckCRUDResult(result, row.Schema.Name, "delete");

            return(result.TotalDocumentsAffected);
        }
Beispiel #7
0
        /// <summary>
        /// Finds a document that satisfied query or null
        /// </summary>
        public BSONDocument FindOne(Query query, BSONDocument selector = null)
        {
          EnsureObjectNotDisposed();

          if (query==null)
           throw new MongoDBConnectorException(StringConsts.ARGUMENT_ERROR+"Collection.FindOne(query==null)");

          var connection = Server.AcquireConnection();
          try
          {
            var reqId = Database.NextRequestID;
            return connection.FindOne(reqId, this, query, selector);
          }
          finally
          {
            connection.Release();
          }
        }
Beispiel #8
0
        protected virtual int DoUpdate(Connector.Database db, Row row, IDataStoreKey key)
        {
            var doc = convertRowToBSONDocumentWith_ID(row, "update");

            var tname = GetCollectionName(row.Schema);

            var collection = db[tname];

            var qry = new Connector.Query();

            qry.Set(doc[Connector.Protocol._ID]);
            var upd = new Connector.UpdateEntry(qry, doc, false, false);

            var result = collection.Update(upd);

            checkCRUDResult(result, row.Schema.Name, "update");

            return(result.TotalDocumentsAffected);
        }
        public void AllocClient()
        {
            using(var client= new MongoClient("My Test"))
              {
            var server = client.DefaultLocalServer;
            Assert.IsNotNull( server );
            Assert.IsTrue( object.ReferenceEquals(server, client[server.Node]) );

            var db = server["db1"];
            Assert.IsNotNull( db );
            Assert.IsTrue( object.ReferenceEquals(db, server[db.Name]) );

            var collection = db["t1"];
            Assert.IsNotNull( collection );
            Assert.IsTrue( object.ReferenceEquals(collection, db[collection.Name]) );

            var query = new Query();
            var result = collection.FindOne( query );
              }
        }
Beispiel #10
0
                             /* BSON Document Schema:
                              * ----------------
                              *
                              * {_id: key, v: {}|binary, lastUseDate: dateUTC, absoluteExpirationDateUTC: date|null, slidingExpirationDays: int|null}
                              * actual field names:
                              * {_id: key, v: {}|binary, d: dateUTC, a: date|null, s: int|null}
                              */

                             internal KDBRecord <TRow> Get <TRow>(string table, byte[] key) where TRow : Row
                             {
                                 var db  = MongoClient.DatabaseFromConnectString(EffectiveConnectionString);
                                 var doc = db[table].FindOne(MongoQuery.ID_EQ_BYTE_ARRAY(key));

                                 if (doc == null)
                                 {
                                     return(KDBRecord <TRow> .Unassigned);
                                 }
                                 var elmValue = doc[FIELD_VALUE] as BSONDocumentElement;

                                 if (elmValue == null)
                                 {
                                     return(KDBRecord <TRow> .Unassigned);
                                 }

                                 DateTime lastUseDate;
                                 DateTime?absoluteExpirationDateUTC;
                                 int      slidingExpirationDays;

                                 readAttrs(table, doc, out lastUseDate, out absoluteExpirationDateUTC, out slidingExpirationDays);

                                 var value = elmValue.Value;

                                 TRow row;

                                 if (value == null)
                                 {
                                     row = Row.MakeRow(new Schema(Guid.NewGuid().ToString()), typeof(TRow)) as TRow;
                                 }
                                 else
                                 {
                                     var schema = Store.m_Converter.InferSchemaFromBSONDocument(value);
                                     row = Row.MakeRow(schema, typeof(TRow)) as TRow;
                                     Store.m_Converter.BSONDocumentToRow(value, row, null);
                                 }
                                 return(new KDBRecord <TRow>(row, slidingExpirationDays, lastUseDate, absoluteExpirationDateUTC));
                             }
Beispiel #11
0
        private void btnFetch_Click(object sender, EventArgs e)
        {
            var c = MongoClient.Instance.DefaultLocalServer["db1"]["t1"];

              var sw = new System.Diagnostics.Stopwatch();

              var cnt = 0;
              var query = new Query();
              BSONDocument lastDoc = null;
              using(var cursor = c.Find(query))
            foreach(var doc in cursor)
            {
              cnt++;
              if (cnt==1) sw.Start();
              lastDoc = doc;
            //  if (cnt>3) break;
            }

              var el = sw.ElapsedMilliseconds;

              Text = "Fetched {0} in {1} ms at {2}ops/sec".Args(cnt, el, cnt / (el / 1000d));
              MessageBox.Show(lastDoc.ToString());
        }
Beispiel #12
0
        /// <summary>
        /// Performs server-side count over cursor
        /// </summary>
        /// <param name="query">Optional. A query that selects which documents to count in a collection</param>
        /// <param name="limit">Optional. The maximum number of matching documents to return</param>
        /// <param name="skip">Optional. The number of matching documents to skip before returning results</param>
        /// <param name="hint">Optional. The index to use. Specify either the index name as a string or the index specification document.</param>
        /// <returns>Count </returns>
        public long Count(Query query = null, int limit = -1, int skip = -1, object hint = null)
        { // See http://docs.mongodb.org/v3.0/reference/command/count/

          EnsureObjectNotDisposed();

          if (hint!=null && (!(hint is string)) && (!(hint is BSONDocument)))
            throw new MongoDBConnectorException(StringConsts.ARGUMENT_ERROR+"Collection.Count(hint must be string | BSONDocument)"); 

          var connection = Server.AcquireConnection();
          try
          {
            var reqId = Database.NextRequestID;
            return connection.Count(reqId, this, query, limit, skip, hint);
          }
          finally
          {
            connection.Release();
          }
        }
Beispiel #13
0
        /// <summary>
        /// Deletes 1 document from the server. Inspect CRUDResult for write errors and docs affected/matched
        /// </summary>
        public CRUDResult DeleteOne(Query query)
        {
          if (query==null)
            throw new MongoDBConnectorException(StringConsts.ARGUMENT_ERROR+"Collection.DeleteOne(query==null)");

          return this.Delete(new DeleteEntry(query, DeleteLimit.OnlyFirstMatch));
        }
Beispiel #14
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            var c = MongoClient.Instance.DefaultLocalServer["db1"]["t1"];

              var query =  new Query("{'_id': '$$ID'}", true, new TemplateArg("ID", BSONElementType.Int32, 7));

              var doc = c.FindOne(query);

              doc["age"].ObjectValue = 777;

              var result = c.Save(doc);

              Text = "Affected: {0} Updated: {1}".Args(result.TotalDocumentsAffected, result.TotalDocumentsUpdatedAffected);
        }
Beispiel #15
0
        protected virtual int DoDelete(Connector.Database db, Row row, IDataStoreKey key)
        {
          var doc = convertRowToBSONDocumentWith_ID(row, "delete");
          
          var tname = GetCollectionName(row.Schema);
          
          var collection = db[tname]; 
          
          var qry = new Connector.Query();
          qry.Set( doc[Connector.Protocol._ID] );

          var result = collection.Delete( new Connector.DeleteEntry( qry, Connector.DeleteLimit.OnlyFirstMatch) );
         
          checkCRUDResult(result, row.Schema.Name, "delete");

          return result.TotalDocumentsAffected;
        }
Beispiel #16
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;
        }
Beispiel #17
0
 public static Query ID_EQ_BYTE_ARRAY(byte[] id)
 {
     var result = new Query();
       result.Set( NFX.Serialization.BSON.RowConverter.ByteBufferID_CLRtoBSON(_ID, id) );
       return result;
 }
Beispiel #18
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);
        }
Beispiel #19
0
                             internal bool Delete(string table, byte[] key)
                             {
                                 var db = MongoClient.DatabaseFromConnectString(EffectiveConnectionString);

                                 return(db[table].DeleteOne(MongoQuery.ID_EQ_BYTE_ARRAY(key)).TotalDocumentsAffected > 0);
                             }
Beispiel #20
0
        /// <summary>
        /// Finds all documents that match the supplied query, optionally skipping some.
        /// Fetches and returns all documents as a list. Be careful not to fetch too many
        /// </summary>
        /// <param name="query">Query to match against</param>
        /// <param name="skipCount">How many documents to skip at the beginning</param>
        /// <param name="cursorFetchLimit">Impose a limit on total number of fetched docs</param>
        /// <param name="fetchBy">Size of fetch block</param>
        /// <param name="selector">Optional field mapping document like: {"field_name": 1}</param>
        /// <returns>A list of documents (may be empty)</returns>
        public List<BSONDocument> FindAndFetchAll(Query query, int skipCount = 0, int cursorFetchLimit = 0, int fetchBy = 0, BSONDocument selector = null)
        {
          var result = new List<BSONDocument>();

          using(var cursor = this.Find(query, skipCount, fetchBy, selector))
            foreach(var doc in cursor)
             if (cursorFetchLimit<=0 || result.Count<cursorFetchLimit)
              result.Add(doc);
             else
              break;

          return result;
        }
Beispiel #21
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); 
        }
Beispiel #22
0
        protected virtual int DoUpdate(Connector.Database db, Row row, IDataStoreKey key, FieldFilterFunc filter = null)
        {
            var doc = convertRowToBSONDocumentWith_ID(row, "update", filter);
              var _id = doc[Connector.Protocol._ID];

              //20160212 spol
              if (filter != null)
              {
            doc.Delete(Connector.Protocol._ID);
            if (doc.Count == 0) return 0; // nothing to update
            var wrapDoc = new BSONDocument();
            wrapDoc.Set(new BSONDocumentElement(Connector.Protocol.SET, doc));
            doc = wrapDoc;
              }

              var tname = GetCollectionName(row.Schema);

              var collection = db[tname];

              var qry = new Connector.Query();
              qry.Set( _id );
              var upd = new Connector.UpdateEntry(qry, doc, false, false);

              var result = collection.Update( upd );

              checkCRUDResult(result, row.Schema.Name, "update");

              return result.TotalDocumentsAffected;
        }
Beispiel #23
0
        protected virtual int DoUpdate(Connector.Database db, Row row, IDataStoreKey key)
        {
          var doc = convertRowToBSONDocumentWith_ID(row, "update");
          
          var tname = GetCollectionName(row.Schema);
          
          var collection = db[tname]; 
          
          var qry = new Connector.Query();
          qry.Set( doc[Connector.Protocol._ID] );
          var upd = new Connector.UpdateEntry(qry, doc, false, false);

          var result = collection.Update( upd );

          checkCRUDResult(result, row.Schema.Name, "update");

          return result.TotalDocumentsAffected;
        }
Beispiel #24
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;
        }
Beispiel #25
0
 public static Query ID_EQ_Int32(Int64 id)
 {
   var result = new Query();
   result.Set( new BSONInt64Element(_ID, id) );
   return result;
 }
Beispiel #26
0
        private void btnOpenCursors_Click(object sender, EventArgs e)
        {
            var c = MongoClient.Instance.DefaultLocalServer["db1"]["t1"];

            var query = new Query();
            for(var i=0; i<10; i++)
              c.Find(query);
        }
Beispiel #27
0
 public static Query ID_EQ_GDID(GDID id)
 {
   var result = new Query();
   result.Set( NFX.Serialization.BSON.RowConverter.GDID_CLRtoBSON(_ID, id) );
   return result;
 }
Beispiel #28
0
        private void btnQuery_Click(object sender, EventArgs e)
        {
            var qry = new Query("{'$and': [{lastName: {'$ge': '$$ln'}}, {firstName: {'$ge': '$$fn'}}], kaka: true}", true,
                               new TemplateArg("fn", BSONElementType.String, "Dima"),
                               new TemplateArg("ln", BSONElementType.String, "Kozlevich")
                          );

               MessageBox.Show( qry.ToString() );
        }