private Filter(string name, IndexCollectionType type, Value begin, Value end)
 {
     this.name = name;
     this.type = type;
     this.begin = begin;
     this.end = end;
 }
 private Filter(string name, IndexCollectionType colType, int valType, Value begin, Value end)
 {
     this.name = name;
     this.colType = colType;
     this.valType = valType;
     this.begin = begin;
     this.end = end;
 }
        private void CreateIndex(AerospikeClient client, Arguments args, IndexCollectionType indexType, string indexName, string binName)
        {
            console.Info("Create GeoJSON {0} index: ns={1} set={2} index={3} bin={4}", indexType, args.ns, args.set, indexName, binName);

            Policy policy = new Policy();
            policy.timeout = 0; // Do not timeout on index create.
            IndexTask task = client.CreateIndex(policy, args.ns, args.set, indexName, binName, IndexType.GEO2DSPHERE, indexType);
            task.Wait();
        }
 /// <summary>
 /// Create contains string filter for query on collection index.
 /// </summary>
 /// <param name="name">bin name</param>
 /// <param name="type">index collection type</param>
 /// <param name="value">filter value</param>
 public static Filter Contains(string name, IndexCollectionType type, string value)
 {
     Value val = Value.Get(value);
     return new Filter(name, type, val.Type, val, val);
 }
 /// <summary>
 /// Create range filter for query on collection index.
 /// Range arguments must be longs or integers which can be cast to longs.
 /// String ranges are not supported.
 /// </summary>
 /// <param name="name">bin name</param>
 /// <param name="type">index collection type inclusive</param>
 /// <param name="begin">filter begin value inclusive</param>
 /// <param name="end">filter end value</param>
 public static Filter Range(string name, IndexCollectionType type, long begin, long end)
 {
     return new Filter(name, type, ParticleType.INTEGER, Value.Get(begin), Value.Get(end));
 }
 private Filter collectionRange(IndexCollectionType collectionType)
 {
     return Filter.Range(Field, collectionType, Value1.ToLong(), Value2.ToLong());
 }
 private Filter collectionContains(IndexCollectionType collectionType)
 {
     Value val = Value1;
     int valType = val.Type;
     switch (valType){
     case ParticleType.INTEGER:
         return Filter.Contains(Field, collectionType, val.ToLong());
     case ParticleType.STRING:
         return Filter.Contains(Field, collectionType, val.ToString());
     }
     return null;
 }
 /// <summary>
 /// Create geospatial "within region" filter for query on collection index.
 /// </summary>
 /// <param name="name">bin name</param>
 /// <param name="type">index collection type</param>
 /// <param name="region">GeoJSON region</param>
 public static Filter GeoWithinRegion(string name, IndexCollectionType type, string region)
 {
     return new Filter(name, type, ParticleType.GEOJSON, Value.Get(region), Value.Get(region));
 }
 /// <summary>
 /// Create range filter for query on collection index.
 /// Range arguments must be longs or integers which can be cast to longs.
 /// String ranges are not supported.
 /// </summary>
 /// <param name="name">bin name</param>
 /// <param name="type">index collection type</param>
 /// <param name="begin">filter begin value</param>
 /// <param name="end">filter end value</param>
 public static Filter Range(string name, IndexCollectionType type, long begin, long end)
 {
     return(new Filter(name, type, Value.Get(begin), Value.Get(end)));
 }
 /// <summary>
 /// Create geospatial "containing point" filter for query on collection index.
 /// </summary>
 /// <param name="name">bin name</param>
 /// <param name="type">index collection type</param>
 /// <param name="lon">longitude</param>
 /// <param name="lat">latitude</param>
 public static Filter GeoContains(string name, IndexCollectionType type, double lon, double lat)
 {
     string point = GeneratePoint(lat, lon);
     return new Filter(name, type, ParticleType.GEOJSON, Value.Get(point), Value.Get(point));
 }
 /// <summary>
 /// Create geospatial "containing point" filter for query on collection index.
 /// </summary>
 /// <param name="name">bin name</param>
 /// <param name="type">index collection type</param>
 /// <param name="point">GeoJSON point</param>
 public static Filter GeoContains(string name, IndexCollectionType type, string point)
 {
     return new Filter(name, type, ParticleType.GEOJSON, Value.Get(point), Value.Get(point));
 }
        private void createIndex(String indexName, String binName, IndexType indexType, IndexCollectionType collectionType)
        {
            // drop index
            client.DropIndex(null, TestQueryEngine.NAMESPACE, SET, indexName);

                Thread.Sleep(50);

            // create index
            IndexTask task = client.CreateIndex(null, TestQueryEngine.NAMESPACE, SET, indexName, binName, indexType, collectionType);
            task.Wait();
        }
        /// <summary>
        /// Create complex secondary index on bins containing collections.
        /// This asynchronous server call will return before command is complete.
        /// The user can optionally wait for command completion by using the returned
        /// IndexTask instance.
        /// <para>
        /// This method is only supported by Aerospike 3 servers.
        /// </para>
        /// </summary>
        /// <param name="policy">generic configuration parameters, pass in null for defaults</param>
        /// <param name="ns">namespace - equivalent to database name</param>
        /// <param name="setName">optional set name - equivalent to database table</param>
        /// <param name="indexName">name of secondary index</param>
        /// <param name="binName">bin name that data is indexed on</param>
        /// <param name="indexType">underlying data type of secondary index</param>
        /// <param name="indexCollectionType">index collection type</param>
        /// <exception cref="AerospikeException">if index create fails</exception>
        public IndexTask CreateIndex(Policy policy, string ns, string setName, string indexName, string binName, IndexType indexType, IndexCollectionType indexCollectionType)
        {
            if (policy == null)
            {
                policy = writePolicyDefault;
            }
            StringBuilder sb = new StringBuilder(500);
            sb.Append("sindex-create:ns=");
            sb.Append(ns);

            if (setName != null && setName.Length > 0)
            {
                sb.Append(";set=");
                sb.Append(setName);
            }

            sb.Append(";indexname=");
            sb.Append(indexName);
            sb.Append(";numbins=1");

            if (indexCollectionType != IndexCollectionType.DEFAULT)
            {
                sb.Append(";indextype=");
                sb.Append(indexCollectionType);
            }

            sb.Append(";indexdata=");
            sb.Append(binName);
            sb.Append(",");
            sb.Append(indexType);
            sb.Append(";priority=normal");

            // Send index command to one node. That node will distribute the command to other nodes.
            String response = SendInfoCommand(policy, sb.ToString());

            if (response.Equals("OK", StringComparison.CurrentCultureIgnoreCase))
            {
                // Return task that could optionally be polled for completion.
                return new IndexTask(cluster, policy, ns, indexName);
            }

            if (response.StartsWith("FAIL:200"))
            {
                // Index has already been created.  Do not need to poll for completion.
                return new IndexTask();
            }

            throw new AerospikeException("Create index failed: " + response);
        }
        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.SetFilter(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();
            }
        }
        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();
            }
        }
 /// <summary>
 /// Create geospatial "within radius" filter for query on collection index.
 /// </summary>
 /// <param name="name">bin name</param>
 /// <param name="type">index collection type</param>
 /// <param name="lng">longitude</param>
 /// <param name="lat">latitude</param>
 /// <param name="radius">radius (meters)</param>
 public static Filter GeoWithinRadius(string name, IndexCollectionType type, double lng, double lat, double radius)
 {
     string rgnstr = string.Format("{{ \"type\": \"AeroCircle\", " + "\"coordinates\": [[{0:F8}, {1:F8}], {2:F}] }}", lng, lat, radius);
     return new Filter(name, type, ParticleType.GEOJSON, Value.Get(rgnstr), Value.Get(rgnstr));
 }
        /// <summary>
        /// Create contains string filter for query on collection index.
        /// </summary>
        /// <param name="name">bin name</param>
        /// <param name="type">index collection type</param>
        /// <param name="value">filter value</param>
        public static Filter Contains(string name, IndexCollectionType type, string value)
        {
            Value val = Value.Get(value);

            return(new Filter(name, type, val, val));
        }