Skip to content

Commit

Permalink
KVS Optimization: DEBUG IN PROGRESS.
Browse files Browse the repository at this point in the history
  • Loading branch information
rcasallas-silabs committed Aug 28, 2024
1 parent 36eb382 commit 2475722
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 33 deletions.
77 changes: 57 additions & 20 deletions src/credentials/GroupDataProviderV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ CHIP_ERROR GroupDataProviderV2::RemoveGroupInfo(chip::FabricIndex fabric_index,

GroupList groups(mStorage, mListener, fabric_index);
GroupInfo info(group_id, nullptr);
return groups.Remove(info);
ReturnErrorOnFailure(groups.Remove(info));
return groups.Save();
}

CHIP_ERROR GroupDataProviderV2::SetGroupInfoAt(chip::FabricIndex fabric_index, size_t index, const GroupInfo & info)
Expand Down Expand Up @@ -423,7 +424,8 @@ CHIP_ERROR GroupDataProviderV2::RemoveGroupInfoAt(chip::FabricIndex fabric_index
ReturnErrorOnFailure(fabrics.Find(fabric_index, found_index));

GroupList groups(mStorage, mListener, fabric_index);
return groups.Remove(index);
ReturnErrorOnFailure(groups.Remove(index));
return groups.Save();
}

bool GroupDataProviderV2::HasEndpoint(chip::FabricIndex fabric_index, chip::GroupId group_id, chip::EndpointId endpoint_id)
Expand Down Expand Up @@ -465,10 +467,29 @@ CHIP_ERROR GroupDataProviderV2::RemoveEndpoint(chip::FabricIndex fabric_index, c
FabricList fabrics(mStorage);
ReturnErrorOnFailure(fabrics.Find(fabric_index, found_index, true));

// Remove endpoint
EndpointMap endpoints(mStorage, fabric_index);
GroupEndpoint mapping = { group_id, endpoint_id };
GroupEndpoint mapping(group_id, endpoint_id);
ReturnErrorOnFailure(endpoints.Find(mapping, found_index));
return endpoints.Remove(found_index);
ReturnErrorOnFailure(endpoints.Remove(found_index));
ReturnErrorOnFailure(endpoints.Save());
bool found = false;
for(size_t i = 0; !found && i < endpoints.Count(); i++)
{
found = (endpoints.At(i).group_id == group_id);
}
if(!found)
{
// The Group has no more endpoints, remove it
GroupList groups(mStorage, mListener, fabric_index);
GroupInfo info(group_id, nullptr);
if(CHIP_NO_ERROR == groups.Find(info, found_index))
{
ReturnErrorOnFailure(groups.Remove(found_index));
ReturnErrorOnFailure(groups.Save());
}
}
return CHIP_NO_ERROR;
}

CHIP_ERROR GroupDataProviderV2::RemoveEndpoint(chip::FabricIndex fabric_index, chip::EndpointId endpoint_id)
Expand All @@ -480,18 +501,22 @@ CHIP_ERROR GroupDataProviderV2::RemoveEndpoint(chip::FabricIndex fabric_index, c
FabricList fabrics(mStorage);
ReturnErrorOnFailure(fabrics.Find(fabric_index, found_index, true));

GroupList groups(mStorage, mListener, fabric_index);
EndpointMap endpoints(mStorage, fabric_index);
GroupEndpoint mapping;
ReturnErrorOnFailure(groups.Load());
ReturnErrorOnFailure(endpoints.Load());
for(uint16_t i = endpoints.Count(); i > 0; i--)
for(size_t i = endpoints.Count(); i > 0; i--)
{
ReturnErrorOnFailure(endpoints.Get(i - 1, mapping));
if(endpoint_id == mapping.endpoint_id)
GroupEndpoint mapping = endpoints.At(i - 1);
if(mapping.endpoint_id == endpoint_id)
{
ReturnErrorOnFailure(endpoints.Remove(i - 1));
GroupInfo info(mapping.group_id, nullptr);
groups.Remove(info);
endpoints.Remove(i - 1);
}
}
return CHIP_NO_ERROR;
ReturnErrorOnFailure(groups.Save());
return endpoints.Save();
}

