<commit_before>/*
 * This file is part of Kotaka, a mud library for DGD
 * http://github.com/shentino/kotaka
 *
 * Copyright (C) 2018  <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <kernel/access.h>
#include <kotaka/assert.h>
#include <kotaka/log.h>
#include <kotaka/paths/bigstruct.h>
#include <kotaka/paths/system.h>
#include <kotaka/privilege.h>
#include <status.h>
#include <type.h>

inherit SECOND_AUTO;

object progdb;	/* program database, index -> program_info */
object pathdb;	/* path database, filename -> latest index */
object inhdb;	/* inherit database, filename -> inheriting objects */
object incdb;	/* include database, filename -> including objects */

atomic void create_database();
atomic void destruct_database();

/* create/destruct */

static void create()
{
}

static void destruct()
{
	destruct_database();
}

/* helpers */

atomic void create_database()
{
	ACCESS_CHECK(SYSTEM());
	ASSERT(find_object(USR_DIR + "/Bigstruct/initd"));
	ASSERT(find_object(BIGSTRUCT_MAP_OBJ));

	rlimits (0; -1) {
		progdb = clone_object(BIGSTRUCT_MAP_OBJ);
		progdb->claim();
		progdb->set_type(T_INT);

		inhdb = clone_object(BIGSTRUCT_MAP_OBJ);
		inhdb->claim();
		inhdb->set_type(T_INT);

		incdb = clone_object(BIGSTRUCT_MAP_OBJ);
		incdb->claim();
		incdb->set_type(T_STRING);

		pathdb = clone_object(BIGSTRUCT_MAP_OBJ);
		pathdb->claim();
		pathdb->set_type(T_STRING);
	}
}

atomic void destruct_database()
{
	ACCESS_CHECK(SYSTEM());

	rlimits (0; -1) {
		int i, sz;
		object *turkeys;

		turkeys = ({ });

		if (progdb) {
			turkeys += ({ progdb });
		}

		if (inhdb) {
			turkeys += ({ inhdb });
		}

		if (incdb) {
			turkeys += ({ incdb });
		}

		if (pathdb) {
			turkeys += ({ pathdb });
		}

		progdb = nil;
		inhdb = nil;
		incdb = nil;
		pathdb = nil;

		sz = sizeof(turkeys);

		for (i = 0; i < sz; i++) {
			destruct_object(turkeys[i]);
		}
	}
}

private void deindex_inherits(int oindex, int *inh)
{
	int sz;

	ASSERT(inhdb);

	for (sz = sizeof(inh); --sz >= 0; ) {
		object submap;
		submap = inhdb->query_element(inh[sz]);
		submap->set_element(oindex, nil);
	}
}

private void deindex_includes(int oindex, string *inc)
{
	int sz;

	ASSERT(incdb);

	for (sz = sizeof(inc); --sz >= 0; ) {
		object submap;
		submap = incdb->query_element(inc[sz]);
		submap->set_element(oindex, nil);
	}
}

private void index_inherits(int oindex, int *inh)
{
	int sz;

	ASSERT(inhdb);

	for (sz = sizeof(inh); --sz >= 0; ) {
		object submap;
		submap = inhdb->query_element(inh[sz]);

		if (!submap) {
			inhdb->set_element(inh[sz], submap = new_object(BIGSTRUCT_MAP_LWO));
			submap->claim();
			submap->set_type(T_INT);
		}

		submap->set_element(oindex, 1);
	}
}

private void index_includes(int oindex, string *inc)
{
	int sz;

	ASSERT(incdb);

	for (sz = sizeof(inc); --sz >= 0; ) {
		object submap;
		submap = incdb->query_element(inc[sz]);

		if (!submap) {
			incdb->set_element(inc[sz], submap = new_object(BIGSTRUCT_MAP_LWO));
			submap->claim();
			submap->set_type(T_INT);
		}

		submap->set_element(oindex, 1);
	}
}

/* objectd hooks */

