<commit_before>/**
 * dbh.js
 */
module.exports = function(conf, main){
	delete(require.cache[require('path').resolve(__filename)]);
	var fs = require('fs');
	var fsX = require('fs-extra');
	var utils79 = require('utils79');
	var it79 = require('iterate79');
	var Sequelize = require('sequelize');
	var sqlite = require('sqlite3');
	var tbls = {};

	/**
	 * DBを初期化
	 * @param  {Function} callback [description]
	 * @return {Void}              void.
	 */
	this.initDb = function(callback){
		callback = callback || function(){};

		var dbPath = require('path').resolve(conf.db.storage);
		// console.log(dbPath);

		var sequelize = new Sequelize(undefined, undefined, undefined, {
			'dialect': 'sqlite',
			'connection': new sqlite.Database( dbPath ),
			'storage': dbPath
		});

		tbls.board = sequelize.define(conf.db.tablePrefix+'-board',
			{
				'boardId': { type: Sequelize.STRING, primaryKey: true, allowNull: false },
				'title': { type: Sequelize.STRING },
				'owner': { type: Sequelize.STRING },
				'opened': { type: Sequelize.INTEGER },
				'microtime': { type: Sequelize.BIGINT }
			}
		);
		tbls.timeline = sequelize.define(conf.db.tablePrefix+'-timeline',
			{
				'id': { type: Sequelize.STRING, primaryKey: true, autoIncrement: false, allowNull: false },
				'boardId': {
					type: Sequelize.STRING,
					unique: 'boardMessageUniqueKey',
					allowNull: false,
					references: {
						// This is a reference to another model
						model: tbls.board,
						key: 'boardId'
					}
				},
				'boardMessageId': { type: Sequelize.BIGINT, unique: 'boardMessageUniqueKey', allowNull: false },
				'content': { type: Sequelize.STRING },
				'contentType': { type: Sequelize.STRING },
				'targetWidget': { type: Sequelize.STRING },
				'owner': { type: Sequelize.STRING },
				'connectionId': { type: Sequelize.STRING },
				'microtime': { type: Sequelize.BIGINT }
			}
		);
		tbls.files = sequelize.define(conf.db.tablePrefix+'-files',
			{
				'fileId': { type: Sequelize.STRING, primaryKey: true, allowNull: false },
				'boardId': {
					type: Sequelize.STRING,
					allowNull: false,
					references: {
						// This is a reference to another model
						model: tbls.board,
						key: 'boardId'
					}
				},
				'filename': { type: Sequelize.STRING },
				'size': { type: Sequelize.INTEGER },
				'type': { type: Sequelize.STRING },
				'base64': { type: Sequelize.TEXT },
				'owner': { type: Sequelize.STRING },
				'microtime': { type: Sequelize.BIGINT }
			}
		);
		sequelize.sync()
			.then(function(){
				callback(true);
			})
		;

		return;
	}

	/**
	 * ボードIDを分解する
	 */
	this.divideBoardId = function(boardId){
		// str.substring(0,2); // 2文字ずつとりだす
		var ary = utils79.divide(boardId, 2);
		return ary.join('/');
	}

	/**
	 * ボードデータの格納パス名を取得する
	 */
	this.getPathBoardDataDir = function(boardId){
		var path = require('path').resolve(conf.dataDir, this.divideBoardId(boardId), '__data')+'/';
		return path;
	}

	/**
	 * 新しいボードを作成する
	 */
	this.createNewBoard = function(boardInfo, callback){
		callback = callback || function(){};
		this.initDb(function(){
			var retryCounter = 0;
			it79.fnc(
				{},
				{
					"insert": function(it1, data){
						retryCounter ++;
						if( retryCounter > 50 ){
							// 50回やっても成功しないなら、諦めて離脱する
							callback(false);
							return;
						}

						var newBoardId = utils79.md5( (+new Date())+'.'+(Math.random() * 10000)+'.'+(Math.random() * 10000) );

						tbls.board.create({
							'boardId': newBoardId,
							'title': boardInfo.title,
							'opened': 1,
							'owner': boardInfo.owner,
							'microtime': Date.now()
						}).then(function(record){
							// successful
							callback(newBoardId);
						}).catch(function(){
							// retry
							retryCounter ++;
							if( retryCounter > 100 ){
								callback(false);
								return;
							}
							it1.goto("insert", data);
						});<commit_msg>ボードメッセージに deletedFlag(削除済みフラグ)を追加<commit_after>/**
 * dbh.js
 */
module.exports = function(conf, main){
	delete(require.cache[require('path').resolve(__filename)]);
	var fs = require('fs');
	var fsX = require('fs-extra');
	var utils79 = require('utils79');
	var it79 = require('iterate79');
	var Sequelize = require('sequelize');
	var sqlite = require('sqlite3');
	var tbls = {};

	/**
	 * DBを初期化
	 * @param  {Function} callback [description]
	 * @return {Void}              void.
	 */
	this.initDb = function(callback){
		callback = callback || function(){};

		var dbPath = require('path').resolve(conf.db.storage);
		// console.log(dbPath);

		var sequelize = new Sequelize(undefined, undefined, undefined, {
			'dialect': 'sqlite',
			'connection': new sqlite.Database( dbPath ),
			'storage': dbPath
		});

		tbls.board = sequelize.define(conf.db.tablePrefix+'-board',
			{
				'boardId': { type: Sequelize.STRING, primaryKey: true, allowNull: false },
				'title': { type: Sequelize.STRING },
				'owner': { type: Sequelize.STRING },
				'openedFlag': { type: Sequelize.INTEGER },
				'microtime': { type: Sequelize.BIGINT }
			}
		);
		tbls.timeline = sequelize.define(conf.db.tablePrefix+'-timeline',
			{
				'id': { type: Sequelize.STRING, primaryKey: true, autoIncrement: false, allowNull: false },
				'boardId': {
					type: Sequelize.STRING,
					unique: 'boardMessageUniqueKey',
					allowNull: false,
					references: {
						// This is a reference to another model
						model: tbls.board,
						key: 'boardId'
					}
				},
				'boardMessageId': { type: Sequelize.BIGINT, unique: 'boardMessageUniqueKey', allowNull: false },
				'content': { type: Sequelize.STRING },
				'contentType': { type: Sequelize.STRING },
				'targetWidget': { type: Sequelize.STRING },
				'owner': { type: Sequelize.STRING },
				'connectionId': { type: Sequelize.STRING },
				'deletedFlag': { type: Sequelize.INTEGER },
				'microtime': { type: Sequelize.BIGINT }
			}
		);
		tbls.files = sequelize.define(conf.db.tablePrefix+'-files',
			{
				'fileId': { type: Sequelize.STRING, primaryKey: true, allowNull: false },
				'boardId': {
					type: Sequelize.STRING,
					allowNull: false,
					references: {
						// This is a reference to another model
						model: tbls.board,
						key: 'boardId'
					}
				},
				'boardMessageId': { type: Sequelize.BIGINT },
				'filename': { type: Sequelize.STRING },
				'size': { type: Sequelize.INTEGER },
				'type': { type: Sequelize.STRING },
				'base64': { type: Sequelize.TEXT },
				'owner': { type: Sequelize.STRING },
				'microtime': { type: Sequelize.BIGINT }
			}
		);
		sequelize.sync()
			.then(function(){
				callback(true);
			})
		;

		return;
	}

	/**
	 * ボードIDを分解する
	 */
	this.divideBoardId = function(boardId){
		// str.substring(0,2); // 2文字ずつとりだす
		var ary = utils79.divide(boardId, 2);
		return ary.join('/');
	}

	/**
	 * ボードデータの格納パス名を取得する
	 */
	this.getPathBoardDataDir = function(boardId){
		var path = require('path').resolve(conf.dataDir, this.divideBoardId(boardId), '__data')+'/';
		return path;
	}

	/**
	 * 新しいボードを作成する
	 */
	this.createNewBoard = function(boardInfo, callback){
		callback = callback || function(){};
		this.initDb(function(){
			var retryCounter = 0;
			it79.fnc(
				{},
				{
					"insert": function(it1, data){
						retryCounter ++;
						if( retryCounter > 50 ){
							// 50回やっても成功しないなら、諦めて離脱する
							callback(false);
							return;
						}

						var newBoardId = utils79.md5( (+new Date())+'.'+(Math.random() * 10000)+'.'+(Math.random() * 10000) );

						tbls.board.create({
							'boardId': newBoardId,
							'title': boardInfo.title,
							'openedFlag': 1,
							'owner': boardInfo.owner,
							'microtime': Date.now()
						}).then(function(record){
							// successful
							callback(newBoardId);
						}).catch(function(){
							// retry
							retryCounter ++;
							if( retryCounter > 100 ){
								callback(false);
								return;
							}
							it1.goto("insert", data);
						});
{'fraction_non_alphanumeric': 0.16840233680467362, 'fraction_numerical': 0.010922021844043688, 'mean_word_length': 2.90625, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6361729', 'n_tokens_mistral': 3561, 'n_tokens_neox': 2747, 'n_words': 517}