jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r628391 - in /jackrabbit/sandbox/jackrabbit-ngp: ./ src/main/java/org/apache/jackrabbit/ngp/ src/main/java/org/apache/jackrabbit/ngp/journal/ src/main/java/org/apache/jackrabbit/ngp/node/ src/main/java/org/apache/jackrabbit/ngp/session/ src...
Date Sat, 16 Feb 2008 23:40:22 GMT
Author: jukka
Date: Sat Feb 16 15:40:20 2008
New Revision: 628391

URL: http://svn.apache.org/viewvc?rev=628391&view=rev
Log:
jackrabbit-ngp: Use location-based addressing

Added:
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/EmptyRecord.java
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/JournalException.java
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/NewNode.java
Removed:
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/ByteBufferInputStream.java
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/SmallRecord.java
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/tree/
    jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/
Modified:
    jackrabbit/sandbox/jackrabbit-ngp/pom.xml
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/NodeImpl.java
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/RepositoryImpl.java
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/SessionImpl.java
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/FileJournal.java
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/Journal.java
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/MediumRecord.java
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/BoundNode.java
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/ClosedNode.java
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/NodeState.java
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/StateSwitch.java
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/session/BoundSession.java
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/session/UnboundSession.java
    jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/AbstractNodeTest.java

Modified: jackrabbit/sandbox/jackrabbit-ngp/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/pom.xml?rev=628391&r1=628390&r2=628391&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/pom.xml (original)
+++ jackrabbit/sandbox/jackrabbit-ngp/pom.xml Sat Feb 16 15:40:20 2008
@@ -52,6 +52,11 @@
       <version>1.3</version>
     </dependency>
     <dependency>
+      <groupId>commons-io</groupId>
+      <artifactId>commons-io</artifactId>
+      <version>1.4</version>
+    </dependency>
+    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.4</version>

Modified: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/NodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/NodeImpl.java?rev=628391&r1=628390&r2=628391&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/NodeImpl.java
(original)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/NodeImpl.java
Sat Feb 16 15:40:20 2008
@@ -119,6 +119,38 @@
         }
     }
 
+    public Node getNode(String relPath) throws RepositoryException {
+        int i = relPath.indexOf('/');
+        if (i > 0) {
+            Node node = getNode(relPath.substring(0, i));
+            return node.getNode(relPath.substring(i + 1));
+        } else if (i == 0) {
+            return getSession().getRootNode().getNode(relPath.substring(1));
+        } else if (relPath.equals("..")) {
+            if (parent != null) {
+                return parent;
+            } else {
+                throw new PathNotFoundException(relPath);
+            }
+        } else if (relPath.equals(".")) {
+            return this;
+        } else {
+            synchronized (session) {
+                NodeState child = state.getNodes().get(relPath);
+                if (child != null) {
+                    return new NodeImpl(session, this, relPath, child);
+                } else {
+                    throw new PathNotFoundException(relPath);
+                }
+            }
+        }
+    }
+
+    public Node addNode(String relPath) throws RepositoryException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
     public void addMixin(String mixinName) throws NoSuchNodeTypeException,
             VersionException, ConstraintViolationException, LockException,
             RepositoryException {
@@ -126,13 +158,6 @@
 
     }
 
-    public Node addNode(String relPath) throws ItemExistsException,
-            PathNotFoundException, VersionException,
-            ConstraintViolationException, LockException, RepositoryException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
     public Node addNode(String relPath, String primaryNodeTypeName)
             throws ItemExistsException, PathNotFoundException,
             NoSuchNodeTypeException, LockException, VersionException,
@@ -194,12 +219,6 @@
 
     public Lock getLock() throws UnsupportedRepositoryOperationException,
             LockException, AccessDeniedException, RepositoryException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    public Node getNode(String relPath) throws PathNotFoundException,
-            RepositoryException {
         // TODO Auto-generated method stub
         return null;
     }

Modified: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/RepositoryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/RepositoryImpl.java?rev=628391&r1=628390&r2=628391&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/RepositoryImpl.java
(original)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/RepositoryImpl.java
Sat Feb 16 15:40:20 2008
@@ -17,7 +17,6 @@
 package org.apache.jackrabbit.ngp;
 
 import java.io.File;
