public void QueryFilter()
        {
            string nameFilter = "Bill";
            string passFilter = "hknfpkj";

            Statement stmt = new Statement();
            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetFilters(Filter.Equal(binName, nameFilter));
            stmt.SetAggregateFunction(Assembly.GetExecutingAssembly(), "Aerospike.Test.Resources.filter_example.lua", "filter_example", "profile_filter", Value.Get(passFilter));

            // passFilter will be applied in filter_example.lua.
            ResultSet rs = client.QueryAggregate(null, stmt);

            try
            {
                int count = 0;

                while (rs.Next())
                {
                    IDictionary map = (IDictionary)rs.Object;
                    Assert.AreEqual(nameFilter, map["name"]);
                    Assert.AreEqual(passFilter, map["password"]);
                    count++;
                }
                Assert.AreNotEqual(0, count);
            }
            finally
            {
                rs.Close();
            }
        }
        public void QueryKey()
        {
            int begin = 2;
            int end = 5;

            Statement stmt = new Statement();
            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetBinNames(binName);
            stmt.SetFilters(Filter.Range(binName, begin, end));

            RecordSet rs = client.Query(null, stmt);

            try
            {
                int count = 0;

                while (rs.Next())
                {
                    Key key = rs.Key;
                    Assert.IsNotNull(key.userKey);

                    object userkey = key.userKey.Object;
                    Assert.IsNotNull(userkey);
                    count++;
                }
                Assert.AreEqual(4, count);
            }
            finally
            {
                rs.Close();
            }
        }
        public void QueryInteger()
        {
            int begin = 14;
            int end = 18;

            Statement stmt = new Statement();
            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetBinNames(binName);
            stmt.SetFilters(Filter.Range(binName, begin, end));

            RecordSet rs = client.Query(null, stmt);

            try
            {
                int count = 0;

                while (rs.Next())
                {
                    count++;
                }
                Assert.AreEqual(5, count);
            }
            finally
            {
                rs.Close();
            }
        }
        public void InsertByKey()
        {
            int i = 0;
            for (int x = 1; x <= TestQueryEngine.RECORD_COUNT; x++)
            {
                String keyString = "selector-test:" + x;

                Bin name = new Bin("name", "name:" + x);
                Bin age = new Bin("age", ages[i]);
                Bin colour = new Bin("color", colours[i]);
                Bin animal = new Bin("animal", animals[i]);
                List<Bin> bins = new List<Bin>() { name, age, colour, animal };

                Key key = new Key(TestQueryEngine.NAMESPACE, TestQueryEngine.SET_NAME, keyString);
                this.client.Delete(null, key);

                KeyQualifier kq = new KeyQualifier(Value.Get(keyString));
                Statement stmt = new Statement();
                stmt.Namespace = TestQueryEngine.NAMESPACE;
                stmt.SetName = TestQueryEngine.SET_NAME;

                queryEngine.Insert(stmt, kq, bins);

                Record record = this.client.Get(null, key);
                Assert.NotNull(record);
                i++;
                if (i == 5)
                    i = 0;
            }
        }
Esempio n. 5
0
        public void selectByKeyNoIndex()
        {
            // Execute the Query
            Statement stmt = new Statement();
            stmt.Namespace = TestQueryEngine.NAMESPACE;
            stmt.SetName = SET;

            Qualifier qual1 = new Qualifier(mapBinNoIndex, Qualifier.FilterOperation.MAP_KEYS_CONTAINS, Value.Get("dogs7"));
            KeyRecordEnumerator it = queryEngine.Select(stmt, qual1);
            int count2 = 0;
            try
            {
                while (it.MoveNext())
                {
                    KeyRecord rec = it.Current;
                    Dictionary<string, Object> map = (Dictionary<string, Object>)rec.record.GetMap(mapBinNoIndex);
                    Assert.True(map.ContainsKey("dogs7"));
                    count2++;
                }
            }
            finally
            {
                it.Close();
            }
        }
 public QueryRecordCommand(Node node, Policy policy, Statement statement, RecordSet recordSet)
     : base(node, true)
 {
     this.policy = policy;
     this.statement = statement;
     this.recordSet = recordSet;
 }
 public QueryAggregateExecutor(Cluster cluster, QueryPolicy policy, Statement statement)
     : base(cluster, policy, statement)
 {
     inputQueue = new BlockingCollection<object>(500);
     resultSet = new ResultSet(this, policy.recordQueueSize, cancel.Token);
     InitializeThreads();
 }
        public void QueryString()
        {
            string filter = valuePrefix + 3;

            Statement stmt = new Statement();
            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetBinNames(binName);
            stmt.SetFilters(Filter.Equal(binName, filter));

            RecordSet rs = client.Query(null, stmt);

            try
            {
                int count = 0;

                while (rs.Next())
                {
                    Record record = rs.Record;
                    string result = record.GetString(binName);
                    Assert.AreEqual(filter, result);
                    count++;
                }

                Assert.AreNotEqual(0, count);
            }
            finally
            {
                rs.Close();
            }
        }
 public void deleteStartsWith()
 {
     Qualifier qual1 = new Qualifier("color", Qualifier.FilterOperation.ENDS_WITH, Value.Get("e"));
     Statement stmt = new Statement();
     stmt.Namespace = TestQueryEngine.NAMESPACE;
     stmt.SetName = TestQueryEngine.SET_NAME;
     IDictionary<string, long> counts = queryEngine.Delete(stmt, qual1);
     Assert.AreEqual(400L, counts["read"]);
     Assert.AreEqual(400L, counts["write"]);
 }
