Skip to content

Commit 700e6d2

Browse files
Switch MTRDevice to always using by-cluster persistent attribute storage. (#33096)
MTRDEVICE_ATTRIBUTE_CACHE_STORE_ATTRIBUTES_BY_CLUSTER was already defaulted to 1; this just removes the codepaths when this is set to 0. Cleans up some of the controller data store bits that then become unused, and improves test coverage of this codepath. Fixes formatting of the endpoint id in _clusterDataKeyForNodeID.
1 parent 2ef3a36 commit 700e6d2

8 files changed

+197
-493
lines changed

src/darwin/Framework/CHIP/MTRDevice.mm

-29
Original file line numberDiff line numberDiff line change
@@ -1040,9 +1040,7 @@ - (void)_handleReportBegin
10401040
for (MTRClusterPath * clusterPath in clusterPaths) {
10411041
NSNumber * dataVersion = _clusterData[clusterPath].dataVersion;
10421042
NSDictionary<NSNumber *, MTRDeviceDataValueDictionary> * attributes = nil;
1043-
#if MTRDEVICE_ATTRIBUTE_CACHE_STORE_ATTRIBUTES_BY_CLUSTER
10441043
attributes = [self _attributesForCluster:clusterPath];
1045-
#endif
10461044
if (dataVersion || attributes) {
10471045
MTRDeviceClusterData * clusterData = [[MTRDeviceClusterData alloc] initWithDataVersion:dataVersion attributes:attributes];
10481046
clusterDataToReturn[clusterPath] = clusterData;
@@ -2340,12 +2338,6 @@ - (NSArray *)_getAttributesToReportWithReportedValues:(NSArray<NSDictionary<NSSt
23402338
NSMutableArray * attributesToReport = [NSMutableArray array];
23412339
NSMutableArray * attributePathsToReport = [NSMutableArray array];
23422340
BOOL dataStoreExists = _deviceController.controllerDataStore != nil;
2343-
#if !MTRDEVICE_ATTRIBUTE_CACHE_STORE_ATTRIBUTES_BY_CLUSTER
2344-
NSMutableArray * attributesToPersist;
2345-
if (dataStoreExists) {
2346-
attributesToPersist = [NSMutableArray array];
2347-
}
2348-
#endif
23492341
for (NSDictionary<NSString *, id> * attributeResponseValue in reportedAttributeValues) {
23502342
MTRAttributePath * attributePath = attributeResponseValue[MTRAttributePathKey];
23512343
NSDictionary * attributeDataValue = attributeResponseValue[MTRDataKey];
@@ -2388,20 +2380,7 @@ - (NSArray *)_getAttributesToReportWithReportedValues:(NSArray<NSDictionary<NSSt
23882380
BOOL readCacheValueChanged = ![self _attributeDataValue:attributeDataValue isEqualToDataValue:_readCache[attributePath]];
23892381
// Check if attribute needs to be persisted - compare only to read cache and disregard expected values
23902382
if (dataStoreExists && readCacheValueChanged) {
2391-
#if MTRDEVICE_ATTRIBUTE_CACHE_STORE_ATTRIBUTES_BY_CLUSTER
23922383
[self _noteChangeForClusterPath:clusterPath];
2393-
#else
2394-
NSDictionary * attributeResponseValueToPersist;
2395-
if (dataVersion) {
2396-
// Remove data version from what we cache in memory and storage
2397-
NSMutableDictionary * attributeResponseValueCopy = [attributeResponseValue mutableCopy];
2398-
attributeResponseValueCopy[MTRDataKey] = attributeDataValue;
2399-
attributeResponseValueToPersist = attributeResponseValueCopy;
2400-
} else {
2401-
attributeResponseValueToPersist = attributeResponseValue;
2402-
}
2403-
[attributesToPersist addObject:attributeResponseValueToPersist];
2404-
#endif
24052384
}
24062385
NSArray * expectedValue = _expectedValueCache[attributePath];
24072386

@@ -2473,12 +2452,6 @@ - (NSArray *)_getAttributesToReportWithReportedValues:(NSArray<NSDictionary<NSSt
24732452

24742453
MTR_LOG_INFO("%@ report from reported values %@", self, attributePathsToReport);
24752454

2476-
#if !MTRDEVICE_ATTRIBUTE_CACHE_STORE_ATTRIBUTES_BY_CLUSTER
2477-
if (dataStoreExists && attributesToPersist.count) {
2478-
[_deviceController.controllerDataStore storeAttributeValues:attributesToPersist forNodeID:_nodeID];
2479-
}
2480-
#endif
2481-
24822455
return attributesToReport;
24832456
}
24842457

@@ -2530,7 +2503,6 @@ - (void)setClusterData:(NSDictionary<MTRClusterPath *, MTRDeviceClusterData *> *
25302503

25312504
std::lock_guard lock(_lock);
25322505

2533-
#if MTRDEVICE_ATTRIBUTE_CACHE_STORE_ATTRIBUTES_BY_CLUSTER
25342506
// For each cluster, extract and create the attribute response-value for the read cache
25352507
// TODO: consider some optimization in how the read cache is structured so there's fewer conversions from this format to what's in the cache
25362508
for (MTRClusterPath * clusterPath in clusterData) {
@@ -2548,7 +2520,6 @@ - (void)setClusterData:(NSDictionary<MTRClusterPath *, MTRDeviceClusterData *> *
25482520
}
25492521
}
25502522
}
2551-
#endif
25522523

25532524
[_clusterData addEntriesFromDictionary:clusterData];
25542525
}

src/darwin/Framework/CHIP/MTRDeviceController.mm

-8
Original file line numberDiff line numberDiff line change
@@ -948,14 +948,6 @@ - (MTRDevice *)_setupDeviceForNodeID:(NSNumber *)nodeID prefetchedClusterData:(N
948948
[deviceToReturn setClusterData:prefetchedClusterData];
949949
}
950950
} else {
951-
#if !MTRDEVICE_ATTRIBUTE_CACHE_STORE_ATTRIBUTES_BY_CLUSTER
952-
// Load persisted attributes if they exist.
953-
NSArray * attributesFromCache = [_controllerDataStore getStoredAttributesForNodeID:nodeID];
954-
MTR_LOG_INFO("Loaded %lu attributes from storage for %@", static_cast<unsigned long>(attributesFromCache.count), deviceToReturn);
955-
if (attributesFromCache.count) {
956-
[deviceToReturn setAttributeValues:attributesFromCache reportChanges:NO];
957-
}
958-
#endif
959951
// Load persisted cluster data if they exist.
960952
NSDictionary * clusterData = [_controllerDataStore getStoredClusterDataForNodeID:nodeID];
961953
MTR_LOG_INFO("Loaded %lu cluster data from storage for %@", static_cast<unsigned long>(clusterData.count), deviceToReturn);

src/darwin/Framework/CHIP/MTRDeviceControllerDataStore.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,10 @@ typedef void (^MTRDeviceControllerDataStoreClusterDataHandler)(NSDictionary<NSNu
7272
/**
7373
* Storage for MTRDevice attribute read cache. This is local-only storage as an optimization. New controller devices using MTRDevice API can prime their own local cache from devices directly.
7474
*/
75-
- (nullable NSArray<NSDictionary *> *)getStoredAttributesForNodeID:(NSNumber *)nodeID;
76-
- (void)storeAttributeValues:(NSArray<NSDictionary *> *)dataValues forNodeID:(NSNumber *)nodeID;
7775
- (nullable NSDictionary<MTRClusterPath *, MTRDeviceClusterData *> *)getStoredClusterDataForNodeID:(NSNumber *)nodeID;
7876
- (void)storeClusterData:(NSDictionary<MTRClusterPath *, MTRDeviceClusterData *> *)clusterData forNodeID:(NSNumber *)nodeID;
79-
- (void)clearStoredAttributesForNodeID:(NSNumber *)nodeID;
80-
- (void)clearAllStoredAttributes;
77+
- (void)clearStoredClusterDataForNodeID:(NSNumber *)nodeID;
78+
- (void)clearAllStoredClusterData;
8179

8280
@end
8381

0 commit comments

Comments
 (0)