@NotNull @Override public NumEntries getNumEntries(@NotNull String principalName, long max) { Tree tree = getPrincipalRoot(principalName); if (tree == null) { return NumEntries.ZERO; } else { // if rep:numPermissions is present it contains the exact number of // access controlled nodes for the given principal name. // if this property is missing (backward compat) we use the old // mechanism and use child-cnt with a max value to get a rough idea // about the magnitude (note: this approximation ignores the hash-collisions) long l = TreeUtil.getLong(tree, REP_NUM_PERMISSIONS, -1); return (l >= 0) ? NumEntries.valueOf(l, true) : NumEntries.valueOf(tree.getChildrenCount(max), false); } }
static NumEntries valueOf(long size, boolean isExact) { if (size == 0) { // if size is zero we assume that this is the correct value // irrespective of the isExact flag. return ZERO; } else { return new NumEntries(size, isExact); } } }
@Test public void testHashCode() { assertEquals(NumEntries.ZERO.hashCode(), NumEntries.ZERO.hashCode()); NumEntries ne1True = NumEntries.valueOf(1, true); NumEntries ne1False = NumEntries.valueOf(1, false); assertEquals(ne1True.hashCode(), ne1True.hashCode()); assertNotEquals(ne1False.hashCode(), ne1True.hashCode()); } }
@Test public void testEquals() { assertEquals(NumEntries.ZERO, NumEntries.ZERO); NumEntries ne1True = NumEntries.valueOf(1, true); NumEntries ne2True = NumEntries.valueOf(2, true); NumEntries ne1False = NumEntries.valueOf(1, false); assertEquals(ne1True, ne1True); assertNotEquals(ne1False, ne1True); assertNotEquals(ne2True, ne1True); assertFalse(ne1True.equals(null)); assertFalse(ne1True.equals(new Object())); }
@NotNull @Override public NumEntries getNumEntries(@NotNull String principalName, long max) { Tree tree = getPrincipalRoot(principalName); if (tree == null) { return NumEntries.ZERO; } else { // if rep:numPermissions is present it contains the exact number of // access controlled nodes for the given principal name. // if this property is missing (backward compat) we use the old // mechanism and use child-cnt with a max value to get a rough idea // about the magnitude (note: this approximation ignores the hash-collisions) long l = TreeUtil.getLong(tree, REP_NUM_PERMISSIONS, -1); return (l >= 0) ? NumEntries.valueOf(l, true) : NumEntries.valueOf(tree.getChildrenCount(max), false); } }
static NumEntries valueOf(long size, boolean isExact) { if (size == 0) { // if size is zero we assume that this is the correct value // irrespective of the isExact flag. return ZERO; } else { return new NumEntries(size, isExact); } } }
@NotNull @Override public NumEntries getNumEntries(@NotNull String principalName, long max) { long cnt = 0; switch (principalName) { case GROUP_LONG_MAX_MINUS_10: cnt = Long.MAX_VALUE - 10; break; case GROUP_50: cnt = 50; break; case GROUP_LONG_MAX: cnt = Long.MAX_VALUE; break; } return NumEntries.valueOf(cnt, true); }
@NotNull @Override public NumEntries getNumEntries(@NotNull String principalName, long max) { long num = 0; boolean isExact = true; for (PermissionStoreImpl store : stores) { NumEntries ne = store.getNumEntries(principalName, max); num = LongUtils.safeAdd(num, ne.size); if (!ne.isExact) { isExact = false; } // if any of the stores doesn't reveal the exact number and max // is reached, stop asking the remaining stores. // as long as every store is reporting the exact number continue // in order to (possibly) be able to return the exact number. if (num >= max && !isExact) { break; } } return NumEntries.valueOf(num, isExact); }
@NotNull @Override public NumEntries getNumEntries(@NotNull String principalName, long max) { long num = 0; boolean isExact = true; for (PermissionStoreImpl store : stores) { NumEntries ne = store.getNumEntries(principalName, max); num = LongUtils.safeAdd(num, ne.size); if (!ne.isExact) { isExact = false; } // if any of the stores doesn't reveal the exact number and max // is reached, stop asking the remaining stores. // as long as every store is reporting the exact number continue // in order to (possibly) be able to return the exact number. if (num >= max && !isExact) { break; } } return NumEntries.valueOf(num, isExact); }
@Test public void testGetNumEntriesMaxReachedExact() throws Exception { PermissionStoreImpl mock = insertMockStore(); when(mock.getNumEntries(anyString(), anyLong())).thenReturn(NumEntries.valueOf(2, true)); NumEntries ne = permissionStore.getNumEntries(EveryonePrincipal.NAME, 10); assertEquals(NumEntries.valueOf(4, true), ne); ne = permissionStore.getNumEntries(EveryonePrincipal.NAME, 2); assertEquals(NumEntries.valueOf(4, true), ne); }
@Test public void testValueOfExactZero() { assertSame(NumEntries.ZERO, NumEntries.valueOf(0, true)); }
@Test public void testValueOfNotExactZero() { NumEntries ne = NumEntries.valueOf(0, false); assertSame(NumEntries.ZERO, ne); }
@Test public void testGetNumEntriesMaxReachedNotExact() throws Exception { PermissionStoreImpl mock = insertMockStore(); when(mock.getNumEntries(anyString(), anyLong())).thenReturn(NumEntries.valueOf(2, false)); NumEntries ne = permissionStore.getNumEntries(EveryonePrincipal.NAME, 10); assertEquals(NumEntries.valueOf(4, false), ne); ne = permissionStore.getNumEntries(EveryonePrincipal.NAME, 2); assertEquals(NumEntries.valueOf(2, false), ne); }
@Test public void testGetNumEntriesMissingPrincipalRoot() { assertEquals(NumEntries.valueOf(0, true), permissionStore.getNumEntries(testPrincipal.getName(), Long.MAX_VALUE)); }
@Test public void testBuildFewEntriesDifferentPaths() throws Exception { PrincipalPermissionEntries ppeA = new PrincipalPermissionEntries(1); ppeA.putEntriesByPath("/path", ImmutableSet.of(new PermissionEntry("/path", false, 0, PrivilegeBits.BUILT_IN.get(PrivilegeBits.REP_READ_NODES), RestrictionPattern.EMPTY))); PrincipalPermissionEntries ppeB = new PrincipalPermissionEntries(1); ppeB.putEntriesByPath("/path", ImmutableSet.of(new PermissionEntry("/path", false, 1, PrivilegeBits.BUILT_IN.get(PrivilegeBits.REP_READ_NODES), RestrictionPattern.EMPTY))); when(store.load("a")).thenReturn(ppeA); when(store.load("b")).thenReturn(ppeB); when(store.getNumEntries(anyString(), anyLong())).thenReturn(NumEntries.valueOf(1, true)); Set<String> principalNames = Sets.newHashSet("a", "b"); assertFalse(permissionCacheBuilder.init(principalNames, Long.MAX_VALUE)); PermissionCache cache = permissionCacheBuilder.build(); assertEquals(SIMPLE_CLASS_NAME, cache.getClass().getName()); }
@Test public void testNoEntriesNonExactCnt() throws Exception { when(store.load("a")).thenReturn(new PrincipalPermissionEntries()); when(store.load("b")).thenReturn(new PrincipalPermissionEntries()); when(store.getNumEntries(anyString(), anyLong())).thenReturn(NumEntries.valueOf(1, false)); Set<String> principalNames = Sets.newHashSet("a", "b"); assertFalse(permissionCacheBuilder.init(principalNames, Long.MAX_VALUE)); PermissionCache cache = permissionCacheBuilder.build(); assertEquals(EMPTY_CLASS_NAME, cache.getClass().getName()); }
@Test public void testValueOfExact() { NumEntries ne = NumEntries.valueOf(25, true); assertTrue(ne.isExact); assertEquals(25, ne.size); }
@Test public void testValueOfNotExact() { NumEntries ne = NumEntries.valueOf(25, false); assertFalse(ne.isExact); assertEquals(25, ne.size); }
@Test public void testBuildFewEntriesSamePath() throws Exception { PrincipalPermissionEntries ppeA = new PrincipalPermissionEntries(1); ppeA.putEntriesByPath("/path", ImmutableSet.of(new PermissionEntry("/path", false, 0, PrivilegeBits.BUILT_IN.get(PrivilegeBits.REP_READ_NODES), RestrictionPattern.EMPTY))); PrincipalPermissionEntries ppeB = new PrincipalPermissionEntries(1); ppeB.putEntriesByPath("/path", ImmutableSet.of(new PermissionEntry("/path", false, 1, PrivilegeBits.BUILT_IN.get(PrivilegeBits.REP_READ_NODES), RestrictionPattern.EMPTY))); when(store.load("a")).thenReturn(ppeA); when(store.load("b")).thenReturn(ppeB); when(store.getNumEntries(anyString(), anyLong())).thenReturn(NumEntries.valueOf(1, true)); Set<String> principalNames = Sets.newHashSet("a", "b"); assertFalse(permissionCacheBuilder.init(principalNames, Long.MAX_VALUE)); PermissionCache cache = permissionCacheBuilder.build(); assertEquals(SIMPLE_CLASS_NAME, cache.getClass().getName()); }
@Test public void testGetNumEntries() { assertEquals(NumEntries.valueOf(2, true), permissionStore.getNumEntries(EveryonePrincipal.NAME, Long.MAX_VALUE)); }