Skip to content

Commit

Permalink
DefaultHeaders#valueIterator to support removal (netty#9063)
Browse files Browse the repository at this point in the history
Motivation:
While iterating values it is often desirable to be able to remove individual
entries. The existing mechanism to do this involves removal of all entries and
conditional re-insertion which is heavy weight in order to remove a single
value.

Modifications:
- DefaultHeaders$ValueIterator supports removal

Result:
It is possible to remove entries while iterating the values in DefaultHeaders.
  • Loading branch information
Scottmitch authored and normanmaurer committed Apr 16, 2019
1 parent 9ed41db commit 1d9090a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
21 changes: 18 additions & 3 deletions codec/src/main/java/io/netty/handler/codec/DefaultHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,16 @@ private V remove0(int h, int i, K name) {
return value;
}

private void remove0(HeaderEntry<K, V> entry) {
int i = index(entry.hash);
HeaderEntry<K, V> e = entries[i];
if (e == entry) {
entries[i] = entry.next;
}
entry.remove();
--size;
}

@SuppressWarnings("unchecked")
private T thisT() {
return (T) this;
Expand Down Expand Up @@ -1055,6 +1065,7 @@ public void remove() {
private final class ValueIterator implements Iterator<V> {
private final K name;
private final int hash;
private HeaderEntry<K, V> previous;
private HeaderEntry<K, V> next;

ValueIterator(K name) {
Expand All @@ -1073,14 +1084,18 @@ public V next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
HeaderEntry<K, V> current = next;
previous = next;
calculateNext(next.next);
return current.value;
return previous.value;
}

@Override
public void remove() {
throw new UnsupportedOperationException("read only");
if (previous == null) {
throw new IllegalStateException();
}
remove0(previous);
previous = null;
}

private void calculateNext(HeaderEntry<K, V> entry) {
Expand Down
38 changes: 38 additions & 0 deletions codec/src/test/java/io/netty/handler/codec/DefaultHeadersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,47 @@ public void multipleValuesPerNameIterator() {
Iterator<CharSequence> itr = headers.valueIterator(of("name"));
while (itr.hasNext()) {
values.add(itr.next());
itr.remove();
}
assertEquals(3, values.size());
assertEquals(0, headers.size());
assertTrue(headers.isEmpty());
assertTrue(values.containsAll(asList(of("value1"), of("value2"), of("value3"))));
itr = headers.valueIterator(of("name"));
assertFalse(itr.hasNext());
}

@Test(expected = IllegalStateException.class)
public void valuesItrRemoveThrowsWhenEmpty() {
TestDefaultHeaders headers = newInstance();
assertEquals(0, headers.size());
assertTrue(headers.isEmpty());
Iterator<CharSequence> itr = headers.valueIterator(of("name"));
itr.remove();
}

@Test
public void valuesItrRemoveThrowsAfterLastElement() {
TestDefaultHeaders headers = newInstance();
headers.add(of("name"), of("value1"));
assertEquals(1, headers.size());

List<CharSequence> values = new ArrayList<CharSequence>();
Iterator<CharSequence> itr = headers.valueIterator(of("name"));
while (itr.hasNext()) {
values.add(itr.next());
itr.remove();
}
assertEquals(1, values.size());
assertEquals(0, headers.size());
assertTrue(headers.isEmpty());
assertTrue(values.contains(of("value1")));
try {
itr.remove();
fail();
} catch (IllegalStateException ignored) {
// ignored
}
}

@Test
Expand Down

0 comments on commit 1d9090a

Please sign in to comment.