Skip to content

Commit

Permalink
implement RamTable (#1183)
Browse files Browse the repository at this point in the history
* implement RamTable  (#49)

* implement RamTable
* plugin ramtable to StandardHugeGrap.edges(query)
* plugin ramtable to GraphTransaction.queryEdgesFromBackend(Query query)
* allow config vertices/edges capacity of ramtable
* support concurrent loading

Change-Id: I4e1a5c06bd331dc29a92dcb76843990863dfe6ca

* add Consumers class to util package

Change-Id: I8765582ba4e9b5fe8de4300719612a84bfce89a3

* support load from file and export to file

Change-Id: Iecfb5bad535e4954a325eb45b1d9def53fdb31c0
usage: graph.reloadRamtable(true)
  • Loading branch information
javeme authored Sep 21, 2020
1 parent 71cda42 commit e35f9fb
Show file tree
Hide file tree
Showing 17 changed files with 2,247 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.baidu.hugegraph.backend.serializer.AbstractSerializer;
import com.baidu.hugegraph.backend.store.BackendFeatures;
import com.baidu.hugegraph.backend.store.BackendStore;
import com.baidu.hugegraph.backend.store.ram.RamTable;
import com.baidu.hugegraph.backend.tx.GraphTransaction;
import com.baidu.hugegraph.backend.tx.SchemaTransaction;
import com.baidu.hugegraph.config.HugeConfig;
Expand Down Expand Up @@ -68,4 +69,5 @@ public interface HugeGraphParams {
public Analyzer analyzer();
public RateLimiter writeRateLimiter();
public RateLimiter readRateLimiter();
public RamTable ramtable();
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.baidu.hugegraph.backend.store.BackendStore;
import com.baidu.hugegraph.backend.store.BackendStoreProvider;
import com.baidu.hugegraph.backend.store.BackendStoreSystemInfo;
import com.baidu.hugegraph.backend.store.ram.RamTable;
import com.baidu.hugegraph.backend.tx.GraphTransaction;
import com.baidu.hugegraph.backend.tx.SchemaTransaction;
import com.baidu.hugegraph.config.CoreOptions;
Expand Down Expand Up @@ -126,26 +127,37 @@ public class StandardHugeGraph implements HugeGraph {
private final BackendStoreProvider storeProvider;
private final TinkerpopTransaction tx;

public StandardHugeGraph(HugeConfig configuration) {
private final RamTable ramtable;

public StandardHugeGraph(HugeConfig config) {
this.params = new StandardHugeGraphParams();
this.configuration = configuration;
this.configuration = config;

this.schemaEventHub = new EventHub("schema");
this.graphEventHub = new EventHub("graph");
this.indexEventHub = new EventHub("index");

final int writeLimit = configuration.get(CoreOptions.RATE_LIMIT_WRITE);
final int writeLimit = config.get(CoreOptions.RATE_LIMIT_WRITE);
this.writeRateLimiter = writeLimit > 0 ?
RateLimiter.create(writeLimit) : null;
final int readLimit = configuration.get(CoreOptions.RATE_LIMIT_READ);
final int readLimit = config.get(CoreOptions.RATE_LIMIT_READ);
this.readRateLimiter = readLimit > 0 ?
RateLimiter.create(readLimit) : null;

boolean ramtableEnable = config.get(CoreOptions.QUERY_RAMTABLE_ENABLE);
if (ramtableEnable) {
long vc = config.get(CoreOptions.QUERY_RAMTABLE_VERTICES_CAPACITY);
int ec = config.get(CoreOptions.QUERY_RAMTABLE_EDGES_CAPACITY);
this.ramtable = new RamTable(this, vc, ec);
} else {
this.ramtable = null;
}

this.taskManager = TaskManager.instance();

this.features = new HugeFeatures(this, true);

this.name = configuration.get(CoreOptions.STORE);
this.name = config.get(CoreOptions.STORE);
this.started = false;
this.closed = false;
this.mode = GraphMode.NONE;
Expand Down Expand Up @@ -403,6 +415,19 @@ private Analyzer analyzer() {
return AnalyzerFactory.analyzer(name, mode);
}

protected void reloadRamtable() {
this.reloadRamtable(false);
}

protected void reloadRamtable(boolean loadFromFile) {
// Expect triggered manually, like gremlin job
if (this.ramtable != null) {
this.ramtable.reload(loadFromFile, this.name);
} else {
LOG.warn("The ramtable feature is not enabled for graph {}", this);
}
}

@Override
public <C extends GraphComputer> C compute(Class<C> clazz)
throws IllegalArgumentException {
Expand Down Expand Up @@ -988,6 +1013,11 @@ public RateLimiter writeRateLimiter() {
public RateLimiter readRateLimiter() {
return StandardHugeGraph.this.readRateLimiter;
}

@Override
public RamTable ramtable() {
return StandardHugeGraph.this.ramtable;
}
}

private class TinkerpopTransaction extends AbstractThreadLocalTransaction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.baidu.hugegraph.backend.query.QueryResults;
import com.baidu.hugegraph.backend.store.BackendMutation;
import com.baidu.hugegraph.backend.store.BackendStore;
import com.baidu.hugegraph.backend.store.ram.RamTable;
import com.baidu.hugegraph.backend.tx.GraphTransaction;
import com.baidu.hugegraph.config.CoreOptions;
import com.baidu.hugegraph.config.HugeConfig;
Expand Down Expand Up @@ -226,6 +227,11 @@ private Iterator<HugeVertex> queryVerticesByIds(IdQuery query) {

@Override
protected final Iterator<HugeEdge> queryEdgesFromBackend(Query query) {
RamTable ramtable = this.params().ramtable();
if (ramtable != null && ramtable.matched(query)) {
return ramtable.query(query);
}

if (query.empty() || query.paging() || query.bigCapacity()) {
// Query all edges or query edges in paging, don't cache it
return super.queryEdgesFromBackend(query);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2017 HugeGraph Authors
*
* 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 com.baidu.hugegraph.backend.store.ram;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;

import com.baidu.hugegraph.HugeException;

public final class IntIntMap implements RamMap {

// TODO: use com.carrotsearch.hppc.IntIntHashMap instead
private final int[] array;

public IntIntMap(int capacity) {
this.array = new int[capacity];
}

public void put(long key, int value) {
assert 0 <= key && key < Integer.MAX_VALUE;
this.array[(int) key] = value;
}

public int get(long key) {
assert 0 <= key && key < Integer.MAX_VALUE;
return this.array[(int) key];
}

@Override
public void clear() {
Arrays.fill(this.array, 0);
}

@Override
public long size() {
return this.array.length;
}

@Override
public void writeTo(DataOutputStream buffer) throws IOException {
buffer.writeInt(this.array.length);
for (int value : this.array) {
buffer.writeInt(value);
}
}

@Override
public void readFrom(DataInputStream buffer) throws IOException {
int size = buffer.readInt();
if (size > this.array.length) {
throw new HugeException("Invalid size %s, expect < %s",
size, this.array.length);
}
for (int i = 0; i < size; i++) {
int value = buffer.readInt();
this.array[i] = value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2017 HugeGraph Authors
*
* 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 com.baidu.hugegraph.backend.store.ram;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;

import com.baidu.hugegraph.HugeException;

public final class IntLongMap implements RamMap {

// TODO: use com.carrotsearch.hppc.IntLongHashMap instead
private final long[] array;
private int size;

public IntLongMap(int capacity) {
this.array = new long[capacity];
this.size = 0;
}

public void put(int key, long value) {
if (key >= this.size || key < 0) {
throw new HugeException("Invalid key %s", key);
}
this.array[key] = value;
}

public int add(long value) {
if (this.size == Integer.MAX_VALUE) {
throw new HugeException("Too many edges %s", this.size);
}
int index = this.size;
this.array[index] = value;
this.size++;
return index;
}

public long get(int key) {
if (key >= this.size || key < 0) {
throw new HugeException("Invalid key %s", key);
}
return this.array[key];
}

@Override
public void clear() {
Arrays.fill(this.array, 0L);
}

@Override
public long size() {
return this.size;
}

@Override
public void writeTo(DataOutputStream buffer) throws IOException {
buffer.writeInt(this.array.length);
for (long value : this.array) {
buffer.writeLong(value);
}
}

@Override
public void readFrom(DataInputStream buffer) throws IOException {
int size = buffer.readInt();
if (size > this.array.length) {
throw new HugeException("Invalid size %s, expect < %s",
size, this.array.length);
}
for (int i = 0; i < size; i++) {
long value = buffer.readLong();
this.array[i] = value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2017 HugeGraph Authors
*
* 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 com.baidu.hugegraph.backend.store.ram;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;

import com.baidu.hugegraph.exception.NotSupportException;

public final class IntObjectMap<V> implements RamMap {

private final Object[] array;

public IntObjectMap(int size) {
this.array = new Object[size];
}

@SuppressWarnings("unchecked")
public V get(int key) {
return (V) this.array[key];
}

public void set(int key, V value) {
this.array[key] = value;
}

@Override
public void clear() {
Arrays.fill(this.array, null);
}

@Override
public long size() {
return this.array.length;
}

@Override
public void writeTo(DataOutputStream buffer) throws IOException {
throw new NotSupportException("IntObjectMap.writeTo");
}

@Override
public void readFrom(DataInputStream buffer) throws IOException {
throw new NotSupportException("IntObjectMap.readFrom");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2017 HugeGraph Authors
*
* 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 com.baidu.hugegraph.backend.store.ram;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public interface RamMap {

public void clear();

public long size();

public void writeTo(DataOutputStream buffer) throws IOException;

public void readFrom(DataInputStream buffer) throws IOException;
}
Loading

0 comments on commit e35f9fb

Please sign in to comment.