1010import java .util .LinkedHashMap ;
1111import java .util .List ;
1212import java .util .Map ;
13+ import java .util .Objects ;
1314import java .util .Optional ;
1415import java .util .stream .Collectors ;
1516import javax .annotation .Nullable ;
@@ -27,29 +28,15 @@ public MutationsGrouper(StorageInfoProvider storageInfoProvider) {
2728 public List <List <Mutation >> groupMutations (Collection <Mutation > mutations )
2829 throws ExecutionException {
2930 // MutationGroup mutations by their storage info and atomicity unit
30- List <MutationGroup > groups = new ArrayList <>();
3131 Map <MutationGroup , List <List <Mutation >>> groupToBatches = new LinkedHashMap <>();
3232
3333 for (Mutation mutation : mutations ) {
3434 assert mutation .forNamespace ().isPresent ();
3535 StorageInfo storageInfo = storageInfoProvider .getStorageInfo (mutation .forNamespace ().get ());
3636
37- MutationGroup matchedGroup = null ;
38- for (MutationGroup group : groups ) {
39- if (group .isSameGroup (mutation , storageInfo )) {
40- matchedGroup = group ;
41- break ;
42- }
43- }
44- if (matchedGroup == null ) {
45- // If no matching group is found, create a new one
46- matchedGroup = new MutationGroup (mutation , storageInfo );
47- groups .add (matchedGroup );
48- }
49-
50- List <List <Mutation >> batches =
51- groupToBatches .computeIfAbsent (matchedGroup , g -> new ArrayList <>());
52- int maxCount = matchedGroup .storageInfo .getMaxAtomicMutationsCount ();
37+ MutationGroup group = new MutationGroup (mutation , storageInfo );
38+ List <List <Mutation >> batches = groupToBatches .computeIfAbsent (group , g -> new ArrayList <>());
39+ int maxCount = group .storageInfo .getMaxAtomicMutationsCount ();
5340
5441 if (batches .isEmpty () || batches .get (batches .size () - 1 ).size () >= maxCount ) {
5542 // If the last batch is full or there are no batches yet, create a new batch
@@ -70,7 +57,7 @@ private static class MutationGroup {
7057 @ Nullable public final Key partitionKey ;
7158 @ Nullable public final Optional <Key > clusteringKey ;
7259
73- public MutationGroup (Mutation mutation , StorageInfo storageInfo ) {
60+ private MutationGroup (Mutation mutation , StorageInfo storageInfo ) {
7461 assert mutation .forNamespace ().isPresent () && mutation .forTable ().isPresent ();
7562
7663 switch (storageInfo .getMutationAtomicityUnit ()) {
@@ -115,41 +102,26 @@ public MutationGroup(Mutation mutation, StorageInfo storageInfo) {
115102 }
116103 }
117104
118- boolean isSameGroup (Mutation otherMutation , StorageInfo otherStorageInfo ) {
119- assert otherMutation .forNamespace ().isPresent () && otherMutation .forTable ().isPresent ();
120-
121- switch (storageInfo .getMutationAtomicityUnit ()) {
122- case RECORD :
123- if (!otherMutation .getClusteringKey ().equals (this .clusteringKey )) {
124- return false ;
125- }
126- // Fall through
127- case PARTITION :
128- if (!otherMutation .getPartitionKey ().equals (this .partitionKey )) {
129- return false ;
130- }
131- // Fall through
132- case TABLE :
133- if (!otherMutation .forTable ().get ().equals (this .table )) {
134- return false ;
135- }
136- // Fall through
137- case NAMESPACE :
138- if (!otherMutation .forNamespace ().get ().equals (this .namespace )) {
139- return false ;
140- }
141- // Fall through
142- case STORAGE :
143- if (!otherStorageInfo .getStorageName ().equals (this .storageInfo .getStorageName ())) {
144- return false ;
145- }
146- break ;
147- default :
148- throw new AssertionError (
149- "Unknown mutation atomicity unit: " + storageInfo .getMutationAtomicityUnit ());
105+ @ Override
106+ public boolean equals (Object o ) {
107+ if (this == o ) {
108+ return true ;
150109 }
110+ if (!(o instanceof MutationGroup )) {
111+ return false ;
112+ }
113+ MutationGroup that = (MutationGroup ) o ;
114+ return Objects .equals (storageInfo .getStorageName (), that .storageInfo .getStorageName ())
115+ && Objects .equals (namespace , that .namespace )
116+ && Objects .equals (table , that .table )
117+ && Objects .equals (partitionKey , that .partitionKey )
118+ && Objects .equals (clusteringKey , that .clusteringKey );
119+ }
151120
152- return true ;
121+ @ Override
122+ public int hashCode () {
123+ return Objects .hash (
124+ storageInfo .getStorageName (), namespace , table , partitionKey , clusteringKey );
153125 }
154126 }
155127}
0 commit comments