summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/openapi/vfs/newvfs/impl/FileNameCache.java
blob: 3f2c4abebf7f2039178aab7ff3ddb930a277b3b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright 2000-2014 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.
 */
package com.intellij.openapi.vfs.newvfs.impl;

import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.newvfs.persistent.FSRecords;
import com.intellij.util.containers.IntObjectLinkedMap;
import com.intellij.util.IntSLRUCache;
import com.intellij.util.io.IOUtil;
import com.intellij.util.io.PersistentStringEnumerator;
import com.intellij.util.text.StringFactory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author peter
 */
public class FileNameCache {
  private static final PersistentStringEnumerator ourNames = FSRecords.getNames();
  @SuppressWarnings("unchecked") private static final IntSLRUCache<IntObjectLinkedMap.MapEntry<Object>>[] ourNameCache = new IntSLRUCache[16];
  static {
    final int protectedSize = 40000 / ourNameCache.length;
    final int probationalSize = 20000 / ourNameCache.length;
    for(int i = 0; i < ourNameCache.length; ++i) {
      ourNameCache[i] = new IntSLRUCache<IntObjectLinkedMap.MapEntry<Object>>(protectedSize, probationalSize);
    }
  }

  public static int storeName(@NotNull String name) {
    final int idx = FSRecords.getNameId(name);
    cacheData(name, idx, calcStripeIdFromNameId(idx));
    return idx;
  }

  @NotNull
  private static IntObjectLinkedMap.MapEntry<Object> cacheData(String name, int id, int stripe) {
    if (name == null) {
      ourNames.markCorrupted();
      throw new RuntimeException("VFS name enumerator corrupted");
    }

    Object rawName = convertToBytesIfAsciiString(name);
    IntObjectLinkedMap.MapEntry<Object> entry = new IntObjectLinkedMap.MapEntry<Object>(id, rawName);
    synchronized (ourNameCache[stripe]) {
      return ourNameCache[stripe].cacheEntry(entry);
    }
  }

  private static int calcStripeIdFromNameId(int id) {
    int h = id;
    h -= (h<<6);
    h ^= (h>>17);
    h -= (h<<9);
    h ^= (h<<4);
    h -= (h<<3);
    h ^= (h<<10);
    h ^= (h>>15);
    return h % ourNameCache.length;
  }

  @NotNull
  private static Object convertToBytesIfAsciiString(@NotNull String name) {
    int length = name.length();
    if (length == 0) return "";

    if (!IOUtil.isAscii(name)) {
      return new String(name); // So we don't hold whole char[] buffer of a lengthy path on JDK 6
    }

    byte[] bytes = new byte[length];
    for (int i = 0; i < length; i++) {
      bytes[i] = (byte)name.charAt(i);
    }
    return bytes;
  }

  @NotNull
  private static IntObjectLinkedMap.MapEntry<Object> getEntry(int id) {
    final int stripe = calcStripeIdFromNameId(id);
    synchronized (ourNameCache[stripe]) {
      IntObjectLinkedMap.MapEntry<Object> entry = ourNameCache[stripe].getCachedEntry(id);
      if (entry != null) {
        return entry;
      }
    }

    return cacheData(FSRecords.getNameByNameId(id), id, stripe);
  }

  @NotNull
  public static String getVFileName(int nameId) {
    IntObjectLinkedMap.MapEntry<Object> entry = getEntry(nameId);
    Object name = entry.value;
    if (name instanceof String) {
      //noinspection StringEquality
      return (String)name;
    }

    byte[] bytes = (byte[])name;
    int length = bytes.length;
    char[] chars = new char[length];
    for (int i = 0; i < length; i++) {
      chars[i] = (char)bytes[i];
    }
    return StringFactory.createShared(chars);
  }

  static int compareNameTo(int nameId, @NotNull String name, boolean ignoreCase) {
    IntObjectLinkedMap.MapEntry<Object> entry = getEntry(nameId);
    Object rawName = entry.value;
    if (rawName instanceof String) {
      String thisName = (String)rawName;
      return VirtualFileSystemEntry.compareNames(thisName, name, ignoreCase);
    }

    byte[] bytes = (byte[])rawName;
    int bytesLength = bytes.length;

    int d = bytesLength - name.length();
    if (d != 0) return d;

    return compareBytes(bytes, name, bytesLength, ignoreCase);
  }

  private static int compareBytes(@NotNull byte[] name1, @NotNull String name2, int len, boolean ignoreCase) {
    for (int i = 0; i < len; i++) {
      char c1 = (char)name1[i];
      char c2 = name2.charAt(i);
      int d = StringUtil.compare(c1, c2, ignoreCase);
      if (d != 0) return d;
    }
    return 0;
  }

  @NotNull
  static char[] appendPathOnFileSystem(int nameId, @Nullable VirtualFileSystemEntry parent, int accumulatedPathLength, @NotNull int[] positionRef) {
    IntObjectLinkedMap.MapEntry<Object> entry = getEntry(nameId);
    Object o = entry.value;
    int nameLength = o instanceof String ? ((String)o).length() : ((byte[])o).length;
    boolean appendSlash = SystemInfo.isWindows && parent == null && nameLength == 2 &&
                          (o instanceof String ? ((String)o).charAt(1) : (char)((byte[])o)[1]) == ':';

    char[] chars;
    if (parent != null) {
      chars = parent.appendPathOnFileSystem(accumulatedPathLength + 1 + nameLength, positionRef);
      if (positionRef[0] > 0 && chars[positionRef[0] - 1] != '/') {
        chars[positionRef[0]++] = '/';
      }
    }
    else {
      int rootPathLength = accumulatedPathLength + nameLength;
      if (appendSlash) ++rootPathLength;
      chars = new char[rootPathLength];
    }

    if (o instanceof String) {
      positionRef[0] = VirtualFileSystemEntry.copyString(chars, positionRef[0], (String)o);
    }
    else {
      byte[] bytes = (byte[])o;
      int pos = positionRef[0];
      //noinspection ForLoopReplaceableByForEach
      for (int i = 0, len = bytes.length; i < len; i++) {
        chars[pos++] = (char)bytes[i];
      }
      positionRef[0] = pos;
    }

    if (appendSlash) {
      chars[positionRef[0]++] = '/';
    }

    return chars;
  }

}