2836d041a1e138fd184c33cff955cbb1a86eef95
[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  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  */
20
21 package org.onap.aaf.auth.batch.helpers;
22
23 public class CQLBatchLoop {
24         private static final int MAX_CHARS = (50 * 1024)/2;
25         
26         private final CQLBatch cqlBatch;
27         private final int maxBatch;
28         private final StringBuilder sb;
29         private final boolean dryRun;
30         private int i;
31         private int total;
32         private int batches;
33         private final StringBuilder current;
34         private boolean showProgress;
35         
36         public CQLBatchLoop(CQLBatch cb, int max, boolean dryRun) {
37                 cqlBatch = cb;
38                 i=0;
39                 total = 0;
40                 maxBatch = max;
41                 sb = cqlBatch.begin();
42                 current = new StringBuilder();
43                 this.dryRun = dryRun;
44                 showProgress = false;
45         }
46
47         public CQLBatchLoop showProgress() {
48                 showProgress = true;
49                 return this;
50         }
51         /**
52          * Assume this is another line in the Batch
53          * @return
54          */
55         public StringBuilder inc() {
56                 if(i>=maxBatch || current.length()+sb.length()>MAX_CHARS) {
57                         if(i>0) {
58                                 cqlBatch.execute(dryRun);
59                                 i = -1;
60                                 incBatch();
61                         }
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 }