Beispiel #1
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            if (ReferenceEquals(this, obj))
            {
                return(true);
            }
            if (!(obj is BsonRegexp))
            {
                return(false);
            }
            BsonRegexp other = (BsonRegexp)obj;

            return(_re == other._re && _opts == other._opts);
        }
Beispiel #2
0
 public void TestIterateRE()
 {
     var doc = new BsonDocument();
     doc["a"] = new BsonRegexp("b", "c");
     doc["d"] = 1;
     doc["e"] = BsonDocument.ValueOf(new { f = new BsonRegexp("g", "") });
     doc["h"] = 2;
     //28-00-00-00
     //0B-61-00-62-00-63-00
     //10-64-00-01-00-00-00
     //03-65-00-0B-00-00-00
     //0B-66-00-67-00-00-00
     //10-68-00-02-00-00-00-00
     var cs = "";
     foreach (var bt in new BsonIterator(doc))
     {
         cs += bt.ToString();
     }
     Assert.AreEqual("REGEXINTOBJECTINT", cs);
     cs = "";
     foreach (var bv in new BsonIterator(doc).Values())
     {
         if (bv.Key == "a")
         {
             cs += ((BsonRegexp)bv.Value).Re;
             cs += ((BsonRegexp)bv.Value).Opts;
         }
         else
         {
             cs += bv.Value;
         }
     }
     Assert.AreEqual("bc1[BsonDocument: [BsonValue: BsonType=REGEX, Key=f, Value=[BsonRegexp: re=g, opts=]]]2", cs);
 }
Beispiel #3
0
 public BsonDocument SetRegexp(int idx, BsonRegexp val)
 {
     return base.SetRegexp(idx.ToString(), val);
 }
Beispiel #4
0
 public BsonArray(BsonRegexp[] arr)
 {
     for (var i = 0; i < arr.Length; ++i)
     {
         SetRegexp(i, arr[i]);
     }
 }
Beispiel #5
0
        protected void WriteBsonValue(BsonValue bv, ExtBinaryWriter bw)
        {
            BsonType bt = bv.BsonType;

            switch (bt)
            {
            case BsonType.EOO:
                break;

            case BsonType.NULL:
            case BsonType.UNDEFINED:
            case BsonType.MAXKEY:
            case BsonType.MINKEY:
                WriteTypeAndKey(bv, bw);
                break;

            case BsonType.OID:
            {
                WriteTypeAndKey(bv, bw);
                ObjectId oid = (ObjectId)bv.Value;
                bw.Write(oid.Byte01);
                bw.Write(oid.Byte02);
                bw.Write(oid.Byte03);
                bw.Write(oid.Byte04);
                bw.Write(oid.Byte05);
                bw.Write(oid.Byte06);
                bw.Write(oid.Byte07);
                bw.Write(oid.Byte08);
                bw.Write(oid.Byte09);
                bw.Write(oid.Byte10);
                bw.Write(oid.Byte11);
                bw.Write(oid.Byte12);
                break;
            }

            case BsonType.STRING:
            case BsonType.CODE:
            case BsonType.SYMBOL:
                WriteTypeAndKey(bv, bw);
                bw.WriteBsonString((string)bv.Value);
                break;

            case BsonType.BOOL:
                WriteTypeAndKey(bv, bw);
                bw.Write((bool)bv.Value);
                break;

            case BsonType.INT:
                WriteTypeAndKey(bv, bw);
                bw.Write((int)bv.Value);
                break;

            case BsonType.LONG:
                WriteTypeAndKey(bv, bw);
                bw.Write((long)bv.Value);
                break;

            case BsonType.ARRAY:
            case BsonType.OBJECT:
            {
                BsonDocument doc = (BsonDocument)bv.Value;
                WriteTypeAndKey(bv, bw);
                doc.Serialize(bw.BaseStream);
                break;
            }

            case BsonType.DATE:
            {
                DateTime dt   = (DateTime)bv.Value;
                var      diff = dt.ToLocalTime() - BsonConstants.Epoch;
                long     time = (long)Math.Floor(diff.TotalMilliseconds);
                WriteTypeAndKey(bv, bw);
                bw.Write(time);
                break;
            }

            case BsonType.DOUBLE:
                WriteTypeAndKey(bv, bw);
                bw.Write((double)bv.Value);
                break;

            case BsonType.REGEX:
            {
                BsonRegexp rv = (BsonRegexp)bv.Value;
                WriteTypeAndKey(bv, bw);
                bw.WriteCString(rv.Re ?? "");
                bw.WriteCString(rv.Opts ?? "");
                break;
            }

            case BsonType.BINDATA:
            {
                BsonBinData bdata = (BsonBinData)bv.Value;
                WriteTypeAndKey(bv, bw);
                bw.Write(bdata.Data.Length);
                bw.Write(bdata.Subtype);
                bw.Write(bdata.Data);
                break;
            }

            case BsonType.DBREF:
                //Unsupported DBREF!
                break;

            case BsonType.TIMESTAMP:
            {
                BsonTimestamp ts = (BsonTimestamp)bv.Value;
                WriteTypeAndKey(bv, bw);
                bw.Write(ts.Inc);
                bw.Write(ts.Ts);
                break;
            }

            case BsonType.CODEWSCOPE:
            {
                BsonCodeWScope cw = (BsonCodeWScope)bv.Value;
                WriteTypeAndKey(bv, bw);
                using (var cwwr = new ExtBinaryWriter(new MemoryStream()))
                {
                    cwwr.WriteBsonString(cw.Code);
                    cw.Scope.Serialize(cwwr.BaseStream);
                    byte[] cwdata = ((MemoryStream)cwwr.BaseStream).ToArray();
                    bw.Write(cwdata.Length);
                    bw.Write(cwdata);
                }
                break;
            }

            default:
                throw new InvalidBsonDataException("Unknown entry type: " + bt);
            }
        }
Beispiel #6
0
 public BsonDocument SetRegexp(string key, BsonRegexp val)
 {
     return(SetBsonValue(new BsonValue(BsonType.REGEX, key, val)));
 }
Beispiel #7
0
 /// <summary>
 /// Sets <see cref="BsonRegexp"/> value at specified position.
 /// </summary>
 /// <param name="index">Index to place value at</param>
 /// <param name="value">An <see cref="BsonRegexp"/> value</param>
 /// <returns>Returns itself</returns>
 public BsonDocument SetRegexp(int index, BsonRegexp value)
 {
     return(SetRegexp(index.ToString(), value));
 }