Esempio n. 10
0
        public void aggregateUsersByTweetCountByRegion()
        {
            ResultSet rs = null;
            try
            {
                int min;
                int max;
                Console.WriteLine("\nEnter Min Tweet Count:");
                min = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter Max Tweet Count:");
                max = int.Parse(Console.ReadLine());

                // NOTE: UDF registration has been included here for convenience and to demonstrate the syntax.
                // NOTE: The recommended way of registering UDFs in production env is via AQL
                string luaDirectory = @"..\..\udf";
                LuaConfig.PackagePath = luaDirectory + @"\?.lua";

                string filename = "aggregationByRegion.lua";
                string path = Path.Combine(luaDirectory, filename);

                RegisterTask rt = client.Register(null, path, filename, Language.LUA);
                rt.Wait();

                string[] bins = { "tweetcount", "region" };
                Statement stmt = new Statement();
                stmt.SetNamespace("test");
                stmt.SetSetName("users");
                stmt.SetIndexName("tweetcount_index");
                stmt.SetBinNames(bins);
                stmt.SetFilters(Filter.Range("tweetcount", min, max));

                Console.WriteLine("\nAggregating users with " + min + "-" + max + " tweets by region. Hang on...\n");

                rs = client.QueryAggregate(null, stmt, "aggregationByRegion", "sum");

                if (rs.Next())
                {
                    Dictionary<object, object> result = (Dictionary<object, object>)rs.Object;
                    Console.WriteLine("Total Users in North: " + result["n"]);
                    Console.WriteLine("Total Users in South: " + result["s"]);
                    Console.WriteLine("Total Users in East: " + result["e"]);
                    Console.WriteLine("Total Users in West: " + result["w"]);
                }
            }
            finally
            {
                if (rs != null)
                {
                    // Close record set
                    rs.Close();
                }
            }
        }
        public QueryAggregateCommand(
			Node node,
			Policy policy,
			Statement statement,
			BlockingCollection<object> inputQueue,
			CancellationToken cancelToken
		)
            : base(node, true)
        {
            this.policy = policy;
            this.statement = statement;
            this.inputQueue = inputQueue;
            this.cancelToken = cancelToken;
        }
        public void QueryExecute()
        {
            int begin = 3;
            int end = 9;

            Statement stmt = new Statement();
            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetFilters(Filter.Range(binName1, begin, end));

            ExecuteTask task = client.Execute(null, stmt, "record_example", "processRecord", Value.Get(binName1), Value.Get(binName2), Value.Get(100));
            task.Wait();
            ValidateRecords();
        }
Esempio n. 13
0
 public void deleteByKey()
 {
     for (int x = 1; x <= TestQueryEngine.RECORD_COUNT; x++)
     {
         String keyString = "selector-test:" + x;
         Key key = new Key(TestQueryEngine.NAMESPACE, TestQueryEngine.SET_NAME, keyString);
         KeyQualifier kq = new KeyQualifier(Value.Get(keyString));
         Statement stmt = new Statement();
         stmt.Namespace = TestQueryEngine.NAMESPACE;
         stmt.SetName = TestQueryEngine.SET_NAME;
         IDictionary<string, long> counts = queryEngine.Delete(stmt, kq);
         Assert.AreEqual(1L, counts["write"]);
         Record record = this.client.Get(null, key);
         Assert.Null(record);
     }
 }
