1/*	$NetBSD: config.c,v 1.2 2021/08/14 16:15:02 christos Exp $	*/
2
3/* OpenLDAP WiredTiger backend */
4/* $OpenLDAP$ */
5/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 *
7 * Copyright 2002-2021 The OpenLDAP Foundation.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
12 * Public License.
13 *
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
17 */
18/* ACKNOWLEDGEMENTS:
19 * This work was developed by HAMANO Tsukasa <hamano@osstech.co.jp>
20 * based on back-bdb for inclusion in OpenLDAP Software.
21 * WiredTiger is a product of MongoDB Inc.
22 */
23
24#include <sys/cdefs.h>
25__RCSID("$NetBSD: config.c,v 1.2 2021/08/14 16:15:02 christos Exp $");
26
27#include "portable.h"
28
29#include <stdio.h>
30#include <ac/string.h>
31#include "back-wt.h"
32#include "slap-config.h"
33
34#include "lutil.h"
35#include "ldap_rq.h"
36
37static ConfigDriver wt_cf_gen;
38
39enum {
40	WT_DIRECTORY = 1,
41	WT_CONFIG,
42	WT_INDEX,
43};
44
45static ConfigTable wtcfg[] = {
46    { "directory", "dir", 2, 2, 0, ARG_STRING|ARG_MAGIC|WT_DIRECTORY,
47	  wt_cf_gen, "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
48	  "DESC 'Directory for database content' "
49	  "EQUALITY caseIgnoreMatch "
50	  "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
51    { "wtconfig", "config", 2, 2, 0, ARG_STRING|ARG_MAGIC|WT_CONFIG,
52	  wt_cf_gen, "( OLcfgDbAt:13.1 NAME 'olcWtConfig' "
53	  "DESC 'Configuration for WiredTiger' "
54	  "EQUALITY caseIgnoreMatch "
55	  "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
56	{ "index", "attr> <[pres,eq,approx,sub]", 2, 3, 0, ARG_MAGIC|WT_INDEX,
57	  wt_cf_gen, "( OLcfgDbAt:0.2 NAME 'olcDbIndex' "
58	  "DESC 'Attribute index parameters' "
59	  "EQUALITY caseIgnoreMatch "
60	  "SYNTAX OMsDirectoryString )", NULL, NULL },
61	{ NULL, NULL, 0, 0, 0, ARG_IGNORED,
62		NULL, NULL, NULL, NULL }
63};
64
65static ConfigOCs wtocs[] = {
66	{ "( OLcfgDbOc:9.1 "
67	  "NAME 'olcWtConfig' "
68	  "DESC 'Wt backend configuration' "
69	  "SUP olcDatabaseConfig "
70	  "MUST olcDbDirectory "
71	  "MAY ( olcWtConfig $ olcDbIndex ) )",
72	  Cft_Database, wtcfg },
73	{ NULL, 0, NULL }
74};
75
76/* reindex entries on the fly */
77static void *
78wt_online_index( void *ctx, void *arg )
79{
80	// Not implement yet
81}
82
83/* Cleanup loose ends after Modify completes */
84static int
85wt_cf_cleanup( ConfigArgs *c )
86{
87	// Not implement yet
88	return 0;
89}
90
91static int
92wt_cf_gen( ConfigArgs *c )
93{
94	struct wt_info *wi = (struct wt_info *) c->be->be_private;
95	int rc;
96
97	if(c->op == SLAP_CONFIG_EMIT) {
98		rc = 0;
99		// not implement yet
100		return rc;
101	}
102
103	switch( c->type ) {
104	case WT_DIRECTORY:
105		ch_free( wi->wi_dbenv_home );
106		wi->wi_dbenv_home = c->value_string;
107		break;
108	case WT_CONFIG:
109		ch_free( wi->wi_dbenv_config );
110		wi->wi_dbenv_config = c->value_string;
111		break;
112
113	case WT_INDEX:
114		rc = wt_attr_index_config( wi, c->fname, c->lineno,
115								   c->argc - 1, &c->argv[1], &c->reply);
116
117		if( rc != LDAP_SUCCESS ) return 1;
118		wi->wi_flags |= WT_OPEN_INDEX;
119
120		if ( wi->wi_flags & WT_IS_OPEN ) {
121			config_push_cleanup( c, wt_cf_cleanup );
122
123			if ( !wi->wi_index_task ) {
124				/* Start the task as soon as we finish here. Set a long
125                 * interval (10 hours) so that it only gets scheduled once.
126                 */
127				if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
128					fprintf( stderr, "%s: "
129							 "\"index\" must occur after \"suffix\".\n",
130							 c->log );
131					return 1;
132				}
133				ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
134				wi->wi_index_task = ldap_pvt_runqueue_insert(&slapd_rq, 36000,
135															 wt_online_index, c->be,
136															 LDAP_XSTRING(wt_online_index),
137															 c->be->be_suffix[0].bv_val );
138				ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
139			}
140		}
141		break;
142
143	}
144	return LDAP_SUCCESS;
145}
146
147int wt_back_init_cf( BackendInfo *bi )
148{
149	int rc;
150	bi->bi_cf_ocs = wtocs;
151
152	rc = config_register_schema( wtcfg, wtocs );
153	if ( rc ) return rc;
154	return 0;
155}
156
157/*
158 * Local variables:
159 * indent-tabs-mode: t
160 * tab-width: 4
161 * c-basic-offset: 4
162 * End:
163 */
164