Re: String-with-number sort and insert order preservation

From: Florin T.PATRASCU (flo..ogers.com)
Date: Tue Feb 02 2010 - 21:39:16 EST

  • Next message: Andrus Adamchik: "Re: BUG: Cayenne Modeler"

    Hi Šabo,

    I don't have a pure Cayenne answer, but in case you can add an extra field to your table, then you can write a simple program to convert your IP addresses to numbers and save the numbers to the newly added field. Here is a simple Java method I am using to convert IPV4s to a long:

        public static long ipAddressToLong(String ipAddress) {
            String[] ipAddressArray = ipAddress.split("\\.");
            long num = 0;

            for (int i = 0; i < ipAddressArray.length; i++) {
                int power = 3 - i;
                num += ((Integer.parseInt(ipAddressArray[i]) % 256 * Math.pow(256, power)));
            }
            return num;
        }

    After this simple data transformation it will be very easy to sort your table.

    I hope it helps.

    V/r
    -florin

    On 02-Feb-10, at 19:45 , Marek Šabo wrote:

    > Hi all,
    >
    > I would like to ask you two questions:
    >
    > 1) Is there any way to sort varchar fields which happen to numbers, more
    > precisely ipv4s?
    > query.addOrdering(Ip4Address.IP_ADDRESS_PROPERTY, SortOrder.ASCENDING);
    > This of course sets the hundreds as first in line, any tips on some
    > custom sorting?
    >
    > 2) I was filling ip table with available ranges like this:
    >
    > ObjectContext oc = DataContext.getThreadObjectContext();
    >
    > for (int i = 33; i < 255; i++) {
    >
    > Ip4Address ip = oc.newObject(Ip4Address.class);
    >
    > ip.setIpAddress("XX.XX.XX." + i);
    >
    > ip.setUsed(Boolean.FALSE);
    >
    > ip.setNat(Boolean.FALSE);
    >
    > }
    >
    > but cayenne inserted rows like this (probably hashed order for
    > optimization):
    >
    > INFO - QueryLogger - [bind: 1->ipAddress:'XX.XX.XX.97', 2->nat:'false', 3->used:'false']
    >
    > INFO - QueryLogger - Generated PK: Ip4Address.id = 1
    >
    > INFO - QueryLogger - === updated 1 row.
    >
    > INFO - QueryLogger - [bind: 1->ipAddress:'XX.XX.XX.71', 2->nat:'false', 3->used:'false']
    >
    > INFO - QueryLogger - Generated PK: Ip4Address.id = 2
    >
    > INFO - QueryLogger - === updated 1 row.
    >
    >
    > Is there any way how to override this mechanism in order to preserve
    > order of inserted rows?
    >
    > TIA,
    >
    > --
    > Marek Šabo
    >



    This archive was generated by hypermail 2.0.0 : Tue Feb 02 2010 - 21:39:49 EST