mirror of
https://github.com/mamoe/mirai.git
synced 2025-04-25 04:50:26 +08:00
Implement concurrent collections, remove PriorityQueue and use sortedBy, use Kotlin 1.7.0-RC
This commit is contained in:
parent
18a29415a7
commit
51e0ccd324
buildSrc/src/main/kotlin
mirai-console/tools/gradle-plugin/src/main/kotlin
mirai-core-utils/src
commonMain/kotlin
jvmBaseMain/kotlin
nativeMain/kotlin
nativeMainInterop
nativeTest/kotlin
mirai-core/src/commonMain/kotlin/message/protocol
@ -22,7 +22,7 @@ object Versions {
|
||||
val consoleIntellij = "221-$project-162-1" // idea-mirai-kotlin-patch
|
||||
val consoleTerminal = project
|
||||
|
||||
const val kotlinCompiler = "1.6.21"
|
||||
const val kotlinCompiler = "1.7.0-RC"
|
||||
const val kotlinStdlib = kotlinCompiler
|
||||
const val dokka = "1.6.20"
|
||||
|
||||
|
@ -41,8 +41,10 @@ public class MiraiConsoleGradlePlugin : Plugin<Project> {
|
||||
try {
|
||||
languageSettings.optIn("kotlin.RequiresOptIn")
|
||||
} catch (e: NoSuchMethodError) {
|
||||
@Suppress("DEPRECATION")
|
||||
languageSettings.useExperimentalAnnotation("kotlin.RequiresOptIn")
|
||||
// User is using < 1.6
|
||||
target.compilations.forEach { compilation ->
|
||||
compilation.kotlinOptions.freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
|
||||
}
|
||||
}
|
||||
dependencies { configureDependencies(project, this@configureSourceSet, target) }
|
||||
}
|
||||
|
@ -23,12 +23,6 @@ public expect fun <E> ConcurrentLinkedDeque(): MutableDeque<E>
|
||||
@Suppress("FunctionName")
|
||||
public fun <E> ConcurrentLinkedQueue(): MutableQueue<E> = ConcurrentLinkedDeque()
|
||||
|
||||
@Suppress("FunctionName")
|
||||
public expect fun <E : Comparable<*>> PriorityQueue(): MutableQueue<E>
|
||||
|
||||
@Suppress("FunctionName")
|
||||
public expect fun <E : Any> PriorityQueue(comparator: Comparator<E>): MutableCollection<E>
|
||||
|
||||
public expect class LinkedList<E> constructor() : MutableList<E> {
|
||||
public fun addLast(element: E)
|
||||
}
|
||||
|
@ -38,16 +38,6 @@ public actual fun <K : Any, V> ConcurrentHashMap(): MutableMap<K, V> {
|
||||
return java.util.concurrent.ConcurrentHashMap()
|
||||
}
|
||||
|
||||
@Suppress("FunctionName")
|
||||
public actual fun <E : Comparable<*>> PriorityQueue(): MutableQueue<E> {
|
||||
return java.util.PriorityQueue()
|
||||
}
|
||||
|
||||
@Suppress("FunctionName")
|
||||
public actual fun <E : Any> PriorityQueue(comparator: Comparator<E>): MutableCollection<E> {
|
||||
return java.util.PriorityQueue(comparator)
|
||||
}
|
||||
|
||||
public actual typealias LinkedList<E> = java.util.LinkedList<E>
|
||||
|
||||
|
||||
|
@ -11,26 +11,187 @@
|
||||
|
||||
package net.mamoe.mirai.utils
|
||||
|
||||
import kotlinx.atomicfu.locks.ReentrantLock
|
||||
import kotlinx.atomicfu.locks.reentrantLock
|
||||
import kotlinx.atomicfu.locks.withLock
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
@Suppress("FunctionName")
|
||||
public actual fun <K : Any, V> ConcurrentHashMap(): MutableMap<K, V> {
|
||||
TODO("Not yet implemented")
|
||||
return LockedConcurrentHashMap(reentrantLock())
|
||||
}
|
||||
|
||||
private class LockedConcurrentHashSet<E>(
|
||||
private val lock: ReentrantLock,
|
||||
private val delegate: MutableSet<E> = mutableSetOf()
|
||||
) : MutableSet<E> {
|
||||
override fun add(element: E): Boolean = lock.withLock {
|
||||
return delegate.add(element)
|
||||
}
|
||||
|
||||
override fun addAll(elements: Collection<E>): Boolean = lock.withLock {
|
||||
return delegate.addAll(elements)
|
||||
}
|
||||
|
||||
override val size: Int get() = lock.withLock { delegate.size }
|
||||
|
||||
override fun clear() = lock.withLock { delegate.clear() }
|
||||
|
||||
override fun isEmpty(): Boolean = lock.withLock { delegate.isEmpty() }
|
||||
override fun containsAll(elements: Collection<E>): Boolean = lock.withLock { delegate.containsAll(elements) }
|
||||
override fun contains(element: E): Boolean = lock.withLock { delegate.contains(element) }
|
||||
|
||||
override fun iterator(): MutableIterator<E> = delegate.iterator() // no effect for locking
|
||||
|
||||
@Suppress("ConvertArgumentToSet")
|
||||
override fun retainAll(elements: Collection<E>): Boolean = lock.withLock { delegate.retainAll(elements) }
|
||||
|
||||
@Suppress("ConvertArgumentToSet")
|
||||
override fun removeAll(elements: Collection<E>): Boolean = lock.withLock { delegate.removeAll(elements) }
|
||||
|
||||
override fun remove(element: E): Boolean = lock.withLock { delegate.remove(element) }
|
||||
|
||||
|
||||
override fun equals(other: Any?): Boolean = delegate == other
|
||||
override fun hashCode(): Int = delegate.hashCode()
|
||||
override fun toString(): String = delegate.toString()
|
||||
}
|
||||
|
||||
private class LockedConcurrentCollection<E>(
|
||||
private val lock: ReentrantLock,
|
||||
private val delegate: MutableCollection<E>
|
||||
) : MutableCollection<E> {
|
||||
override fun add(element: E): Boolean = lock.withLock {
|
||||
return delegate.add(element)
|
||||
}
|
||||
|
||||
override fun addAll(elements: Collection<E>): Boolean = lock.withLock {
|
||||
return delegate.addAll(elements)
|
||||
}
|
||||
|
||||
override val size: Int get() = lock.withLock { delegate.size }
|
||||
|
||||
override fun clear() = lock.withLock { delegate.clear() }
|
||||
|
||||
override fun isEmpty(): Boolean = lock.withLock { delegate.isEmpty() }
|
||||
override fun containsAll(elements: Collection<E>): Boolean = lock.withLock { delegate.containsAll(elements) }
|
||||
override fun contains(element: E): Boolean = lock.withLock { delegate.contains(element) }
|
||||
|
||||
override fun iterator(): MutableIterator<E> = delegate.iterator() // no effect for locking
|
||||
|
||||
@Suppress("ConvertArgumentToSet")
|
||||
override fun retainAll(elements: Collection<E>): Boolean = lock.withLock { delegate.retainAll(elements) }
|
||||
|
||||
@Suppress("ConvertArgumentToSet")
|
||||
override fun removeAll(elements: Collection<E>): Boolean = lock.withLock { delegate.removeAll(elements) }
|
||||
|
||||
override fun remove(element: E): Boolean = lock.withLock { delegate.remove(element) }
|
||||
|
||||
override fun equals(other: Any?): Boolean = delegate == other
|
||||
override fun hashCode(): Int = delegate.hashCode()
|
||||
override fun toString(): String = delegate.toString()
|
||||
}
|
||||
|
||||
private class LockedConcurrentHashMap<K : Any, V>(
|
||||
private val lock: ReentrantLock,
|
||||
private val delegate: MutableMap<K, V> = mutableMapOf()
|
||||
) : MutableMap<K, V> {
|
||||
override val entries: MutableSet<MutableMap.MutableEntry<K, V>>
|
||||
get() = lock.withLock { LockedConcurrentHashSet(lock, delegate.entries) }
|
||||
|
||||
override val keys: MutableSet<K> get() = lock.withLock { LockedConcurrentHashSet(lock, delegate.keys) }
|
||||
override val size: Int get() = lock.withLock { delegate.size }
|
||||
override val values: MutableCollection<V>
|
||||
get() = lock.withLock { LockedConcurrentCollection(lock, delegate.values) }
|
||||
|
||||
override fun clear() = lock.withLock { delegate.clear() }
|
||||
override fun isEmpty(): Boolean = lock.withLock { delegate.isEmpty() }
|
||||
override fun remove(key: K): V? = lock.withLock { delegate.remove(key) }
|
||||
override fun putAll(from: Map<out K, V>) = lock.withLock { delegate.putAll(from) }
|
||||
override fun put(key: K, value: V): V? = lock.withLock { delegate.put(key, value) }
|
||||
override fun get(key: K): V? = lock.withLock { delegate.get(key) }
|
||||
override fun containsValue(value: V): Boolean = lock.withLock { delegate.containsValue(value) }
|
||||
override fun containsKey(key: K): Boolean = lock.withLock { delegate.containsKey(key) }
|
||||
|
||||
override fun equals(other: Any?): Boolean = delegate == other
|
||||
override fun hashCode(): Int = delegate.hashCode()
|
||||
override fun toString(): String = delegate.toString()
|
||||
}
|
||||
|
||||
@Suppress("FunctionName")
|
||||
public actual fun <E> ConcurrentLinkedDeque(): MutableDeque<E> {
|
||||
TODO("Not yet implemented")
|
||||
return LockedConcurrentArrayDeque(reentrantLock())
|
||||
}
|
||||
|
||||
@Suppress("FunctionName")
|
||||
public actual fun <E : Comparable<*>> PriorityQueue(): MutableQueue<E> {
|
||||
TODO("Not yet implemented")
|
||||
private class LockedConcurrentArrayDeque<E>(
|
||||
private val lock: ReentrantLock,
|
||||
private val delegate: ArrayDeque<E> = ArrayDeque()
|
||||
) : MutableDeque<E> {
|
||||
override fun addFirst(element: E) = lock.withLock { delegate.addFirst(element) }
|
||||
|
||||
override fun add(element: E): Boolean {
|
||||
lock.withLock { delegate.add(element) }
|
||||
return true
|
||||
}
|
||||
|
||||
override fun poll(): E? = lock.withLock { delegate.removeFirstOrNull() }
|
||||
|
||||
override fun offer(element: E): Boolean {
|
||||
lock.withLock { delegate.addLast(element) }
|
||||
return true
|
||||
}
|
||||
|
||||
override val size: Int
|
||||
get() = lock.withLock { delegate.size }
|
||||
|
||||
override fun clear() = lock.withLock { delegate.clear() }
|
||||
override fun addAll(elements: Collection<E>): Boolean = lock.withLock { delegate.addAll(elements) }
|
||||
override fun isEmpty(): Boolean = lock.withLock { delegate.isEmpty() }
|
||||
override fun iterator(): MutableIterator<E> = delegate.iterator()
|
||||
override fun retainAll(elements: Collection<E>): Boolean = lock.withLock { delegate.retainAll(elements) }
|
||||
override fun removeAll(elements: Collection<E>): Boolean = lock.withLock { delegate.removeAll(elements) }
|
||||
override fun remove(element: E): Boolean = lock.withLock { delegate.remove(element) }
|
||||
override fun containsAll(elements: Collection<E>): Boolean = lock.withLock { delegate.containsAll(elements) }
|
||||
override fun contains(element: E): Boolean = lock.withLock { delegate.contains(element) }
|
||||
|
||||
override fun equals(other: Any?): Boolean = delegate == other
|
||||
override fun hashCode(): Int = delegate.hashCode()
|
||||
override fun toString(): String = delegate.toString()
|
||||
}
|
||||
|
||||
@Suppress("FunctionName")
|
||||
public actual fun <E : Any> PriorityQueue(comparator: Comparator<E>): MutableCollection<E> {
|
||||
TODO("Not yet implemented")
|
||||
internal class ArrayDequeAsMutableDeque<E>(
|
||||
private val delegate: ArrayDeque<E> = ArrayDeque()
|
||||
) : MutableDeque<E> {
|
||||
override fun addFirst(element: E) = delegate.addFirst(element)
|
||||
|
||||
override fun add(element: E): Boolean {
|
||||
delegate.add(element)
|
||||
return true
|
||||
}
|
||||
|
||||
override fun poll(): E? = delegate.removeFirstOrNull()
|
||||
|
||||
override fun offer(element: E): Boolean {
|
||||
delegate.addLast(element)
|
||||
return true
|
||||
}
|
||||
|
||||
override val size: Int
|
||||
get() = delegate.size
|
||||
|
||||
override fun clear() = delegate.clear()
|
||||
override fun addAll(elements: Collection<E>): Boolean = delegate.addAll(elements)
|
||||
override fun isEmpty(): Boolean = delegate.isEmpty()
|
||||
override fun iterator(): MutableIterator<E> = delegate.iterator()
|
||||
override fun retainAll(elements: Collection<E>): Boolean = delegate.retainAll(elements)
|
||||
override fun removeAll(elements: Collection<E>): Boolean = delegate.removeAll(elements)
|
||||
override fun remove(element: E): Boolean = delegate.remove(element)
|
||||
override fun containsAll(elements: Collection<E>): Boolean = delegate.containsAll(elements)
|
||||
override fun contains(element: E): Boolean = delegate.contains(element)
|
||||
|
||||
override fun equals(other: Any?): Boolean = delegate == other
|
||||
override fun hashCode(): Int = delegate.hashCode()
|
||||
override fun toString(): String = delegate.toString()
|
||||
}
|
||||
|
||||
@Suppress("FunctionName")
|
||||
@ -38,31 +199,16 @@ public actual fun <K : Enum<K>, V> EnumMap(clazz: KClass<K>): MutableMap<K, V> =
|
||||
|
||||
@Suppress("FunctionName")
|
||||
public actual fun <E> ConcurrentSet(): MutableSet<E> {
|
||||
TODO("Not yet implemented")
|
||||
return LockedConcurrentHashSet(reentrantLock())
|
||||
}
|
||||
|
||||
public actual class LinkedList<E> : MutableList<E>, AbstractMutableList<E>() {
|
||||
public actual class LinkedList<E>(
|
||||
private val delegate: ArrayDeque<E>
|
||||
) : MutableList<E> by delegate {
|
||||
public actual constructor() : this(ArrayDeque())
|
||||
|
||||
public actual fun addLast(element: E) {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun add(index: Int, element: E) {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override val size: Int
|
||||
get() = TODO("Not yet implemented")
|
||||
|
||||
override fun get(index: Int): E {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun removeAt(index: Int): E {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun set(index: Int, element: E): E {
|
||||
TODO("Not yet implemented")
|
||||
return delegate.addLast(element)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,7 @@ md5 = "0.7.0"
|
||||
sha1 = "0.10.1"
|
||||
flate2 = "1.0.23"
|
||||
libc = "0.2.126"
|
||||
#chashmap = "2.2.2"
|
||||
|
||||
[lib]
|
||||
name = "mirai_core_utils_i"
|
||||
|
27
mirai-core-utils/src/nativeMainInterop/src/chmap.rs
Normal file
27
mirai-core-utils/src/nativeMainInterop/src/chmap.rs
Normal file
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2019-2022 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
// use std::ops::DerefMut;
|
||||
// use std::ptr::{null, null_mut};
|
||||
//
|
||||
// use chashmap::CHashMap;
|
||||
// use libc::c_void;
|
||||
// //
|
||||
// #[no_mangle]
|
||||
// pub extern "C" fn mirai_chmap_create() -> *mut c_void {
|
||||
// let map = CHashMap::<*mut c_void, *mut c_void>::new();
|
||||
// // Box::into_raw(Box::new(map))
|
||||
// return Box::into_raw(Box::new(map)) as *mut c_void;
|
||||
// }
|
||||
//
|
||||
// #[no_mangle]
|
||||
// pub unsafe extern "C" fn mirai_chmap_put(map: *const c_void, key: *const c_void, value: *const c_void) -> *const c_void {
|
||||
// let chmap = Box::from_raw(map as *mut CHashMap::<*const c_void, *const c_void>);
|
||||
// return chmap.insert(key, value).unwrap_or(null());
|
||||
// }
|
@ -7,6 +7,8 @@
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
// extern crate chashmap;
|
||||
extern crate core;
|
||||
extern crate flate2;
|
||||
extern crate libc;
|
||||
extern crate sha1;
|
||||
@ -14,4 +16,5 @@ extern crate sha1;
|
||||
/// cbindgen:ignore
|
||||
mod bindings;
|
||||
mod crypto;
|
||||
mod chmap;
|
||||
|
||||
|
41
mirai-core-utils/src/nativeTest/kotlin/CollectionsTest.kt
Normal file
41
mirai-core-utils/src/nativeTest/kotlin/CollectionsTest.kt
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2019-2022 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.utils
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFails
|
||||
import kotlin.test.assertFalse
|
||||
|
||||
internal sealed class MapTest(
|
||||
private val map: MutableMap<Int, Int>
|
||||
) {
|
||||
class ConcurrentMapTest : MapTest(ConcurrentHashMap())
|
||||
|
||||
@Test
|
||||
fun `initial state test`() {
|
||||
assertEquals(0, map.size)
|
||||
assertEquals(null, map[1])
|
||||
assertFalse(map.iterator().hasNext())
|
||||
assertFails { map.iterator().next() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get set size`() {
|
||||
assertEquals(0, map.size)
|
||||
assertEquals(null, map[1])
|
||||
map[1] = 2
|
||||
assertEquals(2, map[1])
|
||||
assertEquals(1, map.size)
|
||||
map[2] = 2
|
||||
assertEquals(2, map[2])
|
||||
assertEquals(2, map.size)
|
||||
}
|
||||
}
|
@ -170,7 +170,7 @@ internal class MessageProtocolFacadeImpl(
|
||||
|
||||
override val loaded: List<MessageProtocol> = kotlin.run {
|
||||
val instances = protocols
|
||||
.toCollection(PriorityQueue(MessageProtocol.PriorityComparator.reversed()))
|
||||
.sortedWith(MessageProtocol.PriorityComparator.reversed())
|
||||
for (instance in instances) {
|
||||
instance.collectProcessors(object : ProcessorCollector() {
|
||||
override fun <T : SingleMessage> add(encoder: MessageEncoder<T>, elementType: KClass<T>) {
|
||||
|
Loading…
Reference in New Issue
Block a user