Esempio n. 14
0
        private void RunQuery(AerospikeClient client, Arguments args, string indexName, string binName, string valuePrefix)
        {
            string filter = valuePrefix + 3;

            console.Info("Query for: ns={0} set={1} index={2} bin={3} filter={4}",
                args.ns, args.set, indexName, binName, filter);

            Statement stmt = new Statement();
            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetBinNames(binName);
            stmt.SetFilters(Filter.Equal(binName, filter));

            RecordSet rs = client.Query(null, stmt);

            try
            {
                int count = 0;

                while (rs.Next())
                {
                    Key key = rs.Key;
                    Record record = rs.Record;
                    string result = (string)record.GetValue(binName);

                    if (result.Equals(filter))
                    {
                        console.Info("Record found: namespace={0} set={1} digest={2} bin={3} value={4}",
                            key.ns, key.setName, ByteUtil.BytesToHexString(key.digest), binName, result);
                    }
                    else
                    {
                        console.Error("Query mismatch: Expected {0}. Received {1}.", filter, result);
                    }
                    count++;
                }

                if (count == 0)
                {
                    console.Error("Query failed. No records returned.");
                }
            }
            finally
            {
                rs.Close();
            }
        }
        public QueryExecutor(Cluster cluster, QueryPolicy policy, Statement statement)
        {
            this.policy = policy;
            this.statement = statement;
            this.cancel = new CancellationTokenSource();

            this.nodes = cluster.Nodes;

            if (this.nodes.Length == 0)
            {
                throw new AerospikeException(ResultCode.SERVER_NOT_AVAILABLE, "Query failed because cluster is empty.");
            }

            this.threads = new QueryThread[nodes.Length];

            // Initialize maximum number of nodes to query in parallel.
            this.maxConcurrentNodes = (policy.maxConcurrentNodes == 0 || policy.maxConcurrentNodes >= threads.Length) ? threads.Length : policy.maxConcurrentNodes;
        }
 public void selectOneWitKey()
 {
     Statement stmt = new Statement();
     stmt.Namespace = TestQueryEngine.NAMESPACE;
     stmt.SetName = TestQueryEngine.SET_NAME;
     KeyQualifier kq = new KeyQualifier(Value.Get("selector-test:3"));
     KeyRecordEnumerator it = queryEngine.Select(stmt, kq);
     int count = 0;
     while (it.MoveNext())
     {
         KeyRecord rec = it.Current;
         count++;
         //			System.out.println(rec);
     }
     it.Close();
     //		System.out.println(count);
     Assert.Equals(1, count);
 }
Esempio n. 17
0
        public void selectByKey()
        {
            // Execute the Query
            Statement stmt = new Statement();
            stmt.Namespace = TestQueryEngine.NAMESPACE;
            stmt.SetName = SET;
            stmt.SetFilters(Filter.Contains(mapBin, IndexCollectionType.MAPKEYS, "dogs7"));
            int count = 0;
            RecordSet recordSet = client.Query(null, stmt);
            try
            {
                Console.WriteLine("\nRecords with map keys equal to dogs7:");
                while (recordSet != null & recordSet.Next())
                {
                    //Console.WriteLine("\t" + recordSet.getKey().userKey);
                    count++;
                }
            }
            finally
            {
                if (recordSet != null) recordSet.Close();
            }
            Console.WriteLine("\t" + count);

            Qualifier qual1 = new Qualifier(mapBin, Qualifier.FilterOperation.MAP_KEYS_CONTAINS, Value.Get("dogs7"));
            KeyRecordEnumerator it = queryEngine.Select(stmt, qual1);
            int count2 = 0;
            try
            {
                while (it.MoveNext())
                {
                    KeyRecord rec = it.Current;
                    Dictionary<string, Object> map = (Dictionary<string, Object>)rec.record.GetMap(mapBin);
                    Assert.True(map.ContainsKey("dogs7"));
                    count2++;
                }
            }
            finally
            {
                it.Close();
            }
            Assert.Equals(count, count2);
        }
        public void QuerySum()
        {
            int begin = 4;
            int end = 7;

            Statement stmt = new Statement();
            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetBinNames(binName);
            stmt.SetFilters(Filter.Range(binName, begin, end));
            stmt.SetAggregateFunction(Assembly.GetExecutingAssembly(), "Aerospike.Test.Resources.sum_example.lua", "sum_example", "sum_single_bin", Value.Get(binName));

            ResultSet rs = client.QueryAggregate(null, stmt);

            try
            {
                int expected = 22; // 4 + 5 + 6 + 7
                int count = 0;

                while (rs.Next())
                {
                    object obj = rs.Object;
                    long sum = 0;

                    if (obj is long)
                    {
                        sum = (long)rs.Object;
                    }
                    else
                    {
                        Assert.Fail("Return value not a long: " + obj);
                    }
                    Assert.AreEqual(expected, (int)sum);
                    count++;
                }
                Assert.AreNotEqual(0, count);
            }
            finally
            {
                rs.Close();
            }
        }
        private void RunQuery(AerospikeClient client, Arguments args, string indexName, string binName)
        {
            int begin = 14;
            int end = 18;

            console.Info("Query for: ns={0} set={1} index={2} bin={3} >= {4} <= {5}",
                args.ns, args.set, indexName, binName, begin, end);

            Statement stmt = new Statement();
            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetBinNames(binName);
            stmt.SetFilters(Filter.Range(binName, begin, end));

            RecordSet rs = client.Query(null, stmt);

            try
            {
                int count = 0;

                while (rs.Next())
                {
                    Key key = rs.Key;
                    Record record = rs.Record;
                    long result = record.GetLong(binName);

                    console.Info("Record found: namespace={0} set={1} digest={2} bin={3} value={4}",
                        key.ns, key.setName, ByteUtil.BytesToHexString(key.digest), binName, result);

                    count++;
                }

                if (count != 5)
                {
                    console.Error("Query count mismatch. Expected 5. Received " + count);
                }
            }
            finally
            {
                rs.Close();
            }
        }
        public AsyncQueryExecutor(AsyncCluster cluster, QueryPolicy policy, RecordSequenceListener listener, Statement statement)
        {
            this.listener = listener;
            statement.Prepare(true);

            Node[] nodes = cluster.Nodes;
            if (nodes.Length == 0)
            {
                throw new AerospikeException(ResultCode.SERVER_NOT_AVAILABLE, "Query failed because cluster is empty.");
            }

            // Create commands.
            AsyncQuery[] tasks = new AsyncQuery[nodes.Length];
            int count = 0;

            foreach (Node node in nodes)
            {
                tasks[count++] = new AsyncQuery(this, cluster, (AsyncNode)node, policy, listener, statement);
            }
            // Dispatch commands to nodes.
            Execute(tasks, policy.maxConcurrentNodes);
        }
        public void QueryAverage()
        {
            Statement stmt = new Statement();
            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetFilters(Filter.Range(binName, 0, 1000));
            stmt.SetAggregateFunction(Assembly.GetExecutingAssembly(), "Aerospike.Test.Resources.average_example.lua", "average_example", "average");

            ResultSet rs = client.QueryAggregate(null, stmt);

            try
            {
                if (rs.Next())
                {
                    object obj = rs.Object;

                    if (obj is IDictionary)
                    {
                        IDictionary map = (IDictionary)obj;
                        long sum = (long)map["sum"];
                        long count = (long)map["count"];
                        double avg = (double) sum / count;
                        Assert.AreEqual(5.5, avg, 0.00000001);
                    }
                    else
                    {
                        Assert.Fail("Unexpected object returned: " + obj);
                    }
                }
                else
                {
                    Assert.Fail("Query Assert.Failed. No records returned.");
                }
            }
            finally
            {
                rs.Close();
            }
        }
