You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by th...@apache.org on 2011/06/08 11:24:03 UTC

svn commit: r1133302 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/mem/MemoryDataStore.java test/java/org/apache/jackrabbit/mk/DataStoreTest.java

Author: thomasm
Date: Wed Jun  8 09:24:03 2011
New Revision: 1133302

URL: http://svn.apache.org/viewvc?rev=1133302&view=rev
Log:
A simple in-memory data store implementation.

Added:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryDataStore.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/DataStoreTest.java

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryDataStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryDataStore.java?rev=1133302&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryDataStore.java (added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryDataStore.java Wed Jun  8 09:24:03 2011
@@ -0,0 +1,76 @@
+/*
+ * 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.mk.mem;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import org.apache.jackrabbit.mk.api.MicroKernelException;
+import org.apache.jackrabbit.mk.util.StringUtils;
+
+/**
+ * An in-memory data store implementation.
+ * Useful for testing.
+ */
+public class MemoryDataStore {
+
+    private static final String PREFIX = "mem:";
+
+    public void initialize(File homeDir) {
+        // nothing to do
+    }
+
+    public void close() {
+        // nothing to do
+    }
+
+    public String write(InputStream in) {
+        try {
+            StringBuilder buff = new StringBuilder(PREFIX);
+            int len = 0;
+            while (true) {
+                int x;
+                x = in.read();
+                if (x < 0) {
+                    break;
+                }
+                len++;
+                StringUtils.appendHex(buff, x);
+            }
+            return buff.toString();
+        } catch (IOException e) {
+            throw new MicroKernelException(e);
+        }
+    }
+
+    public long getLength(String blobId) {
+        return (blobId.length() - PREFIX.length()) / 2;
+    }
+
+    public int read(String blobId, long pos, byte[] buff, int off, int length) {
+        int end = off + length;
+        int i = (int) pos * 2 + PREFIX.length();
+        int count = 0;
+        while (off < end && i < blobId.length()) {
+            int x = (StringUtils.getHexDigit(blobId, i++) << 4) + StringUtils.getHexDigit(blobId, i++);
+            buff[off++] = (byte) x;
+            count++;
+        }
+        return count;
+    }
+
+}

Added: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/DataStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/DataStoreTest.java?rev=1133302&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/DataStoreTest.java (added)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/DataStoreTest.java Wed Jun  8 09:24:03 2011
@@ -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.mk;
+
+import java.io.ByteArrayInputStream;
+import junit.framework.TestCase;
+import org.apache.jackrabbit.mk.mem.MemoryKernelImpl;
+import org.apache.jackrabbit.mk.util.MicroKernelInputStream;
+
+/**
+ * Test the data store
+ */
+public class DataStoreTest extends TestCase {
+
+    // private MicroKernelImpl mk;
+    private MemoryKernelImpl mk;
+
+    public void setUp() throws Exception {
+        // mk = new MicroKernelImpl(System.getProperty("homeDir", "."));
+        mk = new MemoryKernelImpl();
+    }
+
+    public void tearDown() throws InterruptedException {
+        // mk.dispose();
+    }
+
+    public void test() {
+        int len = 100;
+        String[] s = new String[len];
+        for (int i = 0; i < len; i++) {
+            s[i] = mk.write(new ByteArrayInputStream(new byte[i]));
+        }
+        for (int i = 0; i < len; i++) {
+            assertEquals(i, mk.getLength(s[i]));
+            byte[] b = MicroKernelInputStream.readFully(mk, s[i]);
+            assertEquals(i, b.length);
+        }
+    }
+
+}