public void ScanTableRandomRows() { var rowKeys = new List<string>(); var cell = new Cell(); ITableScanner scanner; var random = new Random(); while (rowKeys.Count < R) { using (scanner = table.CreateScanner(new ScanSpec { KeysOnly = true }.AddColumn("a"))) { while( scanner.Move(cell) ) { Assert.IsNull(cell.Value); if ((random.Next() % 3) == 0) { rowKeys.Add(cell.Key.Row); if (rowKeys.Count == R) { break; } } } } } var scanSpec1 = new ScanSpec().AddColumn("a"); var scanSpec2 = new ScanSpec(); foreach (var t in rowKeys) { var rowKey = rowKeys[random.Next(rowKeys.Count)]; scanSpec1.AddRow(rowKey); scanSpec2.AddCell(rowKey, "a", null); } Assert.AreEqual(R, scanSpec1.RowCount); Assert.AreEqual(R, scanSpec2.Cells.Count); var c = 0; using (scanner = table.CreateScanner(scanSpec1)) { Assert.AreSame(scanSpec1, scanner.ScanSpec); while( scanner.Move(cell) ) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); Assert.IsTrue(c < scanSpec1.RowCount); Assert.AreEqual(scanSpec1.Rows[c], cell.Key.Row); ++c; } } Assert.AreEqual(R, c); c = 0; using (scanner = table.CreateScanner(scanSpec2)) { Assert.AreSame(scanSpec2, scanner.ScanSpec); while( scanner.Move(cell) ) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); Assert.IsTrue(c < scanSpec2.Cells.Count); Assert.AreEqual(scanSpec2.Cells[c].Row, cell.Key.Row); ++c; } } Assert.AreEqual(R, c); }
public void ScanTableRandomRowsSortedRowKeys() { var cell = new Cell(); ITableScanner scanner; var random = new Random(); var scanSpec1 = new ScanSpec(true).AddColumn("a"); var scanSpec2 = new ScanSpec(true); while (scanSpec1.RowCount < R) { using (scanner = table.CreateScanner(new ScanSpec { KeysOnly = true }.AddColumn("a"))) { while( scanner.Move(cell) ) { Assert.IsNull(cell.Value); if ((random.Next() % 3) == 0) { scanSpec1.AddRow(cell.Key.Row); scanSpec2.AddCell(cell.Key.Row, "a", null); if (scanSpec1.RowCount == R) { break; } } } } } Assert.AreEqual(R, scanSpec1.RowCount); Assert.AreEqual(R, scanSpec2.Cells.Count); var c = 0; var previousRow = string.Empty; using (scanner = table.CreateScanner(scanSpec1)) { while( scanner.Move(cell) ) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); Assert.IsTrue(c < scanSpec1.RowCount); Assert.AreEqual(scanSpec1.Rows[c], cell.Key.Row); Assert.IsTrue(string.Compare(previousRow, cell.Key.Row) < 0); previousRow = cell.Key.Row; ++c; } } Assert.AreEqual(R, c); c = 0; previousRow = string.Empty; using (scanner = table.CreateScanner(scanSpec2)) { while( scanner.Move(cell) ) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); Assert.IsTrue(c < scanSpec2.Cells.Count); Assert.AreEqual(scanSpec2.Cells[c].Row, cell.Key.Row); Assert.IsTrue(string.Compare(previousRow, cell.Key.Row) < 0); previousRow = cell.Key.Row; ++c; } } Assert.AreEqual(R, c); }
public void ScanTableRandomCells() { var random = new Random(); const string Cf = "abcdefg"; const int Count = 10000; using (var _table = EnsureTable("ScanTableRandomCells", Schema)) { var keys = new List<Key>(Count); using (var mutator = _table.CreateMutator()) { for (var i = 0; i < Count; ++i) { var key = new Key { Row = Guid.NewGuid().ToString(), ColumnFamily = new string(new[] { Cf[random.Next(Cf.Length)] }), ColumnQualifier = random.Next(Cf.Length).ToString(CultureInfo.InvariantCulture) }; keys.Add(key); mutator.Set(key, Encoding.GetBytes(key.Row)); } } for (var r = 0; r < 10; ++r) { var countCells = 10 + random.Next(Count - 10); var scanSpec = new ScanSpec(); foreach (var k in Shuffle(keys)) { scanSpec.AddCell(k); if (scanSpec.CellCount == countCells) { break; } } var comparer = new KeyComparer(false); using (var scanner = _table.CreateScanner(scanSpec)) { Assert.AreSame(scanSpec, scanner.ScanSpec); var cell = new Cell(); var c = 0; while( scanner.Move(cell) ) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); Assert.IsTrue(comparer.Equals(scanSpec.Cells[c++], cell.Key)); } Assert.AreEqual(scanSpec.CellCount, c); } } for (var r = 0; r < 10; ++r) { var countCells = 10 + random.Next(Count - 10); var scanSpec = new ScanSpec(); foreach (var k in Shuffle(keys)) { scanSpec.AddRow(k.Row); if (scanSpec.RowCount == countCells) { break; } } using (var scanner = _table.CreateScanner(scanSpec)) { Assert.AreSame(scanSpec, scanner.ScanSpec); var cell = new Cell(); var c = 0; while( scanner.Move(cell) ) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); Assert.AreEqual(scanSpec.Rows[c++], cell.Key.Row); } Assert.AreEqual(scanSpec.RowCount, c); } } for (var r = 0; r < 10; ++r) { var rows = new HashSet<string>(); var countCells = 10 + random.Next(Count - 10); var scanSpec = new ScanSpec(true); foreach (var k in Shuffle(keys)) { scanSpec.AddRow(k.Row); rows.Add(k.Row); if (scanSpec.RowCount == countCells) { break; } } using (var scanner = _table.CreateScanner(scanSpec)) { Assert.AreSame(scanSpec, scanner.ScanSpec); var cell = new Cell(); var c = 0; while( scanner.Move(cell) ) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); Assert.IsTrue(rows.Contains(cell.Key.Row)); ++c; } Assert.AreEqual(scanSpec.RowCount, c); } } for (var r = 0; r < 10; ++r) { var rows = new HashSet<string>(); var columnFamily = new string(new[] { Cf[random.Next(Cf.Length)] }); var columnQualifier = random.Next(Cf.Length).ToString(CultureInfo.InvariantCulture); var countCells = 10 + random.Next(Count / 10); var scanSpec = new ScanSpec { ScanAndFilter = true }; foreach (var k in Shuffle(keys).Where(k => k.ColumnFamily == columnFamily && k.ColumnQualifier == columnQualifier)) { Assert.AreEqual(columnFamily, k.ColumnFamily); Assert.AreEqual(columnQualifier, k.ColumnQualifier); scanSpec.AddColumn(k.ColumnFamily + ":" + k.ColumnQualifier); scanSpec.AddRow(k.Row); rows.Add(k.Row); if (scanSpec.RowCount == countCells) { break; } } using (var scanner = _table.CreateScanner(scanSpec)) { Assert.AreSame(scanSpec, scanner.ScanSpec); var cell = new Cell(); var c = 0; while( scanner.Move(cell) ) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); Assert.AreEqual(columnFamily, cell.Key.ColumnFamily); Assert.AreEqual(columnQualifier, cell.Key.ColumnQualifier); Assert.IsTrue(rows.Contains(cell.Key.Row)); ++c; } Assert.AreEqual(scanSpec.RowCount, c); } } for (var r = 0; r < 10; ++r) { var rows = new HashSet<string>(); var columnQualifier = random.Next(Cf.Length).ToString(CultureInfo.InvariantCulture); var countCells = 10 + random.Next(Count / 10); var scanSpec = new ScanSpec { ScanAndFilter = true }; foreach (var k in Shuffle(keys).Where(k => k.ColumnQualifier == columnQualifier)) { Assert.AreEqual(columnQualifier, k.ColumnQualifier); scanSpec.AddColumn(k.ColumnFamily + ":" + k.ColumnQualifier); scanSpec.AddRow(k.Row); rows.Add(k.Row); if (scanSpec.RowCount == countCells) { break; } } using (var scanner = _table.CreateScanner(scanSpec)) { Assert.AreSame(scanSpec, scanner.ScanSpec); var cell = new Cell(); var c = 0; while( scanner.Move(cell) ) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); Assert.AreEqual(columnQualifier, cell.Key.ColumnQualifier); Assert.IsTrue(rows.Contains(cell.Key.Row)); ++c; } Assert.AreEqual(scanSpec.RowCount, c); } } using (var mutator = _table.CreateMutator()) { var key = new Key { Row = "A", ColumnFamily = "a", ColumnQualifier = "1" }; mutator.Set(key, Encoding.GetBytes(key.Row)); key = new Key { Row = "B", ColumnFamily = "a", ColumnQualifier = "2" }; mutator.Set(key, Encoding.GetBytes(key.Row)); key = new Key { Row = "C", ColumnFamily = "c", ColumnQualifier = "3" }; mutator.Set(key, Encoding.GetBytes(key.Row)); key = new Key { Row = "D", ColumnFamily = "c", ColumnQualifier = "4" }; mutator.Set(key, Encoding.GetBytes(key.Row)); key = new Key { Row = "E", ColumnFamily = "b", ColumnQualifier = "5" }; mutator.Set(key, Encoding.GetBytes(key.Row)); } { var scanSpec = new ScanSpec { ScanAndFilter = true }; scanSpec.AddColumn("a:1"); scanSpec.AddColumn("a:2"); scanSpec.AddColumn("b:5"); scanSpec.AddColumn("c:3"); scanSpec.AddColumn("c:4"); scanSpec.AddRow("A"); scanSpec.AddRow("B"); using (var scanner = _table.CreateScanner(scanSpec)) { Assert.AreSame(scanSpec, scanner.ScanSpec); var cell = new Cell(); var c = 0; while( scanner.Move(cell) ) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); ++c; } Assert.AreEqual(scanSpec.RowCount, c); } scanSpec = new ScanSpec { ScanAndFilter = true }; scanSpec.AddColumn("a", "b:5", "c"); scanSpec.AddRow("A"); scanSpec.AddRow("C"); scanSpec.AddRow("E"); using (var scanner = _table.CreateScanner(scanSpec)) { Assert.AreSame(scanSpec, scanner.ScanSpec); var cell = new Cell(); var c = 0; while( scanner.Move(cell) ) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); ++c; } Assert.AreEqual(scanSpec.RowCount, c); } } } }