public FileBasedHiLoGenerator(long maxLo) { super(new FileBasedGenerator(maxLo)); }
public String getID() throws IOException { try { lock.lock(); // If ID is 0, read initial value from file if possible if (!minimizeReads || !used) { long readInitialValue = readInitialValue(getFilePath()); if (readInitialValue >= 0) { set(readInitialValue); } used = true; } String id = super.getID(); // The id held in the file is always <increment> larger so that // the ID is still unique after a restart. writeNextValue(Long.parseLong(id) + getIncrement()); return id; } finally { lock.unlock(); } }
public void reset() { try { lock.lock(); super.reset(); writeNextValue(0l); } catch (IOException e) { throw new IllegalStateException("Cannot initialize persistent ID generator", e); } finally { lock.unlock(); } }
private void writeNextValue(long id) throws IOException { PrintWriter pw = null; try { pw = new PrintWriter(new FileOutputStream(getFilePath(), false)); pw.println(Long.toString(id)); } catch (IOException e) { if (neverFail) { LOG.warn("Could not write ID to file {}, going to use internal ID generator. {}", getFilePath(), e.getMessage()); return; } throw e; } finally { if (pw != null) pw.close(); } }
public String getID() throws IOException { try { lock.lock(); // If ID is 0, read initial value from file if possible if (!minimizeReads || !used) { long readInitialValue = readInitialValue(getFilePath()); if (readInitialValue >= 0) { set(readInitialValue); } used = true; } String id = super.getID(); // The id held in the file is always <increment> larger so that // the ID is still unique after a restart. writeNextValue(Long.parseLong(id) + getIncrement()); return id; } finally { lock.unlock(); } }
public void reset() { try { lock.lock(); super.reset(); writeNextValue(0l); } catch (IOException e) { throw new IllegalStateException("Cannot initialize persistent ID generator", e); } finally { lock.unlock(); } }
private void writeNextValue(long id) throws IOException { PrintWriter pw = null; try { pw = new PrintWriter(new FileOutputStream(getFilePath(), false)); pw.println(Long.toString(id)); } catch (IOException e) { if (neverFail) { LOG.warn("Could not write ID to file {}, going to use internal ID generator. {}", getFilePath(), e.getMessage()); return; } throw e; } finally { if (pw != null) pw.close(); } }
public FileBasedHiLoGenerator(long maxLo) { super(new FileBasedGenerator(maxLo)); }