atomic object register_program(string path, string *inherits, string *includes)
{
	int oindex;
	object pinfo;

	int *oinherits;
	string *oincludes;

	ACCESS_CHECK(previous_program() == OBJECTD);

	if (!progdb) {
		return nil;
	}

	oindex = status(path, O_INDEX);

	if (pathdb) {
		pathdb->set_element(path, oindex);
	}

	pinfo = progdb->query_element(oindex);

	if (!pinfo) {
		pinfo = new_object(PROGRAM_INFO);
		pinfo->set_path(path);

		progdb->set_element(oindex, pinfo);
	}

	oinherits = pinfo->query_inherits();
	oincludes = pinfo->query_includes();<commit_msg>Warn if we skip a program registration because of a missing progdb<commit_after>/*
 * This file is part of Kotaka, a mud library for DGD
 * http://github.com/shentino/kotaka
 *
 * Copyright (C) 2018  <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <kernel/access.h>
#include <kotaka/assert.h>
#include <kotaka/log.h>
#include <kotaka/paths/bigstruct.h>
#include <kotaka/paths/system.h>
#include <kotaka/privilege.h>
#include <status.h>
#include <type.h>

inherit SECOND_AUTO;

object progdb;	/* program database, index -> program_info */
object pathdb;	/* path database, filename -> latest index */
object inhdb;	/* inherit database, filename -> inheriting objects */
object incdb;	/* include database, filename -> including objects */

atomic void create_database();
atomic void destruct_database();

/* create/destruct */

static void create()
{
}

static void destruct()
{
	destruct_database();
}

/* helpers */

atomic void create_database()
{
	ACCESS_CHECK(SYSTEM());
	ASSERT(find_object(USR_DIR + "/Bigstruct/initd"));
	ASSERT(find_object(BIGSTRUCT_MAP_OBJ));

	rlimits (0; -1) {
		progdb = clone_object(BIGSTRUCT_MAP_OBJ);
		progdb->claim();
		progdb->set_type(T_INT);

		inhdb = clone_object(BIGSTRUCT_MAP_OBJ);
		inhdb->claim();
		inhdb->set_type(T_INT);

		incdb = clone_object(BIGSTRUCT_MAP_OBJ);
		incdb->claim();
		incdb->set_type(T_STRING);

		pathdb = clone_object(BIGSTRUCT_MAP_OBJ);
		pathdb->claim();
		pathdb->set_type(T_STRING);
	}
}

atomic void destruct_database()
{
	ACCESS_CHECK(SYSTEM());

	rlimits (0; -1) {
		int i, sz;
		object *turkeys;

		turkeys = ({ });

		if (progdb) {
			turkeys += ({ progdb });
		}

		if (inhdb) {
			turkeys += ({ inhdb });
		}

		if (incdb) {
			turkeys += ({ incdb });
		}

		if (pathdb) {
			turkeys += ({ pathdb });
		}

		progdb = nil;
		inhdb = nil;
		incdb = nil;
		pathdb = nil;

		sz = sizeof(turkeys);

		for (i = 0; i < sz; i++) {
			destruct_object(turkeys[i]);
		}
	}
}

private void deindex_inherits(int oindex, int *inh)
{
	int sz;

	ASSERT(inhdb);

	for (sz = sizeof(inh); --sz >= 0; ) {
		object submap;
		submap = inhdb->query_element(inh[sz]);
		submap->set_element(oindex, nil);
	}
}

private void deindex_includes(int oindex, string *inc)
{
	int sz;

	ASSERT(incdb);

	for (sz = sizeof(inc); --sz >= 0; ) {
		object submap;
		submap = incdb->query_element(inc[sz]);
		submap->set_element(oindex, nil);
	}
}

private void index_inherits(int oindex, int *inh)
{
	int sz;

	ASSERT(inhdb);

	for (sz = sizeof(inh); --sz >= 0; ) {
		object submap;
		submap = inhdb->query_element(inh[sz]);

		if (!submap) {
			inhdb->set_element(inh[sz], submap = new_object(BIGSTRUCT_MAP_LWO));
			submap->claim();
			submap->set_type(T_INT);
		}

		submap->set_element(oindex, 1);
	}
}

private void index_includes(int oindex, string *inc)
{
	int sz;

	ASSERT(incdb);

	for (sz = sizeof(inc); --sz >= 0; ) {
		object submap;
		submap = incdb->query_element(inc[sz]);

		if (!submap) {
			incdb->set_element(inc[sz], submap = new_object(BIGSTRUCT_MAP_LWO));
			submap->claim();
			submap->set_type(T_INT);
		}

		submap->set_element(oindex, 1);
	}
}

/* objectd hooks */

atomic object register_program(string path, string *inherits, string *includes)
{
	int oindex;
	object pinfo;

	int *oinherits;
	string *oincludes;

	ACCESS_CHECK(previous_program() == OBJECTD);

	if (!progdb) {
		LOGD->post_message("system", LOG_WARNING, "Skipping registration of " + path + ", no progdb");

		return nil;
	}

	oindex = status(path, O_INDEX);

	if (pathdb) {
		pathdb->set_element(path, oindex);
	}

	pinfo = progdb->query_element(oindex);

	if (!pinfo) {
		pinfo = new_object(PROGRAM_INFO);
		pinfo->set_path(path);

		progdb->set_element(oindex, pinfo);
	}

	oinherits = pinfo->query_inherits();
	oincludes = pinfo->query_includes();
{'fraction_non_alphanumeric': 0.14586448866904406, 'fraction_numerical': 0.0036811227424364433, 'mean_word_length': 3.9566704675028506, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 96, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5425041', 'n_tokens_mistral': 3462, 'n_tokens_neox': 3163, 'n_words': 796}