Another Sonar adjustment
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / helpers / CQLBatch.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 import org.onap.aaf.misc.env.LogTarget;
24
25 import com.datastax.driver.core.ResultSet;
26 import com.datastax.driver.core.Session;
27
28 public class CQLBatch {
29     private Session session;
30     private StringBuilder sb;
31     private int hasAdded;
32     private LogTarget log;
33     private long sleep;
34     private long last;
35
36     public CQLBatch(LogTarget log, Session session) {
37         this.log = log;
38         this.session = session;
39         sb = new StringBuilder();
40         hasAdded = 0;
41         sleep = 0L;
42     }
43     public StringBuilder begin() {
44         sb.setLength(0);
45         sb.append("BEGIN BATCH\n");
46         hasAdded = sb.length();
47         return sb;
48     }
49
50     private boolean end() {
51         if(sb.length()==hasAdded) {
52             return false;
53         } else {
54             sb.append("APPLY BATCH;\n");
55             log.log(sb);
56             return true;
57         }
58     }
59
60     public ResultSet execute() {
61         if(end()) {
62             if(sleep>0) {
63                 long left = last - System.currentTimeMillis();
64                 if(left>0) {
65                     try {
66                         Thread.sleep(left);
67                     } catch (InterruptedException e) {
68                         // PER ORACLE, this isn't actually needed, but Sonar idiocy
69                         // requires something or flags as error.
70                         Thread.currentThread().interrupt();
71                     }
72                 }
73                 last = System.currentTimeMillis() + sleep;
74             }
75             return session.execute(sb.toString());
76         } else {
77             return null;
78         }
79     }
80
81     public ResultSet execute(boolean dryRun) {
82         ResultSet rv = null;
83         if(dryRun) {
84             if(sleep>0) {
85                 long left = last - System.currentTimeMillis();
86                 if(left>0) {
87                     try {
88                         Thread.sleep(left);
89                     } catch (InterruptedException e) {
90                         // PER ORACLE, this isn't actually needed, but Sonar idiocy
91                         // requires something or flags as error.
92                         Thread.currentThread().interrupt();
93                     }
94                 }
95                 last = System.currentTimeMillis() + sleep;
96             }
97             end();
98         } else {
99             rv = execute();
100         }
101         sb.setLength(0);
102         return rv;
103     }
104
105     public ResultSet singleExec(StringBuilder query, boolean dryRun) {
106         if(dryRun) {
107             return null;
108         } else {
109             return session.execute(query.toString());
110         }
111     }
112
113     public void touch(String table, int begin, int end, boolean dryRun) {
114         StringBuilder sb = begin();
115         for(int i=begin; i<end; ++i) {
116             sb.append("UPDATE cache SET touched=dateof(now()) WHERE name='");
117             sb.append(table);
118             sb.append("' AND seg=");
119             sb.append(i);
120             sb.append(";\n");
121         }
122         execute(dryRun);
123     }
124
125     public void sleep(long j) {
126         sleep = j*1000;
127     }
128
129     public String toString() {
130         return sb.toString();
131     }
132 }