public LocalState(String backingDir) throws IOException { _vs = new VersionedStore(backingDir); }
private void persist(Map<Object, Object> val, boolean cleanup) throws IOException { byte[] toWrite = Utils.serialize(val); String newPath = _vs.createVersion(); FileUtils.writeByteArrayToFile(new File(newPath), toWrite); _vs.succeedVersion(newPath); if (cleanup) _vs.cleanup(4); } }
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() throws IOException { Long v = mostRecentVersion(); 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<>(versions); for (String p : listDir(_root)) { Long v = parseVersion(p); if (v != null && !keepers.contains(v)) { deleteVersion(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<>(); for (String s : listDir(_root)) { if (s.endsWith(FINISHED_VERSION_SUFFIX)) { 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; }
private Map<Object, Object> deserializeLatestVersion() throws IOException { String latestPath = _vs.mostRecentVersionPath(); Long latestVersion = _vs.mostRecentVersion(); Map<Object, Object> result = new HashMap<>(); while (latestPath != null) { byte[] serialized = FileUtils.readFileToByteArray(new File(latestPath)); if (serialized.length == 0) { LOG.warn("LocalState file '{}' contained no data, skip this state", latestPath); latestPath = _vs.mostRecentVersionPath(latestVersion - 1); latestVersion = _vs.mostRecentVersion(latestVersion - 1); } else { result = (Map<Object, Object>) Utils.javaDeserialize(serialized); break; } } return result; }
public synchronized void cleanup(int keepVersions) throws IOException { _vs.cleanup(keepVersions); }
public VersionedStore(String path) throws IOException { _root = path; mkdirs(_root); }
public synchronized Map<Object, Object> snapshot() throws IOException { String latestPath = _vs.mostRecentVersionPath(); if(latestPath==null) return new HashMap<Object, Object>(); return (Map<Object, Object>) Utils.deserialize(FileUtils.readFileToByteArray(new File(latestPath))); }
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); } } }
public String mostRecentVersionPath(long maxVersion) throws IOException { Long v = mostRecentVersion(maxVersion); 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; }
public void failVersion(String path) throws IOException { deleteVersion(validateAndGetVersion(path)); }
/** * 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)) { ret.add(validateAndGetVersion(s)); } } Collections.sort(ret); Collections.reverse(ret); return ret; }