gzz-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gzz-commits] gzz/lava gzz/storm/BlockId.java gzz/storm/impl/...


From: Benja Fallenstein
Subject: [Gzz-commits] gzz/lava gzz/storm/BlockId.java gzz/storm/impl/...
Date: Thu, 07 Nov 2002 16:42:50 -0500

CVSROOT:        /cvsroot/gzz
Module name:    gzz
Changes by:     Benja Fallenstein <address@hidden>      02/11/07 16:42:50

Modified files:
        lava/gzz/storm : BlockId.java 
        lava/gzz/storm/impl: TransientPool.java 
        lava/test/gzz/storm: BlockId.test StormPoolTest.java 
        lava/test/gzz/storm/impl: TransientPool.test 

Log message:
        More Storm work, can generate ids now

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/gzz/storm/BlockId.java.diff?tr1=1.1&tr2=1.2&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/gzz/storm/impl/TransientPool.java.diff?tr1=1.8&tr2=1.9&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/test/gzz/storm/BlockId.test.diff?tr1=1.1&tr2=1.2&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/test/gzz/storm/StormPoolTest.java.diff?tr1=1.5&tr2=1.6&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/test/gzz/storm/impl/TransientPool.test.diff?tr1=1.3&tr2=1.4&r1=text&r2=text

Patches:
Index: gzz/lava/gzz/storm/BlockId.java
diff -u gzz/lava/gzz/storm/BlockId.java:1.1 gzz/lava/gzz/storm/BlockId.java:1.2
--- gzz/lava/gzz/storm/BlockId.java:1.1 Sat Aug 24 12:51:36 2002
+++ gzz/lava/gzz/storm/BlockId.java     Thu Nov  7 16:42:50 2002
@@ -23,6 +23,7 @@
 package gzz.storm;
 import gzz.util.*;
 import java.io.*;
