public void SetPeriodicFlush(MutatorSpec mutatorSpec) { if (!HasPeriodicFlushTableMutator) { return; } using (var mutator = table.CreateMutator(mutatorSpec)) { for (var i = 0; i < 5; ++i) { var row = string.Format("periodicFlush-{0}", i); var scanSpec = new ScanSpec(row).AddColumn("a"); var key = new Key(row, "a"); mutator.Set(key, Encoding.GetBytes(key.Row)); using (var scanner = table.CreateScanner(scanSpec)) { Cell cell; Assert.IsFalse(scanner.Next(out cell), string.Format("iteration {0}", i)); } Thread.Sleep(3000); // wait enough using (var scanner = table.CreateScanner(scanSpec)) { Cell cell; Assert.IsTrue(scanner.Next(out cell), string.Format("iteration {0}", i)); Assert.AreEqual(row, cell.Key.Row); } } } }
public void FetchByScanSpec() { var ec1 = new EntityC { A = new EntityA(), B = new EntityB(), X = new EntityX(), Y = new EntityY() }; TestBase.TestSerialization(ec1); var ex21 = new EntityX2(); TestBase.TestSerialization(ex21); using (var em = Emf.CreateEntityManager()) { em.Persist(ec1); em.Persist(ex21); } using (var em = Emf.CreateEntityManager()) { var _ec = em.Fetch <EntityC>(new ScanSpec(ec1.Id)); Assert.IsNotNull(_ec); Assert.AreEqual(1, _ec.Count()); Assert.AreEqual(ec1, _ec.First()); var ss = new ScanSpec(); ss.AddColumn("a"); var _ex = em.Fetch <EntityXBase>(ss); Assert.IsNotNull(_ex); Assert.AreEqual(2, _ex.Count()); Assert.AreEqual(ec1.X, _ex.OfType <EntityX>().First()); Assert.AreEqual(ex21, _ex.OfType <EntityX2>().First()); } }
public void TestDistictColumn() { var distictColumn = ScanSpec.DistictColumn(new[] { "a", "b:1", "b:2", "c:X", "c", "a:1" }); Assert.AreEqual(4, distictColumn.Count); Assert.IsTrue(distictColumn.Contains("a")); Assert.IsFalse(distictColumn.Contains("a:1")); Assert.IsFalse(distictColumn.Contains("b")); Assert.IsTrue(distictColumn.Contains("b:1")); Assert.IsTrue(distictColumn.Contains("b:2")); Assert.IsTrue(distictColumn.Contains("c")); Assert.IsFalse(distictColumn.Contains("c:X")); ISet <string> columnFamilies; distictColumn = ScanSpec.DistictColumn(new[] { "a", "b:1", "b:2", "c:X", "c", "a:1" }, out columnFamilies); Assert.AreEqual(4, distictColumn.Count); Assert.IsTrue(distictColumn.Contains("a")); Assert.IsFalse(distictColumn.Contains("a:1")); Assert.IsFalse(distictColumn.Contains("b")); Assert.IsTrue(distictColumn.Contains("b:1")); Assert.IsTrue(distictColumn.Contains("b:2")); Assert.IsTrue(distictColumn.Contains("c")); Assert.IsFalse(distictColumn.Contains("c:X")); Assert.AreEqual(3, columnFamilies.Count); Assert.IsTrue(columnFamilies.Contains("a")); Assert.IsFalse(columnFamilies.Contains("a:1")); Assert.IsTrue(columnFamilies.Contains("b")); Assert.IsFalse(columnFamilies.Contains("b:1")); Assert.IsFalse(columnFamilies.Contains("b:2")); Assert.IsTrue(columnFamilies.Contains("c")); Assert.IsFalse(columnFamilies.Contains("c:X")); }
/// <summary> /// Gets the scan spec for the type specified. /// </summary> /// <param name="entityType"> /// The entity type. /// </param> /// <returns> /// The scan spec. /// </returns> public ScanSpec ScanSpecForType(Type entityType) { if (entityType == null) { throw new ArgumentNullException(nameof(entityType)); } var entityReference = this.EntityReferenceForType(entityType); if (entityReference == null) { throw new PersistenceException(string.Format(CultureInfo.InvariantCulture, @"{0} is not a valid entity", entityType)); } entityReference.EstablishColumnSets(() => this.ColumnNames(entityReference)); var scanSpec = new ScanSpec { MaxVersions = 1 }; scanSpec.AddColumn(entityReference.ColumnSet); if (scanSpec.ColumnCount == 0) { throw new PersistenceException(string.Format(CultureInfo.InvariantCulture, @"{0} is not a valid entity", entityType)); } return(scanSpec); }
/// <summary> /// Creates the scan specification. /// </summary> /// <returns> /// The scan spec. /// </returns> public ScanSpec CreateScanSpec() { var scanSpec = new ScanSpec { MaxVersions = 1, ScanAndFilter = true }; foreach (var entityScanTarget in this.EntityScanTargets) { var key = entityScanTarget.Key; if (!string.IsNullOrEmpty(key.ColumnFamily)) { if (key.ColumnQualifier == null) { scanSpec.AddColumn(key.ColumnFamily); } else { var sb = new StringBuilder(); sb.Append(key.ColumnFamily); sb.Append(":"); sb.Append(key.ColumnQualifier); scanSpec.AddColumn(sb.ToString()); } } scanSpec.AddRow(key.Row); } return(scanSpec); }
/// <summary> /// Initializes a new instance of the <see cref="TableScan" /> class. /// </summary> /// <param name="scanSpec"> /// The scan spec. /// </param> /// <exception cref="ArgumentNullException"> /// Id the <paramref name="scanSpec" /> is null. /// </exception> internal TableScan(ScanSpec scanSpec) { if (scanSpec == null) { throw new ArgumentNullException(nameof(scanSpec)); } this.scanSpec = scanSpec; }
/// <summary> /// Initializes a new instance of the <see cref="TableScan"/> class. /// </summary> /// <param name="scanSpec"> /// The scan spec. /// </param> /// <exception cref="ArgumentNullException"> /// Id the <paramref name="scanSpec"/> is null. /// </exception> internal TableScan(ScanSpec scanSpec) { if (scanSpec == null) { throw new ArgumentNullException("scanSpec"); } this.scanSpec = scanSpec; }
/// <summary> /// Gets the merged scan spec for the types specified. /// </summary> /// <param name="entityTypes"> /// The query types. /// </param> /// <returns> /// The merged scan spec. /// </returns> public ScanSpec ScanSpecForType(IEnumerable <Type> entityTypes) { if (entityTypes == null) { throw new ArgumentNullException(nameof(entityTypes)); } var columnNames = new List <string>(); foreach (var queryType in entityTypes) { var entityReference = this.EntityReferenceForType(queryType); if (entityReference == null) { throw new PersistenceException(string.Format(CultureInfo.InvariantCulture, @"{0} is not a valid query type", queryType)); } entityReference.EstablishColumnSets(() => this.ColumnNames(entityReference)); columnNames.AddRange(entityReference.ColumnSet); } if (columnNames.Count > 5) { var registeredColumnNames = this.RegisteredColumnNames(); foreach (var columnName in columnNames.ToList()) { var split = columnName.Split(':'); if (split.Length > 1) { ISet <string> columnQualifiers; if (registeredColumnNames.TryGetValue(split[0], out columnQualifiers)) { if (columnQualifiers.Remove(split[1]) && columnQualifiers.Count == 0) { columnNames.Add(split[0]); } } } } } var scanSpec = new ScanSpec { MaxVersions = 1 }; scanSpec.AddColumn(ScanSpec.DistictColumn(columnNames)); if (scanSpec.ColumnCount == 0) { throw new PersistenceException("Missing or invalid entity types"); } return(scanSpec); }
/// <summary> /// Reads all entities which belongs to the given scan specification from the database. /// </summary> /// <param name="entityContext"> /// The entity context. /// </param> /// <param name="entityReference"> /// The entity reference. /// </param> /// <param name="scanSpec"> /// The scan spec. /// </param> /// <param name="behaviors"> /// The behaviors. /// </param> /// <returns> /// The entities. /// </returns> internal static IEnumerable Read(EntityContext entityContext, EntityReference entityReference, ScanSpec scanSpec, Behaviors behaviors) { var entityScanResult = new EntityScanResult(entityReference); var entityScanner = new EntityScanner(entityContext); entityScanner.Add(entityScanResult, scanSpec); new EntityReader(entityScanner, behaviors).Read(); return(entityScanResult.Values); }
/// <summary> /// Establish the column set for this entity reference. /// </summary> /// <param name="establishColumns"> /// The establish columns function. /// </param> /// <exception cref="ArgumentNullException"> /// If the <paramref name="establishColumns" /> is null. /// </exception> internal void EstablishColumnSets(Func <IEnumerable <string> > establishColumns) { if (establishColumns == null) { throw new ArgumentNullException(nameof(establishColumns)); } if (this.columnSet == null) { this.columnSet = ScanSpec.DistictColumn(establishColumns(), out this.columnFamilySet); } }
/// <summary> /// Deletes all cells in the table, column family specified. /// </summary> /// <param name = "table">Table.</param> /// <param name = "cf">Column family.</param> protected static void DeleteColumnFamily(ITable table, string cf) { var scanSpec = new ScanSpec { KeysOnly = true }.AddColumn(cf); using (var scanner = table.CreateScanner(scanSpec)) { using (var mutator = table.CreateMutator()) { var cell = new Cell(); while (scanner.Move(cell)) { mutator.Delete(cell.Key); } } } }
/// <summary> /// Adds an entity specification to the scan. /// </summary> /// <param name="entitySpec"> /// The entity specification to add. /// </param> /// <param name="scanSpec"> /// The scan specification. /// </param> internal void Add(EntitySpec entitySpec, ScanSpec scanSpec) { lock (this.syncRoot) { if (this.tables == null) { this.tables = new Map <Pair <string>, ITableScan>(); } this.tables = new Map <Pair <string>, ITableScan>(); var tableScanSpec = this.tables.GetOrAdd(new Pair <string>(entitySpec.Namespace, entitySpec.TableName), kvp => this.CreateTableScan(scanSpec)); tableScanSpec.Add(entitySpec); } }
/// <summary> /// Fetches all entities using the given scan specification from the database. /// </summary> /// <param name="entityType"> /// The entity type. /// </param> /// <param name="scanSpec"> /// The scan specification. /// </param> /// <param name="entitySink"> /// The entity sink, receives the entities fetched. /// </param> /// <param name="behaviors"> /// The behaviors. /// </param> public void Fetch(Type entityType, ScanSpec scanSpec, Action <object> entitySink, Behaviors behaviors) { if (entityType == null) { throw new ArgumentNullException(nameof(entityType)); } if (scanSpec == null) { throw new ArgumentNullException(nameof(scanSpec)); } var entityReference = this.EntityReferenceForType(entityType); if (entityReference == null) { throw new PersistenceException(string.Format(CultureInfo.InvariantCulture, @"{0} is not a valid entity", entityType)); } EntityReader.Read(this, entityReference, scanSpec, entitySink, behaviors); }
/// <summary> /// Removes all entities using the given scan specification from the database. /// </summary> /// <param name="entityType"> /// The entity type. /// </param> /// <param name="scanSpec"> /// The scan specification. /// </param> public void Remove(Type entityType, ScanSpec scanSpec) { if (entityType == null) { throw new ArgumentNullException(nameof(entityType)); } if (scanSpec == null) { throw new ArgumentNullException(nameof(scanSpec)); } var entityReference = this.EntityReferenceForType(entityType); if (entityReference == null) { throw new PersistenceException(string.Format(CultureInfo.InvariantCulture, @"{0} is not a valid entity", entityType)); } if (!scanSpec.KeysOnly) { ////scanSpec = new ScanSpec(scanSpec); //// TODO copy/clone scan spec scanSpec.KeysOnly = true; } var table = this.GetTable(entityReference.Namespace, entityReference.TableName); var mutator = this.GetTableMutator(entityReference.Namespace, entityReference.TableName); using (var scanner = table.CreateScanner(scanSpec)) { var cell = new Cell(); while (scanner.Move(cell)) { //// TODO remove from any cache???? mutator.Delete(cell.Key); } } }
/// <summary> /// Removes all entities using the given scan specification from the database. /// </summary> /// <param name="entityType"> /// The entity type. /// </param> /// <param name="scanSpec"> /// The scan specification. /// </param> public void Remove(Type entityType, ScanSpec scanSpec) { this.ThrowIfDisposed(); this.entityContext.Remove(entityType, scanSpec); }
/// <summary> /// The program entry point. /// </summary> /// <param name="args">Command line arguments</param> private static void Main(string[] args) { // Preamble Console.WriteLine("Welcome to ht4n getting started."); Console.WriteLine("For more information about ht4n, visit http://ht4n.softdev.ch/"); Console.WriteLine(); try { // Compose the connection string - Provider={0};Uri={1} var connectionString = string.Format( CultureInfo.InvariantCulture, "Provider={0};Uri={1}", args.Length > 0 ? args[0] : "Hyper", args.Length > 1 ? args[1] : "net.tcp://localhost"); // Connect to the database instance Console.WriteLine("Connecting {0}", connectionString); using (var context = Context.Create(connectionString)) using (var client = context.CreateClient()) { // Open or create namespace Console.WriteLine("Open or create namespace 'tutorials'"); using (var ns = client.OpenNamespace("tutorials", OpenDispositions.OpenAlways | OpenDispositions.CreateIntermediate)) { // Define the table schema using xml const string TableSchema = "<Schema>" + "<AccessGroup name=\"default\">" + "<ColumnFamily><Name>color</Name></ColumnFamily>" + "<ColumnFamily><Name>energy</Name></ColumnFamily>" + "<ColumnFamily><Name>protein</Name></ColumnFamily>" + "<ColumnFamily><Name>vitamins</Name></ColumnFamily>" + "</AccessGroup>" + "</Schema>"; // Open or create table Console.WriteLine("Open or create table 'fruits'"); using (var table = ns.OpenTable("fruits", TableSchema, OpenDispositions.OpenAlways)) { // Insert some fruits using (var mutator = table.CreateMutator()) { Console.WriteLine("Insert 'apple' into 'fruits'"); var key = new Key { Row = "apple", ColumnFamily = "color" }; mutator.Set(key, Encoding.UTF8.GetBytes("red")); key.ColumnFamily = "energy"; mutator.Set(key, BitConverter.GetBytes(207)); // [KJ] key.ColumnFamily = "protein"; mutator.Set(key, BitConverter.GetBytes(0.4)); // [g] key.ColumnFamily = "vitamins"; key.ColumnQualifier = "C"; mutator.Set(key, BitConverter.GetBytes(15.0)); // [mg] key.ColumnQualifier = "B1"; mutator.Set(key, BitConverter.GetBytes(0.02)); // [mg] Console.WriteLine("Insert 'banana' into 'fruits'"); key = new Key { Row = "banana", ColumnFamily = "color" }; mutator.Set(key, Encoding.UTF8.GetBytes("yellow")); key.ColumnFamily = "energy"; mutator.Set(key, BitConverter.GetBytes(375)); // [KJ] key.ColumnFamily = "protein"; mutator.Set(key, BitConverter.GetBytes(1.2)); // [g] key.ColumnFamily = "vitamins"; key.ColumnQualifier = "C"; mutator.Set(key, BitConverter.GetBytes(10.0)); // [mg] key.ColumnQualifier = "B1"; mutator.Set(key, BitConverter.GetBytes(0.04)); // [mg] } Console.WriteLine(); // Some query examples Console.WriteLine("Select all cells from 'fruits'"); using (var scanner = table.CreateScanner()) { foreach (var cell in scanner) { Console.WriteLine(cell); } } Console.WriteLine(); Console.WriteLine("Select all 'apple' from 'fruits'"); var scanSpec = new ScanSpec("apple"); using (var scanner = table.CreateScanner(scanSpec)) { Cell cell; while (scanner.Next(out cell)) { Console.WriteLine(cell); } } Console.WriteLine(); Console.WriteLine("Select all 'vitamins' for the 'banana'"); scanSpec = new ScanSpec("banana").AddColumn("vitamins"); using (var scanner = table.CreateScanner(scanSpec)) { var cell = new Cell(); while (scanner.Move(cell)) { // caution, re-use cell instance Console.WriteLine("{0} {1} {2}mg", cell.Key.Row, cell.Key.Column, BitConverter.ToDouble(cell.Value, 0)); } } Console.WriteLine(); } // Drop table Console.WriteLine("Drop table 'fruits'"); ns.DropTable("fruits"); } } } catch (Exception e) { Console.WriteLine(); Console.WriteLine(e); } }
/// <summary> /// Reads all entities which belongs to the given scan specification from the database. /// </summary> /// <param name="entityContext"> /// The entity context. /// </param> /// <param name="entityReference"> /// The entity reference. /// </param> /// <param name="scanSpec"> /// The scan spec. /// </param> /// <param name="entitySink"> /// The entity sink, receives the entities fetched. /// </param> /// <param name="behaviors"> /// The behaviors. /// </param> internal static void Read(EntityContext entityContext, EntityReference entityReference, ScanSpec scanSpec, Action<object> entitySink, Behaviors behaviors) { var entityScanResult = new EntityScanResult(entityReference, entitySink); var entityScanner = new EntityScanner(entityContext); entityScanner.Add(entityScanResult, scanSpec); new EntityReader(entityScanner, behaviors).Read(); }
/// <summary> /// Fetches all entities using the given scan specification from the database. /// </summary> /// <param name="scanSpec"> /// The scan specification. /// </param> /// <param name="behaviors"> /// The behaviors. /// </param> /// <typeparam name="T"> /// Type of the entities to fetch. /// </typeparam> /// <returns> /// The entities fetched. /// </returns> public IEnumerable <T> Fetch <T>(ScanSpec scanSpec, Behaviors behaviors) where T : class { return(OfType <T>(this.Fetch(typeof(T), scanSpec, this.CheckBehaviors(behaviors)))); }
/// <summary> /// Creates a table scan instance. /// </summary> /// <param name="scanSpec"> /// The scan spec. /// </param> /// <returns> /// The newly created table scan instance. /// </returns> private ITableScan CreateTableScan(ScanSpec scanSpec) { return new TableScan.TableScan(scanSpec); }
/// <summary> /// The program entry point. /// </summary> /// <param name="args">Command line arguments</param> private static void Main(string[] args) { // Preamble Console.WriteLine("Welcome to ht4n getting started."); Console.WriteLine("For more information about ht4n, visit http://ht4n.softdev.ch/"); Console.WriteLine(); try { // Compose the connection string - Provider={0};Uri={1} var connectionString = string.Format( CultureInfo.InvariantCulture, "Provider={0};Uri={1}", args.Length > 0 ? args[0] : "Hyper", args.Length > 1 ? args[1] : "net.tcp://localhost"); // Connect to the database instance Console.WriteLine("Connecting {0}", connectionString); using (var context = Context.Create(connectionString)) using (var client = context.CreateClient()) { // Open or create namespace Console.WriteLine("Open or create namespace 'tutorials'"); using (var ns = client.OpenNamespace("tutorials", OpenDispositions.OpenAlways | OpenDispositions.CreateIntermediate)) { // Define the table schema using xml const string TableSchema = "<Schema>" + "<AccessGroup name=\"default\">" + "<ColumnFamily><Name>color</Name></ColumnFamily>" + "<ColumnFamily><Name>energy</Name></ColumnFamily>" + "<ColumnFamily><Name>protein</Name></ColumnFamily>" + "<ColumnFamily><Name>vitamins</Name></ColumnFamily>" + "</AccessGroup>" + "</Schema>"; // Open or create table Console.WriteLine("Open or create table 'fruits'"); using (var table = ns.OpenTable("fruits", TableSchema, OpenDispositions.OpenAlways)) { // Insert some fruits using (var mutator = table.CreateMutator()) { Console.WriteLine("Insert 'apple' into 'fruits'"); var key = new Key { Row = "apple", ColumnFamily = "color" }; mutator.Set(key, Encoding.UTF8.GetBytes("red")); key.ColumnFamily = "energy"; mutator.Set(key, BitConverter.GetBytes(207)); // [KJ] key.ColumnFamily = "protein"; mutator.Set(key, BitConverter.GetBytes(0.4)); // [g] key.ColumnFamily = "vitamins"; key.ColumnQualifier = "C"; mutator.Set(key, BitConverter.GetBytes(15.0)); // [mg] key.ColumnQualifier = "B1"; mutator.Set(key, BitConverter.GetBytes(0.02)); // [mg] Console.WriteLine("Insert 'banana' into 'fruits'"); key = new Key { Row = "banana", ColumnFamily = "color" }; mutator.Set(key, Encoding.UTF8.GetBytes("yellow")); key.ColumnFamily = "energy"; mutator.Set(key, BitConverter.GetBytes(375)); // [KJ] key.ColumnFamily = "protein"; mutator.Set(key, BitConverter.GetBytes(1.2)); // [g] key.ColumnFamily = "vitamins"; key.ColumnQualifier = "C"; mutator.Set(key, BitConverter.GetBytes(10.0)); // [mg] key.ColumnQualifier = "B1"; mutator.Set(key, BitConverter.GetBytes(0.04)); // [mg] } Console.WriteLine(); // Some query examples Console.WriteLine("Select all cells from 'fruits'"); using (var scanner = table.CreateScanner()) { foreach (var cell in scanner) { Console.WriteLine(cell); } } Console.WriteLine(); Console.WriteLine("Select all 'apple' from 'fruits'"); var scanSpec = new ScanSpec("apple"); using (var scanner = table.CreateScanner(scanSpec)) { Cell cell; while (scanner.Next(out cell)) { Console.WriteLine(cell); } } Console.WriteLine(); Console.WriteLine("Select all 'vitamins' for the 'banana'"); scanSpec = new ScanSpec("banana").AddColumn("vitamins"); using (var scanner = table.CreateScanner(scanSpec)) { var cell = new Cell(); while (scanner.Move(cell)) // caution, re-use cell instance { Console.WriteLine("{0} {1} {2}mg", cell.Key.Row, cell.Key.Column, BitConverter.ToDouble(cell.Value, 0)); } } Console.WriteLine(); } // Drop table Console.WriteLine("Drop table 'fruits'"); ns.DropTable("fruits"); } } } catch (Exception e) { Console.WriteLine(); Console.WriteLine(e); } }
/// <summary> /// Fetches all entities using the given scan specification from the database. /// </summary> /// <param name="entityType"> /// The entity type. /// </param> /// <param name="scanSpec"> /// The scan specification. /// </param> /// <param name="entitySink"> /// The entity sink, receives the entities fetched. /// </param> /// <param name="behaviors"> /// The behaviors. /// </param> public void Fetch(Type entityType, ScanSpec scanSpec, Action <object> entitySink, Behaviors behaviors) { this.ThrowIfDisposed(); this.entityContext.Fetch(entityType, scanSpec, entitySink, this.CheckBehaviors(behaviors)); }
/// <summary> /// Fetches all entities using the given scan specification from the database. /// </summary> /// <param name="entityType"> /// The entity type. /// </param> /// <param name="scanSpec"> /// The scan specification. /// </param> /// <param name="entitySink"> /// The entity sink, receives the entities fetched. /// </param> public void Fetch(Type entityType, ScanSpec scanSpec, Action <object> entitySink) { this.Fetch(entityType, scanSpec, entitySink, Behaviors.Default); }
/// <summary> /// Fetches all entities using the given scan specification from the database. /// </summary> /// <param name="entityType"> /// The entity type. /// </param> /// <param name="scanSpec"> /// The scan specification. /// </param> /// <param name="behaviors"> /// The behaviors. /// </param> /// <returns> /// The entities fetched. /// </returns> public IEnumerable Fetch(Type entityType, ScanSpec scanSpec, Behaviors behaviors) { this.ThrowIfDisposed(); return(this.entityContext.Fetch(entityType, scanSpec, this.CheckBehaviors(behaviors))); }
/// <summary> /// Fetches all entities using the given scan specification from the database. /// </summary> /// <param name="entityType"> /// The entity type. /// </param> /// <param name="scanSpec"> /// The scan specification. /// </param> /// <returns> /// The entities fetched. /// </returns> public IEnumerable Fetch(Type entityType, ScanSpec scanSpec) { return(this.Fetch(entityType, scanSpec, Behaviors.Default)); }
/// <summary> /// Fetches all entities using the given scan specification from the database. /// </summary> /// <param name="scanSpec"> /// The scan specification. /// </param> /// <param name="entitySink"> /// The entity sink, receives the entities fetched. /// </param> /// <param name="behaviors"> /// The behaviors. /// </param> /// <typeparam name="T"> /// Type of the entities to fetch. /// </typeparam> public void Fetch <T>(ScanSpec scanSpec, Action <T> entitySink, Behaviors behaviors) where T : class { this.Fetch(typeof(T), scanSpec, OfType <T>(entitySink), this.CheckBehaviors(behaviors)); }
/// <summary> /// Fetches all entities using the given scan specification from the database. /// </summary> /// <param name="scanSpec"> /// The scan specification. /// </param> /// <param name="entitySink"> /// The entity sink, receives the entities fetched. /// </param> /// <typeparam name="T"> /// Type of the entities to fetch. /// </typeparam> public void Fetch <T>(ScanSpec scanSpec, Action <T> entitySink) where T : class { this.Fetch <T>(scanSpec, OfType <T>(entitySink), Behaviors.Default); }
private void ScanTableRandomRowsScanAndFilter(int count) { var cell = new Cell(); ITableScanner scanner; var random = new Random(); var scanSpec = new ScanSpec { ScanAndFilter = true }.AddColumn("a"); while (scanSpec.RowCount < count) { using (scanner = table.CreateScanner(new ScanSpec { KeysOnly = true }.AddColumn("a"))) { while( scanner.Move(cell) ) { Assert.IsNull(cell.Value); if ((random.Next() % 3) == 0) { scanSpec.AddRow(cell.Key.Row); if (scanSpec.RowCount == count) { break; } } } } } Assert.AreEqual(count, scanSpec.RowCount); var c = 0; var previousRow = string.Empty; using (scanner = table.CreateScanner(scanSpec)) { while( scanner.Move(cell) ) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); Assert.IsTrue(c < scanSpec.RowCount); Assert.IsTrue(string.Compare(previousRow, cell.Key.Row) < 0); previousRow = cell.Key.Row; ++c; } } Assert.AreEqual(count, c); }
public void ScanTableColumnPredicate() { var key = new Key { Row = "XXX", ColumnFamily = "d" }; using (var mutator = table.CreateMutator()) { mutator.Set(key, Encoding.GetBytes(key.Row)); key.Row = "XXY"; mutator.Set(key, Encoding.GetBytes(key.Row)); key.Row = "XYY"; mutator.Set(key, Encoding.GetBytes(key.Row)); key.Row = "YYY"; mutator.Set(key, Encoding.GetBytes(key.Row)); key.Row = "ZZX"; mutator.Set(key, Encoding.GetBytes(string.Empty)); key.Row = "ZZY"; mutator.Set(key, null); key.Row = "ZZZ"; mutator.Set(key, Encoding.GetBytes("What a wonderful world")); } var scanSpec = new ScanSpec() .AddColumn("d") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValueExact, Encoding.GetBytes("XYY"))); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); ++c; } Assert.AreEqual(1, c); } // Cannot distinguish between 'null' and 'string.Empty' scanSpec = new ScanSpec() .AddColumn("d") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValueExact, Encoding.GetBytes(string.Empty))); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.IsNull(cell.Value); ++c; } Assert.AreEqual(2, c); } // Cannot distinguish between 'null' and 'string.Empty' scanSpec = new ScanSpec() .AddColumn("d") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValueExact, null)); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.IsNull(cell.Value); ++c; } Assert.AreEqual(2, c); } scanSpec = new ScanSpec() .AddColumn("d") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValuePrefix, Encoding.GetBytes(string.Empty))); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { ++c; } Assert.AreEqual(7, c); } scanSpec = new ScanSpec() .AddColumn("d") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValuePrefix, null)); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { ++c; } Assert.AreEqual(7, c); } scanSpec = new ScanSpec() .AddColumn("d") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValuePrefix, Encoding.GetBytes("X"))); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); ++c; } Assert.AreEqual(3, c); } scanSpec = new ScanSpec() .AddColumn("d") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValuePrefix, Encoding.GetBytes("XX"))); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); ++c; } Assert.AreEqual(2, c); } scanSpec = new ScanSpec() .AddColumn("d") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValuePrefix, Encoding.GetBytes("Y"))); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); ++c; } Assert.AreEqual(1, c); } scanSpec = new ScanSpec() .AddColumn("d") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValuePrefix, Encoding.GetBytes("ZYX"))); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); ++c; } Assert.AreEqual(0, c); } scanSpec = new ScanSpec() .AddColumn("d") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValueRegex, Encoding.GetBytes("wonder"))); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("ZZZ", cell.Key.Row); ++c; } Assert.AreEqual(1, c); } key = new Key { Row = "XXX", ColumnFamily = "e" }; using (var mutator = table.CreateMutator()) { mutator.Set(key, Encoding.GetBytes(key.Row)); key.Row = "XXY"; mutator.Set(key, Encoding.GetBytes(key.Row)); key.Row = "XYY"; mutator.Set(key, Encoding.GetBytes(key.Row)); key.Row = "YYY"; mutator.Set(key, Encoding.GetBytes(key.Row)); key.Row = "ZZX"; mutator.Set(key, Encoding.GetBytes(string.Empty)); key.Row = "ZZY"; mutator.Set(key, null); key.Row = "ZZZ"; mutator.Set(key, Encoding.GetBytes("What's going on?")); } scanSpec = new ScanSpec() .AddColumn("d", "e") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValueExact, Encoding.GetBytes("XYY"))) .AddColumnPredicate(new ColumnPredicate("e", MatchKind.ValueExact, Encoding.GetBytes("XYY"))); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); ++c; } Assert.AreEqual(2, c); } scanSpec = new ScanSpec() .AddColumn("d", "e") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValuePrefix, Encoding.GetBytes("X"))) .AddColumnPredicate(new ColumnPredicate("e", MatchKind.ValuePrefix, Encoding.GetBytes("X"))); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); ++c; } Assert.AreEqual(6, c); } scanSpec = new ScanSpec() .AddColumn("d", "e") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValuePrefix, Encoding.GetBytes("XX"))) .AddColumnPredicate(new ColumnPredicate("e", MatchKind.ValuePrefix, Encoding.GetBytes("XX"))); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); ++c; } Assert.AreEqual(4, c); } scanSpec = new ScanSpec() .AddColumn("d", "e") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValuePrefix, Encoding.GetBytes("Y"))) .AddColumnPredicate(new ColumnPredicate("e", MatchKind.ValuePrefix, Encoding.GetBytes("Y"))); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); ++c; } Assert.AreEqual(2, c); } scanSpec = new ScanSpec() .AddColumn("d", "e") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValueExact, Encoding.GetBytes("ZYX"))) .AddColumnPredicate(new ColumnPredicate("e", MatchKind.ValueExact, Encoding.GetBytes("ZYX"))); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); ++c; } Assert.AreEqual(0, c); } // Cannot distinguish between 'null' and 'string.Empty' scanSpec = new ScanSpec() .AddColumn("d", "e") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValueExact, Encoding.GetBytes(string.Empty))) .AddColumnPredicate(new ColumnPredicate("e", MatchKind.ValueExact, Encoding.GetBytes(string.Empty))); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.IsTrue(cell.Key.Row.StartsWith("ZZ")); Assert.IsNull(cell.Value); ++c; } Assert.AreEqual(4, c); } // Cannot distinguish between 'null' and 'string.Empty' scanSpec = new ScanSpec() .AddColumn("d", "e") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValueExact, null)) .AddColumnPredicate(new ColumnPredicate("e", MatchKind.ValueExact, null)); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.IsTrue(cell.Key.Row.StartsWith("ZZ")); Assert.IsNull(cell.Value); ++c; } Assert.AreEqual(4, c); } scanSpec = new ScanSpec() .AddColumn("d", "e") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValuePrefix, Encoding.GetBytes(string.Empty))) .AddColumnPredicate(new ColumnPredicate("e", MatchKind.ValuePrefix, Encoding.GetBytes(string.Empty))); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { ++c; } Assert.AreEqual(14, c); } scanSpec = new ScanSpec() .AddColumn("d", "e") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValuePrefix, null)) .AddColumnPredicate(new ColumnPredicate("e", MatchKind.ValuePrefix, null)); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { ++c; } Assert.AreEqual(14, c); } scanSpec = new ScanSpec() .AddColumn("d", "e") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValueRegex, Encoding.GetBytes("wonder"))) .AddColumnPredicate(new ColumnPredicate("e", MatchKind.ValueRegex, Encoding.GetBytes("What's"))); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("ZZZ", cell.Key.Row); ++c; } Assert.AreEqual(2, c); } scanSpec = new ScanSpec() .AddColumn("d", "e") .AddColumnPredicate(new ColumnPredicate("d", MatchKind.ValueRegex, Encoding.GetBytes("on"))) .AddColumnPredicate(new ColumnPredicate("e", MatchKind.ValueRegex, Encoding.GetBytes("on"))); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("ZZZ", cell.Key.Row); ++c; } Assert.AreEqual(2, c); } key = new Key { Row = "XXX", ColumnFamily = "f", ColumnQualifier = "AAA" }; using (var mutator = table.CreateMutator()) { mutator.Set(key, Encoding.GetBytes(key.Row)); key.Row = "XXY"; key.ColumnQualifier = "AAB"; mutator.Set(key, Encoding.GetBytes(key.Row)); key.Row = "XYY"; key.ColumnQualifier = "ABB"; mutator.Set(key, Encoding.GetBytes(key.Row)); key.Row = "YYY"; key.ColumnQualifier = "BBB"; mutator.Set(key, Encoding.GetBytes(key.Row)); key.Row = "ZZX"; key.ColumnQualifier = string.Empty; mutator.Set(key, Encoding.GetBytes(string.Empty)); key.Row = "ZZY"; key.ColumnQualifier = null; mutator.Set(key, null); key.Row = "ZZZ"; key.ColumnQualifier = "BBC"; mutator.Set(key, Encoding.GetBytes("What a wonderful world")); } scanSpec = new ScanSpec() .AddColumn("f") .AddColumnPredicate(new ColumnPredicate("f", "ABB", MatchKind.QualifierExact)); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); ++c; } Assert.AreEqual(1, c); } // Cannot distinguish between 'null' and 'string.Empty' scanSpec = new ScanSpec() .AddColumn("f") .AddColumnPredicate(new ColumnPredicate("f", string.Empty, MatchKind.QualifierExact)); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.IsNull(cell.Value); ++c; } Assert.AreEqual(2, c); } // Cannot distinguish between 'null' and 'string.Empty' scanSpec = new ScanSpec() .AddColumn("f") .AddColumnPredicate(new ColumnPredicate("f", null, MatchKind.QualifierExact)); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.IsNull(cell.Value); ++c; } Assert.AreEqual(2, c); } scanSpec = new ScanSpec() .AddColumn("f") .AddColumnPredicate(new ColumnPredicate("f", "A", MatchKind.QualifierPrefix)); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); ++c; } Assert.AreEqual(3, c); } scanSpec = new ScanSpec() .AddColumn("f") .AddColumnPredicate(new ColumnPredicate("f", "BB", MatchKind.QualifierPrefix)); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { ++c; } Assert.AreEqual(2, c); } scanSpec = new ScanSpec() .AddColumn("f") .AddColumnPredicate(new ColumnPredicate("f", string.Empty, MatchKind.QualifierPrefix)); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { ++c; } Assert.AreEqual(7, c); } scanSpec = new ScanSpec() .AddColumn("f") .AddColumnPredicate(new ColumnPredicate("f", null, MatchKind.QualifierPrefix)); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { ++c; } Assert.AreEqual(7, c); } scanSpec = new ScanSpec() .AddColumn("f") .AddColumnPredicate(new ColumnPredicate("f", "A[A|B]B", MatchKind.QualifierRegex)); using (var scanner = table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual(cell.Key.Row, Encoding.GetString(cell.Value)); ++c; } Assert.AreEqual(2, c); } }
/// <summary> /// Adds an entity specification to the scan. /// </summary> /// <param name="entitySpec"> /// The entity specification to add. /// </param> /// <param name="scanSpec"> /// The scan specification. /// </param> internal void Add(EntitySpec entitySpec, ScanSpec scanSpec) { lock (this.syncRoot) { if (this.tables == null) { this.tables = new Map<Pair<string>, ITableScan>(); } this.tables = new Map<Pair<string>, ITableScan>(); var tableScanSpec = this.tables.GetOrAdd(new Pair<string>(entitySpec.Namespace, entitySpec.TableName), kvp => this.CreateTableScan(scanSpec)); tableScanSpec.Add(entitySpec); } }
/// <summary> /// Creates a table scan instance. /// </summary> /// <param name="scanSpec"> /// The scan spec. /// </param> /// <returns> /// The newly created table scan instance. /// </returns> private ITableScan CreateTableScan(ScanSpec scanSpec) { return(new TableScan.TableScan(scanSpec)); }
public void ScanTableCancelBlockingAsyncScanner() { if (!HasAsyncTableScanner) { return; } var rng = new Random(); var scanSpecA = new ScanSpec().AddColumn("a"); var scanSpecB = new ScanSpec().AddColumn("a", "b", "c"); var scanSpecC = new ScanSpec().AddColumn("b", "c"); for (var r = 0; r < 5; ++r) { var c = new Dictionary<ScanSpec, int>(); var limit = new Dictionary<ScanSpec, int>(); var total = new Dictionary<ScanSpec, int>(); c[scanSpecA] = 0; c[scanSpecB] = 0; c[scanSpecC] = 0; limit[scanSpecA] = rng.Next(CountA / 2); limit[scanSpecB] = rng.Next(CountB / 2); limit[scanSpecC] = int.MaxValue; using (var asyncResult = new BlockingAsyncResult()) { table.BeginScan(asyncResult, scanSpecA); table.BeginScan(asyncResult, scanSpecB); table.BeginScan(asyncResult, scanSpecC); AsyncScannerContext ctx; IList<Cell> cells; while (asyncResult.TryGetCells(out ctx, out cells)) { c[ctx.ScanSpec] += cells.Count; if (!total.ContainsKey(ctx.ScanSpec) && c[ctx.ScanSpec] > limit[ctx.ScanSpec]) { total.Add(ctx.ScanSpec, c[ctx.ScanSpec]); asyncResult.CancelAsyncScanner(ctx); } } asyncResult.Join(); Assert.IsNull(asyncResult.Error, asyncResult.Error != null ? asyncResult.Error.ToString() : string.Empty); Assert.IsTrue(asyncResult.IsCompleted); Assert.IsFalse(asyncResult.IsCancelled); Assert.AreEqual(total[scanSpecA], c[scanSpecA]); Assert.AreEqual(total[scanSpecB], c[scanSpecB]); Assert.AreEqual(CountB + CountC, c[scanSpecC]); total = new Dictionary<ScanSpec, int>(); c[scanSpecA] = 0; c[scanSpecB] = 0; c[scanSpecC] = 0; limit[scanSpecA] = rng.Next(CountA / 2); limit[scanSpecB] = rng.Next(CountB / 2); limit[scanSpecC] = int.MaxValue; table.BeginScan(asyncResult, scanSpecC); table.BeginScan(asyncResult, scanSpecB); table.BeginScan(asyncResult, scanSpecA); while (asyncResult.TryGetCells(out ctx, out cells)) { c[ctx.ScanSpec] += cells.Count; if (!total.ContainsKey(ctx.ScanSpec) && c[ctx.ScanSpec] > limit[ctx.ScanSpec]) { total.Add(ctx.ScanSpec, c[ctx.ScanSpec]); asyncResult.CancelAsyncScanner(ctx); } } asyncResult.Join(); Assert.IsNull(asyncResult.Error, asyncResult.Error != null ? asyncResult.Error.ToString() : string.Empty); Assert.IsTrue(asyncResult.IsCompleted); Assert.IsFalse(asyncResult.IsCancelled); Assert.AreEqual(total[scanSpecA], c[scanSpecA]); Assert.AreEqual(total[scanSpecB], c[scanSpecB]); Assert.AreEqual(CountB + CountC, c[scanSpecC]); } } }
/// <summary> /// Fetches all entities using the given scan specification from the database. /// </summary> /// <param name="scanSpec"> /// The scan specification. /// </param> /// <typeparam name="T"> /// Type of the entities to fetch. /// </typeparam> /// <returns> /// The entities fetched. /// </returns> public IEnumerable <T> Fetch <T>(ScanSpec scanSpec) where T : class { return(this.Fetch <T>(scanSpec, Behaviors.Default)); }
public void ScanTableCancelBlockingAsyncScanner() { if (!HasAsyncTableScanner) { return; } var rng = new Random(); var scanSpecA = new ScanSpec().AddColumn("a"); var scanSpecB = new ScanSpec().AddColumn("a", "b", "c"); var scanSpecC = new ScanSpec().AddColumn("b", "c"); for (var r = 0; r < 5; ++r) { var c = new Dictionary <ScanSpec, int>(); var limit = new Dictionary <ScanSpec, int>(); var total = new Dictionary <ScanSpec, int>(); c[scanSpecA] = 0; c[scanSpecB] = 0; c[scanSpecC] = 0; limit[scanSpecA] = rng.Next(CountA / 2); limit[scanSpecB] = rng.Next(CountB / 2); limit[scanSpecC] = int.MaxValue; using (var asyncResult = new BlockingAsyncResult()) { table.BeginScan(asyncResult, scanSpecA); table.BeginScan(asyncResult, scanSpecB); table.BeginScan(asyncResult, scanSpecC); AsyncScannerContext ctx; IList <Cell> cells; while (asyncResult.TryGetCells(out ctx, out cells)) { c[ctx.ScanSpec] += cells.Count; if (!total.ContainsKey(ctx.ScanSpec) && c[ctx.ScanSpec] > limit[ctx.ScanSpec]) { total.Add(ctx.ScanSpec, c[ctx.ScanSpec]); asyncResult.CancelAsyncScanner(ctx); } } asyncResult.Join(); Assert.IsNull(asyncResult.Error, asyncResult.Error != null ? asyncResult.Error.ToString() : string.Empty); Assert.IsTrue(asyncResult.IsCompleted); Assert.IsFalse(asyncResult.IsCancelled); Assert.AreEqual(total[scanSpecA], c[scanSpecA]); Assert.AreEqual(total[scanSpecB], c[scanSpecB]); Assert.AreEqual(CountB + CountC, c[scanSpecC]); total = new Dictionary <ScanSpec, int>(); c[scanSpecA] = 0; c[scanSpecB] = 0; c[scanSpecC] = 0; limit[scanSpecA] = rng.Next(CountA / 2); limit[scanSpecB] = rng.Next(CountB / 2); limit[scanSpecC] = int.MaxValue; table.BeginScan(asyncResult, scanSpecC); table.BeginScan(asyncResult, scanSpecB); table.BeginScan(asyncResult, scanSpecA); while (asyncResult.TryGetCells(out ctx, out cells)) { c[ctx.ScanSpec] += cells.Count; if (!total.ContainsKey(ctx.ScanSpec) && c[ctx.ScanSpec] > limit[ctx.ScanSpec]) { total.Add(ctx.ScanSpec, c[ctx.ScanSpec]); asyncResult.CancelAsyncScanner(ctx); } } asyncResult.Join(); Assert.IsNull(asyncResult.Error, asyncResult.Error != null ? asyncResult.Error.ToString() : string.Empty); Assert.IsTrue(asyncResult.IsCompleted); Assert.IsFalse(asyncResult.IsCancelled); Assert.AreEqual(total[scanSpecA], c[scanSpecA]); Assert.AreEqual(total[scanSpecB], c[scanSpecB]); Assert.AreEqual(CountB + CountC, c[scanSpecC]); } } }
public void ScanTableColumnPredicateIndex() { if (!IsHyper && !IsThrift) { return; } const string ScanTableColumnPredicateIndexSchema = "<Schema><AccessGroup name=\"default\" blksz=\"1024\">" + "<ColumnFamily><Name>a</Name></ColumnFamily>" + "<ColumnFamily><Name>b</Name><Index>true</Index></ColumnFamily>" + "<ColumnFamily><Name>c</Name><QualifierIndex>true</QualifierIndex></ColumnFamily>" + "</AccessGroup></Schema>"; using (var _table = EnsureTable("ScanTableColumnPredicateIndex", ScanTableColumnPredicateIndexSchema)) { Assert.IsTrue(Ns.TableExists("^ScanTableColumnPredicateIndex")); Assert.IsTrue(Ns.TableExists("^^ScanTableColumnPredicateIndex")); var key = new Key(); using (var mutator = _table.CreateMutator()) { key.Row = "1"; key.ColumnFamily = "a"; key.ColumnQualifier = null; mutator.Set(key, Encoding.GetBytes("111")); key.ColumnFamily = "b"; key.ColumnQualifier = null; mutator.Set(key, Encoding.GetBytes("1BBB")); key.ColumnFamily = "c"; key.ColumnQualifier = "q11"; mutator.Set(key, Encoding.GetBytes("1QQ1")); key.ColumnFamily = "c"; key.ColumnQualifier = "q12"; mutator.Set(key, Encoding.GetBytes("1QQ2")); key.Row = "2"; key.ColumnFamily = "a"; key.ColumnQualifier = null; mutator.Set(key, Encoding.GetBytes("222")); key.ColumnFamily = "b"; key.ColumnQualifier = null; mutator.Set(key, Encoding.GetBytes("2BBB")); key.ColumnFamily = "c"; key.ColumnQualifier = "q21"; mutator.Set(key, Encoding.GetBytes("2QQ1")); key.ColumnFamily = "c"; key.ColumnQualifier = "q22"; mutator.Set(key, Encoding.GetBytes("2QQ2")); key.Row = "3"; key.ColumnFamily = "a"; key.ColumnQualifier = null; mutator.Set(key, Encoding.GetBytes("333")); key.ColumnFamily = "b"; key.ColumnQualifier = null; mutator.Set(key, Encoding.GetBytes("3BBB")); key.ColumnFamily = "c"; key.ColumnQualifier = "x31"; mutator.Set(key, Encoding.GetBytes("3QQ1")); key.ColumnFamily = "c"; key.ColumnQualifier = "q32"; mutator.Set(key, Encoding.GetBytes("3QQ2")); key.Row = "4"; key.ColumnFamily = "a"; key.ColumnQualifier = null; mutator.Set(key, Encoding.GetBytes("444")); key.ColumnFamily = "b"; key.ColumnQualifier = null; mutator.Set(key, Encoding.GetBytes("1444")); key.ColumnFamily = "c"; key.ColumnQualifier = "x41"; mutator.Set(key, Encoding.GetBytes("3QQ4")); key.ColumnFamily = "c"; key.ColumnQualifier = "x42"; mutator.Set(key, Encoding.GetBytes("3QQ4")); } var scanSpec = new ScanSpec() .AddColumn("a") .AddColumnPredicate(new ColumnPredicate("b", MatchKind.ValueExact, Encoding.GetBytes("1BBB"))); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("a", cell.Key.ColumnFamily); Assert.AreEqual(cell.Key.Row, "1"); ++c; } Assert.AreEqual(1, c); } scanSpec = new ScanSpec() .AddColumn("a") .AddColumnPredicate(new ColumnPredicate("b", MatchKind.ValueExact, Encoding.GetBytes("2BBB"))) .AddColumnPredicate(new ColumnPredicate("b", MatchKind.ValueExact, Encoding.GetBytes("3BBB"))); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("a", cell.Key.ColumnFamily); Assert.IsTrue(cell.Key.Row == "2" || cell.Key.Row == "3"); ++c; } Assert.AreEqual(2, c); } scanSpec = new ScanSpec() .AddColumn("a") .AddColumnPredicate(new ColumnPredicate("b", MatchKind.ValuePrefix, Encoding.GetBytes("1"))); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("a", cell.Key.ColumnFamily); Assert.IsTrue(cell.Key.Row == "1" || cell.Key.Row == "4"); ++c; } Assert.AreEqual(2, c); } // regex prefix required scanSpec = new ScanSpec() .AddColumn("a") .AddColumnPredicate(new ColumnPredicate("b", MatchKind.ValueRegex, Encoding.GetBytes("^1[BBB|444]"))); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.IsTrue(cell.Key.ColumnFamily == "a"); Assert.IsTrue(cell.Key.Row == "1" || cell.Key.Row == "4"); ++c; } Assert.AreEqual(2, c); } scanSpec = new ScanSpec() .AddColumn("a") .AddColumnPredicate(new ColumnPredicate("c", "q12", MatchKind.QualifierExact)); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("a", cell.Key.ColumnFamily); Assert.AreEqual(cell.Key.Row, "1"); ++c; } Assert.AreEqual(1, c); } scanSpec = new ScanSpec() .AddColumn("a") .AddColumnPredicate(new ColumnPredicate("c", "q21", MatchKind.QualifierExact)) .AddColumnPredicate(new ColumnPredicate("c", "q32", MatchKind.QualifierExact)); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("a", cell.Key.ColumnFamily); Assert.IsTrue(cell.Key.Row == "2" || cell.Key.Row == "3"); ++c; } Assert.AreEqual(2, c); } scanSpec = new ScanSpec() .AddColumn("a") .AddColumnPredicate(new ColumnPredicate("c", "x", MatchKind.QualifierPrefix)); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("a", cell.Key.ColumnFamily); Assert.IsTrue(cell.Key.Row == "3" || cell.Key.Row == "4"); ++c; } Assert.AreEqual(2, c); } // regex prefix required scanSpec = new ScanSpec() .AddColumn("a") .AddColumnPredicate(new ColumnPredicate("c", "^q[2|3]", MatchKind.QualifierRegex)); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("a", cell.Key.ColumnFamily); Assert.IsTrue(cell.Key.Row == "2" || cell.Key.Row == "3"); ++c; } Assert.AreEqual(2, c); } const int RowCount = 10000; using (var tableMutator = _table.CreateMutator()) { for (var i = 0; i < RowCount; ++i) { var row = i.ToString("D6"); var k = new Key(row, "a"); tableMutator.Set(k, Encoding.GetBytes(row)); k = new Key(row, "b", "1"); tableMutator.Set(k, BitConverter.GetBytes((i % 100) == 0 ? 7 : 5)); k = new Key(row, "c", (i % 100) == 0 ? "mod100" : "-"); tableMutator.Set(k, null); } } // exact value match and row interval scanSpec = ScanSpecBuilder.Create() .WithColumns("a") .WithRows(new RowInterval("000200", "000500")) .WithColumnPredicates(new ColumnPredicate("b", "1", MatchKind.ValueExact | MatchKind.QualifierExact, BitConverter.GetBytes(7))) .Build(); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("a", cell.Key.ColumnFamily); Assert.IsTrue((int.Parse(cell.Key.Row) % 100) == 0); ++c; } Assert.AreEqual(4, c); } scanSpec = ScanSpecBuilder.Create() .WithColumns("a") .WithRows(new RowInterval("002000", null)) .WithColumnPredicates(new ColumnPredicate("b", "1", MatchKind.ValueExact | MatchKind.QualifierExact, BitConverter.GetBytes(7))) .Build(); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("a", cell.Key.ColumnFamily); Assert.IsTrue((int.Parse(cell.Key.Row) % 100) == 0); ++c; } Assert.AreEqual(80, c); } scanSpec = ScanSpecBuilder.Create() .WithColumns("a") .WithRows(new RowInterval("002000", null)) .WithColumnPredicates(new ColumnPredicate("b", "1", MatchKind.ValueExact | MatchKind.QualifierExact, BitConverter.GetBytes(7))) .Build(); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("a", cell.Key.ColumnFamily); Assert.IsTrue((int.Parse(cell.Key.Row) % 100) == 0); ++c; } Assert.AreEqual(80, c); } scanSpec = ScanSpecBuilder.Create() .WithColumns("a") .WithRows(new RowInterval(string.Empty, false, "002000", false)) .WithColumnPredicates(new ColumnPredicate("b", "1", MatchKind.ValueExact | MatchKind.QualifierExact, BitConverter.GetBytes(7))) .Build(); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("a", cell.Key.ColumnFamily); Assert.IsTrue((int.Parse(cell.Key.Row) % 100) == 0); ++c; } Assert.AreEqual(20, c); } scanSpec = ScanSpecBuilder.Create() .WithColumns("a") .WithRows(new RowInterval(null, false, "002000", false)) .WithColumnPredicates(new ColumnPredicate("b", "1", MatchKind.ValueExact | MatchKind.QualifierExact, BitConverter.GetBytes(7))) .Build(); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("a", cell.Key.ColumnFamily); Assert.IsTrue((int.Parse(cell.Key.Row) % 100) == 0); ++c; } Assert.AreEqual(20, c); } // exact value match and row intervals scanSpec = ScanSpecBuilder.Create() .WithColumns("a") .WithRows(new RowInterval("000200", "000500"), new RowInterval("000700", "000800"), new RowInterval("001400", "001900")) .WithColumnPredicates(new ColumnPredicate("b", "1", MatchKind.ValueExact | MatchKind.QualifierExact, BitConverter.GetBytes(7))) .Build(); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("a", cell.Key.ColumnFamily); Assert.IsTrue((int.Parse(cell.Key.Row) % 100) == 0); ++c; } Assert.AreEqual(12, c); } // exact value match and row interval, scan&filter scanSpec = ScanSpecBuilder.Create() .WithColumns("a") .WithRows(new RowInterval("000200", "000500")) .WithColumnPredicates(new ColumnPredicate("b", "1", MatchKind.ValueExact | MatchKind.QualifierExact, BitConverter.GetBytes(7))) .ScanAndFilter() .Build(); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("a", cell.Key.ColumnFamily); Assert.IsTrue((int.Parse(cell.Key.Row) % 100) == 0); ++c; } Assert.AreEqual(4, c); } // exact value match and row intervals, scan&filter scanSpec = ScanSpecBuilder.Create() .WithColumns("a") .WithRows(new RowInterval("000200", "000500"), new RowInterval("000700", "000800"), new RowInterval("001400", "001900")) .WithColumnPredicates(new ColumnPredicate("b", "1", MatchKind.ValueExact | MatchKind.QualifierExact, BitConverter.GetBytes(7))) .ScanAndFilter() .Build(); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("a", cell.Key.ColumnFamily); Assert.IsTrue((int.Parse(cell.Key.Row) % 100) == 0); ++c; } Assert.AreEqual(12, c); } // exact qualifier match and row interval scanSpec = ScanSpecBuilder.Create() .WithColumns("a") .WithRows(new RowInterval("000200", "000500")) .WithColumnPredicates(new ColumnPredicate("c", "mod100", MatchKind.QualifierExact)) .Build(); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("a", cell.Key.ColumnFamily); Assert.IsTrue((int.Parse(cell.Key.Row) % 100) == 0); ++c; } Assert.AreEqual(4, c); } // exact qualifier match and row intervals scanSpec = ScanSpecBuilder.Create() .WithColumns("a") .WithRows(new RowInterval("000200", "000500"), new RowInterval("000700", "000800"), new RowInterval("001400", "001900")) .WithColumnPredicates(new ColumnPredicate("c", "mod100", MatchKind.QualifierExact)) .Build(); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; Cell cell; while (scanner.Next(out cell)) { Assert.AreEqual("a", cell.Key.ColumnFamily); Assert.IsTrue((int.Parse(cell.Key.Row) % 100) == 0); ++c; } Assert.AreEqual(12, c); } } }
public void FetchByScanSpec() { var ec1 = new EntityC { A = new EntityA(), B = new EntityB(), X = new EntityX(), Y = new EntityY() }; TestBase.TestSerialization(ec1); var ex21 = new EntityX2(); TestBase.TestSerialization(ex21); using (var em = Emf.CreateEntityManager()) { em.Persist(ec1); em.Persist(ex21); } using (var em = Emf.CreateEntityManager()) { var _ec = em.Fetch<EntityC>(new ScanSpec(ec1.Id)); Assert.IsNotNull(_ec); Assert.AreEqual(1, _ec.Count()); Assert.AreEqual(ec1, _ec.First()); var ss = new ScanSpec(); ss.AddColumn("a"); var _ex = em.Fetch<EntityXBase>(ss); Assert.IsNotNull(_ex); Assert.AreEqual(2, _ex.Count()); Assert.AreEqual(ec1.X, _ex.OfType<EntityX>().First()); Assert.AreEqual(ex21, _ex.OfType<EntityX2>().First()); } }
/// <summary> /// Reads all entities which belongs to the given scan specification from the database. /// </summary> /// <param name="entityContext"> /// The entity context. /// </param> /// <param name="entityReference"> /// The entity reference. /// </param> /// <param name="scanSpec"> /// The scan spec. /// </param> /// <param name="entitySink"> /// The entity sink, receives the entities fetched. /// </param> /// <param name="behaviors"> /// The behaviors. /// </param> internal static void Read(EntityContext entityContext, EntityReference entityReference, ScanSpec scanSpec, Action <object> entitySink, Behaviors behaviors) { var entityScanResult = new EntityScanResult(entityReference, entitySink); var entityScanner = new EntityScanner(entityContext); entityScanner.Add(entityScanResult, scanSpec); new EntityReader(entityScanner, behaviors).Read(); }
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); }
/// <summary> /// Reads all entities which belongs to the given scan specification from the database. /// </summary> /// <param name="entityContext"> /// The entity context. /// </param> /// <param name="entityReference"> /// The entity reference. /// </param> /// <param name="scanSpec"> /// The scan spec. /// </param> /// <param name="behaviors"> /// The behaviors. /// </param> /// <returns> /// The entities. /// </returns> internal static IEnumerable Read(EntityContext entityContext, EntityReference entityReference, ScanSpec scanSpec, Behaviors behaviors) { var entityScanResult = new EntityScanResult(entityReference); var entityScanner = new EntityScanner(entityContext); entityScanner.Add(entityScanResult, scanSpec); new EntityReader(entityScanner, behaviors).Read(); return entityScanResult.Values; }
public void ScanTableValueIndex() { const string ScanTableValueIndexSchema = "<Schema><AccessGroup name=\"default\" blksz=\"1024\">" + "<ColumnFamily><Name>a</Name><Index>true</Index></ColumnFamily>" + "<ColumnFamily><Name>b</Name><Index>true</Index></ColumnFamily>" + "<ColumnFamily><Name>c</Name><Index>true</Index></ColumnFamily>" + "</AccessGroup></Schema>"; const int Count = 10000; using (var _table = EnsureTable("ScanTableValueIndex", ScanTableValueIndexSchema)) { if (IsHyper || IsThrift) { Assert.IsTrue(Ns.TableExists("^ScanTableValueIndex")); } var key = new Key(); using (var mutator = _table.CreateMutator()) { for (var i = 0; i < Count; ++i) { var value = Encoding.GetBytes(i.ToString()); key.ColumnFamily = "a"; key.Row = null; mutator.Set(key, value); key.ColumnFamily = "b"; key.Row = null; mutator.Set(key, value); key.ColumnFamily = "c"; key.Row = null; mutator.Set(key, value); } } string[] columnFamilies = { "a", "b", "c" }; var rng = new Random(); for (var i = 0; i < Count / 100; ++i) { var search = rng.Next(Count).ToString(); var column = columnFamilies[rng.Next(columnFamilies.Length)]; var scanSpec = new ScanSpec() .AddColumn(column) .AddColumnPredicate(new ColumnPredicate(column, MatchKind.ValueExact, Encoding.GetBytes(search))); using (var scanner = _table.CreateScanner(scanSpec)) { Cell cell; Assert.IsTrue(scanner.Next(out cell)); Assert.AreEqual(search, Encoding.GetString(cell.Value)); Assert.IsFalse(scanner.Next(out cell)); } } int[] ranges = { 1000, 300, 56, 9999, 789, 4 }; int[] occurrence = { 1, 11, 111, 1, 11, 1111 }; Assert.AreEqual(ranges.Length, occurrence.Length); for (var i = 0; i < ranges.Length; ++i) { var search = ranges[i].ToString(); var column = columnFamilies[rng.Next(columnFamilies.Length)]; var scanSpec = new ScanSpec() .AddColumn(column) .AddColumnPredicate(new ColumnPredicate(column, MatchKind.ValuePrefix, Encoding.GetBytes(search))); using (var scanner = _table.CreateScanner(scanSpec)) { var c = 0; var cell = new Cell(); while( scanner.Move(cell) ) { Assert.IsTrue(Encoding.GetString(cell.Value).StartsWith(search)); ++c; } Assert.AreEqual(occurrence[i], c, "range = {0}", ranges[i]); } } } }
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); } } } }
/// <summary> /// Removes all entities using the given scan specification from the database. /// </summary> /// <typeparam name="T"> /// The entity type. /// </typeparam> /// <param name="scanSpec"> /// The scan specification. /// </param> public void Remove <T>(ScanSpec scanSpec) where T : class { this.Remove(typeof(T), scanSpec); }