This repository has been archived by the owner on Feb 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Small Queue improvements #235
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3822041
Remove suspend from tryOffer
nomisRev 44cda54
Add unsafe constructors for Queue
nomisRev 9cc31a7
Queue - refactor offer1 to tryOffer1
nomisRev 3f892cf
Remove first in last out strategy from Queue
nomisRev ec5c333
Merge branch 'master' into sv-small-queue-improvements
danimontoya 6750d1d
Merge branch 'master' into sv-small-queue-improvements
aballano 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 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 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 |
---|---|---|
|
@@ -39,7 +39,7 @@ interface Enqueue<A> { | |
* | ||
* @param a `A` to enqueue | ||
*/ | ||
suspend fun offer1(a: A): Boolean | ||
fun tryOffer1(a: A): Boolean | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JorgeCastilloPrz breaking change in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that was fast!! thank youu!! 🎉 |
||
} | ||
|
||
/** Provides the ability to dequeue individual elements from a `Queue`. */ | ||
|
@@ -97,7 +97,7 @@ interface Queue<A> : Enqueue<A>, Dequeue1<A>, Dequeue<A> { | |
fun <B> imap(f: (A) -> B, g: (B) -> A): Queue<B> = | ||
object : Queue<B> { | ||
override suspend fun enqueue1(a: B) = enqueue1(g(a)) | ||
override suspend fun offer1(a: B): Boolean = offer1(g(a)) | ||
override fun tryOffer1(a: B): Boolean = tryOffer1(g(a)) | ||
override suspend fun dequeue1(): B = f(this@Queue.dequeue1()) | ||
override suspend fun tryDequeue1(): Option<B> = this@Queue.tryDequeue1().map(f) | ||
override suspend fun dequeueChunk1(maxSize: Int): Chunk<B> = this@Queue.dequeueChunk1(maxSize).map(f) | ||
|
@@ -111,51 +111,71 @@ interface Queue<A> : Enqueue<A>, Dequeue1<A>, Dequeue<A> { | |
companion object { | ||
|
||
/** Creates a queue from the supplied strategy. */ | ||
private suspend fun <S, A> fromStrategy(strategy: PubSub.Strategy<A, Chunk<A>, S, Int>): Queue<A> { | ||
val pubSub = PubSub.from(strategy) | ||
private fun <S, A> fromStrategy(strategy: PubSub.Strategy<A, Chunk<A>, S, Int>): Queue<A> { | ||
val pubSub = PubSub.unsafe(strategy) | ||
return DefaultQueue(pubSub) | ||
} | ||
|
||
/** Creates a queue from the supplied strategy. */ | ||
private suspend fun <S, A> fromStrategyNoneTerminated(strategy: PubSub.Strategy<Option<A>, Option<Chunk<A>>, S, Int>): NoneTerminatedQueue<A> { | ||
val pubSub = PubSub.from(strategy) | ||
private fun <S, A> fromStrategyNoneTerminated(strategy: PubSub.Strategy<Option<A>, Option<Chunk<A>>, S, Int>): NoneTerminatedQueue<A> { | ||
val pubSub = PubSub.unsafe(strategy) | ||
return DefaultNoneTerminatedQueue(pubSub) | ||
} | ||
|
||
/** Creates a FIFO queue with no size bound. */ | ||
suspend fun <A> unbounded(): Queue<A> = | ||
fromStrategy(Strategy.fifo()) | ||
|
||
fun <A> unsafeUnbounded(): Queue<A> = | ||
fromStrategy(Strategy.fifo()) | ||
|
||
/** Creates an unbounded FIFO queue that distributed always at max `fairSize` elements to any subscriber. */ | ||
suspend fun <A> fairUnbounded(fairSize: Int): Queue<A> = | ||
fromStrategy(Strategy.fifo<A>().transformSelector { size, _ -> min(size, fairSize) }) | ||
|
||
fun <A> unsafeFairUnbounded(fairSize: Int): Queue<A> = | ||
fromStrategy(Strategy.fifo<A>().transformSelector { size, _ -> min(size, fairSize) }) | ||
|
||
/** Creates a FIFO queue with the specified size bound. */ | ||
suspend fun <A> bounded(maxSize: Int): Queue<A> = | ||
fromStrategy(Strategy.boundedFifo(maxSize)) | ||
|
||
/** Creates a FILO queue with the specified size bound. */ | ||
suspend fun <A> boundedLife(maxSize: Int): Queue<A> = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JorgeCastilloPrz Second breaking change |
||
fromStrategy(Strategy.boundedLifo(maxSize)) | ||
fun <A> unsafeBounded(maxSize: Int): Queue<A> = | ||
fromStrategy(Strategy.boundedFifo(maxSize)) | ||
|
||
/** Creates a queue which stores the last `maxSize` enqueued elements and which never blocks on enqueue. */ | ||
suspend fun <A> circularBuffer(maxSize: Int): Queue<A> = | ||
fromStrategy(Strategy.circularBuffer(maxSize)) | ||
|
||
fun <A> unsafeCircularBuffer(maxSize: Int): Queue<A> = | ||
fromStrategy(Strategy.circularBuffer(maxSize)) | ||
|
||
/** Created a bounded queue that distributed always at max `fairSize` elements to any subscriber. */ | ||
suspend fun <A> fairBounded(maxSize: Int, fairSize: Int): Queue<A> = | ||
fromStrategy(Strategy.boundedFifo<A>(maxSize).transformSelector { size, _ -> min(size, fairSize) }) | ||
|
||
fun <A> unsafeFairBounded(maxSize: Int, fairSize: Int): Queue<A> = | ||
fromStrategy(Strategy.boundedFifo<A>(maxSize).transformSelector { size, _ -> min(size, fairSize) }) | ||
|
||
/** Creates a queue which allows at most a single element to be enqueued at any time. */ | ||
suspend fun <A> synchronous(): Queue<A> = | ||
fromStrategy(Strategy.synchronous()) | ||
|
||
fun <A> unsafeSynchronous(): Queue<A> = | ||
fromStrategy(Strategy.synchronous()) | ||
|
||
/** Like [synchronous], except that any enqueue of `None` will never block and cancels any dequeue operation. */ | ||
suspend fun <A> synchronousNoneTerminated(): NoneTerminatedQueue<A> { | ||
val strategy = Strategy.synchronous<A>() | ||
val pubSub = PubSub.Strategy.closeNowOption(strategy) | ||
return fromStrategyNoneTerminated(pubSub) | ||
} | ||
|
||
fun <A> unsafeSynchronousNoneTerminated(): NoneTerminatedQueue<A> { | ||
val strategy = Strategy.synchronous<A>() | ||
val pubSub = PubSub.Strategy.closeNowOption(strategy) | ||
return fromStrategyNoneTerminated(pubSub) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -276,7 +296,7 @@ internal class DefaultQueue<A>(private val pubSub: PubSub<A, Chunk<A>, Int>) : Q | |
override suspend fun enqueue1(a: A) = | ||
pubSub.publish(a) | ||
|
||
override suspend fun offer1(a: A): Boolean = | ||
override fun tryOffer1(a: A): Boolean = | ||
pubSub.tryPublish(a) | ||
|
||
override suspend fun dequeue1(): A = | ||
|
@@ -309,7 +329,7 @@ internal class DefaultNoneTerminatedQueue<A>( | |
override suspend fun enqueue1(a: Option<A>) = | ||
pubSub.publish(a) | ||
|
||
override suspend fun offer1(a: Option<A>): Boolean = | ||
override fun tryOffer1(a: Option<A>): Boolean = | ||
pubSub.tryPublish(a) | ||
|
||
override suspend fun dequeue1(): Option<A> = | ||
|
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.
Why was it suspend?
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.
Because the underlying implementation was still relying on
suspension
, which has now been rewritten to work without suspension.EDIT: it was sharing code with
suspend fun publish
.