private void DeleteRecords(AerospikeClient client, Arguments args, string keyPrefix, int size) { for (int i = 0; i < size; i++) { Key key = new Key(args.ns, args.set, keyPrefix + i); client.Delete(args.writePolicy, key); } }
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(); }
private void RunQuery(AerospikeClient client, Arguments args, string indexName, string binName) { int begin = 4; int end = 7; 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)); ResultSet rs = client.QueryAggregate(null, stmt, "sum_example", "sum_single_bin", Value.Get(binName)); try { int expected = 22; // 4 + 5 + 6 + 7 int count = 0; while (rs.Next()) { object obj = rs.Object; if (obj is long) { long sum = (long)rs.Object; if (expected == (int)sum) { console.Info("Sum matched: value=" + expected); } else { console.Error("Sum mismatch: Expected {0}. Received {1}.", expected, sum); } } else { console.Error("Unexpected return value: " + obj); continue; } count++; } if (count == 0) { console.Error("Query failed. No records returned."); } } finally { rs.Close(); } }
private void CreateIndex(AerospikeClient client, Arguments args, string indexName, string binName) { console.Info("Create index: ns={0} set={1} index={2} bin={3}", 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.NUMERIC); task.Wait(); }
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(); } }
private void RunQuery(AerospikeClient client, Arguments args, string indexName, string binName) { StringBuilder rgnsb = new StringBuilder(); rgnsb.Append("{ "); rgnsb.Append(" \"type\": \"Polygon\", "); rgnsb.Append(" \"coordinates\": [ "); rgnsb.Append(" [[-122.500000, 37.000000],[-121.000000, 37.000000], "); rgnsb.Append(" [-121.000000, 38.080000],[-122.500000, 38.080000], "); rgnsb.Append(" [-122.500000, 37.000000]] "); rgnsb.Append(" ] "); rgnsb.Append(" } "); console.Info("QueryRegion: " + rgnsb); Statement stmt = new Statement(); stmt.SetNamespace(args.ns); stmt.SetSetName(args.set); stmt.SetBinNames(binName); stmt.SetFilters(Filter.GeoWithinRegion(binName, rgnsb.ToString())); RecordSet rs = client.Query(null, stmt); try { int count = 0; while (rs.Next()) { Key key = rs.Key; Record record = rs.Record; string result = record.GetGeoJSON(binName); console.Info("Record found: " + result); count++; } if (count != 6) { console.Error("Query count mismatch. Expected 6. Received " + count); } } finally { rs.Close(); } }
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(); } }
/// <summary> /// Query on a secondary index with a filter and then apply an additional filter in the /// user defined function. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { if (!args.hasUdf) { console.Info("Query functions are not supported by the connected Aerospike server."); return; } string indexName = "profileindex"; string keyPrefix = "profilekey"; string binName = args.GetBinName("name"); Register(client, args); CreateIndex(client, args, indexName, binName); WriteRecords(client, args, keyPrefix, binName); RunQuery(client, args, indexName, binName); client.DropIndex(args.policy, args.ns, args.set, indexName); }
private void RunQuery(AerospikeClient client, Arguments args, string indexName, string binName1, string binName2) { StringBuilder rgnsb = new StringBuilder(); rgnsb.Append("{ "); rgnsb.Append(" \"type\": \"Polygon\", "); rgnsb.Append(" \"coordinates\": [ "); rgnsb.Append(" [[-122.500000, 37.000000],[-121.000000, 37.000000], "); rgnsb.Append(" [-121.000000, 38.080000],[-122.500000, 38.080000], "); rgnsb.Append(" [-122.500000, 37.000000]] "); rgnsb.Append(" ] "); rgnsb.Append(" } "); console.Info("QueryRegion: " + rgnsb); string amenStr = "school"; Statement stmt = new Statement(); stmt.SetNamespace(args.ns); stmt.SetSetName(args.set); stmt.SetFilters(Filter.GeoWithinRegion(binName1, rgnsb.ToString())); stmt.SetAggregateFunction("geo_filter_example", "match_amenity", Value.Get(amenStr)); ResultSet rs = client.QueryAggregate(null, stmt); try { int count = 0; while (rs.Next()) { object result = rs.Object; console.Info("Record found: " + result); count++; } if (count != 2) { console.Error("Wrong number of schools found. %d != 2", count); } } 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(); }
/// <summary> /// Geospatial query examples. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { if (!args.hasGeo) { console.Info("Geospatial functions are not supported by the connected Aerospike server."); return; } string indexName = "queryindexloc"; string keyPrefix = "querykeyloc"; string binName = args.GetBinName("querybinloc"); int size = 20; CreateIndex(client, args, indexName, binName); WriteRecords(client, args, keyPrefix, binName, size); RunQuery(client, args, indexName, binName); RunRadiusQuery(client, args, indexName, binName); client.DropIndex(args.policy, args.ns, args.set, indexName); }
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(); } }
/// <summary> /// Apply user defined function on records that match the query filter. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { if (!args.hasUdf) { console.Info("Query functions are not supported by the connected Aerospike server."); return; } string indexName = "qeindex1"; string keyPrefix = "qekey"; string binName1 = args.GetBinName("qebin1"); string binName2 = args.GetBinName("qebin2"); int size = 10; Register(client, args); CreateIndex(client, args, indexName, binName1); WriteRecords(client, args, keyPrefix, binName1, binName2, size); RunQueryExecute(client, args, indexName, binName1, binName2); ValidateRecords(client, args, indexName, binName1, binName2, size); client.DropIndex(args.policy, args.ns, args.set, indexName); }
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 WriteRecords(AerospikeClient client, Arguments args, string keyPrefix, string binName, int size) { console.Info("Write " + size + " records."); for (int i = 1; i <= size; i++) { Key key = new Key(args.ns, args.set, keyPrefix + i); Bin bin = new Bin(binName, i); client.Put(args.writePolicy, key, bin); } }
private void WriteRecords(AerospikeClient client, Arguments args, string keyPrefix, string binName, int size) { console.Info("Write " + size + " records."); for (int i = 0; i < size; i++) { double lng = -122 + (0.1 * i); double lat = 37.5 + (0.1 * i); StringBuilder ptsb = new StringBuilder(); ptsb.Append("{ \"type\": \"Point\", \"coordinates\": ["); ptsb.Append(lng); ptsb.Append(", "); ptsb.Append(lat); ptsb.Append("] }"); Key key = new Key(args.ns, args.set, keyPrefix + i); Bin bin = Bin.AsGeoJSON(binName, ptsb.ToString()); client.Put(args.writePolicy, key, bin); } }
private void WriteRecords(AerospikeClient client, Arguments args, string keyPrefix, string binName) { WriteRecord(client, args, keyPrefix + 1, "Charlie", "cpass"); WriteRecord(client, args, keyPrefix + 2, "Bill", "hknfpkj"); WriteRecord(client, args, keyPrefix + 3, "Doug", "dj6554"); }
private void WriteMapRecords(AerospikeClient client, Arguments args, string keyPrefix, string binName, string binName2, string valuePrefix, int size) { for (int i = 0; i < size; i++) { Key key = new Key(args.ns, args.set, keyPrefix + i); Dictionary<string, Value> map = new Dictionary<string, Value>(); for (int jj = 0; jj < 10; ++jj) { double plat = 0.0 + (0.01 * i); double plng = 0.0 + (0.10 * jj); string geoString = GeneratePoint(plat, plng); map[valuePrefix + "pointkey_" + i + "_" + jj] = Value.GetAsGeoJSON(geoString); double rlat = 0.0 + (0.01 * i); double rlng = 0.0 - (0.10 * jj); geoString = GeneratePolygon(rlat, rlng); map[valuePrefix + "regionkey_" + i + "_" + jj] = Value.GetAsGeoJSON(geoString); } Bin bin = new Bin(binName, map); Bin bin2 = new Bin(binName2, "other_bin_value_" + i); client.Put(args.writePolicy, key, bin, bin2); } console.Info("Write " + size + " records."); }
private void Register(AerospikeClient client, Arguments args) { string packageName = "average_example.lua"; console.Info("Register: " + packageName); LuaExample.Register(client, args.policy, packageName); }
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(); } }
private void RunMapKeyExample(AerospikeClient client, Arguments args) { string indexName = "geo_mapkey"; string keyPrefix = "mapkey"; string mapValuePrefix = "mk"; string binName = "geo_mkey_bin"; string binName2 = "geo_uniq_bin"; int size = 1000; // create collection index on mapKey CreateIndex(client, args, IndexCollectionType.MAPKEYS, indexName, binName); WriteMapKeyRecords(client, args, keyPrefix, binName, binName2, mapValuePrefix, size); RunQuery(client, args, binName, binName2, IndexCollectionType.MAPKEYS); client.DropIndex(args.policy, args.ns, args.set, indexName); DeleteRecords(client,args, keyPrefix, size); }
public abstract void RunExample(Arguments args);
private void RunRadiusQuery(AerospikeClient client, Arguments args, string indexName, string binName) { double lon = -122.0; double lat = 37.5; double radius = 50000.0; console.Info("QueryRadius long=" + lon + " lat= " + lat + " radius=" + radius); Statement stmt = new Statement(); stmt.SetNamespace(args.ns); stmt.SetSetName(args.set); stmt.SetBinNames(binName); stmt.SetFilters(Filter.GeoWithinRadius(binName, lon, lat, radius)); RecordSet rs = client.Query(null, stmt); try { int count = 0; while (rs.Next()) { Key key = rs.Key; Record record = rs.Record; string result = record.GetGeoJSON(binName); console.Info("Record found: " + result); count++; } if (count != 4) { console.Error("Query count mismatch. Expected 4. Received " + count); } } finally { rs.Close(); } }
/// <summary> /// Create secondary index on a string bin and query on it. /// </summary> public override void RunExample(AerospikeClient client, Arguments args) { RunMapExample(client, args); RunMapKeyExample(client, args); RunListExample(client, args); }
private void WriteRecords(AerospikeClient client, Arguments args, string keyPrefix, int size) { for (int i = 1; i <= size; i++) { Key key = new Key(args.ns, args.set, keyPrefix + i); Bin bin = new Bin("l1", i); console.Info("Put: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin.name, bin.value); client.Put(args.writePolicy, key, bin, new Bin("l2", 1)); } }
private void RunListExample(AerospikeClient client, Arguments args) { string indexName = "geo_list"; string keyPrefix = "list"; string binName = "geo_list_bin"; string binName2 = "geo_uniq_bin"; int size = 1000; // create collection index on list CreateIndex(client, args, IndexCollectionType.LIST, indexName, binName); WriteListRecords(client, args, keyPrefix, binName, binName2, size); RunQuery(client, args, binName, binName2, IndexCollectionType.LIST); client.DropIndex(args.policy, args.ns, args.set, indexName); DeleteRecords(client, args, keyPrefix, size); }
private void Register(AerospikeClient client, Arguments args) { string packageName = "sum_example.lua"; console.Info("Register: " + packageName); LuaExample.Register(client, args.policy, packageName); }
private void WriteRecord(AerospikeClient client, Arguments args, string userKey, string name, string password) { Key key = new Key(args.ns, args.set, userKey); Bin bin1 = new Bin("name", name); Bin bin2 = new Bin("password", password); console.Info("Put: namespace={0} set={1} key={2} bin={3} value={4}", key.ns, key.setName, key.userKey, bin1.name, bin1.value); client.Put(args.writePolicy, key, bin1, bin2); }
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(); } }
private void WriteListRecords(AerospikeClient client, Arguments args, string keyPrefix, string binName, string binName2, int size) { for (int i = 0; i < size; i++) { Key key = new Key(args.ns, args.set, keyPrefix + i); List<Value> mylist = new List<Value>(); for (int jj = 0; jj < 10; ++jj) { double plat = 0.0 + (0.01 * i); double plng = 0.0 + (0.10 * jj); string geoString = GeneratePoint(plat, plng); mylist.Add(Value.GetAsGeoJSON(geoString)); double rlat = 0.0 + (0.01 * i); double rlng = 0.0 - (0.10 * jj); geoString = GeneratePolygon(rlat, rlng); mylist.Add(Value.GetAsGeoJSON(geoString)); } Bin bin = new Bin(binName, mylist); Bin bin2 = new Bin(binName2, "other_bin_value_" + i); client.Put(args.writePolicy, key, bin, bin2); } console.Info("Write " + size + " records."); }