MongoDB  2.5.0
bson_db.h
Go to the documentation of this file.
1 
3 /* Copyright 2009 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /*
19  This file contains the implementation of BSON-related methods that are required
20  by the MongoDB database server.
21 
22  Normally, for standalone BSON usage, you do not want this file - it will tend to
23  pull in some other files from the MongoDB project. Thus, bson.h (the main file
24  one would use) does not include this file.
25 */
26 
27 #pragma once
28 
29 #include "mongo/db/repl/optime.h"
30 #include "../util/time_support.h"
31 
32 namespace mongo {
33 
39  inline BSONObjBuilder& BSONObjBuilder::appendTimestamp( const StringData& fieldName , unsigned long long time , unsigned int inc ) {
40  OpTime t( (unsigned) (time / 1000) , inc );
41  appendTimestamp( fieldName , t.asDate() );
42  return *this;
43  }
44 
45  inline BSONObjBuilder& BSONObjBuilder::append(const StringData& fieldName, OpTime optime) {
46  appendTimestamp(fieldName, optime.asDate());
47  return *this;
48  }
49 
50  inline OpTime BSONElement::_opTime() const {
51  if( type() == mongo::Date || type() == Timestamp )
52  return OpTime( *reinterpret_cast< const unsigned long long* >( value() ) );
53  return OpTime();
54  }
55 
56  inline std::string BSONElement::_asCode() const {
57  switch( type() ) {
58  case mongo::String:
59  case Code:
60  return std::string(valuestr(), valuestrsize()-1);
61  case CodeWScope:
62  return std::string(codeWScopeCode(), *(int*)(valuestr())-1);
63  default:
64  log() << "can't convert type: " << (int)(type()) << " to code" << std::endl;
65  }
66  uassert( 10062 , "not code" , 0 );
67  return "";
68  }
69 
70  inline BSONObjBuilder& BSONObjBuilderValueStream::operator<<(const DateNowLabeler& id) {
71  _builder->appendDate(_fieldName, jsTime());
72  _fieldName = StringData();
73  return *_builder;
74  }
75 
76  inline BSONObjBuilder& BSONObjBuilderValueStream::operator<<(const NullLabeler& id) {
77  _builder->appendNull(_fieldName);
78  _fieldName = StringData();
79  return *_builder;
80  }
81 
82  inline BSONObjBuilder& BSONObjBuilderValueStream::operator<<(const UndefinedLabeler& id) {
83  _builder->appendUndefined(_fieldName);
84  _fieldName = StringData();
85  return *_builder;
86  }
87 
88 
89  inline BSONObjBuilder& BSONObjBuilderValueStream::operator<<(const MinKeyLabeler& id) {
90  _builder->appendMinKey(_fieldName);
91  _fieldName = StringData();
92  return *_builder;
93  }
94 
95  inline BSONObjBuilder& BSONObjBuilderValueStream::operator<<(const MaxKeyLabeler& id) {
96  _builder->appendMaxKey(_fieldName);
97  _fieldName = StringData();
98  return *_builder;
99  }
100 
101 }