Skip to content

Commit ccee5bc

Browse files
committed
add plugin functionality
This enables the user to run custom code during database generation. The user can populate the database with new tables, modify entries, etc.
1 parent fdafd1f commit ccee5bc

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

include/bf.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ OF SUCH DAMAGE.
7070

7171
#include "SinglyLinkedList.h"
7272
#include "compress.h"
73+
#include "plugin.h"
7374
#include "trie.h"
7475
#include "xattrs.h"
7576

@@ -274,6 +275,12 @@ struct input {
274275

275276
/* prefix of swap files */
276277
refstr_t swap_prefix;
278+
279+
/*
280+
* Holds pointers to plugin functions for running custom user code to manipulate the databases.
281+
* These will be NULL if no plugin was specified.
282+
*/
283+
const struct plugin_operations *plugin_ops;
277284
};
278285

279286
struct input *input_init(struct input *in);

include/plugin.h

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
This file is part of GUFI, which is part of MarFS, which is released
3+
under the BSD license.
4+
5+
6+
Copyright (c) 2017, Los Alamos National Security (LANS), LLC
7+
All rights reserved.
8+
9+
Redistribution and use in source and binary forms, with or without modification,
10+
are permitted provided that the following conditions are met:
11+
12+
1. Redistributions of source code must retain the above copyright notice, this
13+
list of conditions and the following disclaimer.
14+
15+
2. Redistributions in binary form must reproduce the above copyright notice,
16+
this list of conditions and the following disclaimer in the documentation and/or
17+
other materials provided with the distribution.
18+
19+
3. Neither the name of the copyright holder nor the names of its contributors
20+
may be used to endorse or promote products derived from this software without
21+
specific prior written permission.
22+
23+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26+
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31+
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32+
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33+
34+
35+
From Los Alamos National Security, LLC:
36+
LA-CC-15-039
37+
38+
Copyright (c) 2017, Los Alamos National Security, LLC All rights reserved.
39+
Copyright 2017. Los Alamos National Security, LLC. This software was produced
40+
under U.S. Government contract DE-AC52-06NA25396 for Los Alamos National
41+
Laboratory (LANL), which is operated by Los Alamos National Security, LLC for
42+
the U.S. Department of Energy. The U.S. Government has rights to use,
43+
reproduce, and distribute this software. NEITHER THE GOVERNMENT NOR LOS
44+
ALAMOS NATIONAL SECURITY, LLC MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR
45+
ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE. If software is
46+
modified to produce derivative works, such modified software should be
47+
clearly marked, so as not to confuse it with the version available from
48+
LANL.
49+
50+
THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND CONTRIBUTORS
51+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
52+
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53+
ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS NATIONAL SECURITY, LLC OR
54+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
55+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
56+
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
59+
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
60+
OF SUCH DAMAGE.
61+
*/
62+
63+
#ifndef PLUGIN_H
64+
#define PLUGIN_H
65+
66+
#include <sqlite3.h>
67+
68+
#ifdef __cplusplus
69+
extern "C" {
70+
#endif
71+
72+
/*
73+
* Operations for a user-defined plugin library, allowing the user to make custom
74+
* modifications to the databases as GUFI runs.
75+
*/
76+
struct plugin_operations {
77+
/*
78+
* Give the user an opportunity to initialize any state. The returned pointer
79+
* is passed to all subsequent operations as `user_data`.
80+
*/
81+
void *(*db_init)(sqlite3 *db);
82+
/* Process a directory. */
83+
void (*process_dir)(char *path, sqlite3 *db, void *user_data);
84+
/* Process a file */
85+
void (*process_file)(char *path, sqlite3 *db, void *user_data);
86+
/*
87+
* Clean up any state, and write out any final data to the database, for example
88+
* a summary containing information from each of the files seen.
89+
*/
90+
void (*db_exit)(sqlite3 *db, void *user_data);
91+
};
92+
93+
#ifdef __cplusplus
94+
}
95+
#endif
96+
97+
#endif