-import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -27,7 +26,8 @@
 import javax.jcr.Workspace;
 
 import org.apache.jackrabbit.commons.AbstractRepository;
-import org.apache.jackrabbit.ngp.store.Store;
+import org.apache.jackrabbit.ngp.journal.FileJournal;
+import org.apache.jackrabbit.ngp.journal.Journal;
 
 public class RepositoryImpl extends AbstractRepository {
 
@@ -57,14 +57,14 @@
         // DESCRIPTORS.put(QUERY_XPATH_POS_INDEX, Boolean.TRUE.toString());
     }
 
-    private final Store store;
+    private final Journal journal;
 
-    public RepositoryImpl(Store store) {
-        this.store = store;
+    public RepositoryImpl(Journal journal) {
+        this.journal = journal;
     }
 
-    public RepositoryImpl(File directory) throws IOException {
-        this(new Store(directory));
+    public RepositoryImpl(File directory) throws RepositoryException {
+        this(new FileJournal(directory));
     }
 
     public String getDescriptor(String key) {
@@ -88,7 +88,7 @@
         attributes.put(Credentials.class.getName(), credentials);
         attributes.put(Workspace.class.getName(), workspaceName);
 
-        return new SessionImpl(this, attributes, store);
+        return new SessionImpl(this, attributes, journal);
     }
 
 }

Modified: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/SessionImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/SessionImpl.java?rev=628391&r1=628390&r2=628391&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/SessionImpl.java
(original)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/SessionImpl.java
Sat Feb 16 15:40:20 2008
@@ -39,10 +39,10 @@
 import javax.jcr.version.VersionException;
 
 import org.apache.jackrabbit.commons.AbstractSession;
+import org.apache.jackrabbit.ngp.journal.Journal;
 import org.apache.jackrabbit.ngp.session.ClosedSession;
 import org.apache.jackrabbit.ngp.session.StateSwitch;
 import org.apache.jackrabbit.ngp.session.UnboundSession;
-import org.apache.jackrabbit.ngp.store.Store;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;
 
@@ -56,12 +56,12 @@
 
     SessionImpl(
             Repository repository, Map<String, Object> attributes,
-            Store store) {
+            Journal journal) {
         this.repository = repository;
         this.attributes = attributes;
         this.state = new StateSwitch();
 
-        state.setState(new UnboundSession(store, state));
+        state.setState(new UnboundSession(journal, state));
     }
 
     /**

Added: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/EmptyRecord.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/EmptyRecord.java?rev=628391&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/EmptyRecord.java
(added)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/EmptyRecord.java
Sat Feb 16 15:40:20 2008
@@ -0,0 +1,42 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.ngp.journal;
+
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+
+import org.apache.commons.io.input.ClosedInputStream;
+
+class EmptyRecord implements Record {
+
+    public long getIdentifier() {
+        return 0;
+    }
+
+    public long getLength() {
+        return 0;
+    }
+
+    public InputStream getStream() {
+        return new ClosedInputStream();
+    }
+
+    public ByteBuffer getBuffer() {
+        return ByteBuffer.allocate(0).asReadOnlyBuffer();
+    }
+
+}

Modified: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/FileJournal.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/FileJournal.java?rev=628391&r1=628390&r2=628391&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/FileJournal.java
(original)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/FileJournal.java
Sat Feb 16 15:40:20 2008
@@ -17,149 +17,149 @@
 package org.apache.jackrabbit.ngp.journal;
 
 import java.io.File;
-import java.io.FileOutputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
 import java.io.RandomAccessFile;
-import java.nio.ByteBuffer;
 
 import javax.jcr.RepositoryException;
 
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.output.DeferredFileOutputStream;
 
 public class FileJournal implements Journal {
 
     private final File directory;
 
-    private final RandomAccessFile file;
+    private final RandomAccessFile data;
+
+    private final RandomAccessFile journal;
 
     public FileJournal(File directory) throws RepositoryException {
         this.directory = directory;
         directory.mkdirs();
         if (!directory.isDirectory()) {
-            throw new RepositoryException("Not a data directory: " + directory);
+            throw new RepositoryException(
+                    "Not a journal directory: " + directory);
         }
 
-        File data = new File(directory, "data.ngp");
         try {
-            file = new RandomAccessFile(data, "rw");
-        } catch (IOException e) {
-            throw new RepositoryException("Not a data file: " + data, e);
+            data = new RandomAccessFile(
+                    new File(directory, "data.ngp"), "rw");
+            journal = new RandomAccessFile(
+                    new File(directory, "journal.ngp"), "rw");
+        } catch (FileNotFoundException e) {
+            throw new RepositoryException(
+                    "Data or journal file not found: " + directory, e);
         }
     }
 
-    public void close() throws RepositoryException {
+    public void close() throws JournalException {
         try {
-            file.close();
+            data.close();
+            journal.close();
         } catch (IOException e) {
-            throw new RepositoryException("Unable to close data file", e);
+            throw new JournalException("Data or journal file not closed", e);
         }
     }
 
-    public Record getRecord(long identifier) throws RepositoryException {
-        long length = (identifier >> 48) & 0xffff;
-        if (length <= 6) {
-            return new SmallRecord(identifier);
-        } else if (length < 0xffff) {
-            try {
-                long position = (identifier & 0xffffffffffffL) + 8;
-                ByteBuffer buffer = ByteBuffer.allocate((int) length);
-                while (buffer.hasRemaining()) {
-                    int n = file.getChannel().read(buffer, position);
-                    if (n != -1) {
-                        position += n;
-                    } else {
-                        throw new RepositoryException("Invalid identifier");
-                    }
-                }
-                buffer.flip();
-                return new MediumRecord(identifier, buffer);
-            } catch (IOException e) {
-                throw new RepositoryException("Unable to access a record", e);
+    public synchronized Record getRecord(long identifier)
+            throws JournalException {
+        try {
+            data.seek(identifier);
+            long length = data.readLong();
+            if (length < 0x10000) {
+                byte[] record = new byte[(int) length];
+                data.readFully(record);
+                return new MediumRecord(identifier, record);
+            } else {
+                File record = new File(directory, data.readUTF());
+                return new LargeRecord(identifier, record);
             }
-        } else {
-            return new LargeRecord(identifier, getFile(identifier));
+        } catch (IOException e) {
+            throw new JournalException(
+                    "Error accessing record: " + identifier, e);
         }
     }
 
-    public long addRecord(InputStream stream)
-            throws IOException, RepositoryException {
-        int length = 0;
-        byte[] buffer = new byte[0x10000];
-        while (length < buffer.length) {
-            int n = stream.read(buffer, length, buffer.length - length);
-            if (n != -1) {
-                length += n;
-            } else if (length <= 6) {
-                return SmallRecord.getIdentifier(buffer, length);
-            } else if (length < 0xffff) {
-                return addRecord(buffer, length);
-            } else {
-                break;
-            }
+    public Record addRecord(InputStream stream)
+            throws IOException, JournalException {
+        DeferredFileOutputStream buffer =
+            new DeferredFileOutputStream(0x10000, "record", ".bin", directory);
+        try {
+            IOUtils.copy(stream, buffer);
+        } finally {
+            buffer.close();
         }
-        // Large record
-        return addRecord(buffer, length, stream);
-    }
 
-    private synchronized long addRecord(byte[] buffer, long length)
-            throws RepositoryException {
         try {
-            long position = file.length();
-            long identifier = (length << 48) | position;
-            file.seek(position);
-            file.writeLong(identifier);
-            file.write(buffer, 0, (int) length);
-            while (length++ % 8 == 0) {
-                file.write(0);
-            }
-            return identifier;
+            return addRecord(buffer);
         } catch (IOException e) {
-            throw new RepositoryException(e);
+            throw new JournalException("Error persisting new record", e);
         }
     }
 
-    private long addRecord(byte[] buffer, int length, InputStream input)
-            throws IOException, RepositoryException {
-        File record = File.createTempFile("record", ".bin", directory);
+    private synchronized Record addRecord(DeferredFileOutputStream buffer)
+            throws IOException {
+        long position = data.length();
+        for (data.seek(position); position % 8 != 0; position++) {
+            data.write(0);
+        }
 
-        OutputStream output = new FileOutputStream(record);
-        try {
-            for (int n = length; n != -1; n = input.read(buffer)) {
-                try {
-                    output.write(buffer, 0, n);
-                } catch (IOException e) {
-                    throw new RepositoryException(e);
-                }
+        if (buffer.isInMemory()) {
+            byte[] record = buffer.getData();
+            if (record.length == 0) {
+                return new EmptyRecord();
+            } else {
+                data.writeLong(record.length);
+                data.write(record);
+                return new MediumRecord(position, record);
             }
-        } finally {
-            output.close();
+        } else {
+            File record = buffer.getFile();
+            data.writeLong(record.length());
+            data.writeUTF(record.getName());
+            return new LargeRecord(position, record);
         }
-
-        return addRecord(record);
     }
 
-    private synchronized long addRecord(File record) throws RepositoryException {
+    public Record getDefaultRecord() throws JournalException {
         try {
-            long position = file.length();
-            long identifier = (0xffffL << 48) | position;
-
-            record.renameTo(getFile(identifier));
-
-            file.seek(position);
-            file.writeLong(identifier);
-            return identifier;
+            long entries = journal.length() % 8;
+            if (entries == 0) {
+                return new EmptyRecord();
+            } else {
+                journal.seek((entries - 1) * 8);
+                return getRecord(journal.readLong());
+            }
         } catch (IOException e) {
-            throw new RepositoryException(e);
+            throw new JournalException("Error reading journal", e);
         }
     }
 
-    private File getFile(long identifier) {
-        assert ((identifier >> 48) & 0xffff) == 0xffff;
-        String name = Long.toHexString(identifier & 0x1ffffffffffffL);
-        File data = new File(directory, "data");
-        data.mkdir();
-        return new File(data, name + ".bin");
+    public boolean setDefaultRecord(Record record, Record previous)
+            throws JournalException {
+        try {
+            long entries = journal.length() % 8;
+            if (entries == 0) {
+                if (previous.getIdentifier() != 0) {
+                    return false;
+                }
+                journal.seek(0);
+            } else {
+                journal.seek((entries - 1) * 8);
+                if (previous.getIdentifier() != journal.readLong()) {
+                    return false;
+                }
+            }
+
+            data.getFD().sync();
+            journal.writeLong(record.getIdentifier());
+            journal.getFD().sync();
+            return true;
+        } catch (IOException e) {
+            throw new JournalException("Error writing journal", e);
+        }
     }
 
 }

Modified: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/Journal.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/Journal.java?rev=628391&r1=628390&r2=628391&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/Journal.java
(original)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/Journal.java
Sat Feb 16 15:40:20 2008
@@ -19,8 +19,6 @@
 import java.io.IOException;
 import java.io.InputStream;
 
-import javax.jcr.RepositoryException;
-
 /**
  * Journal of binary records identified by 64 bit identifiers. A record
  * is never modified or removed once it has been added to a journal.
@@ -32,10 +30,10 @@
      *
      * @param identifier record identifier
      * @return identified record
-     * @throws RepositoryException if the identifier is invalid or
-     *                             if the record can not be accessed
+     * @throws JournalException if the identifier is invalid or
+     *                          if the record can not be accessed
      */
-    Record getRecord(long identifier) throws RepositoryException;
+    Record getRecord(long identifier) throws JournalException;
 
     /**
      * Creates a new record.
@@ -43,15 +41,20 @@
      * @param input record content
      * @return record identifier
      * @throws IOException if the given stream can not be read
-     * @throws RepositoryException if the record can not be created
+     * @throws JournalException if the record can not be created
      */
-    long addRecord(InputStream input) throws IOException, RepositoryException;
+    Record addRecord(InputStream input) throws IOException, JournalException;
+
+    Record getDefaultRecord() throws JournalException;
+
+    boolean setDefaultRecord(Record record, Record previous)
+        throws JournalException;
 
     /**
-     * Closes the data store.
+     * Closes the journal.
      *
-     * @throws RepositoryException if the store could not be closed cleanly
+     * @throws JournalException if the store could not be closed cleanly
      */
-    void close() throws RepositoryException;
+    void close() throws JournalException;
 
 }

