7 |
import java.util.*; |
import java.util.*; |
8 |
|
|
9 |
/** A Storm filer that stores diffs between versions if possible. |
/** A Storm filer that stores diffs between versions if possible. |
10 |
* XXX Does not test for spoofing yet! Doesn't handle faulty |
* XXX Doesn't work yet |
|
* diffing gracefully either. |
|
11 |
*/ |
*/ |
12 |
public class DiffingStormFiler extends SimpleStormFiler { |
public class DiffingStormFiler extends SimpleStormFiler { |
13 |
|
|
19 |
index = (DiffIndexType.Index)pool.getIndex(DiffIndexType.indexTypeURI); |
index = (DiffIndexType.Index)pool.getIndex(DiffIndexType.indexTypeURI); |
20 |
} |
} |
21 |
|
|
|
/** |
|
|
public Version load(BlockId id) throws IOException { |
|
|
return load(id, new Header822[2]); |
|
|
} |
|
|
|
|
|
public Version load(BlockId id, Header822 hdr[]) throws IOException { |
|
|
try { |
|
|
return super.load(id); |
|
|
} catch(FileNotFoundException _) { |
|
|
Set diffs = index.getDiffsTo(id); |
|
|
if(diffs.isEmpty()) |
|
|
throw new FileNotFoundException("No diff path to: "+id); |
|
|
|
|
|
BlockId diffId = (BlockId)diffs.iterator().next(); |
|
|
Block diffBlock = pool.get(diffId); |
|
|
InputStream is = diffBlock.getInputStream(); |
|
|
Header822 fromHeader = Headers822.readHeader(is); |
|
|
Header822 toHeader = Headers822.readHeader(is); |
|
|
Version.Diff diff = format.readDiff(is); |
|
|
is.close(); |
|
|
|
|
|
BlockId fromId; |
|
|
try { |
|
|
String hex = diffBlock.getHeader().get("X-Gzz-Diff-From"); |
|
|
fromId = BlockId.getMediaserverId(hex); |
|
|
} catch(NoSuchElementException __) { |
|
|
fromId = null; |
|
|
} |
|
|
|
|
|
Version from; |
|
|
if(fromId != null) |
|
|
from = load(fromId); |
|
|
else |
|
|
from = emptyVersion; |
|
|
|
|
|
hdr[0] = fromHeader; |
|
|
hdr[1] = toHeader; |
|
|
return diff.applyTo(from); |
|
|
} |
|
|
} |
|
22 |
|
|
23 |
public void save(Version v) throws IOException { |
// XXX |
|
Version old; |
|
|
BlockId oldId = null; |
|
|
Header822 oldHeader = null; |
|
|
|
|
|
if(current != null) { |
|
|
Header822[] old_hdrs = new Header822[2]; |
|
|
old = load(current.getTarget(), old_hdrs); |
|
|
oldId = current.getTarget(); |
|
|
oldHeader = old_hdrs[1]; |
|
|
} else { |
|
|
old = emptyVersion; |
|
|
oldHeader = new VerbatimHeader822(); // empty header |
|
|
} |
|
|
|
|
|
if(old.equals(v)) return; |
|
|
|
|
|
Block newBlock = saveVersion(v); |
|
|
|
|
|
BlockId newId = newBlock.getId(); |
|
|
Header822 newHeader = newBlock.getHeader(); |
|
|
|
|
|
Version.Diff diff = v.getDiffFrom(old); |
|
|
|
|
|
Header822 hdr = new SortedHeader822(); |
|
|
hdr.add("Content-Type", diffContentType); |
|
|
hdr.add("Content-Transfer-Encoding", "binary"); |
|
|
if(oldId != null) |
|
|
hdr.add("X-Gzz-Diff-From", oldId.getHex()); |
|
|
hdr.add("X-Gzz-Diff-To", newId.getHex()); |
|
|
|
|
|
BlockOutputStream bos = pool.getBlockOutputStream(hdr); |
|
|
oldHeader.writeTo(bos); |
|
|
newHeader.writeTo(bos); |
|
|
format.writeDiff(bos, diff); |
|
|
bos.close(); |
|
|
|
|
|
//pool.delete(newBlock); |
|
|
} |
|
|
*/ |
|
24 |
|
|
25 |
|
|
26 |
// should be simply named .Group |
// should be simply named .Group |