Skip to content

Commit c10a4bd

Browse files
joezqrentelpirionbhshkh
authored
Add GetPartitions function to emulator server (#5390)
Co-authored-by: Eric Schmidt <[email protected]> Co-authored-by: Baha Aiman <[email protected]>
1 parent 32cbf56 commit c10a4bd

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

bigtable/bttest/inmem.go

+60
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,15 @@ func (s *server) ReadRows(req *btpb.ReadRowsRequest, stream btpb.Bigtable_ReadRo
590590
return nil
591591
}
592592

593+
func (s *server) GetPartitionsByTableName(name string) []*btpb.RowRange {
594+
table, ok := s.tables[name]
595+
if !ok {
596+
return nil
597+
}
598+
return table.rowRanges()
599+
600+
}
601+
593602
// streamRow filters the given row and sends it via the given stream.
594603
// Returns true if at least one cell matched the filter and was streamed, false otherwise.
595604
func streamRow(stream btpb.Bigtable_ReadRowsServer, r *row, f *btpb.RowFilter, s *btpb.ReadIterationStats, ff *btpb.FeatureFlags) error {
@@ -1461,6 +1470,7 @@ type table struct {
14611470
counter uint64 // increment by 1 when a new family is created
14621471
families map[string]*columnFamily // keyed by plain family name
14631472
rows *btree.BTree // indexed by row key
1473+
partitions []*btpb.RowRange // partitions used in change stream
14641474
isProtected bool // whether this table has deletion protection
14651475
}
14661476

@@ -1475,10 +1485,56 @@ func newTable(ctr *btapb.CreateTableRequest) *table {
14751485
c++
14761486
}
14771487
}
1488+
1489+
// Hard code the partitions for testing purpose.
1490+
rowRanges := []*btpb.RowRange{
1491+
{
1492+
StartKey: &btpb.RowRange_StartKeyClosed{StartKeyClosed: []byte("a")},
1493+
EndKey: &btpb.RowRange_EndKeyClosed{EndKeyClosed: []byte("b")},
1494+
},
1495+
{
1496+
StartKey: &btpb.RowRange_StartKeyClosed{StartKeyClosed: []byte("c")},
1497+
EndKey: &btpb.RowRange_EndKeyClosed{EndKeyClosed: []byte("d")},
1498+
},
1499+
{
1500+
StartKey: &btpb.RowRange_StartKeyClosed{StartKeyClosed: []byte("e")},
1501+
EndKey: &btpb.RowRange_EndKeyClosed{EndKeyClosed: []byte("f")},
1502+
},
1503+
{
1504+
StartKey: &btpb.RowRange_StartKeyClosed{StartKeyClosed: []byte("g")},
1505+
EndKey: &btpb.RowRange_EndKeyClosed{EndKeyClosed: []byte("h")},
1506+
},
1507+
{
1508+
StartKey: &btpb.RowRange_StartKeyClosed{StartKeyClosed: []byte("i")},
1509+
EndKey: &btpb.RowRange_EndKeyClosed{EndKeyClosed: []byte("j")},
1510+
},
1511+
{
1512+
StartKey: &btpb.RowRange_StartKeyClosed{StartKeyClosed: []byte("k")},
1513+
EndKey: &btpb.RowRange_EndKeyClosed{EndKeyClosed: []byte("l")},
1514+
},
1515+
{
1516+
StartKey: &btpb.RowRange_StartKeyClosed{StartKeyClosed: []byte("m")},
1517+
EndKey: &btpb.RowRange_EndKeyClosed{EndKeyClosed: []byte("n")},
1518+
},
1519+
{
1520+
StartKey: &btpb.RowRange_StartKeyClosed{StartKeyClosed: []byte("o")},
1521+
EndKey: &btpb.RowRange_EndKeyClosed{EndKeyClosed: []byte("p")},
1522+
},
1523+
{
1524+
StartKey: &btpb.RowRange_StartKeyClosed{StartKeyClosed: []byte("q")},
1525+
EndKey: &btpb.RowRange_EndKeyClosed{EndKeyClosed: []byte("r")},
1526+
},
1527+
{
1528+
StartKey: &btpb.RowRange_StartKeyClosed{StartKeyClosed: []byte("s")},
1529+
EndKey: &btpb.RowRange_EndKeyClosed{EndKeyClosed: []byte("z")},
1530+
},
1531+
}
1532+
14781533
return &table{
14791534
families: fams,
14801535
counter: c,
14811536
rows: btree.New(btreeDegree),
1537+
partitions: rowRanges,
14821538
isProtected: ctr.GetTable().GetDeletionProtection(),
14831539
}
14841540
}
@@ -1577,6 +1633,10 @@ func (t *table) gcReadOnly() (toDelete []btree.Item) {
15771633
return toDelete
15781634
}
15791635

