Friday, June 22, 2012

Quoting password for user in Oracle database

Recently I needed to create a user in Oracle11g database and I was surprised that for other people following expression works well:

CREATE USER name IDENTIFIED BY password
But I'm constantly getting ORA-00922: missing or invalid option error. Quoting the password solves the problem, so following expression works fine:

CREATE USER name IDENTIFIED BY "password"

It turned out that for password which contain only digits or start with a letter quotes aren't needed, so first expression works fine. But if the password starts with digit then quotes are required. So 123456, qwerty and asdf1234 don't need quotes, while "1234qwer" or "2asdf" should be quoted.

Wednesday, June 13, 2012

Collections.addAll performance

I was wondering how efficient addAll() method in Java collections is and looked through the JDK code (Sun/Oracle JDK 1.6.0-27). Here is code for ArrayList.addAll(...):


public boolean addAll(Collection<? extends E> c) {
 Object[] a = c.toArray();
        int numNew = a.length;
 ensureCapacity(size + numNew);  // Increments modCount
        System.arraycopy(a, 0, elementData, size, numNew);
        size += numNew;
 return numNew != 0;
}

So here you can see incomming collection content copied into intermediate array.
And here is code for ArrayList.toArray():


public Object[] toArray() {
 return Arrays.copyOf(elementData, size);
}

Pay attention that content of internal array is copied in order to return some value. And that's OK because internal array may contain empty cells in its tail. But what we have if we put it all together - if you make arrayList1.addAll(arrayList2) then content of arrayList2 is copied 2 times.

Well... honestly, I beleived before that standard data manipulation routines should be better optimized.