Skip to content

AnyPlugin

ZengJingtao edited this page Jan 30, 2023 · 3 revisions

HTML display of arbitrary content based on AnyPlugin

In order to achieve HTML display of arbitrary content based on AnyPlugin, we need the following steps

  1. create a new .cc/.cpp/.cxx file (at your desired location);
  2. define a class deriving from AnyPlugin;
  3. implement the two pure virtual functions of AnyPlugin in the derived class;
  4. register plugin;
  5. rebuild the project;
  6. modify the json file;
  7. run the project and you can see what we show in the browser.
  8. advanced: display the json file in the form of a table.

1. Create a new .cc/.cpp/.cxx file

At the location we want, create a new .cc/.cpp/.cxx file for writing our HTML-displaying plugin.

In this example, we create a new HtmlShowExample.cc file.

Load the header file we will use #include "topling/side_plugin_factory.h".

2. Define a class deriving from AnyPlugin

AnyPlugin is a pure abstract class, including two functions, Update and ToSring, which need to be implemented by derived classes.

First, we define an AnyPlugin-derived class HtmlShowExample, and implement its two pure virtual functions as follows:

#include "topling/side_plugin_factory.h"

namespace rocksdb {
class HtmlShowExample : public AnyPlugin {
public:
  void Update(const json&, const SidePluginRepo&) {}
  std::string ToString(const json& dump_options, const SidePluginRepo&) const {}
};

} // namespace rocksdb

Note

  • When we implement HTML display, the Update function will not be used, but it still needs to be defined and left empty.
  • In the ToString function, we only need to pass in its first parameter dump_options, but we don't have to do anything with this parameter.

3. Implement the two pure virtual functions of AnyPlugin in the derived class

The Update function will not be used here, so it is defined as empty and we will ignore it. Let’s talk about the ToString function below. The return value of the ToString function is of std::string type, and the returned string will be printed in the browser without distinction. We modify the ToString function above as follows:

std::string ToString(const json& dump_options, const SidePluginRepo&) const {
  return "This is an example of HTML show.";
}

In this way, we have implemented a simple HTML display plugin.

4. register plugin

At the end of the HtmlShowExample.cc file, in the topling namespace, enter the following code:

ROCKSDB_REG_DEFAULT_CONS(HtmlShowExample, AnyPlugin);
ROCKSDB_REG_AnyPluginManip("HtmlShowExample");
  • ROCKSDB_REG_DEFAULT_CONS(param1, param2, param3): This macro applies to derived classes that do not use (const json&, const SidePluginRepo&) constructor parameters.
    • param1: The plugin registration name to be written in json, string type, and need to add double quotes.
    • param2: The plugin registration class name to be written in json.
    • param3: Fill in the parent class, in this case AnyPlugin.
    • Note: If the values ​​of param1 and param2 are the same, param1 can be omitted, as shown in the above code.
  • ROCKSDB_REG_AnyPluginManip(param1): Used to register plugins derived from the AnyPlugin class.
    • The parameter is the registration name of the plugin to be written in json, which is the same as param1 of ROCKSDB_REG_DEFAULT_CONS.

5. Rebuild the project

First, we need to add our custom HtmlShowExample.cc to the build file, it may be CmakeLists.txt, Makefile, or a compilation script, depending on the build method of your project. Then, rebuild the project.

6. Modify the json file

Find our Home, add a field

"AnyPlugin": {
    "html-show-example": "HtmlShowExample"
},
  • AnyPlugin: The abstract class name our plugins inherit from.
    • html-show-example: The variable name for our plugin object.
    • HtmlShowExample: The plugin registration name written in json, which is the same as param1 of ROCKSDB_REG_DEFAULT_CONS above.

7. Run the project

Run the built project, enter

192.168.31.2:8088/AnyPlugin/html-show-example

in the browser, and you can see that the browser printed out the value returned by the ToString function in our code "This is an example of HTML show.".

  • 192.168.31.2:8088: The address and listening port of our database. The listening port number is set in the listening_ports field in the http field of the json configuration file.
  • AnyPlugin: The abstract class we use.
  • html-show-example: variable name for our plugin object

8. Advanced: display the json file in the form of a table

Directly displayed string files is quite unreadable. We can store the configuration items that need to be displayed in a json object, and use the JsonToString function to print the contents of the json object to the browser in the form of a table.

Modify our previous ToString function code, and keep other parts unchanged.

std::string ToString(const json& dump_options, const SidePluginRepo&) const {
  json js;
  js["key1"] = "This is an example of HTML show 1.";
  js["key2"] = 2;
  return JsonToString(js, dump_options);
}

Run the compiled project, enter

192.168.31.2:8088/AnyPlugin/html-show-example?html=1

in the browser.

  • html=1:url parameter, indicating that the displayed content will be printed in the form of a table

For more information on url parameters and HTML display information, see the documentation.

Now we can see in the browser what we output as a table.

Clone this wiki locally