Skip to content
Permalink
Browse files
Add Vertex, Edge, Properties and JsonStructGraphOutput (#10)
* Add Vertex, Edge, Properties and JsonStructGraphOutput

* Add Recycler and Allocator

* Use RecycleReference to hold Handle and Recyclable

* Use Factory to create Vertex and Edge
  • Loading branch information
Linary committed Feb 4, 2021
1 parent 2c30085 commit dfe7347b1ac8d88c42c5b4585f5f279aa6d75435
Showing 50 changed files with 2,717 additions and 62 deletions.
@@ -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.computer.core.allocator;

import java.util.function.Supplier;

import com.baidu.hugegraph.computer.core.config.ComputerOptions;
import com.baidu.hugegraph.computer.core.config.Config;
import com.baidu.hugegraph.computer.core.graph.GraphFactory;
import com.baidu.hugegraph.computer.core.graph.edge.Edge;
import com.baidu.hugegraph.computer.core.graph.vertex.Vertex;

import io.netty.util.Recycler;

public final class Allocator {

private final GraphFactory factory;
private final Recycler<RecyclerReference<Vertex>> vertexRecycler;
private final Recycler<RecyclerReference<Edge>> edgeRecycler;

public Allocator(Config config) {
this.factory = new GraphFactory();

int capacityPerThread =
config.get(ComputerOptions.ALLOCATOR_MAX_VERTICES_PER_THREAD);
this.vertexRecycler = this.newRecycler(capacityPerThread,
factory::createVertex);
this.edgeRecycler = this.newRecycler(capacityPerThread,
factory::createEdge);
}

private <T extends Recyclable> Recycler<RecyclerReference<T>>
newRecycler(int capacityPerThread,
Supplier<T> supplier) {
// TODO: Add more params for Recycler
return new Recycler<RecyclerReference<T>>(capacityPerThread) {
@Override
protected RecyclerReference<T> newObject(
Recycler.Handle<RecyclerReference<T>> handle) {
T recyclable = supplier.get();
return new RecyclerReference<T>(recyclable, handle);
}
};
}

public RecyclerReference<Vertex> newVertex() {
return this.vertexRecycler.get();
}

public void freeVertex(RecyclerReference<Vertex> reference) {
reference.close();
}

public RecyclerReference<Edge> newEdge() {
return this.edgeRecycler.get();
}

public void freeEdge(RecyclerReference<Edge> reference) {
reference.close();
}
}
@@ -0,0 +1,23 @@
/*
* 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.computer.core.allocator;

public interface Recyclable {
}
@@ -0,0 +1,41 @@
/*
* 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.computer.core.allocator;

import java.lang.ref.SoftReference;

import io.netty.util.Recycler;

public class RecyclerReference<T extends Recyclable> extends SoftReference<T>
implements AutoCloseable {

private final Recycler.Handle<RecyclerReference<T>> handle;

public RecyclerReference(T referent,
Recycler.Handle<RecyclerReference<T>> handle) {
super(referent);
this.handle = handle;
}

@Override
public void close() {
this.handle.recycle(this);
}
}
@@ -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.computer.core.common;

import java.util.Map;

import com.baidu.hugegraph.computer.core.allocator.Allocator;
import com.baidu.hugegraph.computer.core.config.Config;
import com.baidu.hugegraph.computer.core.graph.GraphFactory;
import com.baidu.hugegraph.util.E;

public final class ComputerContext {

private static volatile ComputerContext INSTANCE;

private final Config config;
private final GraphFactory factory;
private final Allocator allocator;

private ComputerContext(Config config) {
this.config = config;
this.factory = new GraphFactory();
this.allocator = new Allocator(config);
}

public static synchronized void updateOptions(
Map<String, String> options) {
Config config = new Config(options);
INSTANCE = new ComputerContext(config);
}

public static ComputerContext instance() {
E.checkNotNull(INSTANCE, "ComputerContext INSTANCE");
return INSTANCE;
}

public Config config() {
return this.config;
}

public GraphFactory graphFactory() {
return this.factory;
}

public Allocator allocator() {
return this.allocator;
}
}
@@ -0,0 +1,130 @@
/*
* 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.computer.core.config;

import static com.baidu.hugegraph.config.OptionChecker.allowValues;
import static com.baidu.hugegraph.config.OptionChecker.disallowEmpty;
import static com.baidu.hugegraph.config.OptionChecker.positiveInt;

import java.util.Set;

import com.baidu.hugegraph.config.ConfigOption;
import com.baidu.hugegraph.config.OptionHolder;
import com.google.common.collect.ImmutableSet;

public class ComputerOptions extends OptionHolder {

private ComputerOptions() {
super();
}

private static volatile ComputerOptions INSTANCE;

public static synchronized ComputerOptions instance() {
if (INSTANCE == null) {
INSTANCE = new ComputerOptions();
// Should initialize all static members first, then register.
INSTANCE.registerOptions();
}
return INSTANCE;
}

public static final ConfigOption<String> ALGORITHM_NAME =
new ConfigOption<>(
"algorithm.name",
"The name of current algorithm",
disallowEmpty(),
"unknown"
);

public static final ConfigOption<String> VALUE_TYPE =
new ConfigOption<>(
"algorithm.value_type",
"The value type of current algorithm",
disallowEmpty(),
"NULL"
);

public static final ConfigOption<String> VALUE_NAME =
new ConfigOption<>(
"algorithm.value_name",
"The algorithm value name of vertex",
disallowEmpty(),
"value"
);

public static final ConfigOption<String> EDGES_NAME =
new ConfigOption<>(
"algorithm.edges_name",
"The algorithm value name of edges",
disallowEmpty(),
"value"
);

public static final ConfigOption<Boolean> OUTPUT_WITH_ADJACENT_EDGES =
new ConfigOption<>(
"output.with_adjacent_edges",
"Output the adjacent edges of the vertex or not",
allowValues(true, false),
false
);

public static final ConfigOption<Boolean> OUTPUT_WITH_VERTEX_PROPERTIES =
new ConfigOption<>(
"output.with_vertex_properties",
"Output the properties of the vertex or not",
allowValues(true, false),
false
);

public static final ConfigOption<Boolean> OUTPUT_WITH_EDGE_PROPERTIES =
new ConfigOption<>(
"output.with_edge_properties",
"Output the properties of the edge or not",
allowValues(true, false),
false
);

public static final ConfigOption<Integer> VERTEX_AVERAGE_DEGREE =
new ConfigOption<>(
"computer.vertex_average_degree",
"The average degree of a vertex, it represents the " +
"average number of adjacent edges per vertex",
positiveInt(),
10
);

public static final ConfigOption<Integer>
ALLOCATOR_MAX_VERTICES_PER_THREAD =
new ConfigOption<>(
"allocator.max_vertices_per_thread",
"Maximum number of vertices per thread processed " +
"in each memory allocator",
positiveInt(),
10000
);

public static Set<String> REQUIRED_OPTIONS = ImmutableSet.of(
ALGORITHM_NAME.name(),
VALUE_TYPE.name(),
VALUE_NAME.name(),
EDGES_NAME.name()
);
}

0 comments on commit dfe7347

Please sign in to comment.