コード例 #1
0
 public void TestConvertFrom()
 {
     string val = "return 2;";
     Code code = new Code(val);
     BsonCode bcode = BsonConvert.From(code);
     Assert.AreEqual(val, bcode.Val);
 }
コード例 #2
0
 public void TestCanAddAFunctionStrCode()
 {
     string name = "faddsc";
     Code func = new Code("function(x, y){return x + y;}");
     js.Add(name,func);
     Assert.IsNotNull(js[name]);
 }
コード例 #3
0
 public void TestCanAddFunctionByAssignment()
 {
     string name = "fassignadd";
     Code func = new Code("function(x,y){return x + y;}");
     Document doc = new Document().Append("_id", name).Append("value", func);
     js[name] = doc;
     Assert.IsNotNull(js[name]);
 }
コード例 #4
0
 public void TestCanAddAFunctionDoc()
 {
     string name = "fadddoc";
     Code func = new Code("function(x, y){return x + y;}");
     Document doc = new Document().Append("_id", name).Append("value", func);
     js.Add(doc);
     Assert.IsNotNull(js[name]);
 }
コード例 #5
0
 protected void AddFunction(string name)
 {
     Code func = new Code("function(x,y){return x + y;}");
     tests["system.js"].Insert(new Document().Append("_id", name).Append("value", func));
 }
コード例 #6
0
 public void TestCannotAddAFunctionTwice()
 {
     string name = "faddtwice";
     Code func = new Code("function(x,y){return x + y;}");
     js.Add(name, func);
     bool thrown = false;
     try{
         js.Add(name, func);
     }catch(ArgumentException){
         thrown = true;
     }
     Assert.IsTrue(thrown, "Shouldn't be able to add a function twice");
 }
コード例 #7
0
        public void TestCopyTo()
        {
            int cnt = 5;
            Document[] funcs = new Document[cnt];
            Code func = new Code("function(x,y){return x +y;}");

            for(int i = 0; i < cnt; i++){
                string name = "_" + i + "fcopyTo";
                Document doc = new Document().Append("_id", name).Append("value", func);
                js[name] = doc;
            }

            js.CopyTo(funcs, 1);
            Assert.IsNull(funcs[0]);
            Assert.IsNotNull(funcs[1]);
            Assert.IsNotNull(funcs[4]);
            Assert.IsTrue(((string)funcs[1]["_id"]).StartsWith("_1")); //as long as no other _ named functions get in.
        }
コード例 #8
0
ファイル: BsonConvert.cs プロジェクト: averyj/mongodb-csharp
 public static BsonCode From(Code val)
 {
     return new BsonCode(val);
 }
コード例 #9
0
ファイル: BsonReader.cs プロジェクト: henon/mongodb-csharp
        public Object ReadElementType(sbyte typeNum)
        {
            switch ((BsonDataType)typeNum) {
            case BsonDataType.Null:
            case BsonDataType.Undefined:
                return MongoDBNull.Value;
            case BsonDataType.MinKey:
                return MongoMinKey.Value;
            case BsonDataType.MaxKey:
                return MongoMaxKey.Value;
            case BsonDataType.Boolean:
                position++;
                return reader.ReadBoolean ();
            case BsonDataType.Integer:
                position += 4;
                return reader.ReadInt32 ();
            case BsonDataType.Long:
                position += 8;
                return reader.ReadInt64 ();
            case BsonDataType.Date:
                position += 8;
                long millis = reader.ReadInt64 ();
                return epoch.AddMilliseconds (millis);
            case BsonDataType.Oid:
                position += 12;
                return new Oid (reader.ReadBytes (12));
            case BsonDataType.Number:
                position += 8;
                return reader.ReadDouble ();
            case BsonDataType.String:{
                return ReadLenString ();
            }
            case BsonDataType.Obj:{
                Document doc = this.ReadDocument();
                if(DBRef.IsDocumentDBRef(doc)){
                    return DBRef.FromDocument(doc);
                }
                return doc;
            }

            case BsonDataType.Array:{
                Document doc = this.ReadDocument();
                if (ElementsSameType (doc)) {
                    return ConvertToArray (doc);
                } else {
                    return doc;
                }
            }
            case BsonDataType.Regex:{
                MongoRegex r = new MongoRegex ();
                r.Expression = this.ReadString ();
                r.Options = this.ReadString ();
                return r;
            }
            case BsonDataType.Code:{
                Code c = new Code ();
                c.Value = ReadLenString();
                return c;
            }
            case BsonDataType.CodeWScope:{
                int startpos = position;
                int size = reader.ReadInt32 ();
                position += 4;

                String val = this.ReadLenString();
                Document scope = this.ReadDocument();
                if (size != position - startpos) {
                    throw new System.IO.InvalidDataException (string.Format ("Should have read {0} bytes from stream but read {1} in CodeWScope", size, position - startpos));
                }
                return new CodeWScope (val, scope);
            }
            case BsonDataType.Binary:{
                int size = reader.ReadInt32 ();
                position += 4;
                byte subtype = reader.ReadByte ();
                position ++;
                if (subtype == (byte)Binary.TypeCode.General) {
                    size = reader.ReadInt32 ();
                    position += 4;
                }
                byte[] bytes = reader.ReadBytes (size);
                position += size;
                Binary b = new Binary ();
                b.Bytes = bytes;
                b.Subtype = (Binary.TypeCode)subtype;
                return b;
            }
            default:
                throw new ArgumentOutOfRangeException (String.Format ("Type Number: {0} not recognized", typeNum));
            }
        }
コード例 #10
0
 /// <summary>The map function references the variable this to inspect the current object under consideration.
 /// A map function must call emit(key,value) at least once, but may be invoked any number of times, 
 /// as may be appropriate.
 /// </summary>
 public MapReduceBuilder Map(Code function)
 {
     mr.Map = function;
     return this;
 }
コード例 #11
0
 /// <summary>
 /// Function to apply to all the results when finished.
 /// </summary>
 public MapReduceBuilder Finalize(Code function)
 {
     mr.Finalize = function;
     return this;
 }
コード例 #12
0
 /// <summary>
 /// The reduce function receives a key and an array of values. To use, reduce the received values, 
 /// and return a result.
 /// </summary>
 /// <remarks>The MapReduce engine may invoke reduce functions iteratively; thus, these functions 
 /// must be idempotent. If you need to perform an operation only once, use a finalize function.</remarks>
 public MapReduceBuilder Reduce(Code function)
 {
     mr.Reduce = function;
     return this;
 }