private void assignIds(List<RdbmsValue> values, Map<Long, Number> map)
throws SQLException, InterruptedException
{
while (values.size() < getChunkSize()) {
RdbmsValue taken = queue.poll();
if (taken == closeSignal) {
queue.add(taken);
break;
}
if (taken == null)
break;
values.add(taken);
}
Map<Long, Number> existing = lookup(values, map);
Iterator<RdbmsValue> iter = values.iterator();
while (iter.hasNext()) {
RdbmsValue value = iter.next();
Long hash = idseq.hashOf(value);
if (existing.get(hash) != null) {
// already in database
Number id = idseq.idOf(existing.get(hash));
value.setInternalId(id);
value.setVersion(getIdVersion(value));
iter.remove();
}
else {
synchronized (ids) {
if (ids.containsKey(hash)) {
// already inserting this value
Number id = ids.get(hash);
value.setInternalId(id);
value.setVersion(getIdVersion(value));
iter.remove();
}
else {
// new id to be inserted
Number id = idseq.nextId(value);
value.setInternalId(id);
value.setVersion(getIdVersion(value));
ids.put(hash, id);
// keep on list for later insert
}
}
}