Esempio n. 22
0
        protected internal void SetQuery(Policy policy, Statement statement, bool write)
        {
            byte[] functionArgBuffer = null;
            int fieldCount = 0;
            int filterSize = 0;
            int binNameSize = 0;

            Begin();

            if (statement.ns != null)
            {
                dataOffset += ByteUtil.EstimateSizeUtf8(statement.ns) + FIELD_HEADER_SIZE;
                fieldCount++;
            }

            if (statement.indexName != null)
            {
                dataOffset += ByteUtil.EstimateSizeUtf8(statement.indexName) + FIELD_HEADER_SIZE;
                fieldCount++;
            }

            if (statement.setName != null)
            {
                dataOffset += ByteUtil.EstimateSizeUtf8(statement.setName) + FIELD_HEADER_SIZE;
                fieldCount++;
            }

            // Allocate space for TaskId field.
            dataOffset += 8 + FIELD_HEADER_SIZE;
            fieldCount++;

            if (statement.filters != null)
            {
                if (statement.filters.Length >= 1)
                {
                    IndexCollectionType type = statement.filters[0].CollectionType;

                    if (type != IndexCollectionType.DEFAULT)
                    {
                        dataOffset += FIELD_HEADER_SIZE + 1;
                        fieldCount++;
                    }
                }

                dataOffset += FIELD_HEADER_SIZE;
                filterSize++; // num filters

                foreach (Filter filter in statement.filters)
                {
                    filterSize += filter.EstimateSize();
                }
                dataOffset += filterSize;
                fieldCount++;

                // Query bin names are specified as a field (Scan bin names are specified later as operations)
                if (statement.binNames != null)
                {
                    dataOffset += FIELD_HEADER_SIZE;
                    binNameSize++; // num bin names

                    foreach (string binName in statement.binNames)
                    {
                        binNameSize += ByteUtil.EstimateSizeUtf8(binName) + 1;
                    }
                    dataOffset += binNameSize;
                    fieldCount++;
                }
            }
            else
            {
                // Calling query with no filters is more efficiently handled by a primary index scan.
                // Estimate scan options size.
                dataOffset += 2 + FIELD_HEADER_SIZE;
                fieldCount++;
            }

            if (statement.functionName != null)
            {
                dataOffset += FIELD_HEADER_SIZE + 1; // udf type
                dataOffset += ByteUtil.EstimateSizeUtf8(statement.packageName) + FIELD_HEADER_SIZE;
                dataOffset += ByteUtil.EstimateSizeUtf8(statement.functionName) + FIELD_HEADER_SIZE;

                if (statement.functionArgs.Length > 0)
                {
                    functionArgBuffer = Packer.Pack(statement.functionArgs);
                }
                else
                {
                    functionArgBuffer = new byte[0];
                }
                dataOffset += FIELD_HEADER_SIZE + functionArgBuffer.Length;
                fieldCount += 4;
            }

            if (statement.filters == null)
            {
                if (statement.binNames != null)
                {
                    foreach (string binName in statement.binNames)
                    {
                        EstimateOperationSize(binName);
                    }
                }
            }

            SizeBuffer();
            int operationCount = (statement.filters == null && statement.binNames != null) ? statement.binNames.Length : 0;

            if (write)
            {
                WriteHeader((WritePolicy)policy, Command.INFO1_READ, Command.INFO2_WRITE, fieldCount, operationCount);
            }
            else
            {
                WriteHeader(policy, Command.INFO1_READ, 0, fieldCount, operationCount);
            }

            if (statement.ns != null)
            {
                WriteField(statement.ns, FieldType.NAMESPACE);
            }

            if (statement.indexName != null)
            {
                WriteField(statement.indexName, FieldType.INDEX_NAME);
            }

            if (statement.setName != null)
            {
                WriteField(statement.setName, FieldType.TABLE);
            }

            // Write taskId field
            WriteFieldHeader(8, FieldType.TRAN_ID);
            ByteUtil.LongToBytes(statement.taskId, dataBuffer, dataOffset);
            dataOffset += 8;

            if (statement.filters != null)
            {
                if (statement.filters.Length >= 1)
                {
                    IndexCollectionType type = statement.filters[0].CollectionType;

                    if (type != IndexCollectionType.DEFAULT)
                    {
                        WriteFieldHeader(1, FieldType.INDEX_TYPE);
                        dataBuffer[dataOffset++] = (byte)type;
                    }
                }

                WriteFieldHeader(filterSize, FieldType.INDEX_RANGE);
                dataBuffer[dataOffset++] = (byte)statement.filters.Length;

                foreach (Filter filter in statement.filters)
                {
                    dataOffset = filter.Write(dataBuffer, dataOffset);
                }

                // Query bin names are specified as a field (Scan bin names are specified later as operations)
                if (statement.binNames != null)
                {
                    WriteFieldHeader(binNameSize, FieldType.QUERY_BINLIST);
                    dataBuffer[dataOffset++] = (byte)statement.binNames.Length;

                    foreach (string binName in statement.binNames)
                    {
                        int len = ByteUtil.StringToUtf8(binName, dataBuffer, dataOffset + 1);
                        dataBuffer[dataOffset] = (byte)len;
                        dataOffset += len + 1;
                    }
                }
            }
            else
            {
                // Calling query with no filters is more efficiently handled by a primary index scan.
                WriteFieldHeader(2, FieldType.SCAN_OPTIONS);
                byte priority = (byte)policy.priority;
                priority <<= 4;
                dataBuffer[dataOffset++] = priority;
                dataBuffer[dataOffset++] = (byte)100;
            }

            if (statement.functionName != null)
            {
                WriteFieldHeader(1, FieldType.UDF_OP);
                dataBuffer[dataOffset++] = (statement.returnData) ? (byte)1 : (byte)2;
                WriteField(statement.packageName, FieldType.UDF_PACKAGE_NAME);
                WriteField(statement.functionName, FieldType.UDF_FUNCTION);
                WriteField(functionArgBuffer, FieldType.UDF_ARGLIST);
            }

            // Scan bin names are specified after all fields.
            if (statement.filters == null)
            {
                if (statement.binNames != null)
                {
                    foreach (string binName in statement.binNames)
                    {
                        WriteOperation(binName, Operation.Type.READ);
                    }
                }
            }
            End();
        }
        private void RunQuery(AerospikeClient client, Arguments args, string indexName, string binName)
        {
            string nameFilter = "Bill";
            string passFilter = "hknfpkj";

            console.Info("Query for: ns=%s set=%s index=%s name=%s pass=%s", args.ns, args.set, indexName, nameFilter, passFilter);

            Statement stmt = new Statement();
            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetFilters(Filter.Equal(binName, nameFilter));
            stmt.SetAggregateFunction("filter_example", "profile_filter", Value.Get(passFilter));

            // passFilter will be applied in filter_example.lua.
            ResultSet rs = client.QueryAggregate(null, stmt);

            try
            {
                int count = 0;

                while (rs.Next())
                {
                    Dictionary<object, object> map = (Dictionary<object, object>)rs.Object;
                    Validate(map, "name", nameFilter);
                    Validate(map, "password", passFilter);
                    count++;
                }

                if (count == 0)
                {
                    console.Error("Query failed. No records returned.");
                }
            }
            finally
            {
                rs.Close();
            }
        }
        private void RunQueryExecute(AerospikeClient client, Arguments args, string indexName, string binName1, string binName2)
        {
            int begin = 3;
            int end = 9;

            console.Info("For ns={0} set={1} index={2} bin={3} >= {4} <= {5}", args.ns, args.set, indexName, binName1, begin, end);
            console.Info("Even integers: add 100 to existing " + binName1);
            console.Info("Multiple of 5: delete " + binName2 + " bin");
            console.Info("Multiple of 9: delete record");

            Statement stmt = new Statement();
            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetFilters(Filter.Range(binName1, begin, end));

            ExecuteTask task = client.Execute(args.writePolicy, stmt, "record_example", "processRecord", Value.Get(binName1), Value.Get(binName2), Value.Get(100));
            task.Wait();
        }
        private void ValidateRecords(AerospikeClient client, Arguments args, string indexName, string binName1, string binName2, int size)
        {
            int begin = 1;
            int end = size + 100;

            console.Info("Validate records");

            Statement stmt = new Statement();
            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetFilters(Filter.Range(binName1, begin, end));

            RecordSet rs = client.Query(null, stmt);

            try
            {
                int[] expectedList = new int[] {1,2,3,104,5,106,7,108,-1,10};
                int expectedSize = size - 1;
                int count = 0;

                while (rs.Next())
                {
                    Key key = rs.Key;
                    Record record = rs.Record;
                    object value1 = null;
                    object value2 = null;

                    record.bins.TryGetValue(binName1, out value1);
                    record.bins.TryGetValue(binName2, out value2);

                    console.Info("Record found: ns={0} set={1} bin1={2} value1={3} bin2={4} value2={5}",
                        key.ns, key.setName, binName1, value1, binName2, value2);

                    if (value1 == null)
                    {
                        console.Error("Data mismatch. value1 is null");
                        break;
                    }
                    long val1 = (long)value1;

                    if (val1 == 9)
                    {
                        console.Error("Data mismatch. value1 " + val1 + " should not exist");
                        break;
                    }

                    if (val1 == 5)
                    {
                        if (value2 != null)
                        {
                            console.Error("Data mismatch. value2 " + value2 + " should be null");
                            break;
                        }
                    }
                    else
                    {
                        long val2 = (long)value2;

                        if (val1 != expectedList[val2 - 1])
                        {
                            console.Error("Data mismatch. Expected " + expectedList[val2 - 1] + ". Received " + value1);
                            break;
                        }
                    }
                    count++;
                }

                if (count != expectedSize)
                {
                    console.Error("Query count mismatch. Expected " + expectedSize + ". Received " + count);
                }
            }
            finally
            {
                rs.Close();
            }
        }
            public void OnSuccess(Key key)
            {
                int rows = Interlocked.Increment(ref count);

                if (rows == size)
                {
                    int begin = 26;
                    int end = 34;

                    Statement stmt = new Statement();
                    stmt.SetNamespace(args.ns);
                    stmt.SetSetName(args.set);
                    stmt.SetBinNames(binName);
                    stmt.SetFilters(Filter.Range(binName, begin, end));

                    client.Query(null, new RecordSequenceHandler(parent), stmt);
                }
            }
        private void RunQuery(AerospikeClient client, Arguments args, string binName, string binName2, IndexCollectionType indexType)
        {
            console.Info("Query for: ns={0} set={1} bin={2} {3} within <region>", args.ns, args.set, binName, indexType.ToString());

            StringBuilder rgnsb = GenerateQueryRegion();

            Statement stmt = new Statement();
            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetFilters(Filter.GeoWithinRegion(binName, indexType, rgnsb.ToString()));

            RecordSet rs = client.Query(null, stmt);

            try
            {
                int count = 0;
                HashSet<string> uniques = new HashSet<string>();

                while (rs.Next())
                {
                    Record record = rs.Record;
                    string val = record.GetString(binName2);
                    uniques.Add(val);
                    count++;
                }

                if (count != 697)
                {
                    console.Error("Query failed. {0} records expected. {1} returned.", 697, count);
                }
                else if (uniques.Count != 21)
                {
                    console.Error("Query failed. {0} unique records expected. {1} unique returned.", 21, uniques.Count);
                }
                else
                {
                    console.Info("query succeeded with {0} records {1} unique", count, uniques.Count);
                }
            }
            finally
            {
                rs.Close();
            }
        }
        public static void PurgeDay(string date, AerospikeClient client)
        {
            Console.WriteLine ("Purgin data for {0}", date);
            Stopwatch stopwatch = new Stopwatch ();
            stopwatch.Start ();
            Statement statement = new Statement ();
            statement.Namespace = ns;
            statement.SetName = seqSet;

            //statement.SetFilters (Filter.Equal (dayBinName, date));

            ExecuteTask task = client.Execute (null, statement, "utility", "purge",
                Value.Get(dayBinName),
                Value.Get(date));

            while (!task.IsDone())
                Thread.Sleep (100);
            stopwatch.Stop ();
            Console.WriteLine ("Purge completed {0} ms", stopwatch.ElapsedMilliseconds);
        }
        private void RunQuery(AerospikeClient client, Arguments args, string indexName, string binName)
        {
            console.Info("Query for:ns={0} set={1} index={2} bin={3}",
                args.ns, args.set, indexName, binName);

            Statement stmt = new Statement();
            stmt.SetNamespace(args.ns);
            stmt.SetSetName(args.set);
            stmt.SetFilters(Filter.Equal(binName, 1));

            ResultSet rs = client.QueryAggregate(null, stmt, "average_example", "average");

            try
            {
                if (rs.Next())
                {
                    object obj = rs.Object;

                    if (obj is Dictionary<object,object>)
                    {
                        Dictionary<object, object> map = (Dictionary<object, object>)obj;
                        object objsum = map["sum"];
                        object objcount = map["count"];
                        double sum = (double)(long)objsum;
                        double count = (double)(long)objcount;
                        double avg = sum / count;
                        console.Info("Sum=" + sum + " Count=" + count + " Average=" + avg);

                        double expected = 5.5;
                        if (avg != expected)
                        {
                            console.Error("Data mismatch: Expected {0}. Received {1}.", expected, avg);
                        }
                    }
                    else
                    {
                        console.Error("Unexpected object returned: " + obj);
                    }
                }
                else
                {
                    console.Error("Query failed. No records returned.");
                }
            }
            finally
            {
                rs.Close();
            }
        }
