public LocalState(String backingDir, boolean createBackingDir) throws IOException { LOG.debug("New Local State for {}", backingDir); _vs = new VersionedStore(backingDir, createBackingDir); }
public synchronized void cleanup(int keepVersions) throws IOException { _vs.cleanup(keepVersions); }
public void succeedVersion(String path) throws IOException { long version = validateAndGetVersion(path); // should rewrite this to do a file move createNewFile(tokenPath(version)); }
public String mostRecentVersionPath(long maxVersion) throws IOException { Long v = mostRecentVersion(maxVersion); if (v == null) { return null; } return versionPath(v); }
public void failVersion(String path) throws IOException { deleteVersion(validateAndGetVersion(path)); }
public void cleanup(int versionsToKeep) throws IOException { List<Long> versions = getAllVersions(); if (versionsToKeep >= 0) { versions = versions.subList(0, Math.min(versions.size(), versionsToKeep)); } HashSet<Long> keepers = new HashSet<Long>(versions); for (String p : listDir(_root)) { Long v = parseVersion(p); if (v != null && !keepers.contains(v)) { deleteVersion(v); } } }
private void persistInternal(Map<String, ThriftSerializedObject> serialized, TSerializer ser, boolean cleanup) { try { if (ser == null) { ser = new TSerializer(); } byte[] toWrite = ser.serialize(new LocalStateData(serialized)); String newPath = _vs.createVersion(); File file = new File(newPath); FileUtils.writeByteArrayToFile(file, toWrite); if (toWrite.length != file.length()) { throw new IOException("Tried to serialize " + toWrite.length + " bytes to " + file.getCanonicalPath() + ", but " + file.length() + " bytes were written."); } _vs.succeedVersion(newPath); if (cleanup) { _vs.cleanup(4); } } catch (Exception e) { throw new RuntimeException(e); } }
public String createVersion() throws IOException { Long mostRecent = mostRecentVersion(); long version = Time.currentTimeMillis(); if (mostRecent != null && version <= mostRecent) { version = mostRecent + 1; } return createVersion(version); }
public String createVersion(long version) throws IOException { String ret = versionPath(version); if (getAllVersions().contains(version)) { throw new RuntimeException("Version already exists or data already exists"); } else { return ret; } }
/** * Sorted from most recent to oldest. */ public List<Long> getAllVersions() throws IOException { List<Long> ret = new ArrayList<Long>(); for (String s : listDir(_root)) { if (s.endsWith(FINISHED_VERSION_SUFFIX) && new File(s.substring(0, s.length() - FINISHED_VERSION_SUFFIX.length())).exists()) { ret.add(validateAndGetVersion(s)); } } Collections.sort(ret); Collections.reverse(ret); return ret; }
public Long mostRecentVersion(long maxVersion) throws IOException { List<Long> all = getAllVersions(); for (Long v : all) { if (v <= maxVersion) { return v; } } return null; }
/** * Creates a store at the given path. * * @param The path for the store * @param createRootDir option to create the path directory */ public VersionedStore(String path, boolean createRootDir) throws IOException { _root = path; if (createRootDir) { mkdirs(_root); } }
private Map<String, ThriftSerializedObject> partialDeserializeLatestVersion(TDeserializer td) { try { String latestPath = _vs.mostRecentVersionPath(); Map<String, ThriftSerializedObject> result = new HashMap<>(); if (latestPath != null) { byte[] serialized = FileUtils.readFileToByteArray(new File(latestPath)); if (serialized.length == 0) { LOG.warn("LocalState file '{}' contained no data, resetting state", latestPath); } else { if (td == null) { td = new TDeserializer(); } LocalStateData data = new LocalStateData(); td.deserialize(data, serialized); result = data.get_serialized_parts(); } } return result; } catch (Exception e) { throw new RuntimeException(e); } }
public void cleanup(int versionsToKeep) throws IOException { List<Long> versions = getAllVersions(); if(versionsToKeep >= 0) { versions = versions.subList(0, Math.min(versions.size(), versionsToKeep)); } HashSet<Long> keepers = new HashSet<Long>(versions); for(String p: listDir(_root)) { Long v = parseVersion(p); if(v!=null && !keepers.contains(v)) { deleteVersion(v); } } }
private void persistInternal(Map<String, ThriftSerializedObject> serialized, TSerializer ser, boolean cleanup) { try { if (ser == null) { ser = new TSerializer(); } byte[] toWrite = ser.serialize(new LocalStateData(serialized)); String newPath = _vs.createVersion(); File file = new File(newPath); FileUtils.writeByteArrayToFile(file, toWrite); if (toWrite.length != file.length()) { throw new IOException("Tried to serialize " + toWrite.length + " bytes to " + file.getCanonicalPath() + ", but " + file.length() + " bytes were written."); } _vs.succeedVersion(newPath); if(cleanup) _vs.cleanup(4); } catch(Exception e) { throw new RuntimeException(e); } }
public String mostRecentVersionPath() throws IOException { Long v = mostRecentVersion(); if (v == null) { return null; } return versionPath(v); }
public String createVersion() throws IOException { Long mostRecent = mostRecentVersion(); long version = Time.currentTimeMillis(); if(mostRecent!=null && version <= mostRecent) { version = mostRecent + 1; } return createVersion(version); }
public String createVersion(long version) throws IOException { String ret = versionPath(version); if(getAllVersions().contains(version)) throw new RuntimeException("Version already exists or data already exists"); else return ret; }
/** * Sorted from most recent to oldest */ public List<Long> getAllVersions() throws IOException { List<Long> ret = new ArrayList<Long>(); for(String s: listDir(_root)) { if(s.endsWith(FINISHED_VERSION_SUFFIX) && new File(s.substring(0, s.length() - FINISHED_VERSION_SUFFIX.length())).exists()) { ret.add(validateAndGetVersion(s)); } } Collections.sort(ret); Collections.reverse(ret); return ret; }
public void failVersion(String path) throws IOException { deleteVersion(validateAndGetVersion(path)); }