/* SimpleStormFiler.java * * Copyright (c) 2003, Benja Fallenstein * * This file is part of Storm. * * Storm is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Storm is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * You should have received a copy of the GNU Lesser General * Public License along with Storm; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * */ /* * Written by Benja Fallenstein */ package org.nongnu.storm.util; import org.nongnu.storm.*; import org.nongnu.storm.headers.*; import org.nongnu.storm.util.*; import java.io.*; import java.util.*; /** A Storm filer that always saves full versions, no diffs. */ public class SimpleStormFiler implements Filer { public static final String versionContentType = "application/x-GZZ2-version"; public static final String diffContentType = "application/x-GZZ2-msfiler-diff"; protected Version emptyVersion; protected StormPool pool; protected VersionFormat format; protected Pointer pointer; protected PointerBlock current; public SimpleStormFiler(Version emptyVersion, IndexedPool pool, VersionFormat format, String pointerURI) throws IOException { this.emptyVersion = emptyVersion; this.pool = pool; this.format = format; this.pointer = pool.getPointer(pointerURI); Set cur = pointer.getCurrentBlocks(); if(cur.isEmpty()) current = null; else current = (PointerBlock)cur.iterator().next(); } public Version load() throws IOException { if(current == null) return emptyVersion; VersionBlock b = load(current.getTarget()); if(b != null) return b.version; else return emptyVersion; } public VersionBlock load(BlockId id) throws IOException { if(id == null) return null; Block block = pool.get(id); return new VersionBlock(block, format); } public void save(Version v) throws IOException { if(v.equals(load())) return; saveVersion(v); } protected Block saveVersion(Version v) throws IOException { BlockOutputStream bos = pool.getBlockOutputStream(versionContentType); format.writeVersion(bos, v); bos.close(); if(current != null) current = current.update(bos.getBlockId()); else current = pointer.addPointerBlock(bos.getBlockId(), Collections.EMPTY_SET); return bos.getBlock(); } public static class Group extends AbstractGroup { protected Version emptyVersion; protected IndexedPool pool; protected VersionFormat format; public Group(Version emptyVersion, IndexedPool pool, VersionFormat format) { this.emptyVersion = emptyVersion; this.pool = pool; this.format = format; } protected Filer createFiler(String pointerURI) throws IOException { return new SimpleStormFiler(emptyVersion, pool, format, pointerURI); } } }