GroupDataProvider::GroupInfoIterator * GroupDataProviderV2::IterateGroupInfo(chip::FabricIndex fabric_index)
Expand Down Expand Up @@ -643,7 +668,8 @@ CHIP_ERROR GroupDataProviderV2::RemoveGroupKeyAt(chip::FabricIndex fabric_index,
ReturnErrorOnFailure(fabrics.Find(fabric_index, found_index));

KeysetMap keysets(mStorage, fabric_index);
return keysets.Remove(index);
ReturnErrorOnFailure(keysets.Remove(index));
return keysets.Save();
}

CHIP_ERROR GroupDataProviderV2::RemoveGroupKeys(chip::FabricIndex fabric_index)
Expand Down Expand Up @@ -705,10 +731,6 @@ constexpr size_t GroupDataProvider::EpochKey::kLengthBytes;
CHIP_ERROR GroupDataProviderV2::SetKeySet(chip::FabricIndex fabric_index, const ByteSpan & compressed_fabric_id,
const KeySet & in_keyset)
{
if(6 == fabric_index)
{
ChipLogDetail(DeviceLayer, "GroupDataProviderV2::SetKeySet");
}
VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
VerifyOrReturnError(kUndefinedFabricIndex != fabric_index, CHIP_ERROR_INVALID_FABRIC_INDEX);

Expand Down Expand Up @@ -785,13 +807,25 @@ CHIP_ERROR GroupDataProviderV2::RemoveKeySet(chip::FabricIndex fabric_index, uin
size_t found_index = 0;
FabricList fabrics(mStorage);
ReturnErrorOnFailure(fabrics.Find(fabric_index, found_index));

// Remove group mappings
KeysetMap mappings(mStorage, fabric_index);
ReturnErrorOnFailure(mappings.Load());
for(size_t i = mappings.Count(); i > 0; i--)
{
GroupKey mapping = mappings.At(i - 1);
if(mapping.keyset_id == target_id)
{
mappings.Remove(i - 1);
}
}
ReturnErrorOnFailure(mappings.Save());
// Remove the Keyset
KeysetList keysets(mStorage, fabric_index);
KeysetData keyset;
keyset.keyset_id = target_id;
ReturnErrorOnFailure(keysets.Find(keyset, found_index));
keysets.Remove(found_index);
return CHIP_NO_ERROR;
ReturnErrorOnFailure(keysets.Remove(found_index));
return keysets.Save();
}

GroupDataProvider::KeySetIterator * GroupDataProviderV2::IterateKeySets(chip::FabricIndex fabric_index)
Expand Down Expand Up @@ -845,8 +879,6 @@ void GroupDataProviderV2::KeySetIteratorImpl::Release()

CHIP_ERROR GroupDataProviderV2::RemoveFabric(chip::FabricIndex fabric_index)
{
ChipLogProgress(Zcl, "~~~ GroupDataProviderV2::RemoveFabric(%u)", (unsigned)fabric_index);

VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
VerifyOrReturnError(kUndefinedFabricIndex != fabric_index, CHIP_ERROR_INVALID_FABRIC_INDEX);

Expand All @@ -860,28 +892,33 @@ CHIP_ERROR GroupDataProviderV2::RemoveFabric(chip::FabricIndex fabric_index)
{
groups.Remove(i - 1);
}
groups.Save();
// Group Endpoints
EndpointMap endpoints(mStorage, fabric_index);
ReturnErrorOnFailure(endpoints.Load());
for(uint16_t i = endpoints.Count(); i > 0; i--)
{
endpoints.Remove(i - 1);
}
endpoints.Save();
// Keyset Map
KeysetMap keysmap(mStorage, fabric_index);
ReturnErrorOnFailure(keysmap.Load());
for(uint16_t i = keysmap.Count(); i > 0; i--)
{
keysmap.Remove(i - 1);
}
keysmap.Save();
// Keyset List
KeysetList keysets(mStorage, fabric_index);
ReturnErrorOnFailure(keysets.Load());
for(uint16_t i = keysets.Count(); i > 0; i--)
{
keysets.Remove(i - 1);
}
keysets.Save();
fabrics.Remove(found_index);
fabrics.Save();
return CHIP_NO_ERROR;
}

Expand Down
2 changes: 1 addition & 1 deletion src/credentials/GroupDataProviderV2.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class GroupDataProviderV2 : public GroupDataProviderBase
Crypto::GroupOperationalCredentials operational_keys[GroupDataProvider::KeySet::kEpochKeysMax];
};