Esempio n. 30
0
        protected internal void SetQuery(Policy policy, Statement statement, bool write)
        {
            byte[] functionArgBuffer = null;
            int    fieldCount        = 0;
            int    filterSize        = 0;
            int    binNameSize       = 0;

            Begin();

            if (statement.ns != null)
            {
                dataOffset += ByteUtil.EstimateSizeUtf8(statement.ns) + FIELD_HEADER_SIZE;
                fieldCount++;
            }

            if (statement.indexName != null)
            {
                dataOffset += ByteUtil.EstimateSizeUtf8(statement.indexName) + FIELD_HEADER_SIZE;
                fieldCount++;
            }

            if (statement.setName != null)
            {
                dataOffset += ByteUtil.EstimateSizeUtf8(statement.setName) + FIELD_HEADER_SIZE;
                fieldCount++;
            }

            // Allocate space for TaskId field.
            dataOffset += 8 + FIELD_HEADER_SIZE;
            fieldCount++;

            if (statement.filter != null)
            {
                IndexCollectionType type = statement.filter.CollectionType;

                if (type != IndexCollectionType.DEFAULT)
                {
                    dataOffset += FIELD_HEADER_SIZE + 1;
                    fieldCount++;
                }

                dataOffset += FIELD_HEADER_SIZE;
                filterSize++;                 // num filters
                filterSize += statement.filter.EstimateSize();
                dataOffset += filterSize;
                fieldCount++;

                // Query bin names are specified as a field (Scan bin names are specified later as operations)
                if (statement.binNames != null && statement.binNames.Length > 0)
                {
                    dataOffset += FIELD_HEADER_SIZE;
                    binNameSize++;                     // num bin names

                    foreach (string binName in statement.binNames)
                    {
                        binNameSize += ByteUtil.EstimateSizeUtf8(binName) + 1;
                    }
                    dataOffset += binNameSize;
                    fieldCount++;
                }
            }
            else
            {
                // Calling query with no filters is more efficiently handled by a primary index scan.
                // Estimate scan options size.
                dataOffset += 2 + FIELD_HEADER_SIZE;
                fieldCount++;

                // Estimate scan timeout size.
                dataOffset += 4 + FIELD_HEADER_SIZE;
                fieldCount++;
            }

            PredExp[] predExp  = statement.PredExp;
            int       predSize = 0;

            if (predExp != null)
            {
                dataOffset += FIELD_HEADER_SIZE;
                predSize    = PredExp.EstimateSize(predExp);
                dataOffset += predSize;
                fieldCount++;
            }

            if (statement.functionName != null)
            {
                dataOffset += FIELD_HEADER_SIZE + 1;                 // udf type
                dataOffset += ByteUtil.EstimateSizeUtf8(statement.packageName) + FIELD_HEADER_SIZE;
                dataOffset += ByteUtil.EstimateSizeUtf8(statement.functionName) + FIELD_HEADER_SIZE;

                if (statement.functionArgs.Length > 0)
                {
                    functionArgBuffer = Packer.Pack(statement.functionArgs);
                }
                else
                {
                    functionArgBuffer = new byte[0];
                }
                dataOffset += FIELD_HEADER_SIZE + functionArgBuffer.Length;
                fieldCount += 4;
            }

            if (statement.filter == null)
            {
                if (statement.binNames != null)
                {
                    foreach (string binName in statement.binNames)
                    {
                        EstimateOperationSize(binName);
                    }
                }
            }

            SizeBuffer();
            int operationCount = (statement.filter == null && statement.binNames != null) ? statement.binNames.Length : 0;

            if (write)
            {
                WriteHeader((WritePolicy)policy, Command.INFO1_READ, Command.INFO2_WRITE, fieldCount, operationCount);
            }
            else
            {
                QueryPolicy qp       = (QueryPolicy)policy;
                int         readAttr = qp.includeBinData ? Command.INFO1_READ : Command.INFO1_READ | Command.INFO1_NOBINDATA;
                WriteHeader(policy, readAttr, 0, fieldCount, operationCount);
            }

            if (statement.ns != null)
            {
                WriteField(statement.ns, FieldType.NAMESPACE);
            }

            if (statement.indexName != null)
            {
                WriteField(statement.indexName, FieldType.INDEX_NAME);
            }

            if (statement.setName != null)
            {
                WriteField(statement.setName, FieldType.TABLE);
            }

            // Write taskId field
            WriteFieldHeader(8, FieldType.TRAN_ID);
            ByteUtil.LongToBytes(statement.taskId, dataBuffer, dataOffset);
            dataOffset += 8;

            if (statement.filter != null)
            {
                IndexCollectionType type = statement.filter.CollectionType;

                if (type != IndexCollectionType.DEFAULT)
                {
                    WriteFieldHeader(1, FieldType.INDEX_TYPE);
                    dataBuffer[dataOffset++] = (byte)type;
                }

                WriteFieldHeader(filterSize, FieldType.INDEX_RANGE);
                dataBuffer[dataOffset++] = (byte)1;
                dataOffset = statement.filter.Write(dataBuffer, dataOffset);

                // Query bin names are specified as a field (Scan bin names are specified later as operations)
                if (statement.binNames != null && statement.binNames.Length > 0)
                {
                    WriteFieldHeader(binNameSize, FieldType.QUERY_BINLIST);
                    dataBuffer[dataOffset++] = (byte)statement.binNames.Length;

                    foreach (string binName in statement.binNames)
                    {
                        int len = ByteUtil.StringToUtf8(binName, dataBuffer, dataOffset + 1);
                        dataBuffer[dataOffset] = (byte)len;
                        dataOffset            += len + 1;
                    }
                }
            }
            else
            {
                // Calling query with no filters is more efficiently handled by a primary index scan.
                WriteFieldHeader(2, FieldType.SCAN_OPTIONS);
                byte priority = (byte)policy.priority;
                priority <<= 4;

                if (!write && ((QueryPolicy)policy).failOnClusterChange)
                {
                    priority |= 0x08;
                }

                dataBuffer[dataOffset++] = priority;
                dataBuffer[dataOffset++] = (byte)100;

                // Write scan timeout
                WriteFieldHeader(4, FieldType.SCAN_TIMEOUT);
                dataOffset += ByteUtil.IntToBytes((uint)policy.socketTimeout, dataBuffer, dataOffset);
            }

            if (predExp != null)
            {
                WriteFieldHeader(predSize, FieldType.PREDEXP);
                dataOffset = PredExp.Write(predExp, dataBuffer, dataOffset);
            }

            if (statement.functionName != null)
            {
                WriteFieldHeader(1, FieldType.UDF_OP);
                dataBuffer[dataOffset++] = (statement.returnData) ? (byte)1 : (byte)2;
                WriteField(statement.packageName, FieldType.UDF_PACKAGE_NAME);
                WriteField(statement.functionName, FieldType.UDF_FUNCTION);
                WriteField(functionArgBuffer, FieldType.UDF_ARGLIST);
            }

            // Scan bin names are specified after all fields.
            if (statement.filter == null)
            {
                if (statement.binNames != null)
                {
                    foreach (string binName in statement.binNames)
                    {
                        WriteOperation(binName, Operation.Type.READ);
                    }
                }
            }
            End();
        }
 //-------------------------------------------------------
 // Query Operations
 //-------------------------------------------------------
 /// <summary>
 /// Asynchronously execute query on all server nodes.  The query policy's 
 /// <code>maxConcurrentNodes</code> dictate how many nodes can be queried in parallel.
 /// The default is to query all nodes in parallel.
 /// <para>
 /// This method schedules the node's query commands with channel selectors and returns.
 /// Selector threads will process the commands and send the results to the listener.
 /// </para>
 /// </summary>
 /// <param name="policy">query configuration parameters, pass in null for defaults</param>
 /// <param name="listener">where to send results</param>
 /// <param name="statement">database query command parameters</param>
 /// <exception cref="AerospikeException">if query fails</exception>
 public void Query(QueryPolicy policy, RecordSequenceListener listener, Statement statement)
 {
     if (policy == null)
     {
         policy = queryPolicyDefault;
     }
     new AsyncQueryExecutor(cluster, policy, listener, statement);
 }
Esempio n. 32
0
        public AsyncQueryExecutor(AsyncCluster cluster, QueryPolicy policy, RecordSequenceListener listener, Statement statement)
        {
            this.listener = listener;
            statement.Prepare(true);

            Node[] nodes = cluster.Nodes;
            if (nodes.Length == 0)
            {
                throw new AerospikeException(ResultCode.SERVER_NOT_AVAILABLE, "Query failed because cluster is empty.");
            }

            // Create commands.
            AsyncQuery[] tasks = new AsyncQuery[nodes.Length];
            int          count = 0;

            foreach (Node node in nodes)
            {
                tasks[count++] = new AsyncQuery(this, cluster, (AsyncNode)node, policy, listener, statement);
            }
            // Dispatch commands to nodes.
            Execute(tasks, policy.maxConcurrentNodes);
        }