expose ProjectStateStorageManager for upsource

extract DirectoryBasedStorageBase for upsource
This commit is contained in:
Vladimir Krivosheev
2016-02-25 19:24:12 +01:00
parent 5396a1bb18
commit 579c34be31
3 changed files with 43 additions and 24 deletions
@@ -41,16 +41,15 @@ import java.io.IOException
import java.nio.ByteBuffer
import java.nio.file.Path
open class DirectoryBasedStorage(private val dir: Path,
private val splitter: StateSplitter,
private val pathMacroSubstitutor: TrackingPathMacroSubstitutor? = null) : StateStorageBase<StateMap>() {
private @Volatile var virtualFile: VirtualFile? = null
abstract class DirectoryBasedStorageBase(@Suppress("DEPRECATION") protected val splitter: StateSplitter,
protected val pathMacroSubstitutor: TrackingPathMacroSubstitutor? = null) : StateStorageBase<StateMap>() {
protected var componentName: String? = null
private var componentName: String? = null
protected abstract val virtualFile: VirtualFile?
fun setVirtualDir(dir: VirtualFile?) {
virtualFile = dir
}
override fun loadData() = StateMap.fromMap(DirectoryStorageUtil.loadFrom(virtualFile, pathMacroSubstitutor))
override fun startExternalization(): StateStorage.ExternalizationSession? = null
override fun analyzeExternalChangesAndUpdateIfNeed(componentNames: MutableSet<String>) {
// todo reload only changed file, compute diff
@@ -89,15 +88,26 @@ open class DirectoryBasedStorage(private val dir: Path,
return state
}
override fun loadData() = StateMap.fromMap(DirectoryStorageUtil.loadFrom(getVirtualFile(), pathMacroSubstitutor))
override fun hasState(storageData: StateMap, componentName: String) = storageData.hasStates()
}
private fun getVirtualFile(): VirtualFile? {
var result = virtualFile
if (result == null) {
result = LocalFileSystem.getInstance().findFileByPath(dir.systemIndependentPath)
virtualFile = result
open class DirectoryBasedStorage(private val dir: Path,
@Suppress("DEPRECATION") splitter: StateSplitter,
pathMacroSubstitutor: TrackingPathMacroSubstitutor? = null) : DirectoryBasedStorageBase(splitter, pathMacroSubstitutor) {
private @Volatile var cachedVirtualFile: VirtualFile? = null
override val virtualFile: VirtualFile?
get() {
var result = cachedVirtualFile
if (result == null) {
result = LocalFileSystem.getInstance().findFileByPath(dir.systemIndependentPath)
cachedVirtualFile = result
}
return result
}
return result
internal fun setVirtualDir(dir: VirtualFile?) {
cachedVirtualFile = dir
}
override fun startExternalization(): StateStorage.ExternalizationSession? = if (checkIsSavingDisabled()) null else MySaveSession(this, getStorageData())
@@ -159,7 +169,7 @@ open class DirectoryBasedStorage(private val dir: Path,
override fun save() {
val stateMap = StateMap.fromMap(copiedStorageData!!)
var dir = storage.getVirtualFile()
var dir = storage.virtualFile
if (copiedStorageData!!.isEmpty()) {
if (dir != null && dir.exists()) {
deleteFile(this, dir)
@@ -170,7 +180,7 @@ open class DirectoryBasedStorage(private val dir: Path,
if (dir == null || !dir.isValid) {
dir = createDir(storage.dir, this)
storage.virtualFile = dir
storage.cachedVirtualFile = dir
}
if (!dirtyFileNames.isEmpty) {
@@ -233,8 +243,6 @@ open class DirectoryBasedStorage(private val dir: Path,
private fun setStorageData(newStates: StateMap) {
storageDataRef.set(newStates)
}
override fun hasState(storageData: StateMap, componentName: String) = storageData.hasStates()
}
private val NON_EXISTENT_FILE_DATA = Pair.create<ByteArray, String>(null, SystemProperties.getLineSeparator())
@@ -22,7 +22,10 @@ import com.intellij.openapi.components.TrackingPathMacroSubstitutor
import com.intellij.openapi.project.impl.ProjectImpl
import org.jdom.Element
class ProjectStateStorageManager(macroSubstitutor: TrackingPathMacroSubstitutor, private val project: ProjectImpl) : StateStorageManagerImpl("project", macroSubstitutor, project) {
// extended in upsource
open class ProjectStateStorageManager(macroSubstitutor: TrackingPathMacroSubstitutor,
private val project: ProjectImpl,
useVirtualFileTracker: Boolean = true) : StateStorageManagerImpl("project", macroSubstitutor, if (useVirtualFileTracker) project else null) {
companion object {
val VERSION_OPTION = "version"
}
@@ -200,8 +200,10 @@ open class StateStorageManagerImpl(private val rootTagName: String,
val filePath = expandMacros(collapsedPath)
@Suppress("DEPRECATION")
if (stateSplitter != StateSplitter::class.java && stateSplitter != StateSplitterEx::class.java) {
val storage = MyDirectoryStorage(this, Paths.get(filePath), ReflectionUtil.newInstance(stateSplitter))
virtualFileTracker?.put(filePath, storage)
val storage = createDirectoryBasedStorage(filePath, collapsedPath, ReflectionUtil.newInstance(stateSplitter))
if (storage is StorageVirtualFileTracker.TrackedStorage) {
virtualFileTracker?.put(filePath, storage)
}
return storage
}
@@ -210,13 +212,19 @@ open class StateStorageManagerImpl(private val rootTagName: String,
}
val effectiveRoamingType = if (roamingType == RoamingType.DEFAULT && collapsedPath == StoragePathMacros.WORKSPACE_FILE) RoamingType.DISABLED else roamingType
val storage = MyFileStorage(this, File(filePath), collapsedPath, rootTagName, effectiveRoamingType, getMacroSubstitutor(collapsedPath), streamProvider)
if (isUseVfsListener == ThreeState.YES) {
val storage = createFileBasedStorage(filePath, collapsedPath, effectiveRoamingType)
if (isUseVfsListener == ThreeState.YES && storage is StorageVirtualFileTracker.TrackedStorage) {
virtualFileTracker?.put(filePath, storage)
}
return storage
}
protected open fun createFileBasedStorage(path: String, collapsedPath: String, roamingType: RoamingType): StateStorage
= MyFileStorage(this, File(path), collapsedPath, rootTagName, roamingType, getMacroSubstitutor(collapsedPath), streamProvider)
protected open fun createDirectoryBasedStorage(path: String, collapsedPath: String, @Suppress("DEPRECATION") splitter: StateSplitter): StateStorage
= MyDirectoryStorage(this, Paths.get(path), splitter)
private class MyDirectoryStorage(override val storageManager: StateStorageManagerImpl, file: Path, @Suppress("DEPRECATION") splitter: StateSplitter) :
DirectoryBasedStorage(file, splitter, storageManager.pathMacroSubstitutor), StorageVirtualFileTracker.TrackedStorage