From b322366472b48a627bc8eedc4bcee777d6c2a473 Mon Sep 17 00:00:00 2001 From: Rishabh Kumar Date: Thu, 12 Sep 2024 20:55:51 +0530 Subject: [PATCH 1/3] OAK-11112 : replaced Maps.newHashMapWithExpectedSize() with new HashMap<>(initialCapacity) --- .../org/apache/jackrabbit/oak/security/user/GroupImpl.java | 4 ++-- .../jackrabbit/oak/security/user/MembershipWriter.java | 7 +++---- .../apache/jackrabbit/oak/security/user/UserImporter.java | 2 +- .../org/apache/jackrabbit/oak/segment/SegmentIdTable.java | 5 ++--- .../jackrabbit/oak/segment/file/tar/GraphLoader.java | 4 ++-- 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/GroupImpl.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/GroupImpl.java index 473e03eb4c8..7dc575da12e 100644 --- a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/GroupImpl.java +++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/GroupImpl.java @@ -18,7 +18,6 @@ import org.apache.jackrabbit.guava.common.base.Stopwatch; import org.apache.jackrabbit.guava.common.base.Strings; -import org.apache.jackrabbit.guava.common.collect.Maps; import org.apache.jackrabbit.api.security.user.Authorizable; import org.apache.jackrabbit.api.security.user.Group; import org.apache.jackrabbit.api.security.user.UserManager; @@ -36,6 +35,7 @@ import javax.jcr.RepositoryException; import javax.jcr.nodetype.ConstraintViolationException; import java.security.Principal; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; @@ -298,7 +298,7 @@ private Set updateMembers(boolean isRemove, @NotNull String... memberIds } // calculate the contentID for each memberId and remember ids that cannot be processed - Map updateMap = Maps.newHashMapWithExpectedSize(memberIds.length); + Map updateMap = new HashMap<>((int)Math.ceil(memberIds.length / 0.75)); MembershipProvider mp = getMembershipProvider(); for (String memberId : memberIds) { if (Strings.isNullOrEmpty(memberId)) { diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/MembershipWriter.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/MembershipWriter.java index 0f87fb3a977..107f824454c 100644 --- a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/MembershipWriter.java +++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/MembershipWriter.java @@ -16,13 +16,12 @@ */ package org.apache.jackrabbit.oak.security.user; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; -import org.apache.jackrabbit.guava.common.collect.Maps; -import org.apache.jackrabbit.guava.common.collect.Sets; import org.apache.jackrabbit.JcrConstants; import org.apache.jackrabbit.oak.api.PropertyState; import org.apache.jackrabbit.oak.api.Tree; @@ -59,7 +58,7 @@ void setMembershipSizeThreshold(int membershipSizeThreshold) { * @return {@code true} if the member was added */ boolean addMember(@NotNull Tree groupTree, @NotNull String memberContentId) { - Map m = Maps.newHashMapWithExpectedSize(1); + Map m = new HashMap<>(1); m.put(memberContentId, "-"); return addMembers(groupTree, m).isEmpty(); } @@ -189,7 +188,7 @@ private static String nextRefNodeName(@NotNull Tree membersList) { * @return {@code true} if the member was removed. */ boolean removeMember(@NotNull Tree groupTree, @NotNull String memberContentId) { - Map m = Maps.newHashMapWithExpectedSize(1); + Map m = new HashMap<>(1); m.put(memberContentId, "-"); return removeMembers(groupTree, m).isEmpty(); } diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImporter.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImporter.java index c794337df52..9f1d48816ba 100644 --- a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImporter.java +++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImporter.java @@ -657,7 +657,7 @@ void process() throws RepositoryException { @NotNull Map getAuthorizablesToAdd(@NotNull Group gr, @NotNull Map toRemove, @NotNull Map nonExisting) throws RepositoryException { - Map toAdd = Maps.newHashMapWithExpectedSize(members.size()); + Map toAdd = new HashMap<>(members.size()); for (String contentId : members) { // NOTE: no need to check for re-mapped uuids with the referenceTracker because // ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW is not supported for user/group imports (see line 189) diff --git a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentIdTable.java b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentIdTable.java index 0a07dfa582d..e8cc33cf6e9 100644 --- a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentIdTable.java +++ b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentIdTable.java @@ -19,12 +19,12 @@ package org.apache.jackrabbit.oak.segment; import static org.apache.jackrabbit.guava.common.collect.Lists.newArrayList; -import static org.apache.jackrabbit.guava.common.collect.Maps.newHashMapWithExpectedSize; import static java.util.Collections.nCopies; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -130,8 +130,7 @@ void collectReferencedIds(Collection ids) { private synchronized Collection refresh() { int size = references.size(); - Map> ids = - newHashMapWithExpectedSize(size); + Map> ids = new HashMap<>((int)Math.ceil(size / 0.75)); boolean hashCollisions = false; boolean emptyReferences = false; diff --git a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/GraphLoader.java b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/GraphLoader.java index 6837ffe5833..d81af3e5fe7 100644 --- a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/GraphLoader.java +++ b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/GraphLoader.java @@ -18,11 +18,11 @@ */ package org.apache.jackrabbit.oak.segment.file.tar; -import static org.apache.jackrabbit.guava.common.collect.Maps.newHashMapWithExpectedSize; import static org.apache.jackrabbit.oak.segment.file.tar.TarConstants.GRAPH_MAGIC; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; @@ -93,7 +93,7 @@ public static Buffer loadGraph(ReaderAtEnd readerAtEnd) throws IOException { public static Map> parseGraph(Buffer buffer) { int nEntries = buffer.getInt(buffer.limit() - 12); - Map> graph = newHashMapWithExpectedSize(nEntries); + Map> graph = new HashMap<>((int)Math.ceil(nEntries / 0.75)); for (int i = 0; i < nEntries; i++) { long msb = buffer.getLong(); From 57f5d02c83b1f7531673eb2d9383e9238e946eb9 Mon Sep 17 00:00:00 2001 From: Rishabh Kumar Date: Thu, 12 Sep 2024 22:32:19 +0530 Subject: [PATCH 2/3] OAK-11112 : added ensure capacity method to collectionutils to create collections with expected capacity --- .../org/apache/jackrabbit/oak/security/user/GroupImpl.java | 2 +- .../org/apache/jackrabbit/oak/security/user/UserImporter.java | 3 ++- .../java/org/apache/jackrabbit/oak/segment/SegmentIdTable.java | 3 ++- .../apache/jackrabbit/oak/segment/file/tar/GraphLoader.java | 3 ++- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/GroupImpl.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/GroupImpl.java index 7dc575da12e..dc4dcdfc906 100644 --- a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/GroupImpl.java +++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/GroupImpl.java @@ -298,7 +298,7 @@ private Set updateMembers(boolean isRemove, @NotNull String... memberIds } // calculate the contentID for each memberId and remember ids that cannot be processed - Map updateMap = new HashMap<>((int)Math.ceil(memberIds.length / 0.75)); + Map updateMap = new HashMap<>(CollectionUtils.ensureCapacity(memberIds.length)); MembershipProvider mp = getMembershipProvider(); for (String memberId : memberIds) { if (Strings.isNullOrEmpty(memberId)) { diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImporter.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImporter.java index 9f1d48816ba..fdcdb554664 100644 --- a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImporter.java +++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImporter.java @@ -33,6 +33,7 @@ import org.apache.jackrabbit.oak.api.Root; import org.apache.jackrabbit.oak.api.Tree; import org.apache.jackrabbit.oak.api.Type; +import org.apache.jackrabbit.oak.commons.collections.CollectionUtils; import org.apache.jackrabbit.oak.namepath.NamePathMapper; import org.apache.jackrabbit.oak.plugins.identifier.IdentifierManager; import org.apache.jackrabbit.oak.plugins.memory.PropertyStates; @@ -657,7 +658,7 @@ void process() throws RepositoryException { @NotNull Map getAuthorizablesToAdd(@NotNull Group gr, @NotNull Map toRemove, @NotNull Map nonExisting) throws RepositoryException { - Map toAdd = new HashMap<>(members.size()); + Map toAdd = new HashMap<>(CollectionUtils.ensureCapacity(members.size())); for (String contentId : members) { // NOTE: no need to check for re-mapped uuids with the referenceTracker because // ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW is not supported for user/group imports (see line 189) diff --git a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentIdTable.java b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentIdTable.java index e8cc33cf6e9..9723e0e741e 100644 --- a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentIdTable.java +++ b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentIdTable.java @@ -30,6 +30,7 @@ import java.util.Set; import java.util.UUID; +import org.apache.jackrabbit.oak.commons.collections.CollectionUtils; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -130,7 +131,7 @@ void collectReferencedIds(Collection ids) { private synchronized Collection refresh() { int size = references.size(); - Map> ids = new HashMap<>((int)Math.ceil(size / 0.75)); + Map> ids = new HashMap<>(CollectionUtils.ensureCapacity(size)); boolean hashCollisions = false; boolean emptyReferences = false; diff --git a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/GraphLoader.java b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/GraphLoader.java index d81af3e5fe7..9c4ceaac22b 100644 --- a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/GraphLoader.java +++ b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/GraphLoader.java @@ -29,6 +29,7 @@ import java.util.zip.CRC32; import org.apache.jackrabbit.oak.commons.Buffer; +import org.apache.jackrabbit.oak.commons.collections.CollectionUtils; import org.apache.jackrabbit.oak.segment.util.ReaderAtEnd; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -93,7 +94,7 @@ public static Buffer loadGraph(ReaderAtEnd readerAtEnd) throws IOException { public static Map> parseGraph(Buffer buffer) { int nEntries = buffer.getInt(buffer.limit() - 12); - Map> graph = new HashMap<>((int)Math.ceil(nEntries / 0.75)); + Map> graph = new HashMap<>(CollectionUtils.ensureCapacity(nEntries)); for (int i = 0; i < nEntries; i++) { long msb = buffer.getLong(); From 0ec3fc822aad2c909e481dd89d847548b9118f03 Mon Sep 17 00:00:00 2001 From: Rishabh Kumar Date: Mon, 16 Sep 2024 11:36:21 +0530 Subject: [PATCH 3/3] OAK-11112 : added newHashMap api in CollectionUtils with expected capacity --- .../org/apache/jackrabbit/oak/security/user/GroupImpl.java | 3 +-- .../org/apache/jackrabbit/oak/security/user/UserImporter.java | 3 +-- .../java/org/apache/jackrabbit/oak/segment/SegmentIdTable.java | 3 +-- .../apache/jackrabbit/oak/segment/file/tar/GraphLoader.java | 3 +-- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/GroupImpl.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/GroupImpl.java index dc4dcdfc906..662398415de 100644 --- a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/GroupImpl.java +++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/GroupImpl.java @@ -35,7 +35,6 @@ import javax.jcr.RepositoryException; import javax.jcr.nodetype.ConstraintViolationException; import java.security.Principal; -import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; @@ -298,7 +297,7 @@ private Set updateMembers(boolean isRemove, @NotNull String... memberIds } // calculate the contentID for each memberId and remember ids that cannot be processed - Map updateMap = new HashMap<>(CollectionUtils.ensureCapacity(memberIds.length)); + Map updateMap = CollectionUtils.newHashMap(memberIds.length); MembershipProvider mp = getMembershipProvider(); for (String memberId : memberIds) { if (Strings.isNullOrEmpty(memberId)) { diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImporter.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImporter.java index fdcdb554664..06fa095b1b8 100644 --- a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImporter.java +++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/UserImporter.java @@ -19,7 +19,6 @@ import org.apache.jackrabbit.guava.common.base.Stopwatch; import org.apache.jackrabbit.guava.common.collect.Iterables; import org.apache.jackrabbit.guava.common.collect.Maps; -import org.apache.jackrabbit.guava.common.collect.Sets; import org.apache.jackrabbit.api.JackrabbitSession; import org.apache.jackrabbit.api.security.principal.PrincipalIterator; import org.apache.jackrabbit.api.security.principal.PrincipalManager; @@ -658,7 +657,7 @@ void process() throws RepositoryException { @NotNull Map getAuthorizablesToAdd(@NotNull Group gr, @NotNull Map toRemove, @NotNull Map nonExisting) throws RepositoryException { - Map toAdd = new HashMap<>(CollectionUtils.ensureCapacity(members.size())); + Map toAdd = CollectionUtils.newHashMap(members.size()); for (String contentId : members) { // NOTE: no need to check for re-mapped uuids with the referenceTracker because // ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW is not supported for user/group imports (see line 189) diff --git a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentIdTable.java b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentIdTable.java index 9723e0e741e..0ec87d1d507 100644 --- a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentIdTable.java +++ b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentIdTable.java @@ -24,7 +24,6 @@ import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -131,7 +130,7 @@ void collectReferencedIds(Collection ids) { private synchronized Collection refresh() { int size = references.size(); - Map> ids = new HashMap<>(CollectionUtils.ensureCapacity(size)); + Map> ids = CollectionUtils.newHashMap(size); boolean hashCollisions = false; boolean emptyReferences = false; diff --git a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/GraphLoader.java b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/GraphLoader.java index 9c4ceaac22b..0dfd076d6ac 100644 --- a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/GraphLoader.java +++ b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/GraphLoader.java @@ -22,7 +22,6 @@ import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; @@ -94,7 +93,7 @@ public static Buffer loadGraph(ReaderAtEnd readerAtEnd) throws IOException { public static Map> parseGraph(Buffer buffer) { int nEntries = buffer.getInt(buffer.limit() - 12); - Map> graph = new HashMap<>(CollectionUtils.ensureCapacity(nEntries)); + Map> graph = CollectionUtils.newHashMap(nEntries); for (int i = 0; i < nEntries; i++) { long msb = buffer.getLong();