Skip to content

Commit 8e62331

Browse files
authored
Add import-control checkstyle for checking API (#34)
* Also moved maven files to contrib directory, dropping build directory * Updated checkstyle to latest version * Add exceptions to hold the line for API * Use guava PeekingIterator
1 parent 0b3174d commit 8e62331

File tree

9 files changed

+94
-30
lines changed

9 files changed

+94
-30
lines changed

build/checkstyle.xml renamed to contrib/checkstyle.xml

+3-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
<module name="Checker">
1818
<property name="charset" value="UTF-8"/>
1919
<module name="TreeWalker">
20-
<module name="RegexpSinglelineJava">
21-
<!--check that only Accumulo public APIs are imported-->
22-
<property name="format" value="import\s+org\.apache\.accumulo\.(.*\.(impl|thrift|crypto)\..*|(?!core|minicluster|examples).*|core\.(?!client|data|security|cli|iterators).*)"/>
23-
<property name="ignoreComments" value="true" />
24-
<property name="message" value="Accumulo non-public classes imported" />
20+
<!--check that only Accumulo public APIs are imported-->
21+
<module name="ImportControl">
22+
<property name="file" value="contrib/import-control.xml"/>
2523
</module>
2624
</module>
2725
</module>

contrib/import-control.xml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<!DOCTYPE import-control PUBLIC
17+
"-//Checkstyle//DTD ImportControl Configuration 1.4//EN"
18+
"https://checkstyle.org/dtds/import_control_1_4.dtd">
19+
20+
<!-- This checkstyle rule is configured to ensure only use of Accumulo API -->
21+
<import-control pkg="org.apache.accumulo.examples" strategyOnMismatch="allowed">
22+
<!-- allow this package -->
23+
<allow pkg="org.apache.accumulo.examples"/>
24+
<!-- API packages -->
25+
<allow pkg="org.apache.accumulo.core.client"/>
26+
<allow pkg="org.apache.accumulo.core.data"/>
27+
<allow pkg="org.apache.accumulo.core.security"/>
28+
<allow pkg="org.apache.accumulo.core.iterators"/>
29+
<allow pkg="org.apache.accumulo.minicluster"/>
30+
<allow pkg="org.apache.accumulo.hadoop.mapreduce"/>
31+
32+
<!-- exceptions for this project -->
33+
<allow pkg="org.apache.accumulo.core.conf"/>
34+
<allow class="org.apache.accumulo.core.constraints.Constraint"/>
35+
36+
<!-- TODO refactor code to remove the following exceptions -->
37+
<allow class="org.apache.accumulo.tracer.TraceDump"/>
38+
<allow class="org.apache.accumulo.core.trace.DistributedTrace"/>
39+
<allow class="org.apache.accumulo.core.util.format.DefaultFormatter"/>
40+
<allow class="org.apache.accumulo.core.util.TextUtil"/>
41+
<!-- End TODO section -->
42+
43+
<!-- disallow everything else coming from accumulo -->
44+
<disallow pkg="org.apache.accumulo"/>
45+
</import-control>

pom.xml

+19-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<slf4j.version>1.7.21</slf4j.version>
3535
<maven.compiler.source>1.8</maven.compiler.source>
3636
<maven.compiler.target>1.8</maven.compiler.target>
37-
<eclipseFormatterStyle>build/Eclipse-Accumulo-Codestyle.xml</eclipseFormatterStyle>
37+
<eclipseFormatterStyle>contrib/Eclipse-Accumulo-Codestyle.xml</eclipseFormatterStyle>
3838
</properties>
3939
<dependencyManagement>
4040
<dependencies>
@@ -186,14 +186,27 @@
186186
</executions>
187187
</plugin>
188188
<plugin>
189-
<!-- This was added to ensure project only uses public API. Run with the following:
190-
mvn checkstyle:checkstyle
191-
-->
189+
<!-- This was added to ensure project only uses public API -->
192190
<groupId>org.apache.maven.plugins</groupId>
193191
<artifactId>maven-checkstyle-plugin</artifactId>
194-
<version>2.17</version>
192+
<version>3.0.0</version>
193+
<executions>
194+
<execution>
195+
<id>check-style</id>
196+
<goals>
197+
<goal>check</goal>
198+
</goals>
199+
</execution>
200+
</executions>
201+
<dependencies>
202+
<dependency>
203+
<groupId>com.puppycrawl.tools</groupId>
204+
<artifactId>checkstyle</artifactId>
205+
<version>8.17</version>
206+
</dependency>
207+
</dependencies>
195208
<configuration>
196-
<configLocation>build/checkstyle.xml</configLocation>
209+
<configLocation>contrib/checkstyle.xml</configLocation>
197210
</configuration>
198211
</plugin>
199212
<plugin>

src/main/java/org/apache/accumulo/examples/filedata/ChunkInputFormat.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525
import org.apache.accumulo.core.client.mapreduce.InputFormatBase;
2626
import org.apache.accumulo.core.data.Key;
2727
import org.apache.accumulo.core.data.Value;
28-
import org.apache.accumulo.core.util.PeekingIterator;
2928
import org.apache.accumulo.core.util.format.DefaultFormatter;
3029
import org.apache.hadoop.mapreduce.InputSplit;
3130
import org.apache.hadoop.mapreduce.RecordReader;
3231
import org.apache.hadoop.mapreduce.TaskAttemptContext;
3332

33+
import com.google.common.collect.Iterators;
34+
import com.google.common.collect.PeekingIterator;
35+
3436
/**
3537
* An InputFormat that turns the file data ingested with {@link FileDataIngest} into an InputStream
3638
* using {@link ChunkInputStream}. Mappers used with this InputFormat must close the InputStream.
@@ -45,7 +47,7 @@ public RecordReader<List<Entry<Key,Value>>,InputStream> createRecordReader(Input
4547
@Override
4648
public void initialize(InputSplit inSplit, TaskAttemptContext attempt) throws IOException {
4749
super.initialize(inSplit, attempt);
48-
peekingScannerIterator = new PeekingIterator<>(scannerIterator);
50+
peekingScannerIterator = Iterators.peekingIterator(scannerIterator);
4951
currentK = new ArrayList<>();
5052
currentV = new ChunkInputStream();
5153
}

src/main/java/org/apache/accumulo/examples/filedata/ChunkInputStream.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
import org.apache.accumulo.core.data.Key;
2626
import org.apache.accumulo.core.data.PartialKey;
2727
import org.apache.accumulo.core.data.Value;
28-
import org.apache.accumulo.core.util.PeekingIterator;
2928
import org.apache.hadoop.io.Text;
3029
import org.slf4j.Logger;
3130
import org.slf4j.LoggerFactory;
3231

32+
import com.google.common.collect.PeekingIterator;
33+
3334
/**
3435
* An input stream that reads file data stored in one or more Accumulo values. Used by
3536
* {@link ChunkInputFormat} to present input streams to a mapper.

src/main/java/org/apache/accumulo/examples/filedata/FileDataQuery.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
import org.apache.accumulo.core.data.Range;
3131
import org.apache.accumulo.core.data.Value;
3232
import org.apache.accumulo.core.security.Authorizations;
33-
import org.apache.accumulo.core.util.PeekingIterator;
33+
34+
import com.google.common.collect.Iterators;
35+
import com.google.common.collect.PeekingIterator;
3436

3537
/**
3638
* Retrieves file data based on the hash of the file. Used by the
@@ -56,7 +58,7 @@ public ChunkInputStream getData(String hash) throws IOException {
5658
scanner.setRange(new Range(hash));
5759
scanner.setBatchSize(1);
5860
lastRefs.clear();
59-
PeekingIterator<Entry<Key,Value>> pi = new PeekingIterator<>(scanner.iterator());
61+
PeekingIterator<Entry<Key,Value>> pi = Iterators.peekingIterator(scanner.iterator());
6062
if (pi.hasNext()) {
6163
while (!pi.peek().getKey().getColumnFamily().equals(FileDataIngest.CHUNK_CF)) {
6264
lastRefs.add(pi.peek());

src/test/java/org/apache/accumulo/examples/filedata/ChunkInputStreamIT.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.apache.accumulo.core.data.Value;
4141
import org.apache.accumulo.core.security.Authorizations;
4242
import org.apache.accumulo.core.security.ColumnVisibility;
43-
import org.apache.accumulo.core.util.PeekingIterator;
4443
import org.apache.accumulo.harness.AccumuloClusterHarness;
4544
import org.apache.accumulo.miniclusterImpl.MiniAccumuloConfigImpl;
4645
import org.apache.hadoop.conf.Configuration;
@@ -49,6 +48,9 @@
4948
import org.junit.Before;
5049
import org.junit.Test;
5150

51+
import com.google.common.collect.Iterators;
52+
import com.google.common.collect.PeekingIterator;
53+
5254
public class ChunkInputStreamIT extends AccumuloClusterHarness {
5355

5456
@Override
@@ -155,7 +157,7 @@ public void testWithAccumulo() throws AccumuloException, AccumuloSecurityExcepti
155157
ChunkInputStream cis = new ChunkInputStream();
156158
byte[] b = new byte[20];
157159
int read;
158-
PeekingIterator<Entry<Key,Value>> pi = new PeekingIterator<>(scan.iterator());
160+
PeekingIterator<Entry<Key,Value>> pi = Iterators.peekingIterator(scan.iterator());
159161

160162
cis.setSource(pi);
161163
assertEquals(read = cis.read(b), 8);

src/test/java/org/apache/accumulo/examples/filedata/ChunkInputStreamTest.java

+13-12
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
import org.apache.accumulo.core.data.Key;
2929
import org.apache.accumulo.core.data.KeyValue;
3030
import org.apache.accumulo.core.data.Value;
31-
import org.apache.accumulo.core.util.PeekingIterator;
3231
import org.apache.hadoop.io.Text;
3332
import org.junit.Before;
3433
import org.junit.Test;
3534
import org.slf4j.Logger;
3635
import org.slf4j.LoggerFactory;
3736

37+
import com.google.common.collect.Iterators;
38+
import com.google.common.collect.PeekingIterator;
39+
3840
public class ChunkInputStreamTest {
3941
private static final Logger log = LoggerFactory.getLogger(ChunkInputStream.class);
4042
private List<Entry<Key,Value>> data;
@@ -103,8 +105,7 @@ private static void addData(List<Entry<Key,Value>> data, String row, String cf,
103105
@Test
104106
public void testExceptionOnMultipleSetSourceWithoutClose() throws IOException {
105107
ChunkInputStream cis = new ChunkInputStream();
106-
PeekingIterator<Entry<Key,Value>> pi = new PeekingIterator<>(data.iterator());
107-
pi = new PeekingIterator<>(data.iterator());
108+
PeekingIterator<Entry<Key,Value>> pi = Iterators.peekingIterator(data.iterator());
108109
cis.setSource(pi);
109110
try {
110111
cis.setSource(pi);
@@ -118,7 +119,7 @@ public void testExceptionOnMultipleSetSourceWithoutClose() throws IOException {
118119
@Test
119120
public void testExceptionOnGetVisBeforeClose() throws IOException {
120121
ChunkInputStream cis = new ChunkInputStream();
121-
PeekingIterator<Entry<Key,Value>> pi = new PeekingIterator<>(data.iterator());
122+
PeekingIterator<Entry<Key,Value>> pi = Iterators.peekingIterator(data.iterator());
122123

123124
cis.setSource(pi);
124125
try {
@@ -136,7 +137,7 @@ public void testReadIntoBufferSmallerThanChunks() throws IOException {
136137
ChunkInputStream cis = new ChunkInputStream();
137138
byte[] b = new byte[5];
138139

139-
PeekingIterator<Entry<Key,Value>> pi = new PeekingIterator<>(data.iterator());
140+
PeekingIterator<Entry<Key,Value>> pi = Iterators.peekingIterator(data.iterator());
140141

141142
cis.setSource(pi);
142143
int read;
@@ -188,7 +189,7 @@ public void testReadIntoBufferLargerThanChunks() throws IOException {
188189
ChunkInputStream cis = new ChunkInputStream();
189190
byte[] b = new byte[20];
190191
int read;
191-
PeekingIterator<Entry<Key,Value>> pi = new PeekingIterator<>(data.iterator());
192+
PeekingIterator<Entry<Key,Value>> pi = Iterators.peekingIterator(data.iterator());
192193

193194
cis.setSource(pi);
194195
assertEquals(read = cis.read(b), 8);
@@ -247,7 +248,7 @@ public void testBadData() throws IOException {
247248
ChunkInputStream cis = new ChunkInputStream();
248249
byte[] b = new byte[20];
249250
int read;
250-
PeekingIterator<Entry<Key,Value>> pi = new PeekingIterator<>(baddata.iterator());
251+
PeekingIterator<Entry<Key,Value>> pi = Iterators.peekingIterator(baddata.iterator());
251252

252253
cis.setSource(pi);
253254
assumeExceptionOnRead(cis, b);
@@ -293,7 +294,7 @@ public void testBadData() throws IOException {
293294

294295
assertFalse(pi.hasNext());
295296

296-
pi = new PeekingIterator<>(baddata.iterator());
297+
pi = Iterators.peekingIterator(baddata.iterator());
297298
cis.setSource(pi);
298299
assumeExceptionOnClose(cis);
299300
}
@@ -303,7 +304,7 @@ public void testBadDataWithoutClosing() throws IOException {
303304
ChunkInputStream cis = new ChunkInputStream();
304305
byte[] b = new byte[20];
305306
int read;
306-
PeekingIterator<Entry<Key,Value>> pi = new PeekingIterator<>(baddata.iterator());
307+
PeekingIterator<Entry<Key,Value>> pi = Iterators.peekingIterator(baddata.iterator());
307308

308309
cis.setSource(pi);
309310
assumeExceptionOnRead(cis, b);
@@ -344,7 +345,7 @@ public void testBadDataWithoutClosing() throws IOException {
344345

345346
assertFalse(pi.hasNext());
346347

347-
pi = new PeekingIterator<>(baddata.iterator());
348+
pi = Iterators.peekingIterator(baddata.iterator());
348349
cis.setSource(pi);
349350
assumeExceptionOnClose(cis);
350351
}
@@ -354,7 +355,7 @@ public void testMultipleChunkSizes() throws IOException {
354355
ChunkInputStream cis = new ChunkInputStream();
355356
byte[] b = new byte[20];
356357
int read;
357-
PeekingIterator<Entry<Key,Value>> pi = new PeekingIterator<>(multidata.iterator());
358+
PeekingIterator<Entry<Key,Value>> pi = Iterators.peekingIterator(multidata.iterator());
358359

359360
b = new byte[20];
360361

@@ -381,7 +382,7 @@ public void testMultipleChunkSizes() throws IOException {
381382
@Test
382383
public void testSingleByteRead() throws IOException {
383384
ChunkInputStream cis = new ChunkInputStream();
384-
PeekingIterator<Entry<Key,Value>> pi = new PeekingIterator<>(data.iterator());
385+
PeekingIterator<Entry<Key,Value>> pi = Iterators.peekingIterator(data.iterator());
385386

386387
cis.setSource(pi);
387388
assertEquals((byte) 'a', (byte) cis.read());

0 commit comments

Comments
 (0)