예제 #1
0
MojErr MojDbIdGenerator::id(MojObject& idOut)
{
	// an id consists of  a timestamp (in microsecs) concatenated with a 32-bit random number.
	// The goal is to have a very low likelihood of id collision among multiple devices owned
	// by the same user (so they can can share an id-space) while still keeping them sequential
	// to minimize db fragmentation.

	MojThreadGuard guard(m_mutex);
	MojUInt32 randNum;
	MojErr err = MojRandom(&m_randBuf, &randNum);
	MojErrCheck(err);
	guard.unlock();

	MojTime time;
	err = MojGetCurrentTime(time);
	MojErrCheck(err);

	MojBuffer buf;
	MojDataWriter writer(buf);
	err = writer.writeInt64(time.microsecs());
	MojErrCheck(err);
	err = writer.writeUInt32(randNum);
	MojErrCheck(err);

	MojVector<MojByte> byteVec;
	err = buf.toByteVec(byteVec);
	MojErrCheck(err);
	MojString str;
	err = str.base64Encode(byteVec, false);
	MojErrCheck(err);
	idOut = str;

	return MojErrNone;
}
예제 #2
0
MojErr MojDbQuery::Page::toObject(MojObject& objOut) const
{
	MojString str;
	MojErr err = str.base64Encode(m_key.byteVec(), false);
	MojErrCheck(err);
	objOut = str;

	return MojErrNone;
}