Return-Path: X-Original-To: apmail-activemq-commits-archive@www.apache.org Delivered-To: apmail-activemq-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E8BBB8F2B for ; Sat, 3 Sep 2011 21:39:36 +0000 (UTC) Received: (qmail 55320 invoked by uid 500); 3 Sep 2011 21:39:36 -0000 Delivered-To: apmail-activemq-commits-archive@activemq.apache.org Received: (qmail 55258 invoked by uid 500); 3 Sep 2011 21:39:35 -0000 Mailing-List: contact commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@activemq.apache.org Delivered-To: mailing list commits@activemq.apache.org Received: (qmail 55251 invoked by uid 99); 3 Sep 2011 21:39:35 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 03 Sep 2011 21:39:35 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.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; Sat, 03 Sep 2011 21:39:33 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 3953923889E2 for ; Sat, 3 Sep 2011 21:39:13 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1164936 - in /activemq/trunk/kahadb/src: main/java/org/apache/kahadb/index/ main/java/org/apache/kahadb/util/ test/java/org/apache/kahadb/util/ Date: Sat, 03 Sep 2011 21:39:13 -0000 To: commits@activemq.apache.org From: tabish@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110903213913.3953923889E2@eris.apache.org> Author: tabish Date: Sat Sep 3 21:39:12 2011 New Revision: 1164936 URL: http://svn.apache.org/viewvc?rev=1164936&view=rev Log: Some updates and changes to support some work on https://issues.apache.org/jira/browse/AMQ-3467 Added: activemq/trunk/kahadb/src/test/java/org/apache/kahadb/util/ activemq/trunk/kahadb/src/test/java/org/apache/kahadb/util/SequenceSetTest.java (with props) Modified: activemq/trunk/kahadb/src/main/java/org/apache/kahadb/index/ListIndex.java activemq/trunk/kahadb/src/main/java/org/apache/kahadb/index/ListNode.java activemq/trunk/kahadb/src/main/java/org/apache/kahadb/util/SequenceSet.java Modified: activemq/trunk/kahadb/src/main/java/org/apache/kahadb/index/ListIndex.java URL: http://svn.apache.org/viewvc/activemq/trunk/kahadb/src/main/java/org/apache/kahadb/index/ListIndex.java?rev=1164936&r1=1164935&r2=1164936&view=diff ============================================================================== --- activemq/trunk/kahadb/src/main/java/org/apache/kahadb/index/ListIndex.java (original) +++ activemq/trunk/kahadb/src/main/java/org/apache/kahadb/index/ListIndex.java Sat Sep 3 21:39:12 2011 @@ -52,6 +52,11 @@ public class ListIndex implem setHeadPageId(headPageId); } + @SuppressWarnings("rawtypes") + public ListIndex(PageFile pageFile, Page page) { + this(pageFile, page.getPageId()); + } + synchronized public void load(Transaction tx) throws IOException { if (loaded.compareAndSet(false, true)) { LOG.debug("loading"); @@ -82,12 +87,12 @@ public class ListIndex implem } } } - + synchronized public void unload(Transaction tx) { if (loaded.compareAndSet(true, false)) { - } + } } - + protected ListNode getHead(Transaction tx) throws IOException { return loadNode(tx, getHeadPageId()); } @@ -181,7 +186,7 @@ public class ListIndex implem synchronized public Iterator> iterator(final Transaction tx) throws IOException { return getHead(tx).iterator(tx); } - + synchronized public Iterator> iterator(final Transaction tx, long initialPosition) throws IOException { return getHead(tx).iterator(tx, initialPosition); } @@ -223,7 +228,7 @@ public class ListIndex implem public void storeNode(Transaction tx, ListNode node, boolean overflow) throws IOException { tx.store(node.getPage(), marshaller, overflow); } - + public PageFile getPageFile() { return pageFile; } Modified: activemq/trunk/kahadb/src/main/java/org/apache/kahadb/index/ListNode.java URL: http://svn.apache.org/viewvc/activemq/trunk/kahadb/src/main/java/org/apache/kahadb/index/ListNode.java?rev=1164936&r1=1164935&r2=1164936&view=diff ============================================================================== --- activemq/trunk/kahadb/src/main/java/org/apache/kahadb/index/ListNode.java (original) +++ activemq/trunk/kahadb/src/main/java/org/apache/kahadb/index/ListNode.java Sat Sep 3 21:39:12 2011 @@ -54,7 +54,6 @@ public final class ListNode { // The next page after this one. private long next = ListIndex.NOT_SET; - static final class KeyValueEntry extends LinkedNode> implements Entry { private final Key key; @@ -254,7 +253,7 @@ public final class ListNode { } } - @SuppressWarnings("unchecked") + @SuppressWarnings({ "unchecked", "rawtypes" }) public ListNode readPayload(DataInput is) throws IOException { ListNode node = new ListNode(); node.next = is.readLong(); @@ -290,8 +289,8 @@ public final class ListNode { try { getContainingList().storeNode(tx, this, false); } catch ( Transaction.PageOverflowIOException e ) { - // If we get an overflow - split(tx, addFirst); + // If we get an overflow + split(tx, addFirst); } } @@ -384,7 +383,7 @@ public final class ListNode { /////////////////////////////////////////////////////////////////// // Implementation methods /////////////////////////////////////////////////////////////////// - + public long getPageId() { return page.getPageId(); } Modified: activemq/trunk/kahadb/src/main/java/org/apache/kahadb/util/SequenceSet.java URL: http://svn.apache.org/viewvc/activemq/trunk/kahadb/src/main/java/org/apache/kahadb/util/SequenceSet.java?rev=1164936&r1=1164935&r2=1164936&view=diff ============================================================================== --- activemq/trunk/kahadb/src/main/java/org/apache/kahadb/util/SequenceSet.java (original) +++ activemq/trunk/kahadb/src/main/java/org/apache/kahadb/util/SequenceSet.java Sat Sep 3 21:39:12 2011 @@ -20,6 +20,7 @@ import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; @@ -27,15 +28,15 @@ import java.util.NoSuchElementException; * Keeps track of a added long values. Collapses ranges of numbers using a * Sequence representation. Use to keep track of received message ids to find * out if a message is duplicate or if there are any missing messages. - * + * * @author chirino */ -public class SequenceSet extends LinkedNodeList { +public class SequenceSet extends LinkedNodeList implements Iterable { public static class Marshaller implements org.apache.kahadb.util.Marshaller { public static final Marshaller INSTANCE = new Marshaller(); - + public SequenceSet readPayload(DataInput in) throws IOException { SequenceSet value = new SequenceSet(); int count = in.readInt(); @@ -85,17 +86,16 @@ public class SequenceSet extends LinkedN return true; } } - + public void add(Sequence value) { // TODO we can probably optimize this a bit for(long i=value.first; i iterator() { + return new SequenceIterator(); + } + + private class SequenceIterator implements Iterator { + + private Sequence currentEntry; + private long lastReturned; + + public SequenceIterator() { + currentEntry = getHead(); + lastReturned = currentEntry.first - 1; + } + + public boolean hasNext() { + return currentEntry != null; + } + + public Long next() { + if (currentEntry == null) { + throw new NoSuchElementException(); + } + + if(lastReturned < currentEntry.first) { + lastReturned = currentEntry.first; + if (currentEntry.range() == 1) { + currentEntry = currentEntry.getNext(); + } + } else { + lastReturned++; + if (lastReturned == currentEntry.last) { + currentEntry = currentEntry.getNext(); + } + } + + return lastReturned; + } + + public void remove() { + throw new UnsupportedOperationException(); + } + + } + } \ No newline at end of file Added: activemq/trunk/kahadb/src/test/java/org/apache/kahadb/util/SequenceSetTest.java URL: http://svn.apache.org/viewvc/activemq/trunk/kahadb/src/test/java/org/apache/kahadb/util/SequenceSetTest.java?rev=1164936&view=auto ============================================================================== --- activemq/trunk/kahadb/src/test/java/org/apache/kahadb/util/SequenceSetTest.java (added) +++ activemq/trunk/kahadb/src/test/java/org/apache/kahadb/util/SequenceSetTest.java Sat Sep 3 21:39:12 2011 @@ -0,0 +1,129 @@ +/** + * 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.kahadb.util; + +import static org.junit.Assert.*; + +import java.util.Iterator; + +import org.junit.Test; + +public class SequenceSetTest { + + @Test + public void testAddLong() { + SequenceSet set = new SequenceSet(); + set.add(1); + assertEquals(1, set.rangeSize()); + set.add(10); + set.add(20); + assertEquals(3, set.rangeSize()); + } + + @Test + public void testRangeSize() { + SequenceSet set = new SequenceSet(); + set.add(1); + assertEquals(1, set.rangeSize()); + set.add(10); + set.add(20); + assertEquals(3, set.rangeSize()); + set.clear(); + assertEquals(0, set.rangeSize()); + } + + @Test + public void testIsEmpty() { + SequenceSet set = new SequenceSet(); + assertTrue(set.isEmpty()); + set.add(1); + assertFalse(set.isEmpty()); + } + + @Test + public void testClear() { + SequenceSet set = new SequenceSet(); + set.clear(); + assertTrue(set.isEmpty()); + set.add(1); + assertFalse(set.isEmpty()); + set.clear(); + assertTrue(set.isEmpty()); + } + + @Test + public void testContains() { + SequenceSet set = new SequenceSet(); + set.add(new Sequence(0, 10)); + set.add(new Sequence(21, 42)); + set.add(new Sequence(47, 90)); + set.add(new Sequence(142, 512)); + + assertTrue(set.contains(0)); + assertTrue(set.contains(42)); + assertTrue(set.contains(49)); + assertTrue(set.contains(153)); + + assertFalse(set.contains(43)); + assertFalse(set.contains(99)); + assertFalse(set.contains(-1)); + assertFalse(set.contains(11)); + } + + @Test + public void testRemove() { + SequenceSet set = new SequenceSet(); + set.add(new Sequence(0, 100)); + assertEquals(101, set.rangeSize()); + + assertEquals(1, set.size()); + assertFalse(set.remove(101)); + assertTrue(set.remove(50)); + assertEquals(2, set.size()); + assertEquals(100, set.rangeSize()); + assertFalse(set.remove(101)); + + set.remove(0); + assertEquals(2, set.size()); + assertEquals(99, set.rangeSize()); + set.remove(100); + assertEquals(2, set.size()); + assertEquals(98, set.rangeSize()); + + set.remove(10); + assertEquals(3, set.size()); + assertEquals(97, set.rangeSize()); + } + + @Test + public void testIterator() { + SequenceSet set = new SequenceSet(); + set.add(new Sequence(0, 2)); + set.add(new Sequence(4, 5)); + set.add(new Sequence(7)); + set.add(new Sequence(20, 21)); + + long expected[] = new long[]{0, 1, 2, 4, 5, 7, 20, 21}; + int index = 0; + + Iterator iterator = set.iterator(); + while(iterator.hasNext()) { + assertEquals(expected[index++], iterator.next().longValue()); + } + } +} Propchange: activemq/trunk/kahadb/src/test/java/org/apache/kahadb/util/SequenceSetTest.java ------------------------------------------------------------------------------ svn:eol-style = native