jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r594652 - in /jackrabbit/sandbox/jackrabbit-ngp/src: main/java/org/apache/jackrabbit/ngp/journal/ test/java/org/apache/jackrabbit/ngp/journal/
Date Tue, 13 Nov 2007 21:42:46 GMT
Author: jukka
Date: Tue Nov 13 13:42:44 2007
New Revision: 594652

URL: http://svn.apache.org/viewvc?rev=594652&view=rev
Log:
NGP: Added a simple journal of binary records

Added:
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/ByteBufferInputStream.java
  (with props)
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/FileJournal.java
  (with props)
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/Journal.java
  (with props)
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/LargeRecord.java
  (with props)
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/MediumRecord.java
  (with props)
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/Record.java
  (with props)
    jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/SmallRecord.java
  (with props)
    jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/
    jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/ByteBufferInputStreamTest.java
  (with props)
    jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/FileJournalTest.java
  (with props)
    jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/LargeRecordTest.java
  (with props)
    jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/MediumRecordTest.java
  (with props)
    jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/SmallRecordTest.java
  (with props)

Added: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/ByteBufferInputStream.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/ByteBufferInputStream.java?rev=594652&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/ByteBufferInputStream.java
(added)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/ByteBufferInputStream.java
Tue Nov 13 13:42:44 2007
@@ -0,0 +1,78 @@
+/**
+ * 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.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+
+class ByteBufferInputStream extends InputStream {
+
+    private final ByteBuffer buffer;
+
+    public ByteBufferInputStream(ByteBuffer buffer) {
+        this.buffer = buffer;
+    }
+
+    public void close() {
+    }
+
+    public int available() {
+        if (buffer.isDirect()) {
+            return 0;
+        } else {
+            return buffer.remaining();
+        }
+    }
+
+    public int read() throws IOException {
+        if (buffer.hasRemaining()) {
+            return ((int) buffer.get()) & 0xff;
+        } else {
+            return -1;
+        }
+    }
+
+    public int read(byte[] b, int off, int len) {
+        if (buffer.hasRemaining()) {
+            len = Math.min(len, buffer.remaining());
+            buffer.get(b, off, len);
+            return len;
+        } else {
+            return -1;
+        }
+    }
+
+    public long skip(long n) throws IOException {
+        n = Math.min(Math.max(n, 0), buffer.remaining());
+        buffer.position(buffer.position() + (int) n);
+        return n;
+    }
+
+    public boolean markSupported() {
+        return true;
+    }
+
+    public synchronized void mark(int readlimit) {
+        buffer.mark();
+    }
+
+    public synchronized void reset() {
+        buffer.reset();
+    }
+
+}

Propchange: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/ByteBufferInputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 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=594652&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/FileJournal.java
(added)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/FileJournal.java
Tue Nov 13 13:42:44 2007
@@ -0,0 +1,165 @@
+/**
+ * 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.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
+
+import javax.jcr.RepositoryException;
+
+
+public class FileJournal implements Journal {
+
+    private final File directory;
+
+    private final RandomAccessFile file;
+
+    public FileJournal(File directory) throws RepositoryException {
+        this.directory = directory;
+        directory.mkdirs();
+        if (!directory.isDirectory()) {
+            throw new RepositoryException("Not a data 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);
+        }
+    }
+
+    public void close() throws RepositoryException {
+        try {
+            file.close();
+        } catch (IOException e) {
+            throw new RepositoryException("Unable to close data file", 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);
+            }
+        } else {
+            return new LargeRecord(identifier, getFile(identifier));
+        }
+    }
+
+    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 < 8) {
+                return SmallRecord.getIdentifier(buffer, length);
+            } else if (length < 0xffff) {
+                return addRecord(buffer, length);
+            } else {
+                break;
+            }
+        }
+        // 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;
+        } catch (IOException e) {
+            throw new RepositoryException(e);
+        }
+    }
+
+    private long addRecord(byte[] buffer, int length, InputStream input)
+            throws IOException, RepositoryException {
+        File record = File.createTempFile("record", ".bin", directory);
+
+        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);
+                }
+            }
+        } finally {
+            output.close();
+        }
+
+        return addRecord(record);
+    }
+
+    private synchronized long addRecord(File record) throws RepositoryException {
+        try {
+            long position = file.length();
+            long identifier = (0xffffL << 48) | position;
+
+            record.renameTo(getFile(identifier));
+
+            file.seek(position);
+            file.writeLong(identifier);
+            return identifier;
+        } catch (IOException e) {
+            throw new RepositoryException(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");
+    }
+
+}

Propchange: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/FileJournal.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 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=594652&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/Journal.java
(added)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/Journal.java
Tue Nov 13 13:42:44 2007
@@ -0,0 +1,57 @@
+/**
+ * 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.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.
+ */
+public interface Journal {
+
+    /**
+     * Returns the identified record.
+     *
+     * @param identifier record identifier
+     * @return identified record
+     * @throws RepositoryException if the identifier is invalid or
+     *                             if the record can not be accessed
+     */
+    Record getRecord(long identifier) throws RepositoryException;
+
+    /**
+     * Creates a new record.
+     *
+     * @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
+     */
+    long addRecord(InputStream input) throws IOException, RepositoryException;
+
+    /**
+     * Closes the data store.
+     *
+     * @throws RepositoryException if the store could not be closed cleanly
+     */
+    void close() throws RepositoryException;
+
+}

