133 lines
3.5 KiB
C++

//
// Array.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: ObjectId
//
// Definition of the ObjectId class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef _MongoDB_ObjectId_included
#define _MongoDB_ObjectId_included
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/Element.h"
#include "Poco/Timestamp.h"
namespace Poco {
namespace MongoDB {
class MongoDB_API ObjectId
/// ObjectId is a 12-byte BSON type, constructed using:
///
/// - a 4-byte timestamp,
/// - a 3-byte machine identifier,
/// - a 2-byte process id, and
/// - a 3-byte counter, starting with a random value.
///
/// In MongoDB, documents stored in a collection require a unique _id field that acts
/// as a primary key. Because ObjectIds are small, most likely unique, and fast to generate,
/// MongoDB uses ObjectIds as the default value for the _id field if the _id field is not
/// specified; i.e., the mongod adds the _id field and generates a unique ObjectId to assign
/// as its value.
{
public:
typedef SharedPtr<ObjectId> Ptr;
ObjectId();
/// Constructor
virtual ~ObjectId();
/// Destructor
Timestamp timestamp() const;
/// Returns the timestamp which is stored in the first four bytes of the id
std::string toString() const;
/// Returns the id in string format
private:
unsigned char _id[12];
friend class BSONWriter;
friend class BSONReader;
};
inline Timestamp ObjectId::timestamp() const
{
int time;
char* T = (char *) &time;
T[0] = _id[3];
T[1] = _id[2];
T[2] = _id[1];
T[3] = _id[0];
return Timestamp::fromEpochTime((time_t) time);
}
// BSON Embedded Document
// spec: ObjectId
template<>
struct ElementTraits<ObjectId::Ptr>
{
enum { TypeId = 0x07 };
static std::string toString(const ObjectId::Ptr& id, int indent = 0)
{
return id->toString();
}
};
template<>
inline void BSONReader::read<ObjectId::Ptr>(ObjectId::Ptr& to)
{
_reader.readRaw((char*) to->_id, 12);
}
template<>
inline void BSONWriter::write<ObjectId::Ptr>(ObjectId::Ptr& from)
{
_writer.writeRaw((char*) from->_id, 12);
}
}} // Namespace Poco::MongoDB
#endif //_MongoDB_ObjectId_included