Currently, the plug-in method provides extensions in the following dimensions:
hugegraph-core Jar package dependenciesThe details of maven pom.xml are as follows:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.apache.hugegraph</groupId> <artifactId>hugegraph-plugin-demo</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>hugegraph-plugin-demo</name> <dependencies> <dependency> <groupId>org.apache.hugegraph</groupId> <artifactId>hugegraph-core</artifactId> <version>${project.version}</version> </dependency> </dependencies> </project>
org.apache.hugegraph.backend.store.BackendStoreProviderorg.apache.hugegraph.backend.store.AbstractBackendStoreProviderTake the RocksDB backend RocksDBStoreProvider as an example:
public class RocksDBStoreProvider extends AbstractBackendStoreProvider { protected String database() { return this.graph().toLowerCase(); } @Override protected BackendStore newSchemaStore(String store) { return new RocksDBSchemaStore(this, this.database(), store); } @Override protected BackendStore newGraphStore(String store) { return new RocksDBGraphStore(this, this.database(), store); } @Override public String type() { return "rocksdb"; } @Override public String version() { return "1.0"; } }
The BackendStore interface is defined as follows:
public interface BackendStore { // Store name public String store(); // Database name public String database(); // Get the parent provider public BackendStoreProvider provider(); // Open/close database public void open(HugeConfig config); public void close(); // Initialize/clear database public void init(); public void clear(); // Add/delete data public void mutate(BackendMutation mutation); // Query data public Iterator<BackendEntry> query(Query query); // Transaction public void beginTx(); public void commitTx(); public void rollbackTx(); // Get metadata by key public <R> R metadata(HugeType type, String meta, Object[] args); // Backend features public BackendFeatures features(); // Generate an id for a specific type public Id nextId(HugeType type); }
The serializer must inherit the abstract class: org.apache.hugegraph.backend.serializer.AbstractSerializer ( implements GraphSerializer, SchemaSerializer) The main interface is defined as follows:
public interface GraphSerializer { public BackendEntry writeVertex(HugeVertex vertex); public BackendEntry writeVertexProperty(HugeVertexProperty<?> prop); public HugeVertex readVertex(HugeGraph graph, BackendEntry entry); public BackendEntry writeEdge(HugeEdge edge); public BackendEntry writeEdgeProperty(HugeEdgeProperty<?> prop); public HugeEdge readEdge(HugeGraph graph, BackendEntry entry); public BackendEntry writeIndex(HugeIndex index); public HugeIndex readIndex(HugeGraph graph, ConditionQuery query, BackendEntry entry); public BackendEntry writeId(HugeType type, Id id); public Query writeQuery(Query query); } public interface SchemaSerializer { public BackendEntry writeVertexLabel(VertexLabel vertexLabel); public VertexLabel readVertexLabel(HugeGraph graph, BackendEntry entry); public BackendEntry writeEdgeLabel(EdgeLabel edgeLabel); public EdgeLabel readEdgeLabel(HugeGraph graph, BackendEntry entry); public BackendEntry writePropertyKey(PropertyKey propertyKey); public PropertyKey readPropertyKey(HugeGraph graph, BackendEntry entry); public BackendEntry writeIndexLabel(IndexLabel indexLabel); public IndexLabel readIndexLabel(HugeGraph graph, BackendEntry entry); }
When adding a custom backend, it may be necessary to add new configuration items. The implementation process mainly includes:
org.apache.hugegraph.config.OptionHolderpublic static OptionHolder instance(), and call the method when the object is initialized OptionHolder.registerOptions()ConfigOption, multi-value configuration item type is ConfigListOptionTake the RocksDB configuration item definition as an example:
public class RocksDBOptions extends OptionHolder { private RocksDBOptions() { super(); } private static volatile RocksDBOptions instance; public static synchronized RocksDBOptions instance() { if (instance == null) { instance = new RocksDBOptions(); instance.registerOptions(); } return instance; } public static final ConfigOption<String> DATA_PATH = new ConfigOption<>( "rocksdb.data_path", "The path for storing data of RocksDB.", disallowEmpty(), "rocksdb-data" ); public static final ConfigOption<String> WAL_PATH = new ConfigOption<>( "rocksdb.wal_path", "The path for storing WAL of RocksDB.", disallowEmpty(), "rocksdb-data" ); public static final ConfigListOption<String> DATA_DISKS = new ConfigListOption<>( "rocksdb.data_disks", false, "The optimized disks for storing data of RocksDB. " + "The format of each element: `STORE/TABLE: /path/to/disk`." + "Allowed keys are [graph/vertex, graph/edge_out, graph/edge_in, " + "graph/secondary_index, graph/range_index]", null, String.class, ImmutableList.of() ); }
The tokenizer needs to implement the interface org.apache.hugegraph.analyzer.Analyzer, take implementing a SpaceAnalyzer space tokenizer as an example.
package org.apache.hugegraph.plugin; import java.util.Arrays; import java.util.HashSet; import java.util.Set; import org.apache.hugegraph.analyzer.Analyzer; public class SpaceAnalyzer implements Analyzer { @Override public Set<String> segment(String text) { return new HashSet<>(Arrays.asList(text.split(" "))); } }
The plug-in registration entry is HugeGraphPlugin.register(), the custom plug-in must implement this interface method, and register the extension items defined above inside it. The interface org.apache.hugegraph.plugin.HugeGraphPlugin is defined as follows:
public interface HugeGraphPlugin { public String name(); public void register(); public String supportsMinVersion(); public String supportsMaxVersion(); }
And HugeGraphPlugin provides 4 static methods for registering extensions:
The following is an example of registering the SpaceAnalyzer tokenizer:
package org.apache.hugegraph.plugin; public class DemoPlugin implements HugeGraphPlugin { @Override public String name() { return "demo"; } @Override public void register() { HugeGraphPlugin.registerAnalyzer("demo", SpaceAnalyzer.class.getName()); } }
Through maven packaging, execute the command in the project directory mvn package, and a Jar package file will be generated in the target directory. Copy the Jar package to the plugins directory when using it, and restart the service to take effect.