Propchange: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/Journal.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/LargeRecord.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/LargeRecord.java?rev=594652&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/LargeRecord.java
(added)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/LargeRecord.java
Tue Nov 13 13:42:44 2007
@@ -0,0 +1,75 @@
+/**
+ * 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.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel.MapMode;
+
+import javax.jcr.RepositoryException;
+
+
+class LargeRecord implements Record {
+
+    private final long identifier;
+
+    private final File file;
+
+    public LargeRecord(long identifier, File file) {
+        this.identifier = identifier;
+        this.file = file;
+        assert ((identifier >> 48) & 0xffff) == 0xffff;
+        assert file.exists();
+        assert file.length() >= 0xffff;
+    }
+
+    public long getIdentifier() {
+        return identifier;
+    }
+
+    public long getLength() {
+        return file.length();
+    }
+
+    public InputStream getStream() throws RepositoryException {
+        try {
+            return new FileInputStream(file);
+        } catch (IOException e) {
+            throw new RepositoryException("Error accessing record: " + file, e);
+        }
+    }
+
+    public ByteBuffer getBuffer() throws RepositoryException {
+        try {
+            FileInputStream input = new FileInputStream(file);
+            try {
+                return input.getChannel().map(
+                        MapMode.READ_ONLY, 0, getLength());
+            } finally {
+                input.close();
+            }
+        } catch (IllegalArgumentException e) {
+            throw new RepositoryException("Record too large: " + file, e);
+        } catch (IOException e) {
+            throw new RepositoryException("Error accessing record: " + file, e);
+        }
+    }
+
+}

Propchange: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/LargeRecord.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 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=594652&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/MediumRecord.java
(added)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/MediumRecord.java
Tue Nov 13 13:42:44 2007
@@ -0,0 +1,51 @@
+/**
+ * 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;
+
+class MediumRecord implements Record {
+
+    private final long identifier;
+
+    private final ByteBuffer buffer;
+
+    public MediumRecord(long identifier, ByteBuffer buffer) {
+        this.identifier = identifier;
+        this.buffer = buffer;
+        assert 6 < getLength() && getLength() < 0xffff;
+        assert buffer.remaining() == getLength();
+    }
+
+    public long getIdentifier() {
+        return identifier;
+    }
+
+    public long getLength() {
+        return (identifier >> 48) & 0xffff;
+    }
+
+    public InputStream getStream() {
+        return new ByteBufferInputStream(getBuffer());
+    }
+
+    public ByteBuffer getBuffer() {
+        return buffer.asReadOnlyBuffer();
+    }
+
+}

Propchange: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/MediumRecord.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/Record.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/Record.java?rev=594652&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/Record.java
(added)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/Record.java
Tue Nov 13 13:42:44 2007
@@ -0,0 +1,62 @@
+/**
+ * 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 javax.jcr.RepositoryException;
+
+/**
+ * Binary record. This interface is used to access binary records stored
+ * in a {@link Journal journal}.
+ */
+public interface Record {
+
+    /**
+     * Returns the identifier of this record.
+     *
+     * @return record identifier
+     */
+    long getIdentifier();
+
+    /**
+     * Returns the length of this record.
+     *
+     * @return record length
+     * @throws RepositoryException if the record can not be accessed
+     */
+    long getLength() throws RepositoryException;
+
+    /**
+     * Returns an input stream for reading this record.
+     *
+     * @return record input stream
+     * @throws RepositoryException if the record can not be accessed
+     */
+    InputStream getStream() throws RepositoryException;
+
+    /**
+     * Returns the contents of this record as a buffer.
+     *
+     * @return read-only byte buffer
+     * @throws RepositoryException if the record can not be accessed or
+     *                             loaded fully in memory
+     */
+    ByteBuffer getBuffer() throws RepositoryException;
+
+}

