<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Rainbow.Indexing;
using Rainbow.Model;
using Rainbow.Storage.Pathing;
using Rainbow.Storage.Yaml.Formatting;
using Rainbow.Storage.Yaml.Indexing;
using Sitecore.StringExtensions;

namespace Rainbow.Storage.Yaml
{
	public class YamlSerializationDataStore : IndexedDataStore
	{
		private readonly string _rootPath;
		private readonly IFileSystemPathProvider _pathProvider;
		private readonly YamlSerializationFormatter _formatter;

		public YamlSerializationDataStore(string rootPath, IFileSystemPathProvider pathProvider, YamlSerializationFormatter formatter)
			: base(new YamlFrontMatterIndexFactory(rootPath, pathProvider))
		{
			_rootPath = rootPath;
			_pathProvider = pathProvider;
			_formatter = formatter;
		}

		public override IEnumerable<string> GetDatabaseNames()
		{
			return _pathProvider.GetAllStoredDatabaseNames(_rootPath);
		}

		public override void Save(ISerializableItem item)
		{
			var path = _pathProvider.GetStoragePath(new IndexEntry().LoadFrom(item), item.DatabaseName, _rootPath);

			Directory.CreateDirectory(Path.GetDirectoryName(path));

			using (var writer = File.OpenWrite(path))
			{
				_formatter.WriteSerializedItem(item, writer);
			}

			GetIndexForDatabase(item.DatabaseName).Update(new IndexEntry().LoadFrom(item));
		}

		public override void CheckConsistency(string database, bool fixErrors, Action<string> logMessageReceiver)
		{
			// TODO: consistency check<commit_msg>Refactor to support constructor injection<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Rainbow.Formatting;
using Rainbow.Indexing;
using Rainbow.Model;
using Rainbow.Storage.Pathing;
using Rainbow.Storage.Yaml.Formatting;
using Rainbow.Storage.Yaml.Indexing;
using Sitecore.Diagnostics;
using Sitecore.StringExtensions;

namespace Rainbow.Storage.Yaml
{
	public class YamlSerializationDataStore : IndexedDataStore
	{
		private readonly string _rootPath;
		private readonly IFileSystemPathProvider _pathProvider;
		private readonly YamlSerializationFormatter _formatter;

		public YamlSerializationDataStore(string rootPath, IFileSystemPathProvider pathProvider, ISerializationFormatter formatter)
			: base(new YamlFrontMatterIndexFactory(rootPath, pathProvider))
		{
			Assert.ArgumentNotNullOrEmpty(rootPath, "rootPath");
			Assert.ArgumentNotNull(pathProvider, "pathProvider");
			Assert.ArgumentNotNull(formatter, "formatter");

			_rootPath = rootPath;
			_pathProvider = pathProvider;
			_formatter = (YamlSerializationFormatter)formatter;
		}

		public override IEnumerable<string> GetDatabaseNames()
		{
			return _pathProvider.GetAllStoredDatabaseNames(_rootPath);
		}

		public override void Save(ISerializableItem item)
		{
			var path = _pathProvider.GetStoragePath(new IndexEntry().LoadFrom(item), item.DatabaseName, _rootPath);

			Directory.CreateDirectory(Path.GetDirectoryName(path));

			using (var writer = File.OpenWrite(path))
			{
				_formatter.WriteSerializedItem(item, writer);
			}

			GetIndexForDatabase(item.DatabaseName).Update(new IndexEntry().LoadFrom(item));
		}

		public override void CheckConsistency(string database, bool fixErrors, Action<string> logMessageReceiver)
		{
			// TODO: consistency check
{'fraction_non_alphanumeric': 0.08278145695364239, 'fraction_numerical': 0.0, 'mean_word_length': 6.130901287553648, 'pattern_counts': {'":': 0, '<': 7, '<?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': '6716196', 'n_tokens_mistral': 1031, 'n_tokens_neox': 917, 'n_words': 197}