Esempio n. 1
0
 public BsonDocument(BsonIterator it) : this()
 {
     while (it.Next() != BsonType.EOO)
     {
         Add(it.FetchCurrentValue());
     }
 }
Esempio n. 2
0
 public BsonDocument(Stream bstream)
     : this()
 {
     using (BsonIterator it = new BsonIterator(bstream))
     {
         while (it.Next() != BsonType.EOO)
         {
             Add(it.FetchCurrentValue());
         }
     }
 }
Esempio n. 3
0
 public BsonDocument(byte[] bsdata)
     : this()
 {
     using (BsonIterator it = new BsonIterator(bsdata))
     {
         while (it.Next() != BsonType.EOO)
         {
             Add(it.FetchCurrentValue());
         }
     }
 }
Esempio n. 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 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);
        }
Esempio n. 5
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();
        }