src/bf.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ OF SUCH DAMAGE.
6262

6363

6464

65+
#include <dlfcn.h>
6566
#include <pwd.h>
6667
#include <stdint.h>
6768
#include <stdio.h>
@@ -72,6 +73,7 @@ OF SUCH DAMAGE.
7273
#include "bf.h"
7374
#include "dbutils.h"
7475
#include "external.h"
76+
#include "plugin.h"
7577
#include "utils.h"
7678

7779
#define XSTR(x) #x
@@ -90,6 +92,13 @@ const char fielddelim = '\x1E'; /* ASCII Record Separator */
9092

9193
static const char DEFAULT_SWAP_PREFIX[] = "";
9294

95+
static const struct plugin_operations null_plugin_ops = {
96+
.db_init = NULL,
97+
.process_file = NULL,
98+
.process_dir = NULL,
99+
.db_exit = NULL,
100+
};
101+
93102
struct input *input_init(struct input *in) {
94103
if (in) {
95104
memset(in, 0, sizeof(*in));
@@ -118,6 +127,8 @@ struct input *input_init(struct input *in) {
118127

119128
in->swap_prefix.data = DEFAULT_SWAP_PREFIX;
120129
in->swap_prefix.len = 0;
130+
131+
in->plugin_ops = &null_plugin_ops;
121132
}
122133

123134
return in;
@@ -137,6 +148,30 @@ void input_fini(struct input *in) {
137148
}
138149
}
139150

151+
/*
152+
* If a plugin library path is given in in->plugin_name,
153+
* then load that library and add its plugin functions to in->plugin_ops.
154+
*
155+
* Returns 0 on success or 1 on failure.
156+
*/
157+
int load_plugin_library(struct input *in, char *plugin_name) {
158+
void *lib = dlopen(plugin_name, RTLD_NOW);
159+
if (!lib) {
160+
fprintf(stderr, "Could not open plugin library: %s\n", dlerror());
161+
return 1;
162+
}
163+
164+
struct plugin_operations *user_plugin_ops = (struct plugin_operations *) dlsym(lib, "exported_operations");
165+
if (!user_plugin_ops) {
166+
fprintf(stderr, "Could not find exported operations in the plugin library: %s\n", dlerror());
167+
return 1;
168+
}
169+
170+
in->plugin_ops = user_plugin_ops;
171+
172+
return 0;
173+
}
174+
140175
void print_help(const char* prog_name,
141176
const char* getopt_str,
142177
const char* positional_args_help_str) {
@@ -198,6 +233,7 @@ void print_help(const char* prog_name,
198233
" <view> External database file basename, per-attach table name, template + table name, and the resultant view"); break;
199234
case 's': printf(" -s <path> File name prefix for swap files"); break;
200235
case 'p': printf(" -p <path> Source path prefix for %%s in SQL"); break;
236+
case 'U': printf(" -U <library_name> plugin library for modifying db entries"); break;
201237
default: printf("print_help(): unrecognized option '%c'", (char)ch);
202238
}
203239
printf("\n");
@@ -518,6 +554,12 @@ int parse_cmd_line(int argc,
518554
INSTALL_STR(&in->sql_format.source_prefix, optarg);
519555
break;
520556

557+
case 'U':
558+
if (load_plugin_library(in, optarg)) {
559+
retval = -1;
560+
}
561+
break;
562+
521563
case '?':
522564
// getopt returns '?' when there is a problem. In this case it
523565
// also prints, e.g. "getopt_test: illegal option -- z"

test/regression/gufi_dir2index.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ options:
1313
-M <bytes> target memory footprint
1414
-s <path> File name prefix for swap files
1515
-C <count> Number of subdirectories allowed to be enqueued for parallel processing. Any remainders will be processed in-situ
16+
-U <library_name> plugin library for modifying db entries
1617
-e compress work items
1718
-q check that external databases are valid before tracking during indexing
1819

0 commit comments

Comments
 (0)