Example usage for com.google.common.cache Weigher Weigher

List of usage examples for com.google.common.cache Weigher Weigher

Introduction

In this page you can find the example usage for com.google.common.cache Weigher Weigher.

Prototype

Weigher

Source Link

Usage

From source file:com.google.cloud.dataflow.sdk.runners.worker.Weighers.java

public static Weigher<Object, Weighted> fixedWeightKeys(final int keyWeight) {
    return new Weigher<Object, Weighted>() {
        @Override/*from w ww . jav  a2 s.c  o  m*/
        public int weigh(Object key, Weighted value) {
            return (int) Math.min(keyWeight + value.getWeight(), Integer.MAX_VALUE);
        }
    };
}

From source file:com.google.cloud.dataflow.sdk.runners.worker.Weighers.java

public static Weigher<Weighted, Weighted> weightedKeysAndValues() {
    return new Weigher<Weighted, Weighted>() {
        @Override//from   w  w  w.java  2s.  c  o  m
        public int weigh(Weighted key, Weighted value) {
            return (int) Math.min(key.getWeight() + value.getWeight(), Integer.MAX_VALUE);
        }
    };
}

From source file:com.google.gitiles.blame.BlameCacheImpl.java

public static CacheBuilder<Key, List<Region>> weigher(CacheBuilder<? super Key, ? super List<Region>> builder) {
    return builder.weigher(new Weigher<Key, List<Region>>() {
        @Override/* w  w w .j  a va  2s .c o  m*/
        public int weigh(Key key, List<Region> value) {
            return value.size();
        }
    });
}

From source file:org.nickelproject.nickel.blobStore.CachingBlobStore.java

@Inject
public CachingBlobStore(final BlobStore blobStore, final CacheBuilderSpec cacheBuilderSpec) {
    this.blobStore = blobStore;
    this.cache = CacheBuilder.from(cacheBuilderSpec).weigher(new Weigher<BlobRef, byte[]>() {
        @Override//from  w w  w . j a v  a 2 s. co  m
        public int weigh(final BlobRef key, final byte[] value) {
            return value.length;
        }
    }).build(new CacheLoader<BlobRef, byte[]>() {
        @Override
        public byte[] load(final BlobRef key) throws Exception {
            return blobStore.get(key);
        }
    });
}

From source file:com.facebook.presto.rakam.cache.HeapOrcDataSourceFactory.java

@Inject
public HeapOrcDataSourceFactory(CacheConfig config) {
    cache = CacheBuilder.newBuilder().maximumWeight(config.getDataSize().toBytes())
            .weigher(new Weigher<FileRegion, byte[]>() {
                @Override//from   ww w  .j  a v a2 s  .c o m
                public int weigh(FileRegion key, byte[] value) {
                    return value.length;
                }
            }).build(new CacheLoader<FileRegion, byte[]>() {
                public byte[] load(FileRegion key) throws IOException {
                    if (!key.file.exists()) {
                        throw new FileNotFoundException(key.file.toString());
                    }

                    try (RandomAccessFile randomAccessFile = new RandomAccessFile(key.file, "r")) {
                        byte[] bytes = new byte[key.length];
                        randomAccessFile.seek(key.position);
                        randomAccessFile.read(bytes);
                        return bytes;
                    }
                }
            });
}

From source file:org.nickelproject.nickel.objectStore.CachingObjectStore.java

@Inject
public CachingObjectStore(final BlobStore bStore, final CacheBuilderSpec cacheBuilderSpec) {
    this.blobStore = bStore;
    this.cache = CacheBuilder.from(cacheBuilderSpec).weigher(new Weigher<BlobRef, Pair<Integer, Object>>() {
        @Override/*from   www .jav  a 2  s.c o  m*/
        public int weigh(final BlobRef key, final Pair<Integer, Object> value) {
            return value.getA();
        }
    }).build(new CacheLoader<BlobRef, Pair<Integer, Object>>() {
        @Override
        public Pair<Integer, Object> load(final BlobRef key) throws Exception {
            final byte[] bytes = blobStore.get(key);
            if (bytes == null) {
                throw new RuntimeException("Unable to load for key: " + key);
            } else {
                return Pair.of(bytes.length, SerializationUtils.deserialize(bytes));
            }
        }
    });
}

