51a88ef2cb2f40dfc26a0a70c46526d0866ab2f3
[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         
25         private final CQLBatch cqlBatch;
26         private final int maxBatch;
27         private final StringBuilder sb;
28         private final boolean dryRun;
29         private int i;
30         private int count;
31         private int batches;
32         
33         public CQLBatchLoop(CQLBatch cb, int max, boolean dryRun) {
34                 cqlBatch = cb;
35                 i=0;
36                 count = 0;
37                 maxBatch = max;
38                 sb = cqlBatch.begin();
39                 this.dryRun = dryRun;
40         }
41
42         /**
43          * Put at the first part of your Loop Logic... It checks if you have enough lines to
44          * push a batch.
45          */
46         public void preLoop() {
47                 if(i<0) {
48                         cqlBatch.begin();
49                 } else if(i>=maxBatch || sb.length()>24000) {
50                         cqlBatch.execute(dryRun);
51                         cqlBatch.begin();
52                         i=0;
53                         ++batches;
54                 }
55         }
56         
57         /**
58          * Assume this is another line in the Batch
59          * @return
60          */
61         public StringBuilder inc() {
62                 ++i;
63                 ++count;
64                 return sb;
65         }
66         
67         /**
68          * Close up when done.  However, can go back to "preLoop" safely.
69          */
70         public void flush() {
71                 if(i>0) {
72                         cqlBatch.execute(dryRun);
73                         ++batches;
74                 }
75                 i=-1;
76         }
77
78         public int total() {
79                 return count;
80         }
81         
82         public int batches() {
83                 return batches;
84         }
85 }