int nextDocId = 1;
int nextDocId; try { nextDocId = getFreeDocId(); } catch (ReadOnlyException e1) { return 1; } if(nextDocId > -1) return nextDocId; else nextDocId = 1;
	public int getNextDocId(Collection collection) {		int nextDocId = 1;		Value key = new Value("__next_doc_id");		Value data;		Lock lock = collectionsDb.getLock();		try {			lock.acquire(Lock.WRITE_LOCK);			data = collectionsDb.get(key);			if (data != null) {				nextDocId = ByteConversion.byteToInt(data.getData(), 0);				++nextDocId;			}			byte[] d = new byte[4];			ByteConversion.intToByte(nextDocId, d, 0);			try {				collectionsDb.put(key, d, true);			} catch (ReadOnlyException e) {				LOG.debug("database read-only");				return -1;			}		} catch (LockException e) {			LOG.warn("failed to acquire lock on collections store", e);		} finally {			lock.release();		}		return nextDocId;	}