Add an isEmpty convenience method to BpfMap.

Test: new unit test
Change-Id: Ibec09f328e24111aee4760af2f75ea5a80ba84c3
This commit is contained in:
Lorenzo Colitti
2021-01-27 00:48:41 +09:00
parent 7bf39e56e1
commit 77262cea76
2 changed files with 18 additions and 0 deletions

View File

@@ -134,6 +134,11 @@ public class BpfMap<K extends Struct, V extends Struct> implements AutoCloseable
return deleteMapEntry(mMapFd, key.writeToBytes()); return deleteMapEntry(mMapFd, key.writeToBytes());
} }
/** Returns {@code true} if this map contains no elements. */
public boolean isEmpty() throws ErrnoException {
return getFirstKey() == null;
}
private K getNextKeyInternal(@Nullable K key) throws ErrnoException { private K getNextKeyInternal(@Nullable K key) throws ErrnoException {
final byte[] rawKey = getNextRawKey( final byte[] rawKey = getNextRawKey(
key == null ? null : key.writeToBytes()); key == null ? null : key.writeToBytes());

View File

@@ -91,6 +91,7 @@ public final class BpfMapTest {
} }
}); });
assertNull(mTestMap.getFirstKey()); assertNull(mTestMap.getFirstKey());
assertTrue(mTestMap.isEmpty());
} }
private TetherDownstream6Key createTetherDownstream6Key(long iif, String address) private TetherDownstream6Key createTetherDownstream6Key(long iif, String address)
@@ -135,6 +136,18 @@ public final class BpfMapTest {
} }
} }
@Test
public void testIsEmpty() throws Exception {
assertNull(mTestMap.getFirstKey());
assertTrue(mTestMap.isEmpty());
mTestMap.insertEntry(mTestData.keyAt(0), mTestData.valueAt(0));
assertFalse(mTestMap.isEmpty());
mTestMap.deleteEntry((mTestData.keyAt(0)));
assertTrue(mTestMap.isEmpty());
}
@Test @Test
public void testGetFirstKey() throws Exception { public void testGetFirstKey() throws Exception {
// getFirstKey on an empty map returns null. // getFirstKey on an empty map returns null.