HugeGraph Plugin mechanism and plug-in extension process
Background
HugeGraph is not only open source and open, but also simple and easy to use. General users can easily add plug-in extension functions without changing the source code.
HugeGraph supports a variety of built-in storage backends, and also allows users to extend custom backends without changing the existing source code.
HugeGraph supports full-text search. The full-text search function involves word segmentation in various languages. Currently, there are 7 built-in word
breakers (ansj, hanlp, smartcn, jieba, jcseg, mmseg4j, ikanalyzer), and it also allows users to expand custom word breakers without changing the existing source code.
Scalable dimension
Currently, the plug-in method provides extensions in the following dimensions:
backend storage
serializer
Custom configuration items
tokenizer
Plug-in implementation mechanism
HugeGraph provides a plug-in interface HugeGraphPlugin, which supports plug-in through the Java SPI mechanism
HugeGraph provides four extension registration functions as static methods on HugeGraphPlugin: registerOptions(), registerBackend(), registerSerializer(), registerAnalyzer()
The plug-in implementer implements the corresponding Options, Backend, Serializer or Analyzer interface
The plug-in implementer implements register()the method of the HugeGraphPlugin interface, registers the specific
implementation class listed in the above point 3 in this method, and packs it into a jar package
The plug-in user puts the jar package in the HugeGraph Server installation directory plugins, modifies the relevant
configuration items to the plug-in custom value, and restarts to take effect
publicinterfaceBackendStore{// Store nameStringstore();// Stored versionStringstoredVersion();// Database nameStringdatabase();// Get the parent providerBackendStoreProviderprovider();// Get the system schema storeSystemSchemaStoresystemSchemaStore();// Whether it is the storage of schemabooleanisSchemaStore();// Open/close databasevoidopen(HugeConfigconfig);voidclose();booleanopened();// Initialize/clear databasevoidinit();voidclear(booleanclearSpace);booleaninitialized();// Delete all data of database (keep table structure)voidtruncate();// Add/delete datavoidmutate(BackendMutationmutation);// Query dataIterator<BackendEntry>query(Queryquery);NumberqueryNumber(Queryquery);// TransactionvoidbeginTx();voidcommitTx();voidrollbackTx();// Get metadata by key<R>Rmetadata(HugeTypetype,Stringmeta,Object[]args);// Backend featuresBackendFeaturesfeatures();// Increase next id for specific typevoidincreaseCounter(HugeTypetype,longincrement);// Get current counter for a specific typelonggetCounter(HugeTypetype);}
2.1.3 Extending custom serializers
The serializer must inherit the abstract class: org.apache.hugegraph.backend.serializer.AbstractSerializer
( implements GraphSerializer, SchemaSerializer) The main interface is defined as follows:
When adding a custom backend, it may be necessary to add new configuration items. The implementation process mainly includes:
Add a configuration item container class and implement the interface org.apache.hugegraph.config.OptionHolder
Provide a singleton method public static OptionHolder instance(), and call the method when the object is initialized OptionHolder.registerOptions()
Add configuration item declaration, single-value configuration item type is ConfigOption, multi-value configuration item type is ConfigListOption
Take the RocksDB configuration item definition as an example:
publicclassRocksDBOptionsextendsOptionHolder{privateRocksDBOptions(){super();}privatestaticvolatileRocksDBOptionsinstance;publicstaticsynchronizedRocksDBOptionsinstance(){if(instance==null){instance=newRocksDBOptions();instance.registerOptions();}returninstance;}publicstaticfinalConfigOption<String>DATA_PATH=newConfigOption<>("rocksdb.data_path","The path for storing data of RocksDB.",disallowEmpty(),"rocksdb-data/data");publicstaticfinalConfigOption<String>WAL_PATH=newConfigOption<>("rocksdb.wal_path","The path for storing WAL of RocksDB.",disallowEmpty(),"rocksdb-data/wal");publicstaticfinalConfigListOption<String>DATA_DISKS=newConfigListOption<>("rocksdb.data_disks",false,"The optimized disks for storing data of RocksDB. "+"The format of each element: `STORE/TABLE: /path/disk`."+"Allowed keys are [g/vertex, g/edge_out, g/edge_in, "+"g/vertex_label_index, g/edge_label_index, "+"g/range_int_index, g/range_float_index, "+"g/range_long_index, g/range_double_index, "+"g/secondary_index, g/search_index, g/shard_index, "+"g/unique_index, g/olap]",null,String.class,ImmutableList.of());}
2.2 Extend custom tokenizer
The tokenizer needs to implement the interface org.apache.hugegraph.analyzer.Analyzer, take implementing a SpaceAnalyzer space tokenizer as an example.
3. Implement the plug-in interface and register it
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:
Make sure the services directory exists: hugegraph-plugin-demo/resources/META-INF/services
Create a text file in the services directory: org.apache.hugegraph.plugin.HugeGraphPlugin
The content of the file is as follows: org.apache.hugegraph.plugin.DemoPlugin
5. Make Jar package
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.