Пример #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 BSONOid(_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
        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);
                BSONOid oid = (BSONOid)bv.Value;
                Debug.Assert(oid._bytes.Length == 12);
                bw.Write(oid._bytes);
                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);
            }
        }
Пример #4
0
 public BSONDocument SetCodeWScope(string key, BSONCodeWScope val)
 {
     return(SetBSONValue(new BSONValue(BSONType.CODEWSCOPE, key, val)));
 }
Пример #5
0
 public BSONDocument SetCodeWScope(int idx, BSONCodeWScope val)
 {
     return(base.SetCodeWScope(idx.ToString(), val));
 }
Пример #6
0
 public BSONDocument SetCodeWScope(int idx, BSONCodeWScope val)
 {
     return base.SetCodeWScope(idx.ToString(), val);
 }
Пример #7
0
 public BSONArray(BSONCodeWScope[] arr)
 {
     for (var i = 0; i < arr.Length; ++i) {
         SetCodeWScope(i, arr[i]);
     }
 }