diff --git a/dao/src/main/java/org/thingsboard/server/dao/sqlts/dictionary/JpaKeyDictionaryDao.java b/dao/src/main/java/org/thingsboard/server/dao/sqlts/dictionary/JpaKeyDictionaryDao.java index bcb9284371..9d23bdbcb5 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/sqlts/dictionary/JpaKeyDictionaryDao.java +++ b/dao/src/main/java/org/thingsboard/server/dao/sqlts/dictionary/JpaKeyDictionaryDao.java @@ -51,32 +51,29 @@ public class JpaKeyDictionaryDao extends JpaAbstractDaoListeningExecutorService return cached; } var compositeKey = new KeyDictionaryCompositeKey(strKey); - Optional entryOpt = keyDictionaryRepository.findById(compositeKey); - if (entryOpt.isPresent()) { - Integer keyId = entryOpt.get().getKeyId(); - if (keyId != null) { - keyDictionaryMap.put(strKey, keyId); - return keyId; - } + Optional existingId = keyDictionaryRepository.findById(compositeKey) + .map(KeyDictionaryEntry::getKeyId) + .filter(id -> id != 0); + if (existingId.isPresent()) { + return cacheAndReturn(strKey, existingId.get()); } creationLock.lock(); try { - Integer keyId = keyDictionaryMap.get(strKey); - if (keyId != null) { - return keyId; + Integer fromCache = keyDictionaryMap.get(strKey); + if (fromCache != null) { + return fromCache; } - keyId = keyDictionaryRepository.upsertAndGetKeyId(strKey); - if (keyId == null || keyId == 0) { - log.warn("upsertAndGetKeyId returned: [{}] for key: [{}], falling back to findById", keyId, strKey); - entryOpt = keyDictionaryRepository.findById(compositeKey); - if (entryOpt.isEmpty() || - entryOpt.get().getKeyId() == null || - entryOpt.get().getKeyId() == 0) { - throw new IllegalStateException("Failed to resolve keyId for string key: " + strKey + " after fallback. keyId: " + keyId); - } + Integer keyId = keyDictionaryRepository.upsertAndGetKeyId(strKey); + if (keyId != null && keyId != 0) { + return cacheAndReturn(strKey, keyId); } - keyDictionaryMap.put(strKey, keyId); - return keyId; + log.warn("upsertAndGetKeyId returned: [{}] for key: [{}], falling back to findById", keyId, strKey); + keyId = keyDictionaryRepository.findById(compositeKey) + .map(KeyDictionaryEntry::getKeyId) + .filter(id -> id != 0) + .orElseThrow(() -> new IllegalStateException( + "Failed to resolve keyId for string key: " + strKey + " after fallback.")); + return cacheAndReturn(strKey, keyId); } finally { creationLock.unlock(); } @@ -93,4 +90,9 @@ public class JpaKeyDictionaryDao extends JpaAbstractDaoListeningExecutorService return DaoUtil.pageToPageData(keyDictionaryRepository.findAll(DaoUtil.toPageable(pageLink))); } + private Integer cacheAndReturn(String key, Integer keyId) { + keyDictionaryMap.put(key, keyId); + return keyId; + } + }