1636+
func (t *table) rowRanges() []*btpb.RowRange {
1637+
return t.partitions
1638+
}
1639+
15801640
type byRowKey []*row
15811641

15821642
func (b byRowKey) Len() int { return len(b) }

bigtable/bttest/inmem_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,52 @@ func TestCreateTableWithFamily(t *testing.T) {
221221
}
222222
}
223223

224+
func TestGetPartitionsByTableName(t *testing.T) {
225+
s := &server{
226+
tables: make(map[string]*table),
227+
}
228+
ctx := context.Background()
229+
newTbl := btapb.Table{
230+
ColumnFamilies: map[string]*btapb.ColumnFamily{
231+
"cf1": {GcRule: &btapb.GcRule{Rule: &btapb.GcRule_MaxNumVersions{MaxNumVersions: 123}}},
232+
"cf2": {GcRule: &btapb.GcRule{Rule: &btapb.GcRule_MaxNumVersions{MaxNumVersions: 456}}},
233+
},
234+
}
235+
_, err1 := s.CreateTable(ctx, &btapb.CreateTableRequest{Parent: "cluster", TableId: "t1", Table: &newTbl})
236+
if err1 != nil {
237+
t.Fatalf("Creating table: %v", err1)
238+
}
239+
240+
newTbl = btapb.Table{
241+
ColumnFamilies: map[string]*btapb.ColumnFamily{
242+
"cf3": {GcRule: &btapb.GcRule{Rule: &btapb.GcRule_MaxNumVersions{MaxNumVersions: 567}}},
243+
"cf4": {GcRule: &btapb.GcRule{Rule: &btapb.GcRule_MaxNumVersions{MaxNumVersions: 890}}},
244+
},
245+
}
246+
_, err2 := s.CreateTable(ctx, &btapb.CreateTableRequest{Parent: "cluster", TableId: "t2", Table: &newTbl})
247+
if err2 != nil {
248+
t.Fatalf("Creating table: %v", err2)
249+
}
250+
251+
tblNamePrefix := "cluster" + "/tables/"
252+
253+
// A random table name doesn't return partitions.
254+
partitions := s.GetPartitionsByTableName(tblNamePrefix + "random")
255+
if partitions != nil {
256+
t.Fatalf("Getting partitions for table random")
257+
}
258+
259+
partitions = s.GetPartitionsByTableName(tblNamePrefix + "t1")
260+
if len(partitions) != 10 {
261+
t.Fatalf("Getting partitions for table t1")
262+
}
263+
264+
partitions = s.GetPartitionsByTableName(tblNamePrefix + "t2")
265+
if len(partitions) != 10 {
266+
t.Fatalf("Getting partitions for table t2")
267+
}
268+
}
269+
224270
type MockSampleRowKeysServer struct {
225271
responses []*btpb.SampleRowKeysResponse
226272
grpc.ServerStream

internal/generated/snippets/go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ require (
152152
cloud.google.com/go/edgenetwork v0.0.0-00010101000000-000000000000
153153
cloud.google.com/go/identitytoolkit v0.0.0-00010101000000-000000000000
154154
cloud.google.com/go/managedkafka v0.0.0-00010101000000-000000000000
155+
cloud.google.com/go/memorystore v0.0.0-00010101000000-000000000000
155156
cloud.google.com/go/migrationcenter v0.0.0-00010101000000-000000000000
156157
cloud.google.com/go/netapp v0.0.0-00010101000000-000000000000
157158
cloud.google.com/go/networkservices v0.0.0-00010101000000-000000000000

0 commit comments

Comments
 (0)