Beispiel #1
0
 public void add(Session session, Row row) {
   boolean retry = false;
   if (mainIndexColumn != -1) {
     row.setKey(row.getValue(mainIndexColumn).getLong());
   } else {
     if (row.getKey() == 0) {
       row.setKey((int) ++lastKey);
       retry = true;
     }
   }
   if (tableData.getContainsLargeObject()) {
     for (int i = 0, len = row.getColumnCount(); i < len; i++) {
       Value v = row.getValue(i);
       Value v2 = v.link(database, getId());
       if (v2.isLinked()) {
         session.unlinkAtCommitStop(v2);
       }
       if (v != v2) {
         row.setValue(i, v2);
       }
     }
   }
   // when using auto-generated values, it's possible that multiple
   // tries are required (specially if there was originally a primary key)
   if (trace.isDebugEnabled()) {
     trace.debug("{0} add {1}", getName(), row);
   }
   long add = 0;
   while (true) {
     try {
       addTry(session, row);
       break;
     } catch (DbException e) {
       if (e != fastDuplicateKeyException) {
         throw e;
       }
       if (!retry) {
         throw getNewDuplicateKeyException();
       }
       if (add == 0) {
         // in the first re-try add a small random number,
         // to avoid collisions after a re-start
         row.setKey((long) (row.getKey() + Math.random() * 10000));
       } else {
         row.setKey(row.getKey() + add);
       }
       add++;
     } finally {
       store.incrementChangeCount();
     }
   }
   lastKey = Math.max(lastKey, row.getKey());
 }