How to drop all indexes on an Oracle table

Few days ago I needed to delete all indexes on an Oracle table. After some scripting I ended up with the following one:

BEGIN
  FOR ind IN 
    (SELECT index_name FROM user_indexes WHERE table_name = 'my_table' AND index_name NOT IN 
       (SELECT UNIQUE index_name FROM user_constraints WHERE 
          table_name = 'my_table' AND index_name IS NOT NULL))
  LOOP
      execute immediate 'DROP INDEX '||ind.index_name;
  END LOOP;
END;

It drops all indexes which are not referenced by the constraints of the table. It is essential, because if the table has a constraint created using the specified index the drop statement will fail. If you want to use the script just replace the ‘my_table’ string with the appropriate table name.

Tags: , ,

View Comments

COOLuary 2009 – a real belter of an unconference

I spent an additional day in Cracow to participate in the third edition of COOLuary organized by Grzegorz Duda and his Developer’s World. I was really excited to take part in it, since I have heard that the idea of unconference is gaining popularity nowadays.

The unconference consisted of four panel discussions (50 minutes each). Grails, BPM, Terracotta, JMS, proxying are only some examples of topics mentioned during the sessions. What I enjoyed most was the fact that the discussions were informal and all participants had the possibility to change the topic when the conversation headed toward an unintended direction. I also liked the idea of the conference being a purely face-to-face, developer-to-developer sharing of experience – without a trace of fuzz or evangelism!

The most interesting part, however, was the hands-together session with Mark Richards. It lasted three hours and I enjoyed every single minute of it! Mark spun a yarn about some of his most interesting JEE projects and problems related to them. First of all, we discussed all pros and cons of distributed transactions (JSR-95 and compensation frameworks). It is a topic where I am currently performing R&D, so I really absorbed every aspect of the session. Later we moved onto business locks, JMS byte messaging and Web-Services.
The three hours spent with Mark was the best time I have had during JDD and COOLuary and I would even come to Cracow if it were the only session that I could attend. It was so great that I really hope to be able to take part in No Fluff Just Stuff conferences to see Mark again… If you want to see Mark in action watch this movie on InfoQ .

Tags: , ,

View Comments

Java Developer’s Day 2009 in Krakow

During last weekend I had the possibility to attend the Java Developer’s Day 2009 in Krakow (link) . It was the third time that I took part in this conference (also in 2006, 2007). It was a great event with a keynote speech performed by Mark Richards – one of my idols! Not only is Mark a great speaker, but also a well-educated and experienced computer scientist, thus it was a real pleasure to attend his sessions. Generally, I attended the following ones:

  • “Pimp up your domain model with jBPM” (Tom Baeyens and Joram Barrez)
    The session was really good, however Tom and Joram have only presented the overview of features encompassed by jBPM so it wasn’t anything that could attract me. I’ve been using jBPM for almost one year right now so I would rather be interested in sophisticated usage scenarios. However, I admire Tom and Joram for the last example which outlined how to set up the whole jBPM environment in 60 seconds – it was really a great show!
  • “The Art of Messaging” (keynote, Mark Richards)
    What a session it was! Mark presented a large part of knowledge regarding messaging and JMS in particular. I especially liked the results of the benchmarks performed in different messaging scenarios. The jokes were also great. I hope to see Mark back in Poland soon…
  • “Exception Handling in the Systems Built with JEE” (Tomasz Skutnik)
    Session was really interesting, however the speaker delivered some controversial statements regarding checked exceptions with which I could not agree. Tomasz also encouraged us to implement proprietary code for proper resource management. It is really concerning since these features are already implemented in frameworks such as Spring – so I encourage him to get acquainted with that…
  • “Asynchronous, Concurrent and Distributed Processing in Java EE…” (Waldemar Kot)
    The session was good. Earlier, I have never heard about the CommonJ Work Manager API – the “de facto” standard of manual thread management in JEE. One thing that annoyed me a little bit was fact that Waldi kept his hands in pockets almost all the time during the speech – in my opinion it was inappropriate. However, from the technical point of view the session was cool.
  • “Effective Code Review for Agile Java Developers” (Wojciech Seliga)
    Great presentation of the Atlassian and JetBrains products performed by a guy who was experienced in TDD and Agile. Code reviews and pair-programming were the focal points of this talk. It is really sad that I can’t see a way to use these methodologies in my company in the forthcoming decade…
  • “Common Anti-patterns and Ways to Avoid Them (Mark Richards)”
    Interesting session during which Mark presented some most popular anti-patterns. Excellent speech!
  • “The Architecture of Applications Based on DDD that use Seam and Flex” (Slawomir Sobotka)
    I had been checking my emails during this session, so I can say nothing about it… I was also pretty tired at that time.
  • “Resource-Oriented Architecture (ROA) and REST” (Scott Davis)
    Scott is a real Groovy&Grails enthusiast and it was noticeable from the very first seconds of his talk. Groovy examples were great – especially that one regarding REST service invocation implemented in one line. From the technical point of view this session was quite poor, as Scott presented a lot of evangelism. However, considering the fact that it was the last session, it was a great finish of the conference. I wasn’t disappointed…

