This project has retired. For details please refer to its Attic page.
Source code
001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.io.storage.ram;
020
021import org.apache.reef.io.ExternalMap;
022import org.apache.reef.io.serialization.Codec;
023import org.apache.reef.io.storage.util.GetAllIterable;
024import org.apache.reef.tang.annotations.Name;
025import org.apache.reef.tang.annotations.NamedParameter;
026import org.apache.reef.tang.annotations.Parameter;
027
028import javax.inject.Inject;
029import java.util.Map;
030import java.util.Map.Entry;
031import java.util.Set;
032import java.util.concurrent.ConcurrentSkipListMap;
033
034public class CodecRamMap<T> implements ExternalMap<T> {
035
036  private final Codec<T> c;
037  private final ConcurrentSkipListMap<CharSequence, byte[]> map;
038
039  @Inject
040  public CodecRamMap(final RamStorageService ramStore,
041                     @Parameter(RamMapCodec.class) final Codec<T> c) {
042    this.c = c;
043    this.map = new ConcurrentSkipListMap<CharSequence, byte[]>();
044  }
045
046  @Override
047  public boolean containsKey(final CharSequence key) {
048    return map.containsKey(key);
049  }
050
051  @Override
052  public T get(final CharSequence key) {
053    final byte[] ret = map.get(key);
054    return ret != null ? c.decode(ret) : null;
055  }
056
057  @Override
058  public T put(final CharSequence key, final T value) {
059    final byte[] ret = map.put(key, c.encode(value));
060    return ret != null ? c.decode(ret) : null;
061  }
062
063  @Override
064  public T remove(final CharSequence key) {
065    final byte[] ret = map.remove(key);
066    return ret != null ? c.decode(ret) : null;
067  }
068
069  @Override
070  public void putAll(final Map<? extends CharSequence, ? extends T> m) {
071    for (final Entry<? extends CharSequence, ? extends T> entry : m.entrySet()) {
072      map.put(entry.getKey(), c.encode(entry.getValue()));
073    }
074  }
075
076  @Override
077  public Iterable<Map.Entry<CharSequence, T>> getAll(
078      final Set<? extends CharSequence> keys) {
079    return new GetAllIterable<>(keys, this);
080  }
081
082  @NamedParameter
083  public static class RamMapCodec implements Name<Codec<?>> {
084  }
085}