+import java.security.*;
 
 public final class BlockId {
     private static String PREFIX = "storm:block:";
@@ -51,7 +52,7 @@
     public String getURI() { return uri; }
     public String toString() { return uri; }
 
-    /** Check whether the given data bytes match this id.
+    /** Check that the given data bytes match this id.
      */
     public void check(byte[] data) throws IOException {
         throw new UnsupportedOperationException("XXX!");
@@ -90,5 +91,33 @@
      */
     public static BlockId getMediaserverId(String msid) {
         return new BlockId("storm:block:" + msid);
+    }
+
+    /** Get the new-style (01-) id for a given array of bytes.
+     *  The byte array must contain the bytes in a block.
+     */
+    public static BlockId getIdForData(byte[] bytes) {
+       MessageDigest digest;
+       try {
+           digest = MessageDigest.getInstance("SHA");
+       } catch(NoSuchAlgorithmException e) {
+           throw new Error("Fatal error: The SHA-1 algorithm "+
+                           "is not supported by this version "+
+                           "of the Java libraries. "+
+                           "Storm cannot operate without "+
+                           "an SHA-1 implementation.");
+       }
+
+       
+       digest.reset();
+       digest.update(bytes);
+       byte[] hash = digest.digest();
+           
+       byte[] id = new byte[21];
+           
+       id[0] = 0x01;
+       System.arraycopy(hash, 0, id, 1, 20);
+       
+       return new BlockId(id);
     }
 }
Index: gzz/lava/gzz/storm/impl/TransientPool.java
diff -u gzz/lava/gzz/storm/impl/TransientPool.java:1.8 
gzz/lava/gzz/storm/impl/TransientPool.java:1.9
--- gzz/lava/gzz/storm/impl/TransientPool.java:1.8      Wed Nov  6 17:02:56 2002
+++ gzz/lava/gzz/storm/impl/TransientPool.java  Thu Nov  7 16:42:50 2002
@@ -40,46 +40,50 @@
     protected class TransientBlockOutputStream extends BlockOutputStream {
         protected ByteArrayOutputStream baos;
        protected Header822 header;
+       protected int headerLength;
        protected Block block;
 
-       protected TransientBlockOutputStream(Header822 header) {
+       protected TransientBlockOutputStream(Header822 header) 
+                                                     throws IOException {
             super(new ByteArrayOutputStream());
            baos = (ByteArrayOutputStream)out;
            this.header = header;
+           header.writeTo(baos);
+           headerLength = baos.size();
         }
         public Block getBlock() { return block; }
         public Header822 getHeader() { return null; }
-       public void close() {
-                block = new TransientBlock(baos.toByteArray(), header);
+       public void close() throws IOException {
+                block = new TransientBlock(baos.toByteArray(), 
+                                          header, headerLength);
                blocks.put(block.getId(),block);
        }
     }
 
     protected class TransientBlock implements Block {
-        protected byte[] bytes, rawBytes;
-        protected Header822 h;
+        protected byte[] bytes;
+        protected Header822 header;
+       protected int headerLength;
 
-       protected TransientBlock(byte[] b, Header822 h) {
-            bytes = b;
-           this.h = h;
+       protected BlockId id;
+
+       protected TransientBlock(byte[] bytes, Header822 header,
+                                int headerLength) throws IOException {
+            this.bytes = bytes;
+           this.header = header;
+           this.headerLength = headerLength;
+
+           id = BlockId.getIdForData(bytes);
        }
-       public BlockId getId() { return null; }
+       public BlockId getId() { return id; }
        public StormPool getPool() { return null; }
-       public Header822 getHeader() { return h; }
+       public Header822 getHeader() { return header; }
        public InputStream getInputStream() {
-           return new ByteArrayInputStream(bytes);
+           return new ByteArrayInputStream(bytes, headerLength,
+                                           bytes.length - headerLength);
        }
        public InputStream getRawInputStream() throws IOException {
-           if(rawBytes == null) {
-               ByteArrayOutputStream bos = new ByteArrayOutputStream();
-               h.writeTo(bos);
-               bos.close();
-               byte[] header = bos.toByteArray();
-               rawBytes = new byte[header.length + bytes.length];
-               System.arraycopy(header, 0, rawBytes, 0, header.length);
-               System.arraycopy(bytes, 0, rawBytes, header.length, 
bytes.length);
-           }
-           return new ByteArrayInputStream(rawBytes);
+           return new ByteArrayInputStream(bytes);
        }
     }
 
@@ -89,12 +93,14 @@
     public void add(Block b) {}
     public void delete(Block b) {}
     public Set getIds() { return blocks.keySet(); }
-    public BlockOutputStream getBlockOutputStream(String contentType) {
+    public BlockOutputStream getBlockOutputStream(String contentType) 
+                                                          throws IOException {
        Header822 hdr = new UniqueHeader822();
        hdr.add("Content-Type", contentType);
        return new TransientBlockOutputStream(hdr);
     }
-    public BlockOutputStream getBlockOutputStream(Header822 hdr) {
+    public BlockOutputStream getBlockOutputStream(Header822 hdr)
+                                                         throws IOException {
        return new TransientBlockOutputStream(new VerbatimHeader822(hdr));
     }
     public String getDefaultPoolName() { return null; }
Index: gzz/lava/test/gzz/storm/BlockId.test
diff -u gzz/lava/test/gzz/storm/BlockId.test:1.1 
gzz/lava/test/gzz/storm/BlockId.test:1.2
--- gzz/lava/test/gzz/storm/BlockId.test:1.1    Sat Aug 24 12:51:36 2002
+++ gzz/lava/test/gzz/storm/BlockId.test        Thu Nov  7 16:42:50 2002
@@ -15,6 +15,8 @@
 # file for more details.
 # 
 
+import gzz, java
+
 from gzz.storm import *
 from jarray import array
 
@@ -60,3 +62,25 @@
     assert c == d
     assert a != None and b != None and c != None
     assert a != "storm:block:0000" and a != "0000" and a != array([0,0], 'b')
+
+
+def testCreateFromData():
+    s = """\
+    Content-Transfer-Encoding: binary
+    Content-Type: text/plain; charset=UTF-8
+    Date: Tue, 1 Oct 2002 12:30:30 +0000
+    X-Injected-By: address@hidden
+    
+    foo"""
+
+    lines = [l.strip() for l in s.split('\n')]
+    s = '\r\n'.join(lines)
+
+    bytes = java.lang.String(s).getBytes("US-ASCII")
+
+    expectedURI = 'storm:block:01E88CEE7CF19F016EEF00B315C0B930C953DB7EF2'
+    id = BlockId.getIdForData(bytes)
+
+    assert id.getURI() == expectedURI
+    assert id          == BlockId(expectedURI)
+
Index: gzz/lava/test/gzz/storm/StormPoolTest.java
diff -u gzz/lava/test/gzz/storm/StormPoolTest.java:1.5 
gzz/lava/test/gzz/storm/StormPoolTest.java:1.6
--- gzz/lava/test/gzz/storm/StormPoolTest.java:1.5      Wed Nov  6 17:02:56 2002
+++ gzz/lava/test/gzz/storm/StormPoolTest.java  Thu Nov  7 16:42:50 2002
@@ -151,4 +151,33 @@
        if(!newIds.contains(bos.getBlockId()))
            throw new Error("Id was not added");
     }
+
+    /** Add a block and check its id.
+     */
+    public void testBlockId(StormPool pool) throws IOException {
+        Header822 header = new VerbatimHeader822();
+       header.add("Content-Transfer-Encoding", "binary");
+       header.add("Content-Type","text/plain; charset=UTF-8");
+       header.add("Date", "Tue, 1 Oct 2002 12:30:30 +0000");
+       header.add("X-Injected-By", "address@hidden");
+
+       byte[] body = "foo".getBytes("UTF-8");
+
+       BlockId id = new BlockId(
+           "storm:block:01E88CEE7CF19F016EEF00B315C0B930C953DB7EF2");
+
+       BlockOutputStream bos = pool.getBlockOutputStream(header);
+       bos.write(body);
+       bos.close();
+
+       BlockId
+           id1 = bos.getBlockId(),
+           id2 = bos.getBlock().getId();
+
+       if(!id.equals(id1))
+           throw new Error("bos.getBlockId() does not match");
+
+       if(!id.equals(id2))
+           throw new Error("bos.getBlock().getId() does not match");
+    }
 }
Index: gzz/lava/test/gzz/storm/impl/TransientPool.test
diff -u gzz/lava/test/gzz/storm/impl/TransientPool.test:1.3 
gzz/lava/test/gzz/storm/impl/TransientPool.test:1.4
--- gzz/lava/test/gzz/storm/impl/TransientPool.test:1.3 Wed Nov  6 17:02:56 2002
+++ gzz/lava/test/gzz/storm/impl/TransientPool.test     Thu Nov  7 16:42:50 2002
@@ -27,3 +27,5 @@
 def testOwnHeader(): s.testOwnHeader(p())
 def testIdsNotNull(): s.testIdsNotNull(p())
 def testAddId(): s.testAddId(p())
+def testBlockId(): s.testBlockId(p())
+




reply via email to

[Prev in Thread] Current Thread [Next in Thread]