コード例 #1
0
 public BSONDocument(BSONIterator it) : this()
 {
     while (it.Next() != BSONType.EOO)
     {
         Add(it.FetchCurrentValue());
     }
 }
コード例 #2
0
 public BSONDocument(Stream bstream) : this()
 {
     using (BSONIterator it = new BSONIterator(bstream)) {
         while (it.Next() != BSONType.EOO)
         {
             Add(it.FetchCurrentValue());
         }
     }
 }
コード例 #3
0
 public BSONDocument(byte[] bsdata) : this()
 {
     using (BSONIterator it = new BSONIterator(bsdata)) {
         while (it.Next() != BSONType.EOO)
         {
             Add(it.FetchCurrentValue());
         }
     }
 }
コード例 #4
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);
        }
コード例 #5
0
 internal BSONIterator(BSONIterator it) : this(it._input, it._entryLen + 4)
 {
     _closeOnDispose      = false;
     it._entryDataSkipped = true;
 }
コード例 #6
0
        public BSONDocument(BSONIterator it, string[] fields) : this()
        {
            Array.Sort(fields);
            BSONType bt;
            int      ind = -1;
            int      nfc = 0;

            foreach (string f in fields)
            {
                if (f != null)
                {
                    nfc++;
                }
            }
            while ((bt = it.Next()) != BSONType.EOO)
            {
                if (nfc < 1)
                {
                    continue;
                }
                string kk = it.CurrentKey;
                if ((ind = Array.IndexOf(fields, kk)) != -1)
                {
                    Add(it.FetchCurrentValue());
                    fields[ind] = null;
                    nfc--;
                }
                else if (bt == BSONType.OBJECT || bt == BSONType.ARRAY)
                {
                    string[] narr = null;
                    for (var i = 0; i < fields.Length; ++i)
                    {
                        var f = fields[i];
                        if (f == null)
                        {
                            continue;
                        }
                        if (f.IndexOf(kk, StringComparison.Ordinal) == 0 &&
                            f.Length > kk.Length + 1 &&
                            f[kk.Length] == '.')
                        {
                            if (narr == null)
                            {
                                narr = new string[fields.Length];
                            }
                            narr[i]   = f.Substring(kk.Length + 1);
                            fields[i] = null;
                            nfc--;
                        }
                    }
                    if (narr != null)
                    {
                        BSONIterator nit  = new BSONIterator(it);
                        BSONDocument ndoc = new BSONDocument(nit, narr);
                        if (ndoc.KeysCount > 0)
                        {
                            Add(new BSONValue(bt, kk, ndoc));
                        }
                    }
                }
            }
            it.Dispose();
        }