More refactorings of SerializationManager

This commit is contained in:
Dmitry Lomov
2012-06-04 13:15:33 +02:00
parent 252b433fa3
commit 57d28b65f6
7 changed files with 90 additions and 167 deletions
@@ -32,14 +32,14 @@ import java.util.List;
* Author: dmitrylomov
*/
public class StubSerializationHelper {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.stubs.SerializationManagerBase");
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.stubs.StubSerializationHelper");
private AbstractStringEnumerator myNameStorage;
protected final TIntObjectHashMap<StubSerializer<? extends StubElement>> myIdToSerializer = new TIntObjectHashMap<StubSerializer<? extends StubElement>>();
protected final TObjectIntHashMap<StubSerializer<? extends StubElement>> mySerializerToId = new TObjectIntHashMap<StubSerializer<? extends StubElement>>();
protected StubSerializationHelper(AbstractStringEnumerator nameStorage) {
public StubSerializationHelper(AbstractStringEnumerator nameStorage) {
myNameStorage = nameStorage;
}
public void assignId(@NotNull final StubSerializer<? extends StubElement> serializer) throws IOException {
@@ -59,7 +59,7 @@ public class StubSerializationHelper {
}
private void doSerialize(final StubElement rootStub, final StubOutputStream stream) throws IOException {
final StubSerializer serializer = SerializationManager.getInstance().getSerializer(rootStub);
final StubSerializer serializer = StubSerializationUtil.getSerializer(rootStub);
DataInputOutputUtil.writeINT(stream, getClassId(serializer));
serializer.serialize(rootStub, stream);
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,25 +13,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* @author max
*/
package com.intellij.psi.stubs;
import com.intellij.openapi.application.ApplicationManager;
/**
* Author: dmitrylomov
*/
public abstract class StubSerializationUtil {
private StubSerializationUtil() {}
import java.io.InputStream;
import java.io.OutputStream;
public static StubSerializer getSerializer(StubElement rootStub) {
if (rootStub instanceof PsiFileStub) {
final PsiFileStub fileStub = (PsiFileStub)rootStub;
return fileStub.getType();
}
public abstract class SerializationManager {
public static SerializationManager getInstance() {
return ApplicationManager.getApplication().getComponent(SerializationManager.class);
return rootStub.getStubType();
}
public abstract void registerSerializer(StubSerializer<? extends StubElement> serializer);
public abstract StubSerializer getSerializer(StubElement rootStub);
}
}
@@ -67,13 +67,12 @@ public class StubTree {
public Map<StubIndexKey, Map<Object, TIntArrayList>> indexStubTree() {
final Map<StubIndexKey, Map<Object, TIntArrayList>> result = new HashMap<StubIndexKey, Map<Object, TIntArrayList>>();
SerializationManager serializationManager = SerializationManager.getInstance();
StubIndexSink sink = new StubIndexSink(result);
for (int i = 0, plainListSize = myPlainList.size(); i < plainListSize; i++) {
final StubElement<?> stub = myPlainList.get(i);
sink.myStubIdx = i;
serializationManager.getSerializer(stub).indexStub(stub, sink);
StubSerializationUtil.getSerializer(stub).indexStub(stub, sink);
}
return result;
@@ -0,0 +1,70 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* @author max
*/
package com.intellij.psi.stubs;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.IStubFileElementType;
import java.util.ArrayList;
import java.util.List;
public abstract class SerializationManager {
protected final List<StubSerializer<? extends StubElement>> myAllSerializers = new ArrayList<StubSerializer<? extends StubElement>>();
private volatile boolean mySerializersLoaded = false;
public static SerializationManager getInstance() {
return ApplicationManager.getApplication().getComponent(SerializationManager.class);
}
public void registerSerializer(StubSerializer<? extends StubElement> serializer) {
myAllSerializers.add(serializer);
}
protected void initSerializers() {
if (mySerializersLoaded) return;
synchronized (this) {
if (mySerializersLoaded) return;
for (StubElementTypeHolderEP holderEP : Extensions.getExtensions(StubElementTypeHolderEP.EP_NAME)) {
holderEP.initialize();
}
final IElementType[] stubElementTypes = IElementType.enumerate(new IElementType.Predicate() {
public boolean matches(final IElementType type) {
return type instanceof StubSerializer;
}
});
for (IElementType type : stubElementTypes) {
if (type instanceof IStubFileElementType &&
((IStubFileElementType)type).getExternalId().equals(PsiFileStubImpl.TYPE.getExternalId())) {
continue;
}
StubSerializer stubSerializer = (StubSerializer)type;
if (!myAllSerializers.contains(stubSerializer)) {
registerSerializer(stubSerializer);
}
}
mySerializersLoaded = true;
}
}
}
@@ -1,100 +0,0 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* @author max
*/
package com.intellij.psi.stubs;
import com.intellij.util.io.UnsyncByteArrayInputStream;
import org.jetbrains.annotations.Nullable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
public class SerializedStubTree {
private final byte[] myBytes;
private final int myLength;
private StubElement myStubElement;
public SerializedStubTree(final byte[] bytes, int length, @Nullable StubElement stubElement) {
myBytes = bytes;
myLength = length;
myStubElement = stubElement;
}
public SerializedStubTree(DataInput in) throws IOException {
myLength = in.readInt();
myBytes = new byte[myLength];
myStubElement = null;
in.readFully(myBytes);
}
public void write(DataOutput out) throws IOException{
out.writeInt(myLength);
out.write(myBytes, 0, myLength);
}
// willIndexStub is one time optimization hint, once can safely pass false
public StubElement getStub(boolean willIndexStub) {
StubElement stubElement = myStubElement;
if (stubElement != null) {
// not null myStubElement means we just built SerializedStubTree for indexing,
// if we request stub for indexing we can safely use it
myStubElement = null;
if (willIndexStub) return stubElement;
}
return SerializationManagerEx.getInstanceEx().deserialize(new UnsyncByteArrayInputStream(myBytes));
}
public boolean equals(final Object that) {
if (this == that) {
return true;
}
if (!(that instanceof SerializedStubTree)) {
return false;
}
final SerializedStubTree thatTree = (SerializedStubTree)that;
final int length = myLength;
if (length != thatTree.myLength) {
return false;
}
final byte[] thisBytes = myBytes;
final byte[] thatBytes = thatTree.myBytes;
for (int i=0; i< length; i++) {
if (thisBytes[i] != thatBytes[i]) {
return false;
}
}
return true;
}
public int hashCode() {
if (myBytes == null)
return 0;
int result = 1;
for (int i = 0; i < myLength; i++) {
result = 31 * result + myBytes[i];
}
return result;
}
}
@@ -17,26 +17,17 @@ package com.intellij.psi.stubs;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.diagnostic.LogUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.util.ShutDownTracker;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.IStubFileElementType;
import com.intellij.util.io.AbstractStringEnumerator;
import com.intellij.util.io.DataInputOutputUtil;
import com.intellij.util.io.PersistentStringEnumerator;
import gnu.trove.TIntObjectHashMap;
import gnu.trove.TObjectIntHashMap;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
/*
@@ -49,9 +40,7 @@ public class SerializationManagerImpl extends SerializationManagerEx implements
private final File myFile = new File(PathManager.getIndexRoot(), "rep.names");
private final AtomicBoolean myShutdownPerformed = new AtomicBoolean(false);
private AbstractStringEnumerator myNameStorage;
private final List<StubSerializer<? extends StubElement>> myAllSerializers = new ArrayList<StubSerializer<? extends StubElement>>();
private StubSerializationHelper myStubSerializationHelper;
private volatile boolean mySerializersLoaded = false;
public SerializationManagerImpl() {
myFile.getParentFile().mkdirs();
@@ -145,7 +134,7 @@ public class SerializationManagerImpl extends SerializationManagerEx implements
}
public void registerSerializer(@NotNull StubSerializer<? extends StubElement> serializer) {
myAllSerializers.add(serializer);
super.registerSerializer(serializer);
try {
myStubSerializationHelper.assignId(serializer);
}
@@ -156,31 +145,9 @@ public class SerializationManagerImpl extends SerializationManagerEx implements
}
private synchronized void initSerializers() {
if (mySerializersLoaded) return;
for(StubElementTypeHolderEP holderEP: Extensions.getExtensions(StubElementTypeHolderEP.EP_NAME)) {
holderEP.initialize();
}
final IElementType[] stubElementTypes = IElementType.enumerate(new IElementType.Predicate() {
public boolean matches(final IElementType type) {
return type instanceof StubSerializer;
}
});
for(IElementType type: stubElementTypes) {
if (type instanceof IStubFileElementType && ((IStubFileElementType) type).getExternalId().equals(PsiFileStubImpl.TYPE.getExternalId())) {
continue;
}
StubSerializer stubSerializer = (StubSerializer) type;
if (!myAllSerializers.contains(stubSerializer)) {
registerSerializer(stubSerializer);
}
}
mySerializersLoaded = true;
}
public void serialize(StubElement rootStub, OutputStream stream) {
if (!mySerializersLoaded) initSerializers();
initSerializers();
try {
myStubSerializationHelper.serialize(rootStub, stream);
}
@@ -190,17 +157,9 @@ public class SerializationManagerImpl extends SerializationManagerEx implements
}
}
public StubSerializer getSerializer(final StubElement rootStub) {
if (rootStub instanceof PsiFileStub) {
final PsiFileStub fileStub = (PsiFileStub)rootStub;
return fileStub.getType();
}
return rootStub.getStubType();
}
public StubElement deserialize(InputStream stream) {
if (!mySerializersLoaded) initSerializers();
initSerializers();
try {
return myStubSerializationHelper.deserialize(stream);