-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: single linked list 추가 - 생산자 소비자가 병렬적으로 호출할 수 있는 single linked list 추가 * feat: SingleLinkLogQueue 추가 - SingleLinkedList를 활용한 LogQueue 추가
- Loading branch information
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
56 changes: 56 additions & 0 deletions
56
logbat/src/main/java/info/logbat/domain/log/queue/SingleLinkLogQueue.java
This file contains 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,56 @@ | ||
package info.logbat.domain.log.queue; | ||
|
||
import info.logbat.common.event.EventConsumer; | ||
import info.logbat.common.event.EventProducer; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Component | ||
public class SingleLinkLogQueue<T> implements EventProducer<T>, EventConsumer<T> { | ||
|
||
private final SingleLinkedList<T> queue = new SingleLinkedList<>(); | ||
private final long timeout; | ||
private final int bulkSize; | ||
|
||
public SingleLinkLogQueue(@Value("${jdbc.async.timeout}") Long timeout, | ||
@Value("${jdbc.async.bulk-size}") Integer bulkSize) { | ||
this.timeout = timeout; | ||
this.bulkSize = bulkSize; | ||
} | ||
|
||
/* | ||
* Consumer should be one thread | ||
*/ | ||
@Override | ||
public List<T> consume() { | ||
List<T> result = new ArrayList<>(bulkSize); | ||
|
||
final long endTime = System.currentTimeMillis() + timeout; | ||
|
||
while (result.isEmpty()) { | ||
T data = queue.poll(); | ||
if (data != null) { | ||
result.add(data); | ||
} | ||
if (result.size() >= bulkSize) { | ||
break; | ||
} | ||
if (System.currentTimeMillis() >= endTime) { | ||
break; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/* | ||
* Producer should be one thread | ||
*/ | ||
@Override | ||
public void produce(List<T> data) { | ||
queue.addAll(data); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
logbat/src/main/java/info/logbat/domain/log/queue/SingleLinkedList.java
This file contains 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,66 @@ | ||
package info.logbat.domain.log.queue; | ||
|
||
public class SingleLinkedList<E> { | ||
|
||
Node<E> first; | ||
Node<E> lastHolder; | ||
|
||
public SingleLinkedList() { | ||
lastHolder = new Node<>(null, null); | ||
first = lastHolder; | ||
} | ||
|
||
private void linkLast(E e) { | ||
Node<E> newLastHolder = new Node<>(null, null); | ||
|
||
// 1. add next node | ||
lastHolder.next = newLastHolder; | ||
// 2. set item | ||
lastHolder.item = e; | ||
|
||
lastHolder = newLastHolder; | ||
} | ||
|
||
private E unlinkFirst() { | ||
// 1. get first element | ||
E element = first.item; | ||
|
||
// first == lastHolder | ||
if (first.item == null) { | ||
return null; | ||
} | ||
|
||
// 2. get next node | ||
// if element is not null, next node should not be null | ||
Node<E> next = first.next; | ||
first.item = null; | ||
first.next = null; // help GC | ||
first = next; | ||
return element; | ||
} | ||
|
||
|
||
public boolean isEmpty() { | ||
return first.item == null; | ||
} | ||
|
||
public E poll() { | ||
return unlinkFirst(); | ||
} | ||
|
||
public void addAll(Iterable<? extends E> c) { | ||
for (E e : c) { | ||
linkLast(e); | ||
} | ||
} | ||
|
||
private static class Node<E> { | ||
volatile E item; | ||
volatile Node<E> next; | ||
|
||
Node(E element, Node<E> next) { | ||
this.item = element; | ||
this.next = next; | ||
} | ||
} | ||
} |