Skip to content

Commit

Permalink
CopyOnWrite
Browse files Browse the repository at this point in the history
  • Loading branch information
TaiJuWu committed Jun 26, 2024
1 parent 54957fb commit ea5f15e
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
import org.apache.kafka.common.Uuid;
import org.apache.kafka.common.acl.AclBinding;
import org.apache.kafka.common.acl.AclBindingFilter;
import org.apache.kafka.server.immutable.ImmutableMap;
import org.apache.kafka.common.utils.CopyOnWriteMap;
import org.apache.kafka.server.immutable.ImmutableNavigableSet;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* An immutable class that stores the ACLs in KRaft-based clusters.
Expand All @@ -38,13 +39,13 @@ public class AclCache {
/**
* Contains all of the current ACLs indexed by UUID.
*/
private final ImmutableMap<Uuid, StandardAcl> aclsById;
private final Map<Uuid, StandardAcl> aclsById;

AclCache() {
this(ImmutableNavigableSet.empty(), ImmutableMap.empty());
this(ImmutableNavigableSet.empty(), new CopyOnWriteMap<>());
}

private AclCache(final ImmutableNavigableSet<StandardAcl> aclsByResource, final ImmutableMap<Uuid, StandardAcl> aclsById) {
private AclCache(final ImmutableNavigableSet<StandardAcl> aclsByResource, final Map<Uuid, StandardAcl> aclsById) {
this.aclsByResource = aclsByResource;
this.aclsById = aclsById;
}
Expand Down Expand Up @@ -78,7 +79,7 @@ AclCache addAcl(Uuid id, StandardAcl acl) {
throw new RuntimeException("An ACL with ID " + id + " already exists.");
}

ImmutableMap<Uuid, StandardAcl> aclsById = this.aclsById.updated(id, acl);
this.aclsById.put(id, acl);

if (this.aclsByResource.contains(acl)) {
throw new RuntimeException("Unable to add the ACL with ID " + id +
Expand All @@ -94,7 +95,7 @@ AclCache removeAcl(Uuid id) {
if (acl == null) {
throw new RuntimeException("ID " + id + " not found in aclsById.");
}
ImmutableMap<Uuid, StandardAcl> aclsById = this.aclsById.removed(id);
aclsById.remove(id);

if (!this.aclsByResource.contains(acl)) {
throw new RuntimeException("Unable to remove the ACL with ID " + id +
Expand Down

0 comments on commit ea5f15e

Please sign in to comment.