Query records by using record's metadata (lut) and filter expressions

Hi @kuskmen ,

I have reviewed the post and I first wanted to ensure this wasn’t an issue with the C# driver or server.

Based on my understanding of what you are trying to accomplish, I wrote up a C# program in LINQPad (using the Aerospike LINQPad driver) to emulate the behavior you are looking for… It is similar to what @pgupta has provided in his Java sample but using DateTime objects.

After running this sample, the behavior I believe you are looking for seems to work correctly.

After reviewing your provided code snippets, I noticed a possible issue.

In the method CreateRange it is using DateTime.Today (e.g., ((DateTime.Today - _unixEpoch).TotalNanoseconds). DateTime.ToDate returns the local date. To properly calculate a Unix Epoch date/time, this value should be UTC. Maybe you should be using DateTime.Today.ToUniversalTime()?

If this hasn’t resolved the issue, please feel free to provide additional details and I will try to help out…

Thanks

void Main()
{
	var client = ASClient; //use the LINQPad driver connection
	var key1 = new Key("test", "testset", "key1");
	var key2 = new Key("test", "testset", "key2");
	var key3 = new Key("test", "testset", "key3");
	var wPolicy = new WritePolicy()
	{
		sendKey = true
	};
	var b0 = new Bin("b00", Value.Get(123));
	var b1 = new Bin("b01", Value.Get("abc"));

	var epochTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

	var start1Epoch = ((long)DateTime.UtcNow.Subtract(epochTime).TotalNanoseconds).Dump("start Key 1 time");
	Thread.Sleep(1);
	client.Put(wPolicy, key1, b0, b1);	
	var start2Epoch = ((long)DateTime.UtcNow.Subtract(epochTime).TotalNanoseconds).Dump("start Key 2 time");
	Thread.Sleep(1);
	client.Put(wPolicy, key2, b0, b1);
	Thread.Sleep(1);
	var start3Epoch = ((long)DateTime.UtcNow.Subtract(epochTime).TotalNanoseconds).Dump("start Key 3 time");
	client.Put(wPolicy, key3, b0, b1);
	Thread.Sleep(100);
	var endEpoch = ((long)DateTime.UtcNow.Subtract(epochTime).TotalNanoseconds).Dump("end time");

	client.Get(null, key1).Dump("key1", 0);
	client.Get(null, key2).Dump("key2", 0);
	client.Get(null, key3).Dump("key3", 0);

	var lutRangeExp = Exp.Build(Exp.And(
	   								Exp.LT(Exp.Val(start2Epoch), Exp.LastUpdate()),
	   								Exp.GT(Exp.Val(endEpoch), Exp.LastUpdate()))
 						);

	//Run SI query
	var stmt = new Statement()
	{
		Namespace = "test",
		SetName = "testset"
	};

	var qp = new QueryPolicy()
	{
		filterExp = lutRangeExp
	};

	var rs = client.Query(qp, stmt);

	while (rs.Next())
	{
		rs.Record.Dump("Result record");
		rs.Key.Dump("Result Key");
	}

	//Using as a Date (UTC)
	var toDay = ((long)DateTime.Today.ToUniversalTime().Subtract(epochTime).TotalNanoseconds).Dump("ToDay  UTC");
	var tomorrow = ((long)DateTime.Today.AddDays(1).ToUniversalTime().Subtract(epochTime).TotalNanoseconds).Dump("Tomorrow UTC");
	lutRangeExp = Exp.Build(Exp.And(
   								Exp.LE(Exp.Val(toDay), Exp.LastUpdate()),
   								Exp.GE(Exp.Val(tomorrow), Exp.LastUpdate()))
					);

	qp = new QueryPolicy()
	{
		filterExp = lutRangeExp
	};

	rs = client.Query(qp, stmt);

	while (rs.Next())
	{
		rs.Record.Dump("Result record Today and Tomorrow UTC");
		rs.Key.Dump("Result Key Today and Tomorrow UTC");
	}	
}