Пример #1
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            if (ReferenceEquals(this, obj))
            {
                return(true);
            }
            if (!(obj is BsonCodeWScope))
            {
                return(false);
            }
            BsonCodeWScope cw = (BsonCodeWScope)obj;

            if (_code != cw._code)
            {
                return(false);
            }
            return(base.Equals(obj));
        }
Пример #2
0
        public BsonValue FetchCurrentValue()
        {
            CheckDisposed();
            if (_entryDataSkipped)
            {
                return(_entryDataValue);
            }
            _entryDataSkipped = true;
            switch (_ctype)
            {
            case BsonType.EOO:
            case BsonType.UNDEFINED:
            case BsonType.NULL:
            case BsonType.MAXKEY:
            case BsonType.MINKEY:
                _entryDataValue = new BsonValue(_ctype, _entryKey);
                break;

            case BsonType.OID:
                Debug.Assert(_entryLen == 12);
                _entryDataValue = new BsonValue(_ctype, _entryKey, new ObjectId(_input));
                break;

            case BsonType.STRING:
            case BsonType.CODE:
            case BsonType.SYMBOL:
            {
                Debug.Assert(_entryLen - 1 >= 0);
                string sv = Encoding.UTF8.GetString(_input.ReadBytes(_entryLen - 1));
                _entryDataValue = new BsonValue(_ctype, _entryKey, sv);
                var rb = _input.ReadByte();
                Debug.Assert(rb == 0x00);         //trailing zero byte
                break;
            }

            case BsonType.BOOL:
                _entryDataValue = new BsonValue(_ctype, _entryKey, _input.ReadBoolean());
                break;

            case BsonType.INT:
                _entryDataValue = new BsonValue(_ctype, _entryKey, _input.ReadInt32());
                break;

            case BsonType.OBJECT:
            case BsonType.ARRAY:
            {
                BsonDocument doc = (_ctype == BsonType.OBJECT ? new BsonDocument() : new BsonArray());
                BsonIterator sit = new BsonIterator(this);
                while (sit.Next() != BsonType.EOO)
                {
                    doc.Add(sit.FetchCurrentValue());
                }
                _entryDataValue = new BsonValue(_ctype, _entryKey, doc);
                break;
            }

            case BsonType.DOUBLE:
                _entryDataValue = new BsonValue(_ctype, _entryKey, _input.ReadDouble());
                break;

            case BsonType.LONG:
                _entryDataValue = new BsonValue(_ctype, _entryKey, _input.ReadInt64());
                break;

            case BsonType.DATE:
                _entryDataValue = new BsonValue(_ctype, _entryKey,
                                                BsonConstants.Epoch.AddMilliseconds(_input.ReadInt64()));
                break;

            case BsonType.TIMESTAMP:
            {
                int inc = _input.ReadInt32();
                int ts  = _input.ReadInt32();
                _entryDataValue = new BsonValue(_ctype, _entryKey,
                                                new BsonTimestamp(inc, ts));
                break;
            }

            case BsonType.REGEX:
            {
                string re   = _input.ReadCString();
                string opts = _input.ReadCString();
                _entryDataValue = new BsonValue(_ctype, _entryKey,
                                                new BsonRegexp(re, opts));
                break;
            }

            case BsonType.BINDATA:
            {
                byte        subtype = _input.ReadByte();
                BsonBinData bd      = new BsonBinData(subtype, _entryLen - 1, _input);
                _entryDataValue = new BsonValue(_ctype, _entryKey, bd);
                break;
            }

            case BsonType.DBREF:
            {
                //Unsupported DBREF!
                SkipData(true);
                _entryDataValue = new BsonValue(_ctype, _entryKey);
                break;
            }

            case BsonType.CODEWSCOPE:
            {
                int cwlen = _entryLen + 4;
                Debug.Assert(cwlen > 5);
                int            clen = _input.ReadInt32(); //code length
                string         code = Encoding.UTF8.GetString(_input.ReadBytes(clen));
                BsonCodeWScope cw   = new BsonCodeWScope(code);
                BsonIterator   sit  = new BsonIterator(_input, _input.ReadInt32());
                while (sit.Next() != BsonType.EOO)
                {
                    cw.Add(sit.FetchCurrentValue());
                }
                _entryDataValue = new BsonValue(_ctype, _entryKey, cw);
                break;
            }
            }
            return(_entryDataValue);
        }
Пример #3
0
 public BsonDocument SetCodeWScope(int idx, BsonCodeWScope val)
 {
     return base.SetCodeWScope(idx.ToString(), val);
 }
Пример #4
0
 public BsonArray(BsonCodeWScope[] arr)
 {
     for (var i = 0; i < arr.Length; ++i)
     {
         SetCodeWScope(i, arr[i]);
     }
 }
Пример #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);
            }
        }
Пример #6
0
 public BsonDocument SetCodeWScope(string key, BsonCodeWScope val)
 {
     return(SetBsonValue(new BsonValue(BsonType.CODEWSCOPE, key, val)));
 }
Пример #7
0
 /// <summary>
 /// Sets <see cref="BsonCodeWScope"/> value at specified position.
 /// </summary>
 /// <param name="index">Index to place value at</param>
 /// <param name="value">An instance of <see cref="BsonCodeWScope"/></param>
 /// <returns>Returns itself</returns>
 public BsonDocument SetCodeWScope(int index, BsonCodeWScope value)
 {
     return(SetCodeWScope(index.ToString(), value));
 }