Java 类com.intellij.util.containers.SLRUCache 实例源码

项目:intellij-ce-playground    文件:ObjectObjectPersistentMultiMaplet.java   
public ObjectObjectPersistentMultiMaplet(final File file,
                                      final KeyDescriptor<K> keyExternalizer,
                                      final DataExternalizer<V> valueExternalizer,
                                      final CollectionFactory<V> collectionFactory) throws IOException {
  myValueExternalizer = valueExternalizer;
  myMap = new PersistentHashMap<K, Collection<V>>(file, keyExternalizer, new CollectionDataExternalizer<V>(valueExternalizer, collectionFactory));
  myCache = new SLRUCache<K, Collection>(CACHE_SIZE, CACHE_SIZE, keyExternalizer) {
    @NotNull
    @Override
    public Collection createValue(K key) {
      try {
        final Collection<V> collection = myMap.get(key);
        return collection == null? NULL_COLLECTION : collection;
      }
      catch (IOException e) {
        throw new BuildDataCorruptedException(e);
      }
    }
  };
}
项目:intellij-ce-playground    文件:IntIntPersistentMultiMaplet.java   
public IntIntPersistentMultiMaplet(final File file, final KeyDescriptor<Integer> keyExternalizer) throws IOException {
  myMap = new PersistentHashMap<Integer, TIntHashSet>(file, keyExternalizer, new IntSetExternalizer());
  myCache = new SLRUCache<Integer, TIntHashSet>(CACHE_SIZE, CACHE_SIZE) {
    @NotNull
    @Override
    public TIntHashSet createValue(Integer key) {
      try {
        final TIntHashSet collection = myMap.get(key);
        return collection == null? NULL_COLLECTION : collection;
      }
      catch (IOException e) {
        throw new BuildDataCorruptedException(e);
      }
    }
  };
}
项目:intellij-ce-playground    文件:IntObjectPersistentMultiMaplet.java   
public IntObjectPersistentMultiMaplet(final File file,
                                      final KeyDescriptor<Integer> keyExternalizer,
                                      final DataExternalizer<V> valueExternalizer,
                                      final CollectionFactory<V> collectionFactory) throws IOException {
  myValueExternalizer = valueExternalizer;
  myMap = new PersistentHashMap<Integer, Collection<V>>(file, keyExternalizer, new CollectionDataExternalizer<V>(valueExternalizer, collectionFactory));
  myCache = new SLRUCache<Integer, Collection>(CACHE_SIZE, CACHE_SIZE) {
    @NotNull
    @Override
    public Collection createValue(Integer key) {
      try {
        final Collection<V> collection = myMap.get(key);
        return collection == null? NULL_COLLECTION : collection;
      }
      catch (IOException e) {
        throw new BuildDataCorruptedException(e);
      }
    }
  };
}
项目:intellij-ce-playground    文件:FileAccessorCache.java   
public FileAccessorCache(int protectedQueueSize, int probationalQueueSize) {
  myCache = new SLRUCache<K, Handle<T>>(protectedQueueSize, probationalQueueSize, this) {
    @NotNull
    @Override
    public final Handle<T> createValue(K path) {
      try {
        return new Handle<T>(createAccessor(path), FileAccessorCache.this);
      } catch (IOException ex) {
        throw new RuntimeException(ex);
      }
    }

    @Override
    protected final void onDropFromCache(K key, Handle<T> value) {
      value.release();
    }
  };
}
项目:tools-idea    文件:ObjectObjectPersistentMultiMaplet.java   
public ObjectObjectPersistentMultiMaplet(final File file,
                                      final KeyDescriptor<K> keyExternalizer,
                                      final DataExternalizer<V> valueExternalizer,
                                      final CollectionFactory<V> collectionFactory) throws IOException {
  myValueExternalizer = valueExternalizer;
  myMap = new PersistentHashMap<K, Collection<V>>(file, keyExternalizer, new CollectionDataExternalizer<V>(valueExternalizer, collectionFactory));
  myCache = new SLRUCache<K, Collection>(CACHE_SIZE, CACHE_SIZE) {
    @NotNull
    @Override
    public Collection createValue(K key) {
      try {
        final Collection<V> collection = myMap.get(key);
        return collection == null? NULL_COLLECTION : collection;
      }
      catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
  };
}
项目:tools-idea    文件:IntObjectPersistentMaplet.java   
public IntObjectPersistentMaplet(final File file, final DataExternalizer<V> externalizer) {
  try {
    myMap = new PersistentHashMap<Integer, V>(file, new IntInlineKeyDescriptor(), externalizer);
    myCache = new SLRUCache<Integer, Object>(CACHE_SIZE, CACHE_SIZE) {
      @NotNull
      @Override
      public Object createValue(Integer key) {
        try {
          final V v1 = myMap.get(key);
          return v1 == null? NULL_OBJ : v1;
        }
        catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    };
  }
  catch (IOException e) {
    throw new RuntimeException(e);
  }
}
项目:tools-idea    文件:IntIntPersistentMultiMaplet.java   
public IntIntPersistentMultiMaplet(final File file, final KeyDescriptor<Integer> keyExternalizer) throws IOException {
  myMap = new PersistentHashMap<Integer, TIntHashSet>(file, keyExternalizer, new IntSetExternalizer());
  myCache = new SLRUCache<Integer, TIntHashSet>(CACHE_SIZE, CACHE_SIZE) {
    @NotNull
    @Override
    public TIntHashSet createValue(Integer key) {
      try {
        final TIntHashSet collection = myMap.get(key);
        return collection == null? NULL_COLLECTION : collection;
      }
      catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
  };
}
项目:tools-idea    文件:IntObjectPersistentMultiMaplet.java   
public IntObjectPersistentMultiMaplet(final File file,
                                      final KeyDescriptor<Integer> keyExternalizer,
                                      final DataExternalizer<V> valueExternalizer,
                                      final CollectionFactory<V> collectionFactory) throws IOException {
  myValueExternalizer = valueExternalizer;
  myMap = new PersistentHashMap<Integer, Collection<V>>(file, keyExternalizer, new CollectionDataExternalizer<V>(valueExternalizer, collectionFactory));
  myCache = new SLRUCache<Integer, Collection>(CACHE_SIZE, CACHE_SIZE) {
    @NotNull
    @Override
    public Collection createValue(Integer key) {
      try {
        final Collection<V> collection = myMap.get(key);
        return collection == null? NULL_COLLECTION : collection;
      }
      catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
  };
}
项目:intellij-ce-playground    文件:ClassFilesIndexStorageBase.java   
private void initialize() throws IOException {
  myMap = new PersistentHashMap<K, CompiledDataValueContainer<V>>(myIndexFile, myKeyDescriptor,
                                                                  createValueContainerExternalizer(myValueExternalizer),
                                                                  INITIAL_INDEX_SIZE);
  myCache = new SLRUCache<K, CompiledDataValueContainer<V>>(CACHE_QUEUES_SIZE, CACHE_QUEUES_SIZE) {
    @NotNull
    @Override
    public CompiledDataValueContainer<V> createValue(final K key) {
      try {
        final CompiledDataValueContainer<V> valueContainer = myMap.get(key);
        if (valueContainer != null) {
          return valueContainer;
        }
      }
      catch (final IOException e) {
        throw new RuntimeException(e);
      }
      return new CompiledDataValueContainer<V>();
    }

    @Override
    protected void onDropFromCache(final K key, final CompiledDataValueContainer<V> value) {
      try {
        myMap.put(key, value);
      }
      catch (final IOException e) {
        throw new RuntimeException(e);
      }
    }
  };
}
项目:tools-idea    文件:IntIntPersistentMaplet.java   
public IntIntPersistentMaplet(final File file, final KeyDescriptor<Integer> k) {
  try {
    myMap = new PersistentHashMap<Integer, Integer>(file, k, new DataExternalizer<Integer>() {
      @Override
      public void save(DataOutput out, Integer value) throws IOException {
        out.writeInt(value);
      }

      @Override
      public Integer read(DataInput in) throws IOException {
        return in.readInt();
      }
    });
    myCache = new SLRUCache<Integer, Object>(CACHE_SIZE, CACHE_SIZE) {
      @NotNull
      @Override
      public Object createValue(Integer key) {
        try {
          final Integer v1 = myMap.get(key);
          return v1 == null? NULL_OBJ : v1;
        }
        catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    };
  }
  catch (IOException e) {
    throw new RuntimeException(e);
  }
}
项目:intellij-ce-playground    文件:MapIndexStorage.java   
private void initMapAndCache() throws IOException {
  final ValueContainerMap<Key, Value> map;
  PersistentHashMapValueStorage.CreationTimeOptions.EXCEPTIONAL_IO_CANCELLATION.set(ourProgressManagerCheckCancelledIOCanceller);
  try {
    map = new ValueContainerMap<Key, Value>(myStorageFile, myKeyDescriptor, myDataExternalizer, myKeyIsUniqueForIndexedFile);
  } finally {
    PersistentHashMapValueStorage.CreationTimeOptions.EXCEPTIONAL_IO_CANCELLATION.set(null);
  }
  myCache = new SLRUCache<Key, ChangeTrackingValueContainer<Value>>(myCacheSize, (int)(Math.ceil(myCacheSize * 0.25)) /* 25% from the main cache size*/) {
    @Override
    @NotNull
    public ChangeTrackingValueContainer<Value> createValue(final Key key) {
      return new ChangeTrackingValueContainer<Value>(new ChangeTrackingValueContainer.Initializer<Value>() {
        @NotNull
        @Override
        public Object getLock() {
          return map.getDataAccessLock();
        }

        @Nullable
        @Override
        public ValueContainer<Value> compute() {
          ValueContainer<Value> value;
          try {
            value = map.get(key);
            if (value == null) {
              value = new ValueContainerImpl<Value>();
            }
          }
          catch (IOException e) {
            throw new RuntimeException(e);
          }
          return value;
        }
      });
    }

    @Override
    protected void onDropFromCache(final Key key, @NotNull final ChangeTrackingValueContainer<Value> valueContainer) {
      if (valueContainer.isDirty()) {
        try {
          map.put(key, valueContainer);
        }
        catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    }
  };

  myMap = map;

  myKeyHashToVirtualFileMapping = myBuildKeyHashToVirtualFileMapping ? new KeyHash2VirtualFileEnumerator(getProjectFile()) : null;
}
项目:tools-idea    文件:CompilerDependencyStorage.java   
public CompilerDependencyStorage(File file, KeyDescriptor<Key> keyDescriptor, final int cacheSize) throws IOException {
  myMap = new PersistentHashMap<Key, int[]>(file, keyDescriptor, new DataExternalizer<int[]>() {
    public void save(DataOutput out, int[] array) throws IOException {
      out.writeInt(array.length);
      for (int value : array) {
        out.writeInt(value);
      }
    }

    public int[] read(DataInput in) throws IOException {
      final TIntHashSet set = new TIntHashSet();
      DataInputStream stream = (DataInputStream)in;
      while(stream.available() > 0) {
        final int size = stream.readInt();
        final int _size = Math.abs(size);
        for (int idx = 0; idx < _size; idx++) {
          if (size > 0) {
            set.add(stream.readInt());
          }
          else {
            set.remove(stream.readInt());
          }
        }
      }
      return set.toArray();
    }
  });

  myCache = new SLRUCache<Key, IntSet>(cacheSize * 2, cacheSize) {
    @NotNull
    public IntSet createValue(Key key) {
      return new IntSet(key);
    }

    protected void onDropFromCache(Key key, final IntSet set) {
      if (key == myKeyToRemove || !set.isDirty()) {
        return;
      }
      try {
        if (set.needsCompacting()) {
          myMap.put(key, set.getValues());
        }
        else {
          myMap.appendData(key, new PersistentHashMap.ValueDataAppender() {
            public void append(final DataOutput out) throws IOException {
              final Ref<IOException> exception = new Ref<IOException>(null);
              final TIntProcedure saveProc = new TIntProcedure() {
                public boolean execute(int value) {
                  try {
                    out.writeInt(value);
                    return true;
                  }
                  catch (IOException e) {
                    exception.set(e);
                    return false;
                  }
                }
              };

              out.writeInt(-set.getRemovedCount());
              set.processRemovedValues(saveProc);
              if (exception.get() != null) {
                throw exception.get();
              }

              out.writeInt(set.getAddedCount());
              set.processAddedValues(saveProc);
              if (exception.get() != null) {
                throw exception.get();
              }
            }
          });
        }
      }
      catch (IOException e) {
        LOG.error(e);
      }
    }
  };
}
项目:tools-idea    文件:BackwardDependenciesStorage.java   
public BackwardDependenciesStorage(File file, final int cacheSize) throws IOException {
  myMap = new PersistentHashMap<Integer, DependenciesSet>(file, EnumeratorIntegerDescriptor.INSTANCE, new MyDataExternalizer());

  myCache = new SLRUCache<Integer, ReferencerSetHolder>(cacheSize * 2, cacheSize) {
    @NotNull
    public ReferencerSetHolder createValue(Integer key) {
      return new ReferencerSetHolder(key);
    }

    protected void onDropFromCache(Integer key, final ReferencerSetHolder holder) {
      if (key.equals(myKeyToRemove) || !holder.isDirty()) {
        return;
      }
      try {
        if (holder.isDataLoaded() || !myMap.containsMapping(key)) {
          myMap.put(key, new DependenciesSet(holder.getData()));
        }
        else {
          myMap.appendData(key, new PersistentHashMap.ValueDataAppender() {
            public void append(final DataOutput out) throws IOException {
              final Ref<IOException> exception = new Ref<IOException>(null);
              // process removed
              holder.myRemoveRequested.forEach(new TIntProcedure() {
                public boolean execute(int qName) {
                  try {
                    out.writeInt(-qName);
                    return true;
                  }
                  catch (IOException e) {
                    exception.set(e);
                  }
                  return false;
                }
              });
              final IOException _ex = exception.get();
              if (_ex != null) {
                throw _ex;
              }
              // process added members
              for (ReferencerItem item : holder.myAdded) {
                item.save(out);
              }
            }
          });
        }
      }
      catch (IOException e) {
        LOG.error(e);
      }
    }
  };
}
项目:tools-idea    文件:MapIndexStorage.java   
private void initMapAndCache() throws IOException {
  final ValueContainerMap<Key, Value> map = new ValueContainerMap<Key, Value>(myStorageFile, myKeyDescriptor, myDataExternalizer);
  myCache = new SLRUCache<Key, ChangeTrackingValueContainer<Value>>(myCacheSize, (int)(Math.ceil(myCacheSize * 0.25)) /* 25% from the main cache size*/) {
    @Override
    @NotNull
    public ChangeTrackingValueContainer<Value> createValue(final Key key) {
      return new ChangeTrackingValueContainer<Value>(new ChangeTrackingValueContainer.Initializer<Value>() {
        @NotNull
        @Override
        public Object getLock() {
          return map.getDataAccessLock();
        }

        @Nullable
        @Override
        public ValueContainer<Value> compute() {
          ValueContainer<Value> value;
          try {
            value = map.get(key);
            if (value == null) {
              value = new ValueContainerImpl<Value>();
            }
          }
          catch (IOException e) {
            throw new RuntimeException(e);
          }
          return value;
        }
      });
    }

    @Override
    protected void onDropFromCache(final Key key, @NotNull final ChangeTrackingValueContainer<Value> valueContainer) {
      if (valueContainer.isDirty()) {
        try {
          map.put(key, valueContainer);
        }
        catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    }
  };

  myMap = map;
}
项目:consulo    文件:MapIndexStorage.java   
protected void initMapAndCache() throws IOException {
  final ValueContainerMap<Key, Value> map;
  PersistentHashMapValueStorage.CreationTimeOptions.EXCEPTIONAL_IO_CANCELLATION.set(new PersistentHashMapValueStorage.ExceptionalIOCancellationCallback() {
    @Override
    public void checkCancellation() {
      checkCanceled();
    }
  });
  PersistentHashMapValueStorage.CreationTimeOptions.COMPACT_CHUNKS_WITH_VALUE_DESERIALIZATION.set(Boolean.TRUE);
  try {
    map = new ValueContainerMap<Key, Value>(getStorageFile(), myKeyDescriptor, myDataExternalizer, myKeyIsUniqueForIndexedFile);
  }
  finally {
    PersistentHashMapValueStorage.CreationTimeOptions.EXCEPTIONAL_IO_CANCELLATION.set(null);
    PersistentHashMapValueStorage.CreationTimeOptions.COMPACT_CHUNKS_WITH_VALUE_DESERIALIZATION.set(null);
  }
  myCache = new SLRUCache<Key, ChangeTrackingValueContainer<Value>>(myCacheSize, (int)(Math.ceil(myCacheSize * 0.25)) /* 25% from the main cache size*/) {
    @Override
    @Nonnull
    public ChangeTrackingValueContainer<Value> createValue(final Key key) {
      return new ChangeTrackingValueContainer<Value>(new ChangeTrackingValueContainer.Initializer<Value>() {
        @Nonnull
        @Override
        public Object getLock() {
          return map.getDataAccessLock();
        }

        @Nullable
        @Override
        public ValueContainer<Value> compute() {
          ValueContainer<Value> value;
          try {
            value = map.get(key);
            if (value == null) {
              value = new ValueContainerImpl<Value>();
            }
          }
          catch (IOException e) {
            throw new RuntimeException(e);
          }
          return value;
        }
      });
    }

    @Override
    protected void onDropFromCache(final Key key, @Nonnull final ChangeTrackingValueContainer<Value> valueContainer) {
      if (valueContainer.isDirty()) {
        try {
          map.put(key, valueContainer);
        }
        catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    }
  };

  myMap = map;
}
项目:consulo-java    文件:CompilerDependencyStorage.java   
public CompilerDependencyStorage(File file, KeyDescriptor<Key> keyDescriptor, final int cacheSize) throws IOException {
  myMap = new PersistentHashMap<Key, int[]>(file, keyDescriptor, new DataExternalizer<int[]>() {
    public void save(DataOutput out, int[] array) throws IOException {
      out.writeInt(array.length);
      for (int value : array) {
        out.writeInt(value);
      }
    }

    public int[] read(DataInput in) throws IOException {
      final TIntHashSet set = new TIntHashSet();
      DataInputStream stream = (DataInputStream)in;
      while(stream.available() > 0) {
        final int size = stream.readInt();
        final int _size = Math.abs(size);
        for (int idx = 0; idx < _size; idx++) {
          if (size > 0) {
            set.add(stream.readInt());
          }
          else {
            set.remove(stream.readInt());
          }
        }
      }
      return set.toArray();
    }
  });

  myCache = new SLRUCache<Key, IntSet>(cacheSize * 2, cacheSize) {
    @NotNull
    public IntSet createValue(Key key) {
      return new IntSet(key);
    }

    protected void onDropFromCache(Key key, final IntSet set) {
      if (key == myKeyToRemove || !set.isDirty()) {
        return;
      }
      try {
        if (set.needsCompacting()) {
          myMap.put(key, set.getValues());
        }
        else {
          myMap.appendData(key, new PersistentHashMap.ValueDataAppender() {
            public void append(final DataOutput out) throws IOException {
              final Ref<IOException> exception = new Ref<IOException>(null);
              final TIntProcedure saveProc = new TIntProcedure() {
                public boolean execute(int value) {
                  try {
                    out.writeInt(value);
                    return true;
                  }
                  catch (IOException e) {
                    exception.set(e);
                    return false;
                  }
                }
              };

              out.writeInt(-set.getRemovedCount());
              set.processRemovedValues(saveProc);
              if (exception.get() != null) {
                throw exception.get();
              }

              out.writeInt(set.getAddedCount());
              set.processAddedValues(saveProc);
              if (exception.get() != null) {
                throw exception.get();
              }
            }
          });
        }
      }
      catch (IOException e) {
        LOG.error(e);
      }
    }
  };
}
项目:consulo-java    文件:BackwardDependenciesStorage.java   
public BackwardDependenciesStorage(File file, final int cacheSize) throws IOException {
  myMap = new PersistentHashMap<Integer, DependenciesSet>(file, EnumeratorIntegerDescriptor.INSTANCE, new MyDataExternalizer());

  myCache = new SLRUCache<Integer, ReferencerSetHolder>(cacheSize * 2, cacheSize) {
    @NotNull
    public ReferencerSetHolder createValue(Integer key) {
      return new ReferencerSetHolder(key);
    }

    protected void onDropFromCache(Integer key, final ReferencerSetHolder holder) {
      if (key.equals(myKeyToRemove) || !holder.isDirty()) {
        return;
      }
      try {
        if (holder.isDataLoaded() || !myMap.containsMapping(key)) {
          myMap.put(key, new DependenciesSet(holder.getData()));
        }
        else {
          myMap.appendData(key, new PersistentHashMap.ValueDataAppender() {
            public void append(final DataOutput out) throws IOException {
              final Ref<IOException> exception = new Ref<IOException>(null);
              // process removed
              holder.myRemoveRequested.forEach(new TIntProcedure() {
                public boolean execute(int qName) {
                  try {
                    out.writeInt(-qName);
                    return true;
                  }
                  catch (IOException e) {
                    exception.set(e);
                  }
                  return false;
                }
              });
              final IOException _ex = exception.get();
              if (_ex != null) {
                throw _ex;
              }
              // process added members
              for (ReferencerItem item : holder.myAdded) {
                item.save(out);
              }
            }
          });
        }
      }
      catch (IOException e) {
        LOG.error(e);
      }
    }
  };
}