Added: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/JournalException.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/JournalException.java?rev=628391&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/JournalException.java
(added)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/JournalException.java
Sat Feb 16 15:40:20 2008
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.ngp.journal;
+
+import javax.jcr.RepositoryException;
+
+public class JournalException extends RepositoryException {
+
+    public JournalException(String message) {
+        super(message);
+    }
+
+    public JournalException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+}

Modified: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/MediumRecord.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/MediumRecord.java?rev=628391&r1=628390&r2=628391&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/MediumRecord.java
(original)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/MediumRecord.java
Sat Feb 16 15:40:20 2008
@@ -16,6 +16,7 @@
  */
 package org.apache.jackrabbit.ngp.journal;
 
+import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.nio.ByteBuffer;
 
@@ -23,13 +24,11 @@
 
     private final long identifier;
 
-    private final ByteBuffer buffer;
+    private final byte[] data;
 
-    public MediumRecord(long identifier, ByteBuffer buffer) {
+    public MediumRecord(long identifier, byte[] data) {
         this.identifier = identifier;
-        this.buffer = buffer;
-        assert 6 < getLength() && getLength() < 0xffff;
-        assert buffer.remaining() == getLength();
+        this.data = data;
     }
 
     public long getIdentifier() {
@@ -37,15 +36,15 @@
     }
 
     public long getLength() {
-        return (identifier >> 48) & 0xffff;
+        return data.length;
     }
 
     public InputStream getStream() {
-        return new ByteBufferInputStream(getBuffer());
+        return new ByteArrayInputStream(data);
     }
 
     public ByteBuffer getBuffer() {
-        return buffer.asReadOnlyBuffer();
+        return ByteBuffer.wrap(data).asReadOnlyBuffer();
     }
 
 }

