Sonar Fixes, Formatting
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / helpers / CQLBatchLoop.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 IBM.
7  * ===========================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END====================================================
20  */
21
22 package org.onap.aaf.auth.batch.helpers;
23
24 public class CQLBatchLoop {
25     private static final int MAX_CHARS = (50 * 1024)/2;
26
27     private final CQLBatch cqlBatch;
28     private final int maxBatch;
29     private final StringBuilder sb;
30     private final boolean dryRun;
31     private int i;
32     private int total;
33     private int batches;
34     private final StringBuilder current;
35     private boolean showProgress;
36
37     public CQLBatchLoop(CQLBatch cb, int max, boolean dryRun) {
38         cqlBatch = cb;
39         i=0;
40         total = 0;
41         maxBatch = max;
42         sb = cqlBatch.begin();
43         current = new StringBuilder();
44         this.dryRun = dryRun;
45         showProgress = false;
46     }
47
48     public CQLBatchLoop showProgress() {
49         showProgress = true;
50         return this;
51     }
52     /**
53      * Assume this is another line in the Batch
54      * @return
55      */
56     public StringBuilder inc() {
57         if((i>=maxBatch || current.length() + sb.length() > MAX_CHARS) && (i > 0)) {
58
59                 cqlBatch.execute(dryRun);
60                 i = -1;
61                 incBatch();
62     }
63         if(i < 0) {
64             cqlBatch.begin();
65             i = 0;
66         }
67         if(current.length() > MAX_CHARS) {
68             cqlBatch.singleExec(current, dryRun);
69         } else {
70             sb.append(current);
71         }
72         current.setLength(0);
73         ++i;
74         ++total;
75         return current;
76     }
77
78     /**
79      * Close up when finished.
80      */
81     public void flush() {
82         if(current.length() + sb.length() > MAX_CHARS) {
83             if(i > 0) {
84                 cqlBatch.execute(dryRun);
85                 incBatch();
86             }
87             if(current.length() > 0) {
88                 cqlBatch.singleExec(current, dryRun);
89                 current.setLength(0);
90                 incBatch();
91             }
92         } else {
93             if(i < 0) {
94                 cqlBatch.begin();
95             }
96             sb.append(current);
97             current.setLength(0);
98             cqlBatch.execute(dryRun);
99             incBatch();
100         }
101         i = -1;
102     }
103
104     private void incBatch() {
105         ++batches;
106         if(showProgress) {
107             System.out.print('.');
108             if(batches%70 == 0) {
109                 System.out.println();
110             }
111         }
112     }
113
114     public int total() {
115         return total;
116     }
117
118     public int batches() {
119         return batches;
120     }
121
122     public void reset() {
123         total = 0;
124         batches = 0;
125         i = -1;
126     }
127
128     public String toString() {
129         return cqlBatch.toString();
130     }
131 }