jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1057512 - in /jackrabbit/trunk/jackrabbit-core/src: main/java/org/apache/jackrabbit/core/id/NodeIdFactory.java test/java/org/apache/jackrabbit/core/id/NodeIdFactoryTest.java
Date Tue, 11 Jan 2011 08:00:27 GMT
Author: thomasm
Date: Tue Jan 11 08:00:27 2011
New Revision: 1057512

URL: http://svn.apache.org/viewvc?rev=1057512&view=rev
Log:
JCR-2857 Support sequential (non-random) node ids - fix test cases and close streams

Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/id/NodeIdFactory.java
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/id/NodeIdFactoryTest.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/id/NodeIdFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/id/NodeIdFactory.java?rev=1057512&r1=1057511&r2=1057512&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/id/NodeIdFactory.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/id/NodeIdFactory.java
Tue Jan 11 08:00:27 2011
@@ -71,7 +71,12 @@ public class NodeIdFactory {
                 }
             }
             Properties p = new Properties();
-            p.load(new FileInputStream(n));
+            FileInputStream in = new FileInputStream(n);
+            try {
+                p.load(in);
+            } finally {
+                in.close();
+            }
             String defaultMsb = "", defaultLsb = "0";
             int index = seq.indexOf("/");
             if (index >= 0) {
@@ -84,7 +89,12 @@ public class NodeIdFactory {
                 // ensure it doesn't conflict with version 1-5 UUIDs
                 msb &= ~0xf000;
             } else {
-                msb = Long.parseLong(m, 16);
+                if (m.length() == 16) {
+                    msb = (Long.parseLong(m.substring(0, 8), 16) << 32) |
+                        Long.parseLong(m.substring(8), 16);
+                } else {
+                    msb = Long.parseLong(m, 16);
+                }
             }
             storedLsb = nextLsb = Long.parseLong(p.getProperty(NEXT_LSB, defaultLsb), 16);
         } catch (Exception e) {
@@ -105,7 +115,12 @@ public class NodeIdFactory {
         p.setProperty(NEXT_LSB, Long.toHexString(lsb));
         try {
             File temp = new File(repositoryHome, NODE_ID_FILE_TEMP);
-            p.store(new FileOutputStream(temp), null);
+            FileOutputStream out = new FileOutputStream(temp);
+            try {
+                p.store(out, null);
+            } finally {
+                out.close();
+            }
             File n = new File(repositoryHome, NODE_ID_FILE);
             n.delete();
             temp.renameTo(n);

Modified: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/id/NodeIdFactoryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/id/NodeIdFactoryTest.java?rev=1057512&r1=1057511&r2=1057512&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/id/NodeIdFactoryTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/id/NodeIdFactoryTest.java
Tue Jan 11 08:00:27 2011
@@ -27,15 +27,16 @@ public class NodeIdFactoryTest extends T
     private static final String factoryDir = "target/temp/nodeIdFactory";
 
     public void setUp() throws IOException {
+        System.clearProperty(NodeIdFactory.SEQUENTIAL_NODE_ID);
         FileUtils.deleteDirectory(new File(factoryDir));
     }
 
     public void tearDown() throws IOException {
         setUp();
+        System.clearProperty(NodeIdFactory.SEQUENTIAL_NODE_ID);
     }
 
     public void testRandomVersusSequential() throws RepositoryException {
-        System.clearProperty(NodeIdFactory.SEQUENTIAL_NODE_ID);
         NodeIdFactory f = new NodeIdFactory(factoryDir);
         f.open();
         NodeId id = f.newNodeId();
@@ -50,15 +51,43 @@ public class NodeIdFactoryTest extends T
         f.close();
     }
 
+    /**
+     * Test that the version field is reset (0), and that all other MSB bits are
+     * 1 at some point. This also tests the LSB bits.
+     */
+    public void testUUIDVersionFieldReset() throws Exception {
+        System.setProperty(NodeIdFactory.SEQUENTIAL_NODE_ID, "true");
+        long msbOr = 0, msbAnd = -1, lsbOr = 0, lsbAnd = -1;
+        for (int i = 0; i < 0x1f; i++) {
+            FileUtils.deleteDirectory(new File(factoryDir));
+            NodeIdFactory f = new NodeIdFactory(factoryDir);
+            f.open();
+            for (int j = 0; j < 8; j++) {
+                NodeId x = f.newNodeId();
+                msbOr |= x.getMostSignificantBits();
+                msbAnd &= x.getMostSignificantBits();
+                lsbAnd &= x.getLeastSignificantBits();
+                lsbOr |= x.getLeastSignificantBits();
+            }
+            f.close();
+        }
+        System.out.println(Long.toHexString(msbOr));
+        System.out.println(Long.toHexString(lsbOr));
+        assertEquals(0xffffffffffff0fffL, msbOr);
+        assertEquals(0, msbAnd);
+        assertEquals(7, lsbOr);
+        assertEquals(0, lsbAnd);
+     }
+
     public void testNormalUsage() throws RepositoryException {
         System.setProperty(NodeIdFactory.SEQUENTIAL_NODE_ID, "true");
         NodeIdFactory f = new NodeIdFactory(factoryDir);
         f.open();
-        assertEquals("00000000-0000-0000-0000-000000000000", f.newNodeId().toString());
+        assertTrue(f.newNodeId().toString().endsWith("-0000-000000000000"));
         f.close();
         f = new NodeIdFactory(factoryDir);
         f.open();
-        assertEquals("00000000-0000-0000-0000-000000000001", f.newNodeId().toString());
+        assertTrue(f.newNodeId().toString().endsWith("-0000-000000000001"));
         f.close();
     }
 
@@ -104,7 +133,7 @@ public class NodeIdFactoryTest extends T
         System.setProperty(NodeIdFactory.SEQUENTIAL_NODE_ID, "true");
         NodeIdFactory f = new NodeIdFactory(factoryDir);
         f.open();
-        assertEquals("00000000-0000-0000-0000-000000000000", f.newNodeId().toString());
+        assertTrue(f.newNodeId().toString().endsWith("-0000-000000000000"));
         f.close();
         File id = new File(factoryDir, "nodeId.properties");
         assertTrue(id.exists());
@@ -112,7 +141,7 @@ public class NodeIdFactoryTest extends T
         id.renameTo(idTemp);
         f = new NodeIdFactory(factoryDir);
         f.open();
-        assertEquals("00000000-0000-0000-0000-000000000001", f.newNodeId().toString());
+        assertTrue(f.newNodeId().toString().endsWith("-0000-000000000001"));
         assertFalse(idTemp.exists());
         f.close();
     }



Mime
View raw message