To sum up. JDD09 was a great event! After the conference I had the possibility to meet my friends from studies – with whom I (after)partied in Jazz Rock! See you all next year!

Tags: , ,

View Comments

Splitting and Tokenizing Strings in Java

There are at least two ways of tokenizing Strings in Java. Simple examples work like a charm, but it is very easy to encounter some weird or unintuitive behavior while experimenting with complex regexes or some corner cases. Thus, in order to gain deeper knowledge, I am gonna dive into the implementations details and present some general rules of string tokenizing in Java.

How to tokenize a String:

  • String[] Pattern.compile(String regex).split(CharSequence input, int limit)
  • // (This method is actually the equivalent of the method presented above)
    String[] String.split(String regex, int limit)
  • Scanner s = new Scanner(String text).useDelimiter(String regex); 
    while(s.hasNext()) s.next();

To pass for example SCJP a programmer has to predict the exact result of the split method invocation (including the empty matches – what causes most of the problems). I will now present general rules which could probably help in systematizing the knowledge regarding the String tokenization.

The rules are organized in such a way, that the preceding rules have a bigger priority that the following rules, so you should follow the list until one of the rules matches the situation you are actually examining:

1.) If the regex expression does not match any part of the input:

  • Matcher: returns exactly one element – namely the given String
  • Scanner: returns exactly one element – namely the given String

Example:

  • Matcher: “James Bond”.split(“MI6″, 0) == ["James Bond"]
  • Scanner: new Scanner(“James Bond”).useDelimiter(“MI6″).next() == “James Bond”;

2.) When the given String is empty:

  • Matcher: returns exactly one empty match [""]
  • Scanner: the result is empty []

Example:

  • Matcher: “”.split(“MI6″, 0) = [""]
  • Scanner: new Scanner(“”).useDelimiter(“MI6″).hasNext() == false;

3.) When the delimiter regex is empty:

  • Matcher (index == 0): Tokenized characters of the given String preceded by one empty String
  • Matcher (index < 0): Tokenized characters of the given String preceded and followed by one empty String
  • Scanner: Tokenized characters of the given String

Example:

  • Matcher (index == 0): “007″.split(“”, 0) = ["", "0", "0", "7"]
  • Matcher (index < 0): "007".split("", -1) = ["", "0", "0", "7", ""]
  • Scanner: Scanner s = new Scanner(“”).useDelimiter(“MI6″); s.next() = “0″, s.next() = “0″, s.next() = “7″, s.hasNext() = false;

The last thing I want to mention are the “empty-matches” or “zero-length” matches:

  • Matcher (index == 0): Returns: leading and inner “empty-matches” – BUT IF ALL MATCHES ARE EMPTY – the result set is empty
  • Matcher (index < 0): Returns: leading, inner and trailing "empty-matches"
  • Scanner: Returns: only inner “empty-matches”

Example:

  • Matcher (index == 0): “::”.split(“:”, 0) = []
  • Matcher (index < 0): "::".split(":", -1) = ["", "", ""]
  • Matcher (index == 0): “:1::2::”.split(“:”, 0) = ["" , "1", "", "2"]
  • Matcher (index < 0): ":1::2::".split(":", -1) = ["" , "1", "", "2", "", ""]
  • Scanner: Scanner s = new Scanner(“::”).useDelimiter(“:”); s.next() = “”, s.hasNext() = false;

Hope this helps!

Tags: ,

View Comments

String literal pool and JLS rules for String objects

