-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement stateful functions for StreamingDataFrames
- Set default state dir to `state` - Add `stateful` parameter to `StreamingDataFrame.apply()` - Add TransactionState class that implements a very limited key-value storage interface to be passed to stateful functions
- Loading branch information
1 parent
53ba93b
commit ac5dab5
Showing
11 changed files
with
264 additions
and
97 deletions.
There are no files selected for viewing
This file contains 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 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 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
49 changes: 49 additions & 0 deletions
49
src/StreamingDataFrames/streamingdataframes/state/state.py
This file contains 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,49 @@ | ||
from typing import Any, Optional | ||
|
||
from .types import State, PartitionTransaction | ||
|
||
|
||
class TransactionState(State): | ||
def __init__(self, transaction: PartitionTransaction): | ||
""" | ||
Simple key-value state to be provided into `StreamingDataFrame` functions | ||
:param transaction: instance of `PartitionTransaction` | ||
""" | ||
self._transaction = transaction | ||
|
||
def get(self, key: Any, default: Any = None) -> Optional[Any]: | ||
""" | ||
Get the value for key if key is present in the state, else default | ||
:param key: key | ||
:param default: default value to return if the key is not found | ||
:return: value or None if the key is not found and `default` is not provided | ||
""" | ||
return self._transaction.get(key=key, default=default) | ||
|
||
def set(self, key: Any, value: Any): | ||
""" | ||
Set value for the key. | ||
:param key: key | ||
:param value: value | ||
""" | ||
return self._transaction.set(key=key, value=value) | ||
|
||
def delete(self, key: Any): | ||
""" | ||
Delete value for the key. | ||
This function always returns `None`, even if value is not found. | ||
:param key: key | ||
""" | ||
return self._transaction.delete(key=key) | ||
|
||
def exists(self, key: Any) -> bool: | ||
""" | ||
Check if the key exists in state. | ||
:param key: key | ||
:return: True if key exists, False otherwise | ||
""" | ||
|
||
return self._transaction.exists(key=key) |
This file contains 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 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
Oops, something went wrong.