-
Notifications
You must be signed in to change notification settings - Fork 11
GH-600 parallel parsing of NQUADS and N-Triples #601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hmottestad
wants to merge
6
commits into
the-qa-company:dev
Choose a base branch
from
HASMAC-AS:GH-600-parallel-parsing-of-NQUADS-and-N-Triples-files
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1c92fd9
GH-600 implement a splitting mechanism and multithreaded approach to …
hmottestad 3649b4e
add some synchronization
hmottestad 7c38f0e
fix test
hmottestad a15e5ce
fix test
hmottestad 7c63bfe
wip
hmottestad 62c7416
add some tests
ate47 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
.../java/com/the_qa_company/qendpoint/core/iterator/utils/AsyncIteratorFetcherUnordered.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package com.the_qa_company.qendpoint.core.iterator.utils; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.Iterator; | ||
import java.util.Queue; | ||
|
||
/** | ||
* Synchronise an iterator | ||
* | ||
* @param <E> iterator type | ||
* @author Håvard M. Ottestad | ||
* @author Antoine Willerval | ||
*/ | ||
public class AsyncIteratorFetcherUnordered<E> extends AsyncIteratorFetcher<E> { | ||
|
||
private static final int CORES = Runtime.getRuntime().availableProcessors(); | ||
|
||
public static final int BUFFER = 1024 * 4; | ||
private final Iterator<E> iterator; | ||
private boolean end; | ||
volatile Queue<E>[] queue = new Queue[CORES * 2]; | ||
|
||
{ | ||
for (int i = 0; i < queue.length; i++) { | ||
queue[i] = new ArrayDeque<>(BUFFER); | ||
} | ||
} | ||
|
||
public AsyncIteratorFetcherUnordered(Iterator<E> iterator) { | ||
super(iterator); | ||
this.iterator = iterator; | ||
} | ||
|
||
/** | ||
* @return an element from the iterator, this method is thread safe | ||
*/ | ||
@Override | ||
public E get() { | ||
|
||
int index = (int) (Thread.currentThread().getId() % queue.length); | ||
|
||
Queue<E> es = queue[index]; | ||
if (es == null) { | ||
for (Queue<E> eQueue : queue) { | ||
if (eQueue != null) { | ||
synchronized (eQueue) { | ||
E poll = eQueue.poll(); | ||
|
||
if (poll != null) { | ||
return poll; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (es != null) { | ||
// With this approach there is some risk that a queue is filled but | ||
// never emptied. Maybe we should look for another queue to read | ||
// from | ||
// before filling our own queue? | ||
synchronized (es) { | ||
E poll = es.poll(); | ||
|
||
if (poll != null) { | ||
return poll; | ||
} | ||
|
||
synchronized (this) { | ||
es = queue[index]; | ||
if (es != null) { | ||
|
||
poll = es.poll(); | ||
if (poll == null) { | ||
if (iterator.hasNext()) { | ||
poll = iterator.next(); | ||
for (int i = 0; i < BUFFER && iterator.hasNext(); i++) { | ||
es.add(iterator.next()); | ||
} | ||
} | ||
|
||
} | ||
|
||
if (poll == null) { | ||
queue[index] = null; | ||
} else { | ||
return poll; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
for (Queue<E> eQueue : queue) { | ||
if (eQueue != null) { | ||
|
||
synchronized (eQueue) { | ||
synchronized (this) { | ||
E poll = eQueue.poll(); | ||
|
||
if (poll != null) { | ||
return poll; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
synchronized (this) { | ||
if (iterator.hasNext()) { | ||
E poll = iterator.next(); | ||
return poll; | ||
} | ||
} | ||
|
||
end = true; | ||
return null; | ||
|
||
} | ||
|
||
/** | ||
* @return is the end | ||
*/ | ||
public boolean isEnd() { | ||
return end; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to propose a sync and unsync version (or a wrapper) to avoid a sync during a single thread usage