Skip to content

Commit f1be64d

Browse files
llingllinggit
authored andcommitted
Change count in splitters from ThreadLocal<Long> back to long. Mentioned in JavaDoc that splitters are not thread-safe.
1 parent 53f1f8a commit f1be64d

File tree

6 files changed

+32
-32
lines changed

6 files changed

+32
-32
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/JSONSplitter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static public JSONSplitter<StringHandle> makeArraySplitter() {
5757
}
5858

5959
private JSONSplitter.Visitor<T> visitor;
60-
private ThreadLocal<Long> count = new ThreadLocal<>();
60+
private long count = 0;
6161
private String splitFilename;
6262

6363
/**
@@ -89,7 +89,7 @@ public void setVisitor(JSONSplitter.Visitor<T> visitor) {
8989

9090
@Override
9191
public long getCount() {
92-
return count.get();
92+
return count;
9393
}
9494

9595
/**
@@ -148,7 +148,7 @@ public Stream<T> split(JsonParser input) throws IOException {
148148
if (input == null) {
149149
throw new IllegalArgumentException("Input cannot be null");
150150
}
151-
count.set(0L);
151+
count = 0;
152152

153153
JSONSplitter.HandleSpliterator<T> handleSpliterator = new JSONSplitter.HandleSpliterator<>(this, input);
154154
return StreamSupport.stream(handleSpliterator, true);
@@ -166,7 +166,7 @@ public Stream<DocumentWriteOperation> splitWriteOperations(JsonParser input, Str
166166
if (input == null) {
167167
throw new IllegalArgumentException("Input cannot be null");
168168
}
169-
count.set(0L);
169+
count = 0;
170170

171171
this.splitFilename = splitFilename;
172172
JSONSplitter.DocumentWriteOperationSpliterator spliterator =
@@ -479,7 +479,7 @@ public boolean tryAdvance(Consumer<? super T> action) {
479479
return false;
480480
}
481481

482-
getSplitter().count.set(getSplitter().getCount() + 1);
482+
getSplitter().count = getSplitter().getCount() + 1;
483483
action.accept(handle);
484484

485485
return true;
@@ -511,7 +511,7 @@ public boolean tryAdvance(Consumer<? super DocumentWriteOperation> action) {
511511
}
512512
}
513513

514-
getSplitter().count.set(getSplitter().getCount() + 1);
514+
getSplitter().count = getSplitter().getCount() + 1;
515515
DocumentWriteOperation documentWriteOperation = getSplitter().getVisitor().makeDocumentWriteOperation(
516516
getSplitter().getUriMaker(),
517517
getSplitter().getCount(),

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/LineSplitter.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*/
3232
public class LineSplitter implements Splitter<StringHandle> {
3333
private Format format = Format.JSON;
34-
private static ThreadLocal<Long> count = new ThreadLocal<>();
34+
private long count = 0;
3535

3636
/**
3737
* Returns the document format set to splitter.
@@ -60,7 +60,7 @@ public void setFormat(Format format) {
6060
*/
6161
@Override
6262
public long getCount() {
63-
return count.get();
63+
return count;
6464
}
6565

6666
/**
@@ -105,7 +105,7 @@ public Stream<DocumentWriteOperation> splitWriteOperations(InputStream input, St
105105
throw new IllegalArgumentException("InputStream cannot be null.");
106106
}
107107

108-
count.set(0L);
108+
count = 0;
109109
String extension = getFormat().getDefaultExtension();
110110
if (getUriMaker() == null) {
111111
LineSplitter.UriMakerImpl uriMaker = new LineSplitter.UriMakerImpl();
@@ -125,9 +125,9 @@ public Stream<DocumentWriteOperation> splitWriteOperations(InputStream input, St
125125
.lines()
126126
.filter(line -> line.length() != 0)
127127
.map(line -> {
128-
count.set(count.get() + 1);
128+
count = count + 1;
129129
StringHandle handle = new StringHandle(line);
130-
String uri = getUriMaker().makeUri(count.get(), handle);
130+
String uri = getUriMaker().makeUri(count, handle);
131131
return new DocumentWriteOperationImpl(
132132
DocumentWriteOperation.OperationType.DOCUMENT_WRITE,
133133
uri,
@@ -167,12 +167,12 @@ public Stream<StringHandle> split(Reader input) throws IOException {
167167
throw new IllegalArgumentException("Reader cannot be null.");
168168
}
169169

170-
count.set(0L);
170+
count = 0;
171171
return new BufferedReader(input)
172172
.lines()
173173
.filter(line -> line.length() != 0)
174174
.map(line -> {
175-
count.set(count.get() + 1);
175+
count = count + 1;
176176
return new StringHandle(line).withFormat(getFormat());
177177
});
178178
}

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/Splitter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.marklogic.client.io.marker.AbstractWriteHandle;
2424

2525
/**
26-
* Splitter splits an input stream into a Java stream of write handles.
26+
* Splitter splits an input stream into a Java stream of write handles. It is not thread-safe.
2727
*/
2828
public interface Splitter<T extends AbstractWriteHandle> {
2929
/**

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/UnarySplitter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*/
3232
public class UnarySplitter implements Splitter<InputStreamHandle> {
3333
private UnarySplitter.UriMaker uriMaker;
34-
private ThreadLocal<Long> count = new ThreadLocal<>();
34+
private long count = 0;
3535

3636
/**
3737
* Get the UriMaker of the splitter
@@ -62,7 +62,7 @@ public Stream<InputStreamHandle> split(InputStream input) throws Exception {
6262
if (input == null) {
6363
throw new IllegalArgumentException("Input cannot be null");
6464
}
65-
count.set(0L);
65+
count = 0;
6666

6767
InputStreamHandle handle = new InputStreamHandle(input);
6868
return Stream.of(handle);
@@ -90,7 +90,7 @@ public Stream<DocumentWriteOperation> splitWriteOperations(InputStream input, St
9090
if (input == null) {
9191
throw new IllegalArgumentException("Input cannot be null");
9292
}
93-
count.set(0L);
93+
count = 0;
9494
InputStreamHandle handle = new InputStreamHandle(input);
9595

9696
if (getUriMaker() == null) {
@@ -111,7 +111,7 @@ public Stream<DocumentWriteOperation> splitWriteOperations(InputStream input, St
111111
handle
112112
);
113113

114-
this.count.set(getCount() + 1);
114+
this.count = getCount() + 1;
115115
return Stream.of(documentWriteOperation);
116116
}
117117

@@ -120,7 +120,7 @@ public Stream<DocumentWriteOperation> splitWriteOperations(InputStream input, St
120120
* @return the number of splits
121121
*/
122122
public long getCount() {
123-
return this.count.get();
123+
return this.count;
124124
}
125125

126126
/**

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/XMLSplitter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static public XMLSplitter<StringHandle> makeSplitter(String nsUri, String localN
6363
}
6464

6565
private XMLSplitter.Visitor<T> visitor;
66-
private ThreadLocal<Long> count = new ThreadLocal<>();
66+
private long count = 0;
6767
private String splitFilename;
6868

6969
/**
@@ -141,7 +141,7 @@ public Stream<DocumentWriteOperation> splitWriteOperations(InputStream input, St
141141

142142
@Override
143143
public long getCount() {
144-
return count.get();
144+
return count;
145145
}
146146

147147
/**
@@ -155,7 +155,7 @@ public Stream<T> split(XMLStreamReader input) throws IOException {
155155
if (input == null) {
156156
throw new IllegalArgumentException("Input cannot be null");
157157
}
158-
count.set(0L);
158+
count = 0;
159159

160160
XMLSplitter.HandleSpliterator<T> handleSpliterator = new XMLSplitter.HandleSpliterator<>(this, input);
161161

@@ -175,7 +175,7 @@ public Stream<DocumentWriteOperation> splitWriteOperations(XMLStreamReader input
175175
if (input == null) {
176176
throw new IllegalArgumentException("Input cannot be null");
177177
}
178-
count.set(0L);
178+
count = 0;
179179

180180
this.splitFilename = splitFilename;
181181
XMLSplitter.DocumentWriteOperationSpliterator<T> documentWriteOperationSpliterator =
@@ -478,7 +478,7 @@ public boolean tryAdvance(Consumer<? super T> action) {
478478
return false;
479479
}
480480

481-
getSplitter().count.set(getSplitter().getCount() + 1);
481+
getSplitter().count = getSplitter().getCount() + 1;
482482
action.accept(handle);
483483

484484
return true;
@@ -510,7 +510,7 @@ public boolean tryAdvance(Consumer<? super DocumentWriteOperation> action) {
510510
}
511511
}
512512

513-
splitter.count.set(splitter.getCount() + 1);
513+
splitter.count = splitter.getCount() + 1;
514514
DocumentWriteOperation documentWriteOperation = splitter.getVisitor().makeDocumentWriteOperation(
515515
splitter.getUriMaker(),
516516
splitter.getCount(),

marklogic-client-api/src/main/java/com/marklogic/client/datamovement/ZipSplitter.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class ZipSplitter implements Splitter<BytesHandle> {
4444
private Predicate<ZipEntry> entryFilter;
4545
private Function<String, String> uriTransformer;
4646
private String splitFilename;
47-
private static ThreadLocal<Long> count = new ThreadLocal<>();
47+
private long count = 0;
4848
private static Pattern extensionRegex = Pattern.compile("^(.+)\\.([^.]+)$");
4949

5050
/**
@@ -108,7 +108,7 @@ public ZipSplitter() {
108108

109109
@Override
110110
public long getCount() {
111-
return this.count.get();
111+
return this.count;
112112
}
113113

114114
/**
@@ -143,7 +143,7 @@ public Stream<BytesHandle> split(ZipInputStream input) throws IOException {
143143
if (input == null) {
144144
throw new IllegalArgumentException("Input cannot be null");
145145
}
146-
count.set(0L);
146+
count = 0;
147147

148148
BytesHandleSpliterator bytesHandleSpliterator = new BytesHandleSpliterator(this);
149149
bytesHandleSpliterator.setZipStream(input);
@@ -214,7 +214,7 @@ public Stream<DocumentWriteOperation> splitWriteOperations(ZipInputStream input,
214214
if (input == null) {
215215
throw new IllegalArgumentException("Input cannot be null");
216216
}
217-
count.set(0L);
217+
count = 0;
218218

219219
DocumentWriteOperationSpliterator documentWriteOperationSpliterator =
220220
new DocumentWriteOperationSpliterator(this);
@@ -364,7 +364,7 @@ public boolean tryAdvance(Consumer<? super BytesHandle> action) {
364364
return false;
365365
}
366366

367-
splitter.count.set(splitter.getCount() + 1);
367+
splitter.count = splitter.getCount() + 1;
368368
BytesHandle nextBytesHandle = readEntry(nextEntry);
369369
action.accept(nextBytesHandle);
370370

@@ -394,7 +394,7 @@ public boolean tryAdvance(Consumer<? super DocumentWriteOperation> action) {
394394
return false;
395395
}
396396

397-
splitter.count.set(splitter.getCount() + 1);
397+
splitter.count = splitter.getCount() + 1;
398398
BytesHandle nextBytesHandle = readEntry(nextEntry);
399399
String name = nextEntry.getZipEntry().getName();
400400
String uri = name;
@@ -409,7 +409,7 @@ public boolean tryAdvance(Consumer<? super DocumentWriteOperation> action) {
409409
if (splitter.splitFilename != null) {
410410
splitter.getUriMaker().setSplitFilename(splitter.splitFilename);
411411
}
412-
uri = splitter.getUriMaker().makeUri(splitter.count.get(), name, nextBytesHandle);
412+
uri = splitter.getUriMaker().makeUri(splitter.getCount(), name, nextBytesHandle);
413413
} else {
414414
uri = splitter.uriTransformer.apply(name);
415415
}

0 commit comments

Comments
 (0)