From source file:com.addthis.hydra.job.store.CachedSpawnDataStore.java

public CachedSpawnDataStore(SpawnDataStore dataStore, long dataStoreCacheSize) {
    this.dataStore = dataStore;
    this.cache = CacheBuilder.newBuilder().weigher(new Weigher<Pair<String, String>, String>() {
        @Override//from  w ww. ja v a2  s. c  om
        public int weigh(Pair<String, String> key, String value) {
            int leftWeight = key.getLeft() != null ? key.getLeft().length() : 0;

            int rightWeight = key.getRight() != null ? key.getRight().length() : 0;

            // Multiply strlen by 2 (full width characters in java
            return 2 * (value.length() + leftWeight + rightWeight);
        }
    }).maximumWeight(dataStoreCacheSize).build(new CacheLoader<Pair<String, String>, String>() {
        @Override
        public String load(Pair<String, String> key) throws Exception {
            String path = key.getLeft();
            String childId = key.getRight();

            if (childId == null) {
                return CachedSpawnDataStore.this.dataStore.get(path);
            } else {
                return CachedSpawnDataStore.this.dataStore.getChild(path, childId);
            }
        }
    });
}

From source file:com.facebook.presto.rakam.cache.MemoryMappedOrcDataSourceFactory.java

@Inject
public MemoryMappedOrcDataSourceFactory(CacheConfig config) {
    cache = CacheBuilder.newBuilder().maximumWeight(config.getDataSize().toBytes())
            .weigher(new Weigher<FileRegion, Slice>() {
                @Override//  www .  j  ava  2  s  .  c  o m
                public int weigh(FileRegion key, Slice value) {
                    return value.length();
                }
            }).build(new CacheLoader<FileRegion, Slice>() {
                public Slice load(FileRegion key) throws IOException {
                    if (!key.file.exists()) {
                        throw new FileNotFoundException(key.file.toString());
                    }

                    try (RandomAccessFile randomAccessFile = new RandomAccessFile(key.file, "r");
                            FileChannel channel = randomAccessFile.getChannel()) {
                        MappedByteBuffer byteBuffer = channel.map(FileChannel.MapMode.READ_ONLY, key.position,
                                key.length);
                        return Slices.wrappedBuffer(byteBuffer);
                    }
                }
            });
}

From source file:com.mellowtech.core.cache.CacheLRUMemory.java

@Override
protected LoadingCache<K, CacheValue<V>> buildCache() {

    Weigher<K, CacheValue<V>> weighter = new Weigher<K, CacheValue<V>>() {
        @Override/*from  ww w  .j  ava 2 s  . c om*/
        public int weigh(K key, CacheValue<V> value) {
            return key.byteSize() + value.getValue().byteSize();
        }
    };

    RemovalListener<K, CacheValue<V>> removalListener = this.getRemoveListener();
    CacheLoader<K, CacheValue<V>> cacheLoader = this.getCacheLoader();

    if (removalListener != null) {
        return CacheBuilder.newBuilder().maximumWeight(maxSize()).weigher(weighter)
                .removalListener(removalListener).build(cacheLoader);
    } else
        return CacheBuilder.newBuilder().maximumWeight(maxSize()).weigher(weighter).build(cacheLoader);
}

From source file:com.mellowtech.core.cache.CacheLRUMemoryMapping.java

@Override
protected LoadingCache<K, CacheValue<V>> buildCache() {

    Weigher<K, CacheValue<V>> weighter = new Weigher<K, CacheValue<V>>() {
        @Override/*w w  w .  j  a  v a2 s .c o  m*/
        public int weigh(K key, CacheValue<V> value) {
            return keyMapping.byteSize(key) + valueMapping.byteSize(value.getValue());
        }
    };

    RemovalListener<K, CacheValue<V>> removalListener = this.getRemoveListener();
    CacheLoader<K, CacheValue<V>> cacheLoader = this.getCacheLoader();

    if (removalListener != null) {
        return CacheBuilder.newBuilder().maximumWeight(maxSize()).weigher(weighter)
                .removalListener(removalListener).build(cacheLoader);
    } else
        return CacheBuilder.newBuilder().maximumWeight(maxSize()).weigher(weighter).build(cacheLoader);
}