Propchange: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/Record.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/SmallRecord.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/SmallRecord.java?rev=594652&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/SmallRecord.java
(added)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/SmallRecord.java
Tue Nov 13 13:42:44 2007
@@ -0,0 +1,74 @@
+/**
+ * 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;
+
+class SmallRecord implements Record {
+
+    private final long identifier;
+
+    public static long getIdentifier(byte[] buffer, long length) {
+        assert length <= 6 && length <= buffer.length;
+        long identifier = length << 48;
+        for (int i = 0; i < length; i++) {
+            identifier |= (((long) buffer[i]) & 0xff) << (5 - i) * 8;
+        }
+        return identifier;
+    }
+
+    public SmallRecord(long identifier) {
+        this.identifier = identifier;
+        assert 0 <= getLength() && getLength() <= 6;
+    }
+
+    public long getIdentifier() {
+        return identifier;
+    }
+
+    public long getLength() {
+        return identifier >> 48;
+    }
+
+    public InputStream getStream() {
+        return new InputStream() {
+            private int position = 0;
+            public int read() {
+                if (position < getLength()) {
+                    return (int) getByte(position++) & 0xff;
+                } else {
+                    return -1;
+                }
+            }
+        };
+    }
+
+    public ByteBuffer getBuffer() {
+        byte[] buffer = new byte[(int) getLength()];
+        for (int i = 0; i < buffer.length; i++) {
+            buffer[i] = (byte) getByte(i);
+        }
+        return ByteBuffer.wrap(buffer);
+    }
+
+    private byte getByte(int position) {
+        assert position < getLength();
+        return (byte) (identifier >> ((5 - position) * 8));
+    }
+
+}

Propchange: jackrabbit/sandbox/jackrabbit-ngp/src/main/java/org/apache/jackrabbit/ngp/journal/SmallRecord.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/ByteBufferInputStreamTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/ByteBufferInputStreamTest.java?rev=594652&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/ByteBufferInputStreamTest.java
(added)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/ByteBufferInputStreamTest.java
Tue Nov 13 13:42:44 2007
@@ -0,0 +1,80 @@
+/**
+ * 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.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+
+import junit.framework.TestCase;
+
+public class ByteBufferInputStreamTest extends TestCase {
+
+    private InputStream stream;
+
+    protected void setUp() {
+        ByteBuffer buffer = ByteBuffer.allocate(10);
+        for (byte i = 0; i < 10; i++) {
+            buffer.put(i);
+        }
+        buffer.rewind();
+        stream = new ByteBufferInputStream(buffer);
+    }
+
+    public void testRead() throws IOException {
+        for (int i = 0; i < 10; i++) {
+            assertEquals(i, stream.read());
+        }
+        assertEquals(-1, stream.read());
+    }
+
+    public void testReadMany() throws IOException {
+        byte[] buffer = new byte[5];
+        assertEquals(5, stream.read(buffer));
+        for (int i = 0; i < 5; i++) {
+            assertEquals(i, buffer[i]);
+        }
+        assertEquals(2, stream.read(buffer, 0, 2));
+        assertEquals(3, stream.read(buffer, 2, 3));
+        for (int i = 0; i < 5; i++) {
+            assertEquals(i + 5, buffer[i]);
+        }
+        assertEquals(-1, stream.read(buffer));
+    }
+
+    public void testSkip() throws IOException {
+        assertEquals(5, stream.skip(5));
+        assertEquals(5, stream.read());
+        assertEquals(0, stream.skip(0));
+        assertEquals(0, stream.skip(-1));
+        assertEquals(4, stream.skip(10));
+        assertEquals(-1, stream.read());
+    }
+
+    public void testMark() throws IOException {
+        assertTrue(stream.markSupported());
+        assertEquals(0, stream.read());
+        stream.mark(2);
+        assertEquals(1, stream.read());
+        stream.reset();
+        assertEquals(1, stream.read());
+        assertEquals(2, stream.read());
+        stream.reset();
+        assertEquals(1, stream.read());
+    }
+
+}

Propchange: jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/ByteBufferInputStreamTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/FileJournalTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/FileJournalTest.java?rev=594652&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/FileJournalTest.java
(added)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/FileJournalTest.java
Tue Nov 13 13:42:44 2007
@@ -0,0 +1,115 @@
+/**
+ * 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.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+
+import org.apache.jackrabbit.ngp.journal.FileJournal;
+import org.apache.jackrabbit.ngp.journal.Journal;
+import org.apache.jackrabbit.ngp.journal.Record;
+
+import junit.framework.TestCase;
+
+public class FileJournalTest extends TestCase {
+
+    private File directory;
+
+    protected void setUp() throws Exception {
+        directory = File.createTempFile("FileDataStore", "");
+        directory.delete();
+        directory.mkdir();
+    }
+
+    protected void tearDown() {
+        delete(directory);
+    }
+
+    private void delete(File file) {
+        File[] files = file.listFiles();
+        for (int i = 0; files != null && i < files.length; i++) {
+            delete(files[i]);
+        }
+        System.out.println(file.getPath() + ": " + file.length());
+        file.delete();
+    }
+
+    public void testDataStore() throws Exception {
+        Journal store = new FileJournal(directory);
+        try {
+            Record empty = store.getRecord(
+                store.addRecord(new ByteArrayInputStream(new byte[0])));
+            assertEquals(0, empty.getLength());
+
+            long abc =
+                store.addRecord(new ByteArrayInputStream("abc".getBytes()));
+            long xyz =
+                store.addRecord(new ByteArrayInputStream("xyz".getBytes()));
+            assertTrue(abc != xyz);
+            System.out.println(abc);
+            System.out.println(xyz);
+
+            byte[] data = new byte[] { 123, 34, 54, 77, 48, 24, 5, 28, 89, 2 };
+            long identifier = store.addRecord(new ByteArrayInputStream(data));
+            System.out.println(identifier);
+
+            Record record = store.getRecord(identifier);
+            assertEquals(identifier, record.getIdentifier());
+            assertEquals(data.length, record.getLength());
+
+            InputStream stream = record.getStream();
+            try {
+                for (int i = 0; i < data.length; i++) {
+                    assertEquals(data[i], (byte) stream.read());
+                }
+                assertEquals(-1, stream.read());
+            } finally {
+                stream.close();
+            }
+
+            ByteBuffer buffer = record.getBuffer();
+            assertEquals(data.length, buffer.remaining());
+            for (int i = 0; i < data.length; i++) {
+                assertEquals(data[i], buffer.get());
+            }
+
+            dump(store, new File("d:\\src\\pdfbox"));
+        } finally {
+            store.close();
+        }
+    }
+
+    private void dump(Journal store, File file) throws Exception {
+        File[] files = file.listFiles();
+        for (int i = 0; files != null && i < files.length; i++) {
+            dump(store, files[i]);
+        }
+        if (file.isFile()) {
+            InputStream stream = new FileInputStream(file);
+            try {
+                System.out.println(
+                        file.getPath() + ": " + store.addRecord(stream));
+            } finally {
+                stream.close();
+            }
+        }
+    }
+
+}

Propchange: jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/FileJournalTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/LargeRecordTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/LargeRecordTest.java?rev=594652&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/LargeRecordTest.java
(added)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/LargeRecordTest.java
Tue Nov 13 13:42:44 2007
@@ -0,0 +1,58 @@
+/**
+ * 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.File;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+
+import junit.framework.TestCase;
+
+import org.apache.jackrabbit.ngp.journal.LargeRecord;
+import org.apache.jackrabbit.ngp.journal.Record;
+
+public class LargeRecordTest extends TestCase {
+
+    private File file;
+
+    protected void setUp() throws Exception {
+        file = File.createTempFile("MediumRecord", ".bin");
+    }
+
+    protected void tearDown() throws Exception {
+        file.delete();
+    }
+
+    public void testRecord() throws Exception {
+        OutputStream stream = new FileOutputStream(file);
+        try {
+            for (int i = 0; i < 100000; i++) {
+                stream.write(i);
+            }
+        } finally {
+            stream.close();
+        }
+
+        Record record = new LargeRecord(0xffffL << 48, file);
+        assertEquals(0xffffL << 48, record.getIdentifier());
+        assertEquals(100000, record.getLength());
+        assertEquals(0, record.getStream().read());
+        assertEquals(100000, record.getBuffer().remaining());
+        assertEquals(43, record.getBuffer().get(56*256 + 43));
+    }
+
+}

Propchange: jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/LargeRecordTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/MediumRecordTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/MediumRecordTest.java?rev=594652&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/MediumRecordTest.java
(added)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/MediumRecordTest.java
Tue Nov 13 13:42:44 2007
@@ -0,0 +1,47 @@
+/**
+ * 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.nio.ByteBuffer;
+
+import junit.framework.TestCase;
+
+import org.apache.jackrabbit.ngp.journal.MediumRecord;
+import org.apache.jackrabbit.ngp.journal.Record;
+
+public class MediumRecordTest extends TestCase {
+
+    private Record record;
+
+    protected void setUp() {
+        ByteBuffer buffer = ByteBuffer.allocate(100);
+        for (byte i = 0; i < 100; i++) {
+            buffer.put(i);
+        }
+        buffer.flip();
+        record = new MediumRecord(100L << 48, buffer);
+    }
+
+    public void testRecord() throws Exception {
+        assertEquals(100L << 48, record.getIdentifier());
+        assertEquals(100, record.getLength());
+        assertEquals(0, record.getStream().read());
+        assertEquals(100, record.getBuffer().remaining());
+        assertEquals(56, record.getBuffer().get(56));
+    }
+
+}

Propchange: jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/MediumRecordTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/SmallRecordTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/SmallRecordTest.java?rev=594652&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/SmallRecordTest.java
(added)
+++ jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/SmallRecordTest.java
Tue Nov 13 13:42:44 2007
@@ -0,0 +1,54 @@
+/**
+ * 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 org.apache.jackrabbit.ngp.journal.Record;
+import org.apache.jackrabbit.ngp.journal.SmallRecord;
+
+import junit.framework.TestCase;
+
+public class SmallRecordTest extends TestCase {
+
+    public void testEmptyRecord() throws Exception {
+        Record record = new SmallRecord(
+                SmallRecord.getIdentifier(new byte[0], 0));
+        assertEquals(0, record.getIdentifier());
+        assertEquals(0, record.getLength());
+        assertEquals(-1, record.getStream().read());
+        assertEquals(0, record.getBuffer().remaining());
+    }
+
+    public void testSmallRecord() throws Exception {
+        Record record = new SmallRecord(
+                SmallRecord.getIdentifier(new byte[] { 32 }, 1));
+        assertEquals(0x0001200000000000L, record.getIdentifier());
+        assertEquals(1, record.getLength());
+        assertEquals(32, record.getStream().read());
+        assertEquals(1, record.getBuffer().remaining());
+        assertEquals(32, record.getBuffer().get());
+    }
+
+    public void testFullRecord() throws Exception {
+        Record record = new SmallRecord(
+                SmallRecord.getIdentifier(new byte[] { 0, 1, 2, 3, 4, 5 }, 6));
+        assertEquals(0x0006000102030405L, record.getIdentifier());
+        assertEquals(6, record.getLength());
+        assertEquals(0, record.getStream().read());
+        assertEquals(6, record.getBuffer().remaining());
+        assertEquals(4, record.getBuffer().get(4));
+    }
+}

Propchange: jackrabbit/sandbox/jackrabbit-ngp/src/test/java/org/apache/jackrabbit/ngp/journal/SmallRecordTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message