[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Gzz-commits] gzz/lava/gzz/storm/impl DirPool.java TransientP...
From: |
Benja Fallenstein |
Subject: |
[Gzz-commits] gzz/lava/gzz/storm/impl DirPool.java TransientP... |
Date: |
Fri, 15 Nov 2002 21:15:16 -0500 |
CVSROOT: /cvsroot/gzz
Module name: gzz
Changes by: Benja Fallenstein <address@hidden> 02/11/15 21:15:15
Modified files:
lava/gzz/storm/impl: DirPool.java TransientPool.java
Added files:
lava/gzz/storm/impl: AbstractPool.java
Log message:
Refactor: Abstract out some parts
CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/gzz/storm/impl/AbstractPool.java?rev=1.1
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/gzz/storm/impl/DirPool.java.diff?tr1=1.7&tr2=1.8&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/gzz/storm/impl/TransientPool.java.diff?tr1=1.14&tr2=1.15&r1=text&r2=text
Patches:
Index: gzz/lava/gzz/storm/impl/DirPool.java
diff -u gzz/lava/gzz/storm/impl/DirPool.java:1.7
gzz/lava/gzz/storm/impl/DirPool.java:1.8
--- gzz/lava/gzz/storm/impl/DirPool.java:1.7 Fri Nov 15 20:35:58 2002
+++ gzz/lava/gzz/storm/impl/DirPool.java Fri Nov 15 21:15:14 2002
@@ -28,8 +28,14 @@
/** A StormPool storing blocks in individual files in a directory.
* File names have the form <code>b_</code><i>idstring</i>.
+ * <p>
+ * Ideas for making this more efficient if needed:
+ * Don't use byte arrays. When creating a new block, write it
+ * to a temporary file first and multiplex to another
+ * output stream to get the id; rename the file when
+ * the whole block is through and the id is known.
*/
-public class DirPool implements StormPool {
+public class DirPool extends AbstractPool {
/** The directory we store our blocks in.
*/
@@ -42,20 +48,27 @@
String hex = gzz.util.HexUtil.byteArrToHex(id.getBytes());
return new File(dir, "b_" + hex);
}
+
+ /** Read the header of the block
+ * corresponding to the given id.
+ */
+ protected Header822 getFileHeader(BlockId id) throws IOException {
+ InputStream is = new FileInputStream(getFile(id));
+ Header822 header = Headers822.readHeader(is);
+ is.close();
+ return header;
+ }
- protected class FileBlockOutputStream extends BlockOutputStream {
+
+ protected class FileBlockOutputStream extends AbstractBlockOutputStream {
protected ByteArrayOutputStream baos;
- protected Header822 header;
- protected Block block;
protected FileBlockOutputStream(Header822 header) throws IOException {
- super(new ByteArrayOutputStream());
+ super(new ByteArrayOutputStream(), header);
baos = (ByteArrayOutputStream)out;
- this.header = header;
header.writeTo(baos);
}
- public Block getBlock() { return block; }
- public Header822 getHeader() { return header; }
+
public void close() throws IOException {
byte[] bytes = baos.toByteArray();
BlockId id = BlockId.getIdForData(bytes);
@@ -69,28 +82,14 @@
}
}
- protected class FileBlock implements Block {
+ protected class FileBlock extends AbstractBlock {
protected File file;
- protected BlockId id;
- protected Header822 header;
-
protected FileBlock(BlockId id) throws IOException {
- this.id = id;
+ super(id, getFileHeader(id));
this.file = getFile(id);
-
- InputStream is = new FileInputStream(file);
- this.header = Headers822.readHeader(is);
- is.close();
- }
- public BlockId getId() { return id; }
- public StormPool getPool() { return DirPool.this; }
- public Header822 getHeader() { return header; }
- public InputStream getInputStream() throws IOException {
- InputStream is = new FileInputStream(file);
- Headers822.readHeader(is);
- return is;
}
+
public InputStream getRawInputStream() throws IOException {
return new FileInputStream(file);
}
Index: gzz/lava/gzz/storm/impl/TransientPool.java
diff -u gzz/lava/gzz/storm/impl/TransientPool.java:1.14
gzz/lava/gzz/storm/impl/TransientPool.java:1.15
--- gzz/lava/gzz/storm/impl/TransientPool.java:1.14 Fri Nov 15 20:35:58 2002
+++ gzz/lava/gzz/storm/impl/TransientPool.java Fri Nov 15 21:15:15 2002
@@ -30,58 +30,38 @@
/** A StormPool whose contents are exclusively stored in memory.
* Should become an IndexedPool eventually.
*/
-public class TransientPool implements StormPool {
+public class TransientPool extends AbstractPool {
/** The blocks in this pool.
* A mapping from <code>BlockId</code> objects
* to <code>TransientBlock</code>s.
*/
protected Map blocks = new HashMap();
- protected class TransientBlockOutputStream extends BlockOutputStream {
+ protected class TransientBlockOutputStream extends
AbstractBlockOutputStream {
protected ByteArrayOutputStream baos;
- protected Header822 header;
- protected int headerLength;
- protected Block block;
protected TransientBlockOutputStream(Header822 header)
throws IOException {
- super(new ByteArrayOutputStream());
+ super(new ByteArrayOutputStream(), header);
baos = (ByteArrayOutputStream)out;
- this.header = header;
header.writeTo(baos);
- headerLength = baos.size();
}
- public Block getBlock() { return block; }
- public Header822 getHeader() { return header; }
+
public void close() throws IOException {
- block = new TransientBlock(baos.toByteArray(),
- header, headerLength);
- blocks.put(block.getId(),block);
+ block = new TransientBlock(baos.toByteArray(), header);
+ blocks.put(block.getId(),block);
}
}
- protected class TransientBlock implements Block {
+ protected class TransientBlock extends AbstractBlock {
protected byte[] bytes;
- protected Header822 header;
protected int headerLength;
- protected BlockId id;
-
- protected TransientBlock(byte[] bytes, Header822 header,
- int headerLength) throws IOException {
+ protected TransientBlock(byte[] bytes, Header822 header) throws
IOException {
+ super(BlockId.getIdForData(bytes), header);
this.bytes = bytes;
- this.header = header;
- this.headerLength = headerLength;
-
- id = BlockId.getIdForData(bytes);
- }
- public BlockId getId() { return id; }
- public StormPool getPool() { return TransientPool.this; }
- public Header822 getHeader() { return header; }
- public InputStream getInputStream() {
- return new ByteArrayInputStream(bytes, headerLength,
- bytes.length - headerLength);
}
+
public InputStream getRawInputStream() throws IOException {
return new ByteArrayInputStream(bytes);
}
@@ -97,13 +77,11 @@
byte[] body = gzz.util.CopyUtil.readBytes(b.getInputStream());
byte[] raw = gzz.util.CopyUtil.readBytes(b.getRawInputStream());
- InputStream is = b.getRawInputStream();
+ InputStream is = new ByteArrayInputStream(raw);
Header822 header = Headers822.readHeader(is);
is.close();
- int headerLength = raw.length - body.length;
-
- Block block = new TransientBlock(raw, header, headerLength);
+ Block block = new TransientBlock(raw, header);
BlockId id = block.getId();
if(!id.equals(b.getId()))
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Gzz-commits] gzz/lava/gzz/storm/impl DirPool.java TransientP...,
Benja Fallenstein <=