Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/snippets_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,45 @@ def test_bigtable_add_row_add_row_range_add_row_range_from_keys():
expected_row_keys = [b"row_key_3", b"row_key_4", b"row_key_5", b"row_key_6"]
found_row_keys = [row.row_key for row in read_rows]
assert found_row_keys == expected_row_keys
table.truncate(timeout=200)


def test_bigtable_add_row_range_with_prefix():
row_keys = [
b"row_key_1",
b"row_key_2",
b"row_key_3",
b"sample_row_key_1",
b"sample_row_key_2",
]

rows = []
for row_key in row_keys:
row = Config.TABLE.row(row_key)
row.set_cell(COLUMN_FAMILY_ID, COL_NAME1, CELL_VAL1)
rows.append(row)
Config.TABLE.mutate_rows(rows)

# [START bigtable_add_row_range_with_prefix]
from google.cloud.bigtable import Client
from google.cloud.bigtable.row_set import RowSet

client = Client(admin=True)
instance = client.instance(INSTANCE_ID)
table = instance.table(TABLE_ID)

row_set = RowSet()
row_set.add_row_range_with_prefix("row")
# [END bigtable_add_row_range_with_prefix]

read_rows = table.read_rows(row_set=row_set)
expected_row_keys = [
b"row_key_1",
b"row_key_2",
b"row_key_3",
]
found_row_keys = [row.row_key for row in read_rows]
assert found_row_keys == expected_row_keys
table.truncate(timeout=200)


Expand Down
18 changes: 18 additions & 0 deletions google/cloud/bigtable/row_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ def add_row_range_from_keys(
row_range = RowRange(start_key, end_key, start_inclusive, end_inclusive)
self.row_ranges.append(row_range)

def add_row_range_with_prefix(self, row_key_prefix):
"""Add row range to row_ranges list that start with the row_key_prefix from the row keys

For example:

.. literalinclude:: snippets_table.py
:start-after: [START bigtable_add_row_range_with_prefix]
:end-before: [END bigtable_add_row_range_with_prefix]

:type row_key_prefix: str
:param row_key_prefix: To retrieve all rows that start with this row key prefix.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To retrieve all rows that start with this row key prefix.

The grammar here can be improved.

Prefix cannot be zero length."""
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect indentation, and the comment doesn't seem to be very understandable.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


end_key = row_key_prefix[:-1] + chr(ord(row_key_prefix[-1]) + 1)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems strange. If you'll pass "row" (as you did in your sample), you'll get "rox". What it should mean?
Sounds to me, in this variable we should have a name of the key on which we'd like to stop fetching data from table.

In Billy's example there is a "#" symbol. After +1 it'll become "$" which is the "end of line" regex symbol. It sound logical, but incrementing the code of the last symbol overall doesn't seem correct to me.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# and $ doesn't work here we need to remove manually , Please see the implementation of prefix in go and node:

Following links of integration test :
Go: https://github.com/googleapis/google-cloud-go/blob/51eb5ee54e44743973ca6a638660fd1067b17365/bigtable/integration_test.go#L673

Node:
https://github.com/googleapis/google-cloud-node/pull/1802/files#diff-5528ad90973774d4a89e43a0462bf388R233

self.add_row_range_from_keys(
row_key_prefix.encode("utf-8"), end_key.encode("utf-8")
)

def _update_message_request(self, message):
"""Add row keys and row range to given request message

Expand Down
32 changes: 32 additions & 0 deletions tests/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,38 @@ def test_yield_rows_with_row_set(self):
found_row_keys = [row.row_key for row in read_rows]
self.assertEqual(found_row_keys, expected_row_keys)

def test_add_row_range_by_prefix_from_keys(self):
row_keys = [
b"row_key_1",
b"row_key_2",
b"row_key_3",
b"row_key_4",
b"sample_row_key_1",
b"sample_row_key_2",
]

rows = []
for row_key in row_keys:
row = self._table.row(row_key)
row.set_cell(COLUMN_FAMILY_ID1, COL_NAME1, CELL_VAL1)
rows.append(row)
self.rows_to_delete.append(row)
self._table.mutate_rows(rows)

row_set = RowSet()
row_set.add_row_range_with_prefix("row")

read_rows = self._table.yield_rows(row_set=row_set)

expected_row_keys = [
b"row_key_1",
b"row_key_2",
b"row_key_3",
b"row_key_4",
]
found_row_keys = [row.row_key for row in read_rows]
self.assertEqual(found_row_keys, expected_row_keys)

def test_read_large_cell_limit(self):
self._maybe_emulator_skip(
"Maximum gRPC received message size for emulator is 4194304 bytes."
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_row_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ def test_add_row_range_from_keys(self):
)
self.assertEqual(row_set.row_ranges[0].end_key, b"row_key9")

def test_add_row_range_with_prefix(self):
row_set = self._make_one()
row_set.add_row_range_with_prefix("row")
self.assertEqual(row_set.row_ranges[0].end_key, b"rox")

def test__update_message_request(self):
row_set = self._make_one()
table_name = "table_name"
Expand Down