static constexpr size_t kKeysetListMax = CHIP_CONFIG_MAX_GROUP_KEYS_PER_FABRIC;
static constexpr size_t kKeysetListMax = CHIP_CONFIG_MAX_GROUPS_PER_FABRIC;
static constexpr size_t kKeysetSerializedMax = 5 + 3 * sizeof(uint16_t) + GroupDataProvider::KeySet::kEpochKeysMax * (5 + sizeof(uint64_t) + sizeof(uint16_t) + Crypto::CHIP_CRYPTO_SYMMETRIC_KEY_LENGTH_BYTES);
// KeysetIdTag(1) + sizeof(uint16_t) + TagPolicy(1) + sizeof(uint16_t) + NumKeysTag(1) + sizeof(uint16_t) +
// GroupCredentialsTag(1) + GroupDataProvider::KeySet::kEpochKeysMax * [ AnonymousTag(1)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/core/CHIPConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ extern const char CHIP_NON_PRODUCTION_MARKER[];
* Binds to number of KeySet entries to support per fabric (Need at least 1 for Identity Protection Key)
*/
#ifndef CHIP_CONFIG_MAX_GROUP_KEYS_PER_FABRIC
#define CHIP_CONFIG_MAX_GROUP_KEYS_PER_FABRIC 6
#define CHIP_CONFIG_MAX_GROUP_KEYS_PER_FABRIC 3
#endif

#if CHIP_CONFIG_MAX_GROUP_KEYS_PER_FABRIC < 1
Expand Down
5 changes: 2 additions & 3 deletions src/lib/support/PersistentArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ struct PersistentArray : public PersistentData<kPersistentArraySerializedMinSize
*/
CHIP_ERROR Remove(size_t index)
{
ReturnErrorOnFailure(this->Load(true));
ReturnErrorOnFailure(this->Load());
VerifyOrReturnError(index < this->mLimit, CHIP_ERROR_NOT_FOUND);
VerifyOrReturnError(index < this->mCount, CHIP_ERROR_NOT_FOUND);
EntryType old;
Expand All @@ -218,7 +218,6 @@ struct PersistentArray : public PersistentData<kPersistentArraySerializedMinSize
Copy(mEntries[i], mEntries[i + 1]);
}
ClearEntry(mEntries[this->mCount]);
ReturnErrorOnFailure(this->Save());
OnEntryRemoved(old);
return CHIP_NO_ERROR;
}
Expand All @@ -232,7 +231,7 @@ struct PersistentArray : public PersistentData<kPersistentArraySerializedMinSize
*/
CHIP_ERROR Remove(const EntryType & value)
{
ReturnErrorOnFailure(this->Load(true));
ReturnErrorOnFailure(this->Load());
for (size_t i = 0; i < this->mCount; ++i)
{
if (Compare(mEntries[i], value))
Expand Down
9 changes: 1 addition & 8 deletions src/lib/support/PersistentData.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,7 @@ struct PersistentData
// Serialize the data
TLV::TLVWriter writer;
writer.Init(mBuffer, sizeof(mBuffer));

CHIP_ERROR err = Serialize(writer);
if(CHIP_NO_ERROR != err)
{
ChipLogProgress(DeviceLayer, "~~~ S!\"%s\"(%u)", key.KeyName(), (unsigned)writer.GetLengthWritten());
}
ReturnErrorOnFailure(err);
ChipLogProgress(DeviceLayer, "~~~ S:\"%s\"(%u)", key.KeyName(), (unsigned)writer.GetLengthWritten());
ReturnErrorOnFailure(Serialize(writer));

// Save serialized data
return storage->SyncSetKeyValue(key.KeyName(), mBuffer, static_cast<uint16_t>(writer.GetLengthWritten()));
Expand Down

0 comments on commit 2475722

Please sign in to comment.