Skip to content
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

Closed
wants to merge 4 commits into from
Closed

Some code suggestion from CodeGuru #577

wants to merge 4 commits into from

Conversation

XinyuLiu5566
Copy link

  • 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().

@pankajagrawal16
Copy link
Contributor

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;
Copy link
Contributor

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

Suggested change
return node ? node.value : null;
return node != null ? node.value : null;

…ertools/parameters/cache/DataStore.java

Co-authored-by: Michael Brewer <[email protected]>
jeromevdl pushed a commit that referenced this pull request Nov 15, 2022
* 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]>
@kozub
Copy link
Contributor

kozub commented Nov 16, 2022

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.

@jeromevdl
Copy link
Contributor

Fixed with #984

@jeromevdl jeromevdl closed this Nov 16, 2022
jeromevdl pushed a commit that referenced this pull request Nov 24, 2022
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants