Storage Engine API
record_data.h
Go to the documentation of this file.
1 // record_data.h
2 
31 #pragma once
32 
33 #include "mongo/bson/bsonobj.h"
34 #include "mongo/util/shared_buffer.h"
35 
36 namespace mongo {
37 
38 // TODO: Does this need to have move support?
43 class RecordData {
44 public:
45  RecordData() : _data(NULL), _size(0) {}
46  RecordData(const char* data, int size) : _data(data), _size(size) {}
47 
48  RecordData(SharedBuffer ownedData, int size)
49  : _data(ownedData.get()), _size(size), _ownedData(std::move(ownedData)) {}
50 
51  const char* data() const {
52  return _data;
53  }
54 
55  int size() const {
56  return _size;
57  }
58 
62  bool isOwned() const {
63  return _ownedData.get();
64  }
65 
66  SharedBuffer releaseBuffer() {
67  return std::move(_ownedData);
68  }
69 
70  BSONObj toBson() const& {
71  return isOwned() ? BSONObj(_ownedData) : BSONObj(_data);
72  }
73 
74  BSONObj releaseToBson() {
75  return isOwned() ? BSONObj(releaseBuffer()) : BSONObj(_data);
76  }
77 
78  BSONObj toBson() && {
79  return releaseToBson();
80  }
81 
82  RecordData getOwned() const {
83  if (isOwned())
84  return *this;
85  auto buffer = SharedBuffer::allocate(_size);
86  memcpy(buffer.get(), _data, _size);
87  return RecordData(buffer, _size);
88  }
89 
90  void makeOwned() {
91  if (isOwned())
92  return;
93  *this = getOwned();
94  }
95 
96 private:
97  const char* _data;
98  int _size;
99  SharedBuffer _ownedData;
100 };
101 
102 } // namespace mongo
BSONObj toBson() &&
Definition: record_data.h:78
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
A replacement for the Record class.
Definition: record_data.h:43
RecordData getOwned() const
Definition: record_data.h:82
BSONObj toBson() const &
Definition: record_data.h:70
SharedBuffer _ownedData
Definition: record_data.h:99
RecordData()
Definition: record_data.h:45
BSONObj releaseToBson()
Definition: record_data.h:74
bool isOwned() const
Returns true if this owns its own memory, and false otherwise.
Definition: record_data.h:62
const char * data() const
Definition: record_data.h:51
const char * _data
Definition: record_data.h:97
RecordData(const char *data, int size)
Definition: record_data.h:46
SharedBuffer releaseBuffer()
Definition: record_data.h:66
int _size
Definition: record_data.h:98
int size() const
Definition: record_data.h:55
void makeOwned()
Definition: record_data.h:90
RecordData(SharedBuffer ownedData, int size)
Definition: record_data.h:48