-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some code suggestion from CodeGuru #577
Conversation
Hi @XinyuLiu5566 Thanks for opening the PR. There seems to be some failing tests. Can you please look into it ? |
@@ -46,7 +46,8 @@ public void remove(String Key){ | |||
} | |||
|
|||
public Object get(String key) { | |||
return store.containsKey(key)?store.get(key).value:null; | |||
ValueNode node = store.get(key); | |||
return node ? node.value : null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@XinyuLiu5566 - this would fix the runtime error
return node ? node.value : null; | |
return node != null ? node.value : null; |
…ertools/parameters/cache/DataStore.java Co-authored-by: Michael Brewer <[email protected]>
* avoid repeated get() * %s to %d for integer * change put() to putIfAbsent() * Update powertools-parameters/src/main/java/software/amazon/lambda/powertools/parameters/cache/DataStore.java Co-authored-by: Michael Brewer <[email protected]> * Fix logic for caching json schema. Co-authored-by: liuxinyu <[email protected]> Co-authored-by: Pankaj Agrawal <[email protected]> Co-authored-by: Michael Brewer <[email protected]>
Thanks @jeromevdl for merging #984. Could you please close this PR, as it contains the same changes and my PR was created just to fix some issues with this one. |
Fixed with #984 |
* avoid repeated get() * %s to %d for integer * change put() to putIfAbsent() * Update powertools-parameters/src/main/java/software/amazon/lambda/powertools/parameters/cache/DataStore.java Co-authored-by: Michael Brewer <[email protected]> * Fix logic for caching json schema. Co-authored-by: liuxinyu <[email protected]> Co-authored-by: Pankaj Agrawal <[email protected]> Co-authored-by: Michael Brewer <[email protected]>
The code is using ConcurrentHashMap, but the usage of containsKey() and get() may not be thread-safe. In between the check and the get() another thread can remove the key and the get() will return null. Fix:
Consider calling get(), checking instead of your current check if the returned object is null, and then using that object only, without calling get() again.
This code uses '%s' to format int: 'size' expression. Use %d, not %s, for integers. This ensures locale-sensitive formatting.
Replacing put() with putIfAbsent() to help prevent accidental overwriting. putIfAbsent() puts the value only if the ConcurrentHashMap does not contain the key and therefore avoids overwriting the value written there by the other thread's putIfAbsent().