Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.util.kvstore;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.HashSet;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import org.apache.spark.annotation.Private;
/**
* Implementation of KVStore that keeps data deserialized in memory. This store does not index
* data; instead, whenever iterating over an indexed field, the stored data is copied and sorted
* according to the index. This saves memory but makes iteration more expensive.
*/
@Private
public class InMemoryStore implements KVStore {
private Object metadata;
private InMemoryLists inMemoryLists = new InMemoryLists();
@Override
public T getMetadata(Class klass) {
return klass.cast(metadata);
}
@Override
public void setMetadata(Object value) {
this.metadata = value;
}
@Override
public long count(Class type) {
InstanceList list = inMemoryLists.get(type);
return list != null ? list.size() : 0;
}
@Override
public long count(Class type, String index, Object indexedValue) throws Exception {
InstanceList list = inMemoryLists.get(type);
int count = 0;
Object comparable = asKey(indexedValue);
KVTypeInfo.Accessor accessor = list.getIndexAccessor(index);
for (Object o : view(type)) {
if (Objects.equal(comparable, asKey(accessor.get(o)))) {
count++;
}
}
return count;
}
@Override
public T read(Class klass, Object naturalKey) {
InstanceList list = inMemoryLists.get(klass);
T value = list != null ? list.get(naturalKey) : null;
if (value == null) {
throw new NoSuchElementException();
}
return value;
}
@Override
public void write(Object value) throws Exception {
inMemoryLists.write(value);
}
@Override
public void delete(Class type, Object naturalKey) {
InstanceList list = inMemoryLists.get(type);
if (list != null) {
list.delete(naturalKey);
}
}
@Override
public KVStoreView view(Class type){
InstanceList list = inMemoryLists.get(type);
return list != null ? list.view() : emptyView();
}
@Override
public void close() {
metadata = null;
inMemoryLists.clear();
}
@Override
public boolean removeAllByIndexValues(
Class klass,
String index,
Collection indexValues) {
InstanceList list = inMemoryLists.get(klass);
if (list != null) {
return list.countingRemoveAllByIndexValues(index, indexValues) > 0;
} else {
return false;
}
}
@SuppressWarnings("unchecked")
private static Comparable