Return-Path: Delivered-To: apmail-jackrabbit-commits-archive@www.apache.org Received: (qmail 49541 invoked from network); 29 Sep 2010 17:39:18 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 29 Sep 2010 17:39:18 -0000 Received: (qmail 80759 invoked by uid 500); 29 Sep 2010 17:39:18 -0000 Delivered-To: apmail-jackrabbit-commits-archive@jackrabbit.apache.org Received: (qmail 80689 invoked by uid 500); 29 Sep 2010 17:39:18 -0000 Mailing-List: contact commits-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jackrabbit.apache.org Delivered-To: mailing list commits@jackrabbit.apache.org Received: (qmail 80676 invoked by uid 99); 29 Sep 2010 17:39:17 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 29 Sep 2010 17:39:17 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 29 Sep 2010 17:39:13 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 556FC2388906; Wed, 29 Sep 2010 17:38:51 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1002775 - in /jackrabbit/trunk/jackrabbit-core/src: main/java/org/apache/jackrabbit/core/persistence/bundle/ main/java/org/apache/jackrabbit/core/persistence/pool/ main/java/org/apache/jackrabbit/core/persistence/util/ test/java/org/apache... Date: Wed, 29 Sep 2010 17:38:51 -0000 To: commits@jackrabbit.apache.org From: jukka@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100929173851.556FC2388906@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jukka Date: Wed Sep 29 17:38:50 2010 New Revision: 1002775 URL: http://svn.apache.org/viewvc?rev=1002775&view=rev Log: JCR-2762: Optimize bundle serialization Split HashMapIndex to an in-memory base class and a file-based subclass for easier testing. Add basic test cases for HashMapIndex and BundleBinding. The goal is to ensure backwards compatibility of potential bundle serialization improvements. Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/FileBasedIndex.java (with props) jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/BundleBindingTest.java (with props) jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/HashMapIndexTest.java (with props) jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/TestAll.java (with props) Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/AbstractBundlePersistenceManager.java jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/AbstractBundlePersistenceManager.java jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleBinding.java jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/HashMapIndex.java jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/NodePropBundle.java Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/AbstractBundlePersistenceManager.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/AbstractBundlePersistenceManager.java?rev=1002775&r1=1002774&r2=1002775&view=diff ============================================================================== --- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/AbstractBundlePersistenceManager.java (original) +++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/AbstractBundlePersistenceManager.java Wed Sep 29 17:38:50 2010 @@ -39,7 +39,7 @@ import org.apache.jackrabbit.core.util.S import org.apache.jackrabbit.core.persistence.util.BLOBStore; import org.apache.jackrabbit.core.persistence.util.BundleBinding; import org.apache.jackrabbit.core.persistence.util.BundleCache; -import org.apache.jackrabbit.core.persistence.util.HashMapIndex; +import org.apache.jackrabbit.core.persistence.util.FileBasedIndex; import org.apache.jackrabbit.core.persistence.util.LRUNodeIdCache; import org.apache.jackrabbit.core.persistence.util.NodePropBundle; import org.apache.jackrabbit.spi.Name; @@ -249,7 +249,7 @@ public abstract class AbstractBundlePers // load name and ns index FileSystemResource nsFile = new FileSystemResource(context.getFileSystem(), RES_NS_INDEX); if (nsFile.exists()) { - nsIndex = new HashMapIndex(nsFile); + nsIndex = new FileBasedIndex(nsFile); } else { nsIndex = (StringIndex) context.getNamespaceRegistry(); } @@ -270,7 +270,8 @@ public abstract class AbstractBundlePers public StringIndex getNameIndex() { try { if (nameIndex == null) { - nameIndex = new HashMapIndex(new FileSystemResource(context.getFileSystem(), RES_NAME_INDEX)); + nameIndex = new FileBasedIndex(new FileSystemResource( + context.getFileSystem(), RES_NAME_INDEX)); } return nameIndex; } catch (Exception e) { Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/AbstractBundlePersistenceManager.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/AbstractBundlePersistenceManager.java?rev=1002775&r1=1002774&r2=1002775&view=diff ============================================================================== --- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/AbstractBundlePersistenceManager.java (original) +++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/AbstractBundlePersistenceManager.java Wed Sep 29 17:38:50 2010 @@ -35,7 +35,7 @@ import org.apache.jackrabbit.core.persis import org.apache.jackrabbit.core.persistence.PersistenceManager; import org.apache.jackrabbit.core.persistence.util.BLOBStore; import org.apache.jackrabbit.core.persistence.util.BundleCache; -import org.apache.jackrabbit.core.persistence.util.HashMapIndex; +import org.apache.jackrabbit.core.persistence.util.FileBasedIndex; import org.apache.jackrabbit.core.persistence.util.LRUNodeIdCache; import org.apache.jackrabbit.core.persistence.util.NodePropBundle; import org.apache.jackrabbit.core.state.ItemState; @@ -248,7 +248,7 @@ public abstract class AbstractBundlePers // load name and ns index FileSystemResource nsFile = new FileSystemResource(context.getFileSystem(), RES_NS_INDEX); if (nsFile.exists()) { - nsIndex = new HashMapIndex(nsFile); + nsIndex = new FileBasedIndex(nsFile); } else { nsIndex = (StringIndex) context.getNamespaceRegistry(); } @@ -269,7 +269,8 @@ public abstract class AbstractBundlePers public StringIndex getNameIndex() { try { if (nameIndex == null) { - nameIndex = new HashMapIndex(new FileSystemResource(context.getFileSystem(), RES_NAME_INDEX)); + nameIndex = new FileBasedIndex(new FileSystemResource( + context.getFileSystem(), RES_NAME_INDEX)); } return nameIndex; } catch (Exception e) { Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleBinding.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleBinding.java?rev=1002775&r1=1002774&r2=1002775&view=diff ============================================================================== --- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleBinding.java (original) +++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleBinding.java Wed Sep 29 17:38:50 2010 @@ -16,8 +16,6 @@ */ package org.apache.jackrabbit.core.persistence.util; -import java.io.DataInputStream; -import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/FileBasedIndex.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/FileBasedIndex.java?rev=1002775&view=auto ============================================================================== --- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/FileBasedIndex.java (added) +++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/FileBasedIndex.java Wed Sep 29 17:38:50 2010 @@ -0,0 +1,118 @@ +/* + * 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.core.persistence.util; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Collections; +import java.util.Map; +import java.util.Properties; + +import org.apache.jackrabbit.core.fs.FileSystemException; +import org.apache.jackrabbit.core.fs.FileSystemResource; +import org.apache.jackrabbit.core.util.StringIndex; + +/** + * Implements a {@link StringIndex} that is based on a hashmap and persists + * the names as property file. + */ +public class FileBasedIndex extends HashMapIndex { + + /** + * the filesystem resource that stores the lookup tables. + */ + private FileSystemResource file; + + /** + * the time when the resource was last modified. + */ + private long lastModified = -1; + + /** + * Creates a new hashmap index and loads the lookup tables from the + * filesystem resource. If it does not exist yet, it will create a new one. + * + * @param file the filesystem resource that stores the lookup tables. + * + * @throws IOException if an I/O error occurs. + * @throws FileSystemException if an I/O error occurs. + */ + public FileBasedIndex(FileSystemResource file) + throws FileSystemException, IOException { + this.file = file; + if (!file.exists()) { + file.makeParentDirs(); + file.getOutputStream().close(); + } + load(); + } + + /** + * Loads the lookup table from the filesystem resource. + */ + protected void load() { + try { + long modTime = file.lastModified(); + if (modTime != lastModified) { + lastModified = modTime; + + InputStream in = file.getInputStream(); + try { + Properties properties = new Properties(); + properties.load(in); + for (Object name + : Collections.list(properties.propertyNames())) { + String string = name.toString(); + Integer index = + Integer.valueOf(properties.getProperty(string)); + stringToIndex.put(string, index); + indexToString.put(index, string); + } + } finally { + in.close(); + } + } + } catch (Exception e) { + throw new IllegalStateException("Unable to load lookup table", e); + } + } + + /** + * Saves the lookup table to the filesystem resource. + */ + protected void save() { + try { + OutputStream out = file.getOutputStream(); + try { + Properties properties = new Properties(); + for (Map.Entry entry + : stringToIndex.entrySet()) { + properties.setProperty( + entry.getKey(), entry.getValue().toString()); + } + properties.store(out, "string index"); + } finally { + out.close(); + } + lastModified = file.lastModified(); + } catch (Exception e) { + throw new IllegalStateException("Unable to store lookup table", e); + } + } + +} Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/FileBasedIndex.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/HashMapIndex.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/HashMapIndex.java?rev=1002775&r1=1002774&r2=1002775&view=diff ============================================================================== --- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/HashMapIndex.java (original) +++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/HashMapIndex.java Wed Sep 29 17:38:50 2010 @@ -18,106 +18,39 @@ package org.apache.jackrabbit.core.persi import java.util.HashMap; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.Iterator; -import java.util.Properties; - -import org.apache.jackrabbit.core.fs.FileSystemException; -import org.apache.jackrabbit.core.fs.FileSystemResource; import org.apache.jackrabbit.core.util.StringIndex; /** - * Implements a {@link StringIndex} that is based on a hashmap and persists - * the names as property file. - *

