-
Notifications
You must be signed in to change notification settings - Fork 73
CP Subsytem - Atomic Reference #224
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import hazelcast | ||
|
|
||
| client = hazelcast.HazelcastClient() | ||
|
|
||
| my_ref = client.cp_subsystem.get_atomic_reference("my-ref").blocking() | ||
| my_ref.set(42) | ||
|
|
||
| value = my_ref.get() | ||
| print("Value:", value) | ||
|
|
||
| result = my_ref.compare_and_set(42, "value") | ||
| print("CAS result:", result) | ||
|
|
||
| final_value = my_ref.get() | ||
| print("Final value:", final_value) | ||
|
|
||
| client.shutdown() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| atomic_long_get_codec, atomic_long_get_and_add_codec, atomic_long_get_and_set_codec, atomic_long_alter_codec, \ | ||
| atomic_long_apply_codec | ||
| from hazelcast.proxy.cp import BaseCPProxy | ||
| from hazelcast.util import check_not_none, check_is_int | ||
|
|
||
|
|
||
| class AtomicLong(BaseCPProxy): | ||
|
|
@@ -31,6 +32,7 @@ def add_and_get(self, delta): | |
| hazelcast.future.Future[int]: The updated value, the given value added | ||
| to the current value | ||
| """ | ||
| check_is_int(delta) | ||
| codec = atomic_long_add_and_get_codec | ||
| request = codec.encode_request(self._group_id, self._object_name, delta) | ||
| return self._invoke(request, codec.decode_response) | ||
|
|
@@ -47,6 +49,8 @@ def compare_and_set(self, expect, update): | |
| hazelcast.future.Future[bool]: ``True`` if successful; or ``False`` if | ||
| the actual value was not equal to the expected value. | ||
| """ | ||
| check_is_int(expect) | ||
| check_is_int(update) | ||
| codec = atomic_long_compare_and_set_codec | ||
| request = codec.encode_request(self._group_id, self._object_name, expect, update) | ||
| return self._invoke(request, codec.decode_response) | ||
|
|
@@ -87,6 +91,7 @@ def get_and_add(self, delta): | |
| Returns: | ||
| hazelcast.future.Future[int]: The old value before the add. | ||
| """ | ||
| check_is_int(delta) | ||
| codec = atomic_long_get_and_add_codec | ||
| request = codec.encode_request(self._group_id, self._object_name, delta) | ||
| return self._invoke(request, codec.decode_response) | ||
|
|
@@ -100,6 +105,7 @@ def get_and_set(self, new_value): | |
| Returns: | ||
| hazelcast.future.Future[int]: The old value. | ||
| """ | ||
| check_is_int(new_value) | ||
| codec = atomic_long_get_and_set_codec | ||
| request = codec.encode_request(self._group_id, self._object_name, new_value) | ||
| return self._invoke(request, codec.decode_response) | ||
|
|
@@ -130,6 +136,7 @@ def set(self, new_value): | |
| Returns: | ||
| hazelcast.future.Future[None]: | ||
| """ | ||
| check_is_int(new_value) | ||
| codec = atomic_long_get_and_set_codec | ||
| request = codec.encode_request(self._group_id, self._object_name, new_value) | ||
| return self._invoke(request) | ||
|
|
@@ -145,13 +152,18 @@ def alter(self, function): | |
|
|
||
| Args: | ||
| function (hazelcast.serialization.api.Portable or hazelcast.serialization.api.IdentifiedDataSerializable): | ||
| The function applied to the currently stored value. | ||
| The function that alters the currently stored value. | ||
|
|
||
| Returns: | ||
| hazelcast.future.Future[None]: | ||
| """ | ||
| check_not_none(function, "Function cannot be None") | ||
puzpuzpuz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| function_data = self._to_data(function) | ||
| codec = atomic_long_alter_codec | ||
| # 1 means return the new value. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a nice addition. I had to look at the codec and server-side code to understand what |
||
| # There is no way to tell server to return nothing as of now (30.09.2020) | ||
| # The new value is `long` (comes with the initial frame) and we | ||
| # don't try to decode it. So, this shouldn't cause any problems. | ||
| request = codec.encode_request(self._group_id, self._object_name, function_data, 1) | ||
| return self._invoke(request) | ||
|
|
||
|
|
@@ -166,13 +178,15 @@ def alter_and_get(self, function): | |
|
|
||
| Args: | ||
| function (hazelcast.serialization.api.Portable or hazelcast.serialization.api.IdentifiedDataSerializable): | ||
| The function applied to the currently stored value. | ||
| The function that alters the currently stored value. | ||
|
|
||
| Returns: | ||
| hazelcast.future.Future[int]: The new value. | ||
| """ | ||
| check_not_none(function, "Function cannot be None") | ||
| function_data = self._to_data(function) | ||
| codec = atomic_long_alter_codec | ||
| # 1 means return the new value. | ||
| request = codec.encode_request(self._group_id, self._object_name, function_data, 1) | ||
| return self._invoke(request, codec.decode_response) | ||
|
|
||
|
|
@@ -187,13 +201,15 @@ def get_and_alter(self, function): | |
|
|
||
| Args: | ||
| function (hazelcast.serialization.api.Portable or hazelcast.serialization.api.IdentifiedDataSerializable): | ||
| The function applied to the currently stored value. | ||
| The function that alters the currently stored value. | ||
|
|
||
| Returns: | ||
| hazelcast.future.Future[int]: The old value. | ||
| """ | ||
| check_not_none(function, "Function cannot be None") | ||
| function_data = self._to_data(function) | ||
| codec = atomic_long_alter_codec | ||
| # 0 means return the old value. | ||
| request = codec.encode_request(self._group_id, self._object_name, function_data, 0) | ||
| return self._invoke(request, codec.decode_response) | ||
|
|
||
|
|
@@ -213,6 +229,7 @@ def apply(self, function): | |
| Returns: | ||
| hazelcast.future.Future[any]: The result of the function application. | ||
| """ | ||
| check_not_none(function, "Function cannot be None") | ||
| function_data = self._to_data(function) | ||
| codec = atomic_long_apply_codec | ||
| request = codec.encode_request(self._group_id, self._object_name, function_data) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.