I test the time cost for find(), obtain a linear complexity with the following codes:
int iBlock = 0x10000; for (int i = 1; i <= 3*iBlock; i++) { trans.begin(); Node o = new Node(); Node p = em.find(Node.class, i/3); if (null==p) System.out.printf("Node %d has no parent.%n", i); o.setParent( p ); em.persist(o); trans.commit(); if ((i & 0xFF)==0){ end = System.currentTimeMillis(); System.out.printf("%d @ %s %n",i, (end-start)/1000f); } }
The time cost of each 256 commits changed from 0.343 to 59.344 seconds, the graph attached shows a linear complexity, i.e. O(n). So what algorithm in find()? Can it improved to O(log(n)) complexity?