Github user squito commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17902#discussion_r119412048
  
    --- Diff: 
common/kvstore/src/main/java/org/apache/spark/kvstore/LevelDB.java ---
    @@ -0,0 +1,303 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You 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 org.apache.spark.kvstore;
    +
    +import java.io.File;
    +import java.io.IOException;
    +import java.util.HashMap;
    +import java.util.Iterator;
    +import java.util.Map;
    +import java.util.NoSuchElementException;
    +import java.util.concurrent.ConcurrentHashMap;
    +import java.util.concurrent.ConcurrentMap;
    +import java.util.concurrent.atomic.AtomicReference;
    +import static java.nio.charset.StandardCharsets.UTF_8;
    +
    +import com.google.common.annotations.VisibleForTesting;
    +import com.google.common.base.Objects;
    +import com.google.common.base.Preconditions;
    +import com.google.common.base.Throwables;
    +import org.fusesource.leveldbjni.JniDBFactory;
    +import org.iq80.leveldb.DB;
    +import org.iq80.leveldb.Options;
    +import org.iq80.leveldb.WriteBatch;
    +
    +/**
    + * Implementation of KVStore that uses LevelDB as the underlying data 
store.
    + */
    +public class LevelDB implements KVStore {
    +
    +  @VisibleForTesting
    +  static final long STORE_VERSION = 1L;
    +
    +  @VisibleForTesting
    +  static final byte[] STORE_VERSION_KEY = "__version__".getBytes(UTF_8);
    +
    +  /** DB key where app metadata is stored. */
    +  private static final byte[] METADATA_KEY = "__meta__".getBytes(UTF_8);
    +
    +  /** DB key where type aliases are stored. */
    +  private static final byte[] TYPE_ALIASES_KEY = 
"__types__".getBytes(UTF_8);
    +
    +  final AtomicReference<DB> _db;
    +  final KVStoreSerializer serializer;
    +
    +  private final ConcurrentMap<String, byte[]> typeAliases;
    +  private final ConcurrentMap<Class<?>, LevelDBTypeInfo> types;
    +
    +  public LevelDB(File path) throws Exception {
    +    this(path, new KVStoreSerializer());
    +  }
    +
    +  public LevelDB(File path, KVStoreSerializer serializer) throws Exception 
{
    +    this.serializer = serializer;
    +    this.types = new ConcurrentHashMap<>();
    +
    +    Options options = new Options();
    +    options.createIfMissing(!path.exists());
    +    this._db = new AtomicReference<>(JniDBFactory.factory.open(path, 
options));
    +
    +    byte[] versionData = db().get(STORE_VERSION_KEY);
    +    if (versionData != null) {
    +      long version = serializer.deserializeLong(versionData);
    +      if (version != STORE_VERSION) {
    +        throw new UnsupportedStoreVersionException();
    +      }
    +    } else {
    +      db().put(STORE_VERSION_KEY, serializer.serialize(STORE_VERSION));
    +    }
    +
    +    Map<String, byte[]> aliases;
    +    try {
    +      aliases = get(TYPE_ALIASES_KEY, TypeAliases.class).aliases;
    +    } catch (NoSuchElementException e) {
    +      aliases = new HashMap<>();
    +    }
    +    typeAliases = new ConcurrentHashMap<>(aliases);
    +  }
    +
    +  @Override
    +  public <T> T getMetadata(Class<T> klass) throws Exception {
    +    try {
    +      return get(METADATA_KEY, klass);
    +    } catch (NoSuchElementException nsee) {
    +      return null;
    +    }
    +  }
    +
    +  @Override
    +  public void setMetadata(Object value) throws Exception {
    +    if (value != null) {
    +      put(METADATA_KEY, value);
    +    } else {
    +      db().delete(METADATA_KEY);
    +    }
    +  }
    +
    +  <T> T get(byte[] key, Class<T> klass) throws Exception {
    +    byte[] data = db().get(key);
    +    if (data == null) {
    +      throw new NoSuchElementException(new String(key, UTF_8));
    +    }
    +    return serializer.deserialize(data, klass);
    +  }
    +
    +  private void put(byte[] key, Object value) throws Exception {
    +    Preconditions.checkArgument(value != null, "Null values are not 
allowed.");
    +    db().put(key, serializer.serialize(value));
    +  }
    +
    +  @Override
    +  public <T> T read(Class<T> klass, Object naturalKey) throws Exception {
    +    Preconditions.checkArgument(naturalKey != null, "Null keys are not 
allowed.");
    +    byte[] key = getTypeInfo(klass).naturalIndex().start(null, naturalKey);
    +    return get(key, klass);
    +  }
    +
    +  @Override
    +  public void write(Object value) throws Exception {
    +    Preconditions.checkArgument(value != null, "Null values are not 
allowed.");
    +    LevelDBTypeInfo ti = getTypeInfo(value.getClass());
    +
    +    try (WriteBatch batch = db().createWriteBatch()) {
    +      byte[] data = serializer.serialize(value);
    +      synchronized (ti) {
    +        Object existing;
    +        try {
    +          existing = get(ti.naturalIndex().entityKey(null, value), 
value.getClass());
    +        } catch (NoSuchElementException e) {
    +          existing = null;
    +        }
    +
    +        PrefixCache cache = new PrefixCache(value);
    +        byte[] naturalKey = 
ti.naturalIndex().toKey(ti.naturalIndex().getValue(value));
    --- End diff --
    
    if the key is null, I guess this will complain somewhere in here, but a 
nicer error msg would be better.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to