- * Please note that this class is not synchronized and the calls need to ensure - * thread safeness. + * Implements a {@link StringIndex} that is based on a hashmap. Subclasses + * can override the protected {@link #load()} and {@link #save()} methods + * to implement persistent storage of the string index. + *

+ * This class is thread-safe. */ public class HashMapIndex implements StringIndex { /** * holds the string-to-index lookups. */ - private final HashMap stringToIndex = new HashMap(); + protected final HashMap stringToIndex = + new HashMap(); /** * holds the index-to-string lookups. */ - private final HashMap indexToString = new HashMap(); - - /** - * a copy of the {@link #stringToIndex} as properties class for faster - * storing. - */ - private final Properties stringToIndexProps = new Properties(); + protected final HashMap indexToString = + new HashMap(); /** - * the filesystem resource that stores the lookup tables. + * Loads the lookup table. */ - private FileSystemResource file; - - /** - * the time when the resource was last modified. - */ - private long lastModified = -1; - - /** - * Creates a new hashmap index and loads the lookup tables from the - * filesystem resource. If it does not exist yet, it will create a new one. - * - * @param file the filesystem resource that stores the lookup tables. - * - * @throws IOException if an I/O error occurs. - * @throws FileSystemException if an I/O error occurs. - */ - public HashMapIndex(FileSystemResource file) - throws FileSystemException, IOException { - this.file = file; - if (!file.exists()) { - file.makeParentDirs(); - file.getOutputStream().close(); - } - load(); - } - - /** - * Loads the lookup table from the filesystem resource. - * - * @throws IOException if an I/O error occurs. - * @throws FileSystemException if an I/O error occurs. - */ - private void load() throws IOException, FileSystemException { - long modTime = file.lastModified(); - if (modTime > lastModified) { - InputStream in = file.getInputStream(); - stringToIndexProps.clear(); - stringToIndexProps.load(in); - Iterator iter = stringToIndexProps.keySet().iterator(); - while (iter.hasNext()) { - String uri = (String) iter.next(); - String prop = stringToIndexProps.getProperty(uri); - Integer idx = Integer.valueOf(prop); - stringToIndex.put(uri, idx); - indexToString.put(idx, uri); - } - in.close(); - } - lastModified = modTime; + protected void load() { } /** - * Saves the lookup table to the filesystem resource. - * - * @throws IOException if an I/O error occurs. - * @throws FileSystemException if an I/O error occurs. + * Saves the lookup table. */ - private void save() throws IOException, FileSystemException { - OutputStream out = file.getOutputStream(); - stringToIndexProps.store(out, "string index"); - out.close(); - lastModified = file.lastModified(); + protected void save() { } /** @@ -126,30 +59,17 @@ public class HashMapIndex implements Str * This implementation reloads the table from the resource if a lookup fails * and if the resource was modified since. */ - public int stringToIndex(String nsUri) { + public synchronized int stringToIndex(String nsUri) { Integer idx = stringToIndex.get(nsUri); if (idx == null) { - try { - load(); - } catch (Exception e) { - IllegalStateException ise = new IllegalStateException("Unable to load lookup table for uri: " + nsUri); - ise.initCause(e); - throw ise; - } + load(); idx = stringToIndex.get(nsUri); } if (idx == null) { idx = Integer.valueOf(indexToString.size()); stringToIndex.put(nsUri, idx); indexToString.put(idx, nsUri); - stringToIndexProps.put(nsUri, idx.toString()); - try { - save(); - } catch (Exception e) { - IllegalStateException ise = new IllegalStateException("Unable to store lookup table for uri: " + nsUri); - ise.initCause(e); - throw ise; - } + save(); } return idx.intValue(); } @@ -160,19 +80,14 @@ public class HashMapIndex implements Str * This implementation reloads the table from the resource if a lookup fails * and if the resource was modified since. */ - public String indexToString(int i) { + public synchronized String indexToString(int i) { Integer idx = Integer.valueOf(i); String s = indexToString.get(idx); if (s == null) { - try { - load(); - } catch (Exception e) { - IllegalStateException ise = new IllegalStateException("Unable to load lookup table for index: " + i); - ise.initCause(e); - throw ise; - } + load(); s = indexToString.get(idx); } return s; } + } Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/NodePropBundle.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/NodePropBundle.java?rev=1002775&r1=1002774&r2=1002775&view=diff ============================================================================== --- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/NodePropBundle.java (original) +++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/NodePropBundle.java Wed Sep 29 17:38:50 2010 @@ -451,6 +451,24 @@ public class NodePropBundle { this.size = size; } + //--------------------------------------------------------------< Object > + + public boolean equals(Object object) { + if (object instanceof NodePropBundle) { + NodePropBundle that = (NodePropBundle) object; + return id.equals(that.id) + && parentId.equals(that.parentId) + && nodeTypeName.equals(that.nodeTypeName) + && mixinTypeNames.equals(that.mixinTypeNames) + && isReferenceable == that.isReferenceable + && sharedSet.equals(that.sharedSet) + && properties.equals(that.properties) + && childNodeEntries.equals(that.childNodeEntries); + } else { + return false; + } + } + //-----------------------------------------------------< ChildNodeEntry >--- /** Added: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/BundleBindingTest.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/BundleBindingTest.java?rev=1002775&view=auto ============================================================================== --- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/BundleBindingTest.java (added) +++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/BundleBindingTest.java Wed Sep 29 17:38:50 2010 @@ -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.core.persistence.util; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.util.Collections; + +import org.apache.jackrabbit.core.id.NodeId; +import org.apache.jackrabbit.spi.Name; +import org.apache.jackrabbit.spi.commons.name.NameConstants; + +import junit.framework.TestCase; + +public class BundleBindingTest extends TestCase { + + private BundleBinding binding; + + protected void setUp() throws Exception { + binding = new BundleBinding( + null, null, new HashMapIndex(), new HashMapIndex(), null); + } + + public void testEmptyBundle() throws Exception { + NodePropBundle bundle = new NodePropBundle(new NodeId()); + bundle.setParentId(new NodeId()); + bundle.setNodeTypeName(NameConstants.NT_UNSTRUCTURED); + bundle.setMixinTypeNames(Collections.emptySet()); + bundle.setSharedSet(Collections.emptySet()); + assertBundleRoundtrip(bundle); + } + + private void assertBundleRoundtrip(NodePropBundle bundle) + throws Exception { + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + binding.writeBundle(buffer, bundle); + byte[] bytes = buffer.toByteArray(); + + assertTrue(binding.checkBundle(new ByteArrayInputStream(bytes))); + + assertEquals(bundle, binding.readBundle( + new ByteArrayInputStream(bytes), bundle.getId())); + } +} Propchange: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/BundleBindingTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/HashMapIndexTest.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/HashMapIndexTest.java?rev=1002775&view=auto ============================================================================== --- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/HashMapIndexTest.java (added) +++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/HashMapIndexTest.java Wed Sep 29 17:38:50 2010 @@ -0,0 +1,79 @@ +/* + * 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.core.persistence.util; + +import junit.framework.TestCase; + +import org.apache.jackrabbit.core.util.StringIndex; + +public class HashMapIndexTest extends TestCase { + + private StringIndex index; + + private int load; + private int save; + + protected void setUp() throws Exception { + index = new HashMapIndex() { + @Override + protected void load() { + load++; + } + @Override + protected void save() { + save++; + } + }; + load = 0; + save = 0; + } + + public void testIndex() { + assertEquals(0, load); + assertEquals(0, save); + + int test = index.stringToIndex("test"); + assertEquals(1, load); + assertEquals(1, save); + + assertEquals(test, index.stringToIndex("test")); + assertEquals(1, load); + assertEquals(1, save); + + assertEquals("test", index.indexToString(test)); + assertEquals(1, load); + assertEquals(1, save); + + assertNull(index.indexToString(test + 1)); + assertEquals(2, load); + assertEquals(1, save); + + int foo = index.stringToIndex("foo"); + assertTrue(test != foo); + assertEquals(3, load); + assertEquals(2, save); + + assertEquals(foo, index.stringToIndex("foo")); + assertEquals(3, load); + assertEquals(2, save); + + assertEquals("foo", index.indexToString(foo)); + assertEquals(3, load); + assertEquals(2, save); + } + +} Propchange: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/HashMapIndexTest.java ------------------------------------------------------------------------------ svn:eol-style = native Added: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/TestAll.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/TestAll.java?rev=1002775&view=auto ============================================================================== --- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/TestAll.java (added) +++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/TestAll.java Wed Sep 29 17:38:50 2010 @@ -0,0 +1,33 @@ +/* + * 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.core.persistence.util; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class TestAll extends TestCase { + + public static Test suite() { + TestSuite suite = new TestSuite("Persistence utility tests"); + + suite.addTestSuite(HashMapIndexTest.class); + + return suite; + } + +} Propchange: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/persistence/util/TestAll.java ------------------------------------------------------------------------------ svn:eol-style = native