Modified: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/BoundNode.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/BoundNode.java?rev=628391&r1=628390&r2=628391&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/BoundNode.java
(original)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/BoundNode.java
Sat Feb 16 15:40:20 2008
@@ -26,22 +26,30 @@
 import javax.jcr.RepositoryException;
 
 import org.apache.commons.collections.map.ReferenceMap;
-import org.apache.jackrabbit.ngp.store.Record;
-import org.apache.jackrabbit.ngp.store.Store;
+import org.apache.jackrabbit.ngp.journal.Journal;
+import org.apache.jackrabbit.ngp.journal.Record;
 
 public class BoundNode implements NodeState {
 
-    private final Store store;
+    private final Journal journal;
 
     private final Record record;
 
     private final ReferenceMap children = new ReferenceMap();
 
-    public BoundNode(Store store, Record record) {
-        this.store = store;
+    private final Map<String, StateSwitch> changes =
+        new HashMap<String, StateSwitch>();
+
+    public BoundNode(Journal journal, Record record) {
+        this.journal = journal;
         this.record = record;
     }
 
+    public Record persist() throws RepositoryException {
+        return record;
+    }
+
+
     public void refresh(Record record) throws RepositoryException {
         if (!record.equals(this.record)) {
             accept(new EntryVisitor() {
@@ -58,20 +66,52 @@
 
     public Map<String, NodeState> getNodes() throws RepositoryException {
         final Map<String, NodeState> nodes = new HashMap<String, NodeState>();
+
         accept(new EntryVisitor() {
             public void visit(String name, Record record) {
                 StateSwitch child = (StateSwitch) children.get(name);
                 if (child == null) {
                     child = new StateSwitch();
-                    child.setState(new BoundNode(store, record));
+                    child.setState(new BoundNode(journal, record));
                     children.put(name, child);
                 }
                 nodes.put(name, child);
             }
         });
+
+        for (Map.Entry<String, StateSwitch> entry : changes.entrySet()) {
+            StateSwitch state = entry.getValue();
+            if (state != null) {
+                nodes.put(entry.getKey(), state);
+            } else {
+                nodes.remove(entry.getKey());
+            }
+        }
+
         return nodes;
     }
 
+    public NodeState addNode(final String nodeName) throws RepositoryException {
+        if (changes.containsKey(nodeName)) {
+            if (changes.get(nodeName) != null) {
+                throw new RepositoryException("Node already exists: " + nodeName);
+            }
+        } else {
+            accept(new EntryVisitor() {
+                public void visit(String name, Record record)
+                    throws RepositoryException {
+                    if (name.equals(nodeName)) {
+                        throw new RepositoryException("Node already exists: " + name);
+                    }
+                }
+            });
+        }
+        StateSwitch node = new StateSwitch();
+        node.setState(new NewNode(journal));
+        changes.put(nodeName, node);
+        return node;
+    }
+
     private interface EntryVisitor {
 
         void visit(String name, Record record) throws RepositoryException;
@@ -81,14 +121,13 @@
     public void accept(EntryVisitor visitor) throws RepositoryException {
         if (record.getLength() > 0) {
             try {
-                InputStream stream = record.getInputStream();
+                InputStream stream = record.getStream();
                 try {
                     DataInputStream input = new DataInputStream(stream);
-                    byte[] id = new byte[20];
                     while (true) {
                         String name = input.readUTF();
-                        input.readFully(id);
-                        visitor.visit(name, store.getRecord(id));
+                        long identifier = input.readLong();
+                        visitor.visit(name, journal.getRecord(identifier));
                     }
                 } catch (EOFException e) {
                     // end of input, exit loop

Modified: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/ClosedNode.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/ClosedNode.java?rev=628391&r1=628390&r2=628391&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/ClosedNode.java
(original)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/ClosedNode.java
Sat Feb 16 15:40:20 2008
@@ -20,11 +20,15 @@
 
 import javax.jcr.RepositoryException;
 
-import org.apache.jackrabbit.ngp.store.Record;
+import org.apache.jackrabbit.ngp.journal.Record;
 
 public class ClosedNode implements NodeState {
 
     private final String message = "This node is closed";
+
+    public Record persist() throws RepositoryException {
+        throw new RepositoryException(message);
+    }
 
     public void refresh(Record record) {
     }

Added: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/NewNode.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/NewNode.java?rev=628391&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/NewNode.java
(added)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/NewNode.java
Sat Feb 16 15:40:20 2008
@@ -0,0 +1,63 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.ngp.node;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.UnsupportedRepositoryOperationException;
+
+import org.apache.jackrabbit.ngp.journal.Journal;
+import org.apache.jackrabbit.ngp.journal.Record;
+
+public class NewNode implements NodeState {
+
+    private final Journal journal;
+
+    private final Map<String, StateSwitch> changes =
+        new HashMap<String, StateSwitch>();
+
+    public NewNode(Journal journal) {
+        this.journal = journal;
+    }
+
+    public Record persist() throws RepositoryException {
+        throw new UnsupportedRepositoryOperationException();
+    }
+
+    public void refresh(Record record) {
+        // TODO: What should we do here?
+    }
+
+    public Map<String, NodeState> getNodes() throws RepositoryException {
+        return new HashMap<String, NodeState>(changes);
+    }
+
+    public NodeState addNode(final String nodeName) throws RepositoryException {
+        if (changes.containsKey(nodeName)) {
+            if (changes.get(nodeName) != null) {
+                throw new RepositoryException("Node already exists: " + nodeName);
+            }
+        }
+        StateSwitch node = new StateSwitch();
+        node.setState(new NewNode(journal));
+        changes.put(nodeName, node);
+        return node;
+    }
+
+}

Modified: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/NodeState.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/NodeState.java?rev=628391&r1=628390&r2=628391&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/NodeState.java
(original)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/NodeState.java
Sat Feb 16 15:40:20 2008
@@ -20,9 +20,11 @@
 
 import javax.jcr.RepositoryException;
 
-import org.apache.jackrabbit.ngp.store.Record;
+import org.apache.jackrabbit.ngp.journal.Record;
 
 public interface NodeState {
+
+    Record persist() throws RepositoryException;
 
     void refresh(Record record) throws RepositoryException;
 

Modified: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/StateSwitch.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/StateSwitch.java?rev=628391&r1=628390&r2=628391&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/StateSwitch.java
(original)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/node/StateSwitch.java
Sat Feb 16 15:40:20 2008
@@ -20,7 +20,7 @@
 
 import javax.jcr.RepositoryException;
 
-import org.apache.jackrabbit.ngp.store.Record;
+import org.apache.jackrabbit.ngp.journal.Record;
 
 public class StateSwitch implements NodeState {
 
@@ -28,6 +28,10 @@
 
     public void setState(NodeState state) {
         this.state = state;
+    }
+
+    public Record persist() throws RepositoryException {
+        return state.persist();
     }
 
     public void refresh(Record record) throws RepositoryException {

Modified: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/session/BoundSession.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/session/BoundSession.java?rev=628391&r1=628390&r2=628391&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/session/BoundSession.java
(original)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/session/BoundSession.java
Sat Feb 16 15:40:20 2008
@@ -16,28 +16,22 @@
  */
 package org.apache.jackrabbit.ngp.session;
 
-import java.io.IOException;
-
 import javax.jcr.RepositoryException;
 
+import org.apache.jackrabbit.ngp.journal.Journal;
 import org.apache.jackrabbit.ngp.node.NodeState;
-import org.apache.jackrabbit.ngp.store.Store;
 
 public class BoundSession extends UnboundSession {
 
     private final NodeState root;
 
-    public BoundSession(Store store, StateSwitch change, NodeState root) {
-        super(store, change);
+    public BoundSession(Journal journal, StateSwitch change, NodeState root) {
+        super(journal, change);
         this.root = root;
     }
 
     public void refresh(boolean keepChanges) throws RepositoryException {
-        try {
-            root.refresh(store.getDefaultRecord());
-        } catch (IOException e) {
-            throw new RepositoryException("Failed to refresh session", e);
-        }
+        root.refresh(journal.getDefaultRecord());
     }
 
     public NodeState getRootNode() {

Modified: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/session/UnboundSession.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/session/UnboundSession.java?rev=628391&r1=628390&r2=628391&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/session/UnboundSession.java
(original)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/session/UnboundSession.java
Sat Feb 16 15:40:20 2008
@@ -16,22 +16,20 @@
  */
 package org.apache.jackrabbit.ngp.session;
 
-import java.io.IOException;
-
 import javax.jcr.RepositoryException;
 
+import org.apache.jackrabbit.ngp.journal.Journal;
 import org.apache.jackrabbit.ngp.node.BoundNode;
 import org.apache.jackrabbit.ngp.node.NodeState;
-import org.apache.jackrabbit.ngp.store.Store;
 
 public class UnboundSession implements SessionState {
 
-    protected final Store store;
+    protected final Journal journal;
 
     protected final StateSwitch state;
 
-    public UnboundSession(Store store, StateSwitch state) {
-        this.store = store;
+    public UnboundSession(Journal journal, StateSwitch state) {
+        this.journal = journal;
         this.state = state;
     }
 
@@ -41,18 +39,14 @@
 
     public void refresh(boolean keepChanges) throws RepositoryException {
         if (!keepChanges) {
-            state.setState(new UnboundSession(store, state));
+            state.setState(new UnboundSession(journal, state));
         }
     }
 
     public NodeState getRootNode() throws RepositoryException {
-        try {
-            NodeState root = new BoundNode(store, store.getDefaultRecord());
-            state.setState(new BoundSession(store, state, root));
-            return root;
-        } catch (IOException e) {
-            throw new RepositoryException("Unable to access the root node", e);
-        }
+        NodeState root = new BoundNode(journal, journal.getDefaultRecord());
+        state.setState(new BoundSession(journal, state, root));
+        return root;
     }
 
 }

Modified: jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/AbstractNodeTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/AbstractNodeTest.java?rev=628391&r1=628390&r2=628391&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/AbstractNodeTest.java
(original)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/AbstractNodeTest.java
Sat Feb 16 15:40:20 2008
@@ -30,7 +30,8 @@
     public void setUp() throws Exception {
         super.setUp();
         root = session.getRootNode();
-        node = root.getNode("test");
+        node = root.addNode("test");
+        session.save();
     }
 
 }



Mime
View raw message