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.Set;
031import java.util.concurrent.ConcurrentSkipListMap;
032
033public class CodecRamMap<T> implements ExternalMap<T> {
034
035  private final Codec<T> c;
036  private final ConcurrentSkipListMap<CharSequence, byte[]> map;
037
038  @Inject
039  public CodecRamMap(RamStorageService ramStore,
040                     @Parameter(RamMapCodec.class) final Codec<T> c) {
041    this.c = c;
042    this.map = new ConcurrentSkipListMap<CharSequence, byte[]>();
043  }
044
045  @Override
046  public boolean containsKey(final CharSequence key) {
047    return map.containsKey(key);
048  }
049
050  @Override
051  public T get(final CharSequence key) {
052    final byte[] ret = map.get(key);
053    return ret != null ? c.decode(ret) : null;
054  }
055
056  @Override
057  public T put(final CharSequence key, T value) {
058    final byte[] ret = map.put(key, c.encode(value));
059    return ret != null ? c.decode(ret) : null;
060  }
061
062  @Override
063  public T remove(final CharSequence key) {
064    final byte[] ret = map.remove(key);
065    return ret != null ? c.decode(ret) : null;
066  }
067
068  @Override
069  public void putAll(final Map<? extends CharSequence, ? extends T> m) {
070    for (final CharSequence x : m.keySet()) {
071      map.put(x, c.encode(m.get(x)));
072    }
073  }
074
075  @Override
076  public Iterable<Map.Entry<CharSequence, T>> getAll(
077      final Set<? extends CharSequence> keys) {
078    return new GetAllIterable<T>(keys, this);
079  }
080
081  @NamedParameter
082  static public class RamMapCodec implements Name<Codec<?>> {
083  }
084}