<commit_before>﻿using System;
using System.Linq;
using Gibson;
using Gibson.Data;
using Gibson.Indexing;
using Gibson.Model;
using Sitecore.Configuration;
using Sitecore.Diagnostics;
using Unicorn.Data;
using Unicorn.Predicates;
using Unicorn.Serialization;

namespace Unicorn.Gibson
{
	public class GibsonSerializationProvider : ISerializationProvider
	{
		private readonly SerializationStore _store;
		private readonly GibsonDeserializer _deserializer;
		private readonly string _logName;

		public GibsonSerializationProvider(GibsonDeserializer deserializer, IPredicate predicate, string rootPath = null, string logName = "GibsonItemSerialization")
		{
			// TODO: predicate is here so we can ignore moving descendants to ignored locations
			// TODO: gibson doesnt know about databases. seems like we should allow this to hold more than one gib store for multiple DBs and abstract the root path junk?
			// TODO: 'default' root path semantic not supported
			// TODO: unicorn reserialize does not delete existing gib folders
			// TODO: work out dependencies handling (in general, also registering serialization formatters?)
			// TODO: reconcile deserializer with main project and formatters in unicorn - incl logging TODOs
			// TODO: non-transactional provider for writing? with index flushing?
			// TODO: field predicate-like functionality - maybe in SerializableItem as a dependency you can inject? (note: revision and such are being serialized now)

			_store = new TransactionalFileSystemSerializationStore(rootPath, new PathProvider(), new JsonSerializationFormatter(), new LineOrientedIndexFormatter(), new Index());
			_deserializer = deserializer;
			_logName = logName;
		}

		public string LogName
		{
			get { return _logName; }
		}

		public ISerializedItem SerializeItem(ISourceItem item)
		{
			Assert.ArgumentNotNull(item, "item");

			var sitecoreSourceItem = item as SitecoreSourceItem;

			var sitecoreItem = sitecoreSourceItem != null ? sitecoreSourceItem.InnerItem : Factory.GetDatabase(item.DatabaseName).GetItem(item.Id);

			Assert.IsNotNull(sitecoreItem, "Item to serialize did not exist!");

			var serializable = new SerializableItem(sitecoreItem);

			_store.Save(serializable);

			return new GibsonSerializedItem(serializable, _deserializer, _store);
		}<commit_msg>Add a bunch of todo notes<commit_after>﻿using System;
using System.Linq;
using Gibson;
using Gibson.Data;
using Gibson.Indexing;
using Gibson.Model;
using Sitecore.Configuration;
using Sitecore.Diagnostics;
using Unicorn.Data;
using Unicorn.Predicates;
using Unicorn.Serialization;

namespace Unicorn.Gibson
{
	public class GibsonSerializationProvider : ISerializationProvider
	{
		private readonly SerializationStore _store;
		private readonly GibsonDeserializer _deserializer;
		private readonly string _logName;

		public GibsonSerializationProvider(GibsonDeserializer deserializer, IPredicate predicate, string rootPath = null, string logName = "GibsonItemSerialization")
		{
			// TODO: predicate is here so we can ignore moving descendants to ignored locations
			// TODO: gibson doesnt know about databases. seems like we should allow this to hold more than one gib store for multiple DBs and abstract the root path junk?
			// TODO: 'default' root path semantic not supported
			// TODO: unicorn reserialize does not delete existing gib folders
			// TODO: work out dependencies handling (in general, also registering serialization formatters?)
			// TODO: reconcile deserializer with main project and formatters in unicorn - incl logging TODOs
			// TODO: non-transactional provider for writing? with index flushing?
			// TODO: field predicate-like functionality - maybe in SerializableItem as a dependency you can inject? (note: revision and such are being serialized now)
			// TODO: of note: merging the json is nice except the lack of field names adds additional complexity that is not required
			// TODO: blob storage?
			// TODO: evaluate reducing verbosity of emitted JSON, with shorter identifiers?
			// TODO: evaluate reducing index verbosity with single letter prefixes?
			// TODO: formatting of security field values?
			// TODO: having field type on all the JSON field values is real verbose. maybe omit if it wasnt formatted?
			// TODO: extend unicorn data provider model to allow optional dp features like consistency checks, updating items with a field when deleting a t-field


			_store = new TransactionalFileSystemSerializationStore(rootPath, new PathProvider(), new JsonSerializationFormatter(), new LineOrientedIndexFormatter(), new Index());
			_deserializer = deserializer;
			_logName = logName;
		}

		public string LogName
		{
			get { return _logName; }
		}

		public ISerializedItem SerializeItem(ISourceItem item)
		{
			Assert.ArgumentNotNull(item, "item");

			var sitecoreSourceItem = item as SitecoreSourceItem;

			var sitecoreItem = sitecoreSourceItem != null ? sitecoreSourceItem.InnerItem : Factory.GetDatabase(item.DatabaseName).GetItem(item.Id);

			Assert.IsNotNull(sitecoreItem, "Item to serialize did not exist!");

			var serializable = new SerializableItem(sitecoreItem);

			_store.Save(serializable);

			return new GibsonSerializedItem(serializable, _deserializer, _store);
		}
{'fraction_non_alphanumeric': 0.0662087387903072, 'fraction_numerical': 0.0, 'mean_word_length': 4.811529933481153, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '819649', 'n_tokens_mistral': 1511, 'n_tokens_neox': 1324, 'n_words': 528}