Blog entries in category "Java" shown here:
See other entries in category :
Whateveah | Java | All
20060329 Wednesday March 29, 2006

Oracle Spatial Database

I ve been using some of Oracle 10g's new Spatial features and their Java Spatial API lately. The flexibility and features it provides for GIS type applications simply rocks. For example you can create a table and have a colum that is of type SDO_GEOMETRY and store the coordiates in terms of X,Y (like longitude and latitude) or other Geometric/GML data. The API allows you to then query that Spatially indexed colum in a variety of ways. For example you can use the JDBC API to query
 slect ... where sdo_filter (mycolumname ,sdo_geometry(2003,null,null,sdo_elem_info_array(1,1003,3),sdo_ordinate_array (0,0,600,800))) = 'TRUE' 
returns all the points as oracle.spatial.geometry.JGeometry objects that are in the MBR (mean bounding rectangle) from 0,0 to 600,800 ! With the set of coordinates in hand you can link them up and draw a route similar to what you see in Google or mapquest when you query for driving directions. Theres a bunch of other operators that allow you to insert/query a variety of geometic shapes for range and nearest neighbour information. Pretty cool ! ( Mar 29 2006, 10:40:34 PM EST ) Permalink Comments [0]
20060325 Saturday March 25, 2006

C# and SubMaps

It took me a while to figure out why my C# code wasnt working as expected today. After much debugging it turned out that C# doesnt have a clean SubMap implementation, or least one that I could find. What I was assuming will work wasnt quite cutting it.In Java you can easily get a subMap using string keys based on a regular expression magically. For example the code, Prints out something like...
TreeMap  sortedMap = new TreeMap ();
for(int i = 0;i<10;i++)
    sortedMap.put("SomeKey_"+i, "SomeValue");
System.out.println(sortedMap);
String low = "SomeKey_3", high = "SomeKey~";
System.out.println(low);
System.out.println(high); 

{SomeKey_0=SomeValue, SomeKey_1=SomeValue, SomeKey_2=SomeValue, SomeKey_3=SomeValue, SomeKey_4=SomeValue,
 SomeKey_5=SomeValue, SomeKey_6=SomeValue, SomeKey_7=SomeValue, SomeKey_8=SomeValue, SomeKey_9=SomeValue}
SomeKey_3
SomeKey~
{SomeKey_3=SomeValue, SomeKey_4=SomeValue, SomeKey_5=SomeValue, SomeKey_6=SomeValue, SomeKey_7=SomeValue, 
SomeKey_8=SomeValue, SomeKey_9=SomeValue}

The closest way of doing this in C# is a bit of a hack using the System.Collections.SortedList and the Comparer classes and its specific to what I was trying to do.I replace the regex ~ with a series of 9's.
 public static SortedList SubMap(SortedList list, System.Object lowerLimit, System.Object upperLimit) {
            string upperStr = upperLimit.ToString();
              if (upperStr.EndsWith("~"))
                upperLimit = upperStr.Replace("~", "999999999999999");
            Comparer comparer = Comparer.Default;
            SortedList newList = new SortedList();
            if (list != null){
                if ((list.Count > 0) &&(comparer.Compare(lowerLimit,upperLimit) <1)){
                    int index = 0;
                    while (comparer.Compare(list.GetKey(index), lowerLimit) < 0)
                        index++;
                    for (; index < list.Count; index++){
                        if (comparer.Compare(list.GetKey(index), upperLimit) > 0)
                            break;
                          newList.Add(list.GetKey(index), list[list.GetKey(index)]);
                    }
                }
            }
            return newList;
        }
Certainly not clean but its all I can think of right now. For all the C# code floating on the web I couldnt find a SubMap implementation that does what the J2SE classes do. Anyone have any better ideas ? ( Mar 25 2006, 03:31:39 AM EST ) Permalink Comments [0]
20060322 Wednesday March 22, 2006

C#, Assembiles and JITing

