From 3b762836519b8ba2b3349f6514f94de0db25525c Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Thu, 20 Aug 2015 15:36:48 +0200 Subject: [PATCH] cleanup --- .../configuration-store-impl/src/StateMap.kt | 89 +++++-------------- .../src/XmlElementStorage.kt | 2 - 2 files changed, 22 insertions(+), 69 deletions(-) diff --git a/platform/configuration-store-impl/src/StateMap.kt b/platform/configuration-store-impl/src/StateMap.kt index ba2f3ec8d996..1abcab0d2944 100644 --- a/platform/configuration-store-impl/src/StateMap.kt +++ b/platform/configuration-store-impl/src/StateMap.kt @@ -28,9 +28,9 @@ import org.iq80.snappy.SnappyOutputStream import org.jdom.Element import org.jdom.output.Format import java.io.ByteArrayInputStream -import java.io.IOException import java.io.OutputStreamWriter import java.util.Arrays +import java.util.TreeMap import java.util.concurrent.atomic.AtomicReferenceArray class StateMap private constructor(private val names: Array, private val states: AtomicReferenceArray) { @@ -47,23 +47,23 @@ class StateMap private constructor(private val names: Array, private val } val names = map.keySet().toTypedArray() - Arrays.sort(names) + if (map !is TreeMap) { + Arrays.sort(names) + } + val states = AtomicReferenceArray(names.size()) - var i = 0 - val n = names.size() - while (i < n) { + for (i in names.indices) { states.set(i, map.get(names[i])) - i++ } return StateMap(names, states) } - public fun stateToElement(key: String, state: Any?, newLiveStates: Map = emptyMap()): Element { + public fun stateToElement(key: String, state: Any?, newLiveStates: Map? = null): Element { if (state is Element) { return state.clone() } else { - return newLiveStates.get(key) ?: unarchiveState(state as ByteArray) + return newLiveStates?.get(key) ?: unarchiveState(state as ByteArray) } } @@ -73,13 +73,15 @@ class StateMap private constructor(private val names: Array, private val return null } else if (LOG.isDebugEnabled() && SystemProperties.getBooleanProperty("idea.log.changed.components", false)) { + fun stateToString(state: Any) = JDOMUtil.writeParent(state as? Element ?: unarchiveState(state as ByteArray), "\n") + val before = stateToString(oldState) val after = stateToString(newState) if (before == after) { LOG.debug("Serialization error: serialized are different, but unserialized are equal") } else { - LOG.debug(key + " " + StringUtil.repeat("=", 80 - key.length()) + "\nBefore:\n" + before + "\nAfter:\n" + after) + LOG.debug("$key ${StringUtil.repeat("=", 80 - key.length())}\nBefore:\n$before\nAfter:\n$after") } } return newBytes @@ -87,42 +89,15 @@ class StateMap private constructor(private val names: Array, private val private fun archiveState(state: Element): ByteArray { val byteOut = BufferExposingByteArrayOutputStream() - try { - val writer = OutputStreamWriter(SnappyOutputStream(byteOut), CharsetToolkit.UTF8_CHARSET) - try { - val xmlOutputter = JDOMUtil.MyXMLOutputter() - xmlOutputter.setFormat(XML_FORMAT) - xmlOutputter.output(state, writer) - } - finally { - writer.close() - } + OutputStreamWriter(SnappyOutputStream(byteOut), CharsetToolkit.UTF8_CHARSET).use { + val xmlOutputter = JDOMUtil.MyXMLOutputter() + xmlOutputter.setFormat(XML_FORMAT) + xmlOutputter.output(state, it) } - catch (e: IOException) { - throw RuntimeException(e) - } - return ArrayUtil.realloc(byteOut.getInternalBuffer(), byteOut.size()) } private fun unarchiveState(state: ByteArray) = JDOMUtil.load(SnappyInputStream(ByteArrayInputStream(state))) - - fun stateToString(state: Any): String { - val element: Element - if (state is Element) { - element = state - } - else { - try { - element = unarchiveState(state as ByteArray) - } - catch (e: Throwable) { - LOG.error(e) - return "internal error" - } - } - return JDOMUtil.writeParent(element, "\n") - } } public fun toMutableMap(): MutableMap { @@ -136,31 +111,20 @@ class StateMap private constructor(private val names: Array, private val /** * Sorted by name. */ - fun keys(): Array { - return names - } + fun keys() = names public fun get(key: String): Any? { val index = Arrays.binarySearch(names, key) return if (index < 0) null else states.get(index) } - public fun getElement(key: String, newLiveStates: Map): Element { - return stateToElement(key, get(key), newLiveStates) - } + fun getElement(key: String, newLiveStates: Map) = stateToElement(key, get(key), newLiveStates) - public fun isEmpty(): Boolean { - return names.size() == 0 - } + fun isEmpty() = names.isEmpty() - public fun getState(key: String): Element? { - val state = get(key) - return if (state is Element) state else null - } + fun getState(key: String) = get(key) as? Element - public fun hasState(key: String): Boolean { - return get(key) is Element - } + fun hasState(key: String) = get(key) is Element public fun hasStates(): Boolean { if (isEmpty()) { @@ -194,16 +158,7 @@ class StateMap private constructor(private val names: Array, private val return null } - val state = states.get(index) - if (state !is Element) { - return null - } - - if (states.compareAndSet(index, state, archiveState(state))) { - return state - } - else { - return getStateAndArchive(key) - } + val state = states.get(index) as? Element ?: return null + return if (states.compareAndSet(index, state, archiveState(state))) state else getStateAndArchive(key) } } \ No newline at end of file diff --git a/platform/configuration-store-impl/src/XmlElementStorage.kt b/platform/configuration-store-impl/src/XmlElementStorage.kt index 43b4aecb93e2..a23318dfc1f8 100644 --- a/platform/configuration-store-impl/src/XmlElementStorage.kt +++ b/platform/configuration-store-impl/src/XmlElementStorage.kt @@ -25,7 +25,6 @@ import com.intellij.util.containers.SmartHashSet import gnu.trove.THashMap import org.jdom.Attribute import org.jdom.Element -import org.jdom.JDOMException import java.io.IOException abstract class XmlElementStorage protected constructor(protected val fileSpec: String, @@ -60,7 +59,6 @@ abstract class XmlElementStorage protected constructor(protected val fileSpec: S return if (element == null) StateMap.EMPTY else loadState(element) } - throws(IOException::class, JDOMException::class) private fun loadDataFromProvider() = JDOMUtil.load(provider!!.loadContent(fileSpec, roamingType)) private fun loadState(element: Element): StateMap {