MongoDB  2.6.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/bson/optime.h"
30 #include "mongo/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 }
Utility for creating a BSONObj.
Definition: bsonobjbuilder.h:52
BSONObjBuilder & append(const BSONElement &e)
append element to the object we are building
Definition: bsonobjbuilder.h:85
BSONType type() const
Returns the type of the element.
Definition: bsonelement.h:112
Updated to a Date with value next OpTime on insert.
Definition: bsontypes.h:82
date type
Definition: bsontypes.h:66
const char * value() const
raw data of the element&#39;s value (so be careful).
Definition: bsonelement.h:162
const char * valuestr() const
Get a string&#39;s value.
Definition: bsonelement.h:255
BSONObjBuilder & appendDate(const StringData &fieldName, Date_t dt)
Append a date.
Definition: bsonobjbuilder.h:320
void appendNull()
Implements builder interface but no-op in ObjBuilder.
Definition: bsonobjbuilder.h:405
javascript code that can execute on the database server, with SavedContext
Definition: bsontypes.h:78
Definition: optime.h:41
const char * codeWScopeCode() const
Get javascript code of a CodeWScope data element.
Definition: bsonelement.h:269
LogstreamBuilder log()
Returns a LogstreamBuilder for logging a message with LogSeverity::Log().
Definition: log.h:69
deprecated / use CodeWScope
Definition: bsontypes.h:74
Date_t jsTime()
Date_t is milliseconds since epoch.
Definition: time_support.cpp:915
int valuestrsize() const
Size (length) of a string element.
Definition: bsonelement.h:243
character string, stored in utf8
Definition: bsontypes.h:52