I have been playing with the String objects in order to review all the rules defined by the JLS regarding the String creation and handling. I have recently created a JUnit test case which examines all the aforementioned rules. Enjoy!

JLS rules for Strings:

  • Literal strings within the same class (§8) in the same package (§7) represent references to the same String object (§4.3.1).
  • Literal strings within different classes in the same package represent references to the same String object.
  • Literal strings within different classes in different packages likewise represent references to the same String object.
  • Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.
  • Strings computed by concatenation at run time are newly created and therefore distinct.

Other rules:

  • Strings from the string pool are not eligible for garbage-collection since JVM maintains a single reference to each unique String object from the String pool (Subject of discussion: LINK)
  • new operator does not put newly created string into the string pool
  • From the javadoc: “String.intern() returns a canonical representation for the string object. When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

All test methods are presented below. The code compiles and runs without errors.

  • @Test
    public void testPooledStringScenario1() {
     
    	// creates "abc" string in the string-pool
    	// x references the "abc" string from the pool
    	String x = "abc";
     
    	// does not create new string since "abc" string exists in the pool
    	// y references the "abc" string from the pool
    	String y = "abc";
     
    	// uses "abc" string from the pool to pass it to the constructor
    	// argument, but z is a new object which does not reside in the pool
    	// (as new operator was used)
    	String z = new String("abc");
     
    	// should be true - x and y references the same object from the
    	// string-pool
    	assertTrue(x == y);
     
    	// should be false - x references the object from the pool, while z
    	// references "non-pooled" object from the heap
    	assertFalse(x == z);
    }
  • @Test
    public void testPooledStringScenario2() {
     
    	// creates "1" string from integer. 
    	// concatenates "abc" and "1" creating "abc1" string in the string-pool
    	// x references the "abc1" string from the pool
    	String x = "abc" + 1;
     
    	// y1 references the "abc1" string from the pool
    	String y1 = "abc" + 1;
     
    	// y2 references the "abc1" string from the pool
    	String y2 = "abc1";
     
    	// uses "abc" string from the pool, but y3 initialization is computed at
    	// runtime, thus new object is created
    	int i = 1;
    	String y3 = "abc" + i;
     
    	// uses "abc1" string from the pool to pass it to the constructor
    	// argument but z is a new object which does not reside in the pool -
    	// since new operator was used
    	String z = new String("abc1");
     
    	// should be true - x and y1 and y2 references the same object from the
    	// string-pool
    	assertTrue(x == y1);
    	assertTrue(x == y2);
     
    	// should be false - x references the object from the pool, while z and
    	// y3 references "non-pooled" objects (computed/created at runtime) from
    	// the heap
    	assertFalse(x == z);
    	assertFalse(x == y3);
    }
  • @Test
    public void testPooledStringScenario3() {
     
    	// computed at compile time
    	String x = "ab" + "c" + 1;
    	String y1 = "a" + "bc" + 1;
    	String y2 = "abc1";
     
    	// uses "abc1" string from the pool to pass it to the constructor
    	// argument but z is a new object which does not reside in the pool -
    	// since new operator was used
    	String z = new String("abc1");
     
    	// should be true - x and y1 and y2 references the same object from the
    	// string-pool
    	assertTrue(x == y1);
    	assertTrue(x == y2);
     
    	// should be false - x references the object from the pool, while z
    	// references "non-pooled" object from the heap
    	assertFalse(x == z);
     
    	assertTrue(x.intern() == z.intern());
    }
  • @Test
    public void testPooledStringComputedAtRuntime() {
     
    	String x1 = "ab";
    	String x2 = "c1";
     
    	// computed at runtime (as non-literals are used in the computation)
    	// could be in-lined by the compiler but is not...
    	String x = x1 + x2;
    	String y1 = "ab" + x2;
    	String y2 = "ab" + x2;
     
    	// computed at compile time
    	String z = "ab" + "c1";
     
    	// all should be false
    	assertFalse(y1 == y2);
    	assertFalse(x == y1);
    	assertFalse(x == z);
    	assertFalse(y1 == z);
     
    	// all should be true
    	assertTrue(y1.intern() == y2.intern());
    	assertTrue(x.intern() == y1.intern());
    	assertTrue(x.intern() == z.intern());
    	assertTrue(y1.intern() == z.intern());
    }

Tags: , ,

View Comments