The last few days I ve been doing more C# than anything else. Never thought it would come to this! So heres my first post on some notes I ve taken.
In .NET an assembly is a reusable, self-describing, versionable deployment unit for types and resources. Because it is self-describing, it allows the .NET runtime to fully understand the application and enforce dependency and versioning rules. Unlike Java—in which both a stand-alone application and a component library are deployed as a .jar file—assemblies come in four types or formats and essentially consist of a manifest that contains the assembly metadata, one or more modules and other resource files such as icons etc.
  • exe. A console executable. The application must contain one entry point defined in the Main method. There is also a tool called the MSIL Disassembler ( Ildasm.exe ) which is a counterpart of the MSIL Assembler (Ilasm.exe) which takes the portable executable (PE) file that contains Microsoft intermediate language (MSIL) code and creates a text file suitable as input to Ilasm.exe. - talk about round trip decompilation?
  • Library. A library (DLL) that can be used by other assemblies.
  • Module. A nonexecutable collection of compiled code for use in other assemblies.
  • winexe. A graphical Windows executable. The assembly must contain one entry point.
Programmatically a .NET assembly is represented by the Assembly class of the System.Reflection namespace and can be programmatically used to query the assembly. Eg Assembly asm = Assembly.LoadFrom("WSTest.exe");

Another concept similar to Java is that of shared asseblys (just like Jars are shared in an app server) . But .NET does not have the concept of class loaders or class paths so the CLR looks in only once place - the Global Assembly Cache (GAC) which is really a directory (usually %Systemroot%\assembly or %Systemroot%\assembly) where the CLR can locate such shared assemblies. There is a tool called the GAC tool (gacutil.exe) to install shared assemblies in the GAC. To share an assembly requires it to have globally unique name (aka a strong name) which consists of the name, version information, culture information, and a public key for cryptography. To create a public key, the .NET Framework provides a strong name tool called sn.exe, which creates a key file that can be referenced in the AssemblyInfo.cs. For my example I used sn–k WSTest.snk and created an Assembly.cs that contained something like

using System.Reflection;
using System.Runtime.CompilerServices;

[assembly: AssemblyTitle("WSTest")]
[assembly: AssemblyDescription("WSTest app ")]
[assembly: AssemblyCompany("Sun Microsystems")]
[assembly: AssemblyProduct("WSTest")]
[assembly: AssemblyCopyright("This is the property of Sun Microsystems ")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyKeyFile("WSTest.snk")]

Now to create the WSTest.dll with with a strong name: csc /target:library /out:WSTest.dll WStest.cs AssemblyInfo.cs and then stored it in the GAC using gacutil /i WSTest.dll
Boom ! We have a shared assembly. So whats the idea - well the plan was to JIT it up and see what happenes with performance.
Theres a bunch of detail on the MSDN website about how runtime objects are created and another link on pros/cons of JIT'ing Essentially when a reference to an object is first encountered, the JITer loads a stub for each method that matches that method's declaration.When the same method is later invoked, the IL for it is compiled and the stub is replaced with the address of the method's compiled code. This happens each time a method is invoked for the first time, and the resulting native code is cached so that it can be reused the next time the assembly is loaded during that session. The JIT compiled code is not actually stored on disk and reused for subsequent executions of the same application. This actually can be done if needed with what Microsoft calls the Native Image Generator - Ngen.exe that a pre-compiles (pre-JIT) IL application code into native machine language code after installation. . A quick peek inside teh C:\WINNT\assembly folder shows that not all shared assemblys are in fact JIT compiled to native code. (see x86 and MSIL as proc arch) . The reason is apparently that JIT performs lots of on-the-fly optimizations when compiling MSIL. Many of these optimizations, particularly those involving the use of registers and memory, are driven by the current demands made on the system. Compiling assemblies in one large batch prevents these optimizations from being made and therefore may actually result in slower final code for the server side execution.

Hmm....Did'nt we do this five or more years back with the JDK 1.1.x HotSpot VM ? ( Mar 22 2006, 11:00:00 AM EST ) Permalink Comments [0]