-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace WeakHashMap by LimitedPersistentMap in IndexDb and move most …
…of qbit code into qbit-core
- Loading branch information
Aleksey Zhidkov
committed
May 30, 2020
1 parent
1fd7eb0
commit 2306e7f
Showing
41 changed files
with
324 additions
and
156 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
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
7 changes: 0 additions & 7 deletions
7
qbit-core-jvm/src/commonMain/kotlin/qbit/platform/Collections.kt
This file was deleted.
Oops, something went wrong.
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
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
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
8 changes: 0 additions & 8 deletions
8
qbit-core-jvm/src/jvmMain/kotlin/qbit/platform/Collections.kt
This file was deleted.
Oops, something went wrong.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 0 additions & 4 deletions
4
.../commonMain/kotlin/qbit/api/model/Hash.kt → .../commonMain/kotlin/qbit/api/model/Hash.kt
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
File renamed without changes.
18 changes: 18 additions & 0 deletions
18
qbit-core/src/commonMain/kotlin/qbit/collections/LimitedPersistentMap.kt
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,18 @@ | ||
package qbit.collections | ||
|
||
|
||
class LimitedPersistentMap<K : Any, V : Any>( | ||
private val limit: Int, | ||
private val delegate: PersistentMap<K, V> = PersistentMapStub() | ||
) : PersistentMap<K, V> by delegate { | ||
|
||
override fun put(key: K, value: V): LimitedPersistentMap<K, V> { | ||
val limited = if (delegate.size < limit) { | ||
this.delegate | ||
} else { | ||
this.delegate.remove(this.keys.first()) | ||
} | ||
return LimitedPersistentMap(limit, limited.put(key, value)) | ||
} | ||
|
||
} |
34 changes: 3 additions & 31 deletions
34
qbit-core/src/commonMain/kotlin/qbit/collections/PersistentMap.kt
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 |
---|---|---|
@@ -1,38 +1,10 @@ | ||
package qbit.collections | ||
|
||
import kotlinx.atomicfu.AtomicRef | ||
import kotlinx.atomicfu.atomic | ||
import kotlinx.atomicfu.updateAndGet | ||
|
||
interface PersistentMap<K : Any, V : Any> : Map<K, V> { | ||
|
||
/** | ||
* Dump persistent map as stub for real persistent map from kotlinx-immutable while Kotlin 1.4 stabiliaztion | ||
*/ | ||
class PersistentMap<K : Any, V : Any> { | ||
|
||
private val mapRef: AtomicRef<Map<K, V>> = atomic(HashMap()) | ||
|
||
val keys: Set<K> | ||
get() = mapRef.value.keys | ||
|
||
fun put(key: K, value: V): PersistentMap<K, V> { | ||
mapRef.updateAndGet { | ||
it.plus(key to value) | ||
} | ||
return this | ||
} | ||
|
||
operator fun get(key: K): V? { | ||
return mapRef.value[key] | ||
} | ||
|
||
fun containsKey(key: K): Boolean { | ||
return mapRef.value.containsKey(key) | ||
} | ||
|
||
operator fun contains(key: K): Boolean { | ||
return containsKey(key) | ||
} | ||
fun put(key: K, value: V): PersistentMap<K, V> | ||
|
||
fun remove(key: K): PersistentMap<K, V> | ||
|
||
} |
Oops, something went wrong.