[dataset-local] cache Active/Pending Timestamp value (#2599)

This commit caches the Active/Pending Timestamp value in memory so that
subsequent calls to `DatasetLocal::Compare()` does not require reading
from non-volatile settings and allocating another dataset buffer on the
stack to do so.
This commit is contained in:
Jonathan Hui
2018-03-02 17:16:36 +00:00
committed by GitHub
parent 0e9c0cd6b7
commit afa7f34b26
3 changed files with 79 additions and 24 deletions
+61 -21
View File
@@ -55,14 +55,42 @@ DatasetLocal::DatasetLocal(Instance &aInstance, const Tlv::Type aType)
: InstanceLocator(aInstance)
, mUpdateTime(0)
, mType(aType)
, mTimestampPresent(false)
{
mTimestamp.Init();
}
void DatasetLocal::Clear(void)
{
mTimestamp.Init();
mTimestampPresent = false;
otPlatSettingsDelete(&GetInstance(), GetSettingsKey(), -1);
}
otError DatasetLocal::Restore(Dataset &aDataset)
{
const Timestamp *timestamp;
otError error;
error = Get(aDataset);
SuccessOrExit(error);
timestamp = aDataset.GetTimestamp();
if (timestamp != NULL)
{
mTimestamp = *timestamp;
mTimestampPresent = true;
}
else
{
mTimestampPresent = false;
}
exit:
return error;
}
otError DatasetLocal::Get(Dataset &aDataset) const
{
DelayTimerTlv *delayTimer;
@@ -131,8 +159,9 @@ exit:
otError DatasetLocal::Set(const Dataset &aDataset)
{
Dataset dataset(aDataset);
otError error;
Dataset dataset(aDataset);
const Timestamp *timestamp;
otError error;
if (mType == Tlv::kActiveTimestamp)
{
@@ -153,6 +182,18 @@ otError DatasetLocal::Set(const Dataset &aDataset)
SuccessOrExit(error);
timestamp = dataset.GetTimestamp();
if (timestamp != NULL)
{
mTimestamp = *timestamp;
mTimestampPresent = true;
}
else
{
mTimestampPresent = false;
}
mUpdateTime = TimerMilli::GetNow();
exit:
@@ -177,32 +218,31 @@ uint16_t DatasetLocal::GetSettingsKey(void) const
int DatasetLocal::Compare(const Timestamp *aCompareTimestamp)
{
const Timestamp *thisTimestamp;
Dataset dataset(mType);
int rval = 1;
int rval = 1;
SuccessOrExit(Get(dataset));
thisTimestamp = dataset.GetTimestamp();
if (aCompareTimestamp == NULL && thisTimestamp == NULL)
if (aCompareTimestamp == NULL)
{
rval = 0;
}
else if (aCompareTimestamp == NULL && thisTimestamp != NULL)
{
rval = -1;
}
else if (aCompareTimestamp != NULL && thisTimestamp == NULL)
{
rval = 1;
if (!mTimestampPresent)
{
rval = 0;
}
else
{
rval = -1;
}
}
else
{
rval = thisTimestamp->Compare(*aCompareTimestamp);
if (!mTimestampPresent)
{
rval = 1;
}
else
{
rval = mTimestamp.Compare(*aCompareTimestamp);
}
}
exit:
return rval;
}
+17 -2
View File
@@ -71,6 +71,19 @@ public:
*/
void Clear(void);
/**
* This method restores and retrieves the dataset from non-volatile memory.
*
* This method also sets the memory-cached timestamp for subsequent calls to `Compare()`.
*
* @param[out] aDataset Where to place the dataset.
*
* @retval OT_ERROR_NONE Successfully retrieved the dataset.
* @retval OT_ERROR_NOT_FOUND There is no corresponding dataset stored in non-volatile memory.
*
*/
otError Restore(Dataset &aDataset);
/**
* This method retrieves the dataset from non-volatile memory.
*
@@ -135,8 +148,10 @@ private:
uint16_t GetSettingsKey(void) const;
void SetTimestamp(const Dataset &aDataset);
uint32_t mUpdateTime; ///< Local time last updated
Tlv::Type mType; ///< Active or Pending
Timestamp mTimestamp; ///< Active or Pending Timestamp
uint32_t mUpdateTime; ///< Local time last updated
Tlv::Type mType; ///< Active or Pending
bool mTimestampPresent : 1; ///< Whether or not timestamp is present
};
} // namespace MeshCoP
+1 -1
View File
@@ -111,7 +111,7 @@ otError DatasetManager::Restore(void)
mTimer.Stop();
SuccessOrExit(error = mLocal.Get(dataset));
SuccessOrExit(error = mLocal.Restore(dataset));
timestamp = dataset.GetTimestamp();