Partial copy of dicts useful?

The use of specific JADE features and proposals for new feature suggestions
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Partial copy of dicts useful?

Postby ConvertFromOldNGs » Fri Aug 07, 2009 10:50 am

by Carl Ranson >> Sun, 16 Sep 2001 23:30:09 GMT

Hi all,

Just wanted to moot this idea and see if anyone though it had merit.

I've seen quite a few blocks of code that do an iteration on a dictionary based on *some* of its keys.

For instance you might have orders by Company, Date & OrderID and want to get all orders for a given company and date.

iter := dict.createIterator;
targetCompany := "Jade";
targetDate := "10/10/01".Date;
dict.startKeyGeq(targetCompany, targetDate, null, iter);
while iter.next(order) do
if order.myCompany.name <> targetCompany or order.date <> targetDate then
break;
endif;
// do stuff here.
endwhile;

The messy part is, of course, that you need to re-check those keys you're interested in each time through the loop.

This got me thinking about alternative approaches that would allow one to do a "Partial iteraton" on a dictionary...something along the lines of

foreach order in dict.whereKeys(targetCompany, targetDate) do
// do stuff.
endforeach;

The idea being that whereKeys takes a list of keys and matches to any entry that matches. If a key is not provided it matches all values. So whereKeys(targetCompany) is like saying
getAtKey(targetCompany, *any*, *any*)

perhaps this could also be a copy operation that works in a similar manner such as this:
dict.copyWhere(dict2, targetCompany) would copy all orders for that company.

I'd be keen to hear what people think of this one.

Cheers,
Carl Ranson
Unify Limited

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Partial copy of dicts useful?

Postby ConvertFromOldNGs » Fri Aug 07, 2009 10:50 am

by Graeme Joyce >> Sun, 16 Sep 2001 23:43:54 GMT
I'd be keen to hear what people think of this one.

Carl,

Good idea.

I've often written code like this. What you describe sounds very useful.

Graeme

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Partial copy of dicts useful?

Postby ConvertFromOldNGs » Fri Aug 07, 2009 10:50 am

by Craig Shearer >> Mon, 17 Sep 2001 0:06:09 GMT

Hi Carl

I certainly like this idea, and think it has great merit.

I also like it from the perspective that it is both efficient (in that it uses the dictionary keys rather than retrieving the property values from objects) and also that is reduces the possibity for error if developers write the following sort of code:

foreach order in dict where order.targetCompany = "Jade" and order.targetDate = "1/7/2001".Date do
// do stuff.
endforeach;

In the above code, there is no guarantee that the version of the Order object is actually the version whose keys were used to populate the dictionary. So, there is potential for erroneous execution if the cache happens to contain an old version of the Order object.

I also like the idea of the copy too.

Craig

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Partial copy of dicts useful?

Postby ConvertFromOldNGs » Fri Aug 07, 2009 10:51 am

by Carl Ranson >> Mon, 17 Sep 2001 1:25:40 GMT

One further extension I've thought of is that a range could be specified for the last key given.

so you could do a search for all orders in a given quarter for comany X by specifying the start and end dates of the quarter.

The nice thing about this is that it's consistent with the ISAM/B-Tree mechanism employed by jade and should be very efficient. Not sure what syntax I would use though to distinguish this case from the previous examples.

CR

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Partial copy of dicts useful?

Postby ConvertFromOldNGs » Fri Aug 07, 2009 10:51 am

by Carl Ranson >> Mon, 17 Sep 2001 1:34:52 GMT

In fact, now that I've had time to think about it a bit more....

A lot of the benifits could be achieved simply by adding an method to the iterator/dictionary.

iter.stopAtKey(x,x,....)

So you could do

orders.startAtKeyGeq("Jade", "1/10/00", null, Iter); orders.stopAtKeyGeq("Jade", "1/11/00", null, Iter);

then iter.next(order) would return false if the stopAt test succeeds.

Thus

orders.startAtKeyGeq("Jade", "1/10/00", null, Iter); orders.stopAtKeyGeq("Jade", "1/11/00", null, Iter);
while iter.next(order) do
// do suff - only orders in october for company "Jade" here. endwhile;

Would allow you to simply process all orders for a given key range. In theroy the only thing that would have to be changed in Jade is the addition of the stopAtKeyXXX methods and modifying the .next & .prev methods to check the end condition if applicable.

Cute eh?

CR

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Partial copy of dicts useful?

Postby ConvertFromOldNGs » Fri Aug 07, 2009 10:51 am

by Hugh McColl >> Mon, 17 Sep 2001 7:02:12 GMT

Hi Carl,

Good ideas! There is already an internal request for a capability similar to the one proposed below (for use in the query engine used by the report writer in 5.2). There have also been requests for a special case variant of this - to iterate all objects with equal keys where duplicate key entries exist. Such a feature would first need to be implemented at the dictionary / iterator level . The JADE foreach statement is just 'syntactical sugar' for an equivalent iterator loop.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Partial copy of dicts useful?

Postby ConvertFromOldNGs » Fri Aug 07, 2009 10:51 am

by Carl Ranson >> Mon, 17 Sep 2001 23:04:09 GMT

I have opened a NFS for this stuff:

thanks all for your input.

CR


Return to “Feature Discussions”

Who is online

Users browsing this forum: No registered users and 15 guests

cron