<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#if TEST_WINPHONE || WINDOWS_PHONE
using Microsoft.Phone.Reactive;
using Microsoft.Phone.Info;
using System.Threading;
using System.Net;
using System.IO;
using System.IO.IsolatedStorage;
using System.Reflection;
using System.Windows;
#else
using System.Threading.Tasks;
using System.Net.Http;
using Windows.Globalization;
using Windows.Storage.Streams;
using Windows.System.Profile;
using Windows.ApplicationModel;
#endif

namespace TapstreamMetrics.Sdk
{
    class PlatformImpl : Platform
    {
        private const string FIRED_EVENTS_KEY = "__tapstream_sdk_fired_events";
        private const string UUID_KEY = "__tapstream_sdk_uuid";

        public string LoadUuid()
        {
#if TEST_WINPHONE || WINDOWS_PHONE
            string guid = null;
            if(IsolatedStorageSettings.ApplicationSettings.TryGetValue<string>(UUID_KEY, out guid))
            {
                return guid;
            }
            guid = Guid.NewGuid().ToString();
            IsolatedStorageSettings.ApplicationSettings[UUID_KEY] = guid;
            IsolatedStorageSettings.ApplicationSettings.Save();
            return guid;
#else
            Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            if(localSettings.Values.ContainsKey(UUID_KEY))
            {
                return (string)localSettings.Values[UUID_KEY];
            }
            string guid = Guid.NewGuid().ToString();
            localSettings.Values[UUID_KEY] = guid;
            return guid;            
#endif
        }

        public HashSet<string> LoadFiredEvents()
        {
#if TEST_WINPHONE || WINDOWS_PHONE
            HashSet<string> firedEvents = new HashSet<string>();
            Dictionary<string, bool> contents;
            if(IsolatedStorageSettings.ApplicationSettings.TryGetValue<Dictionary<string, bool>>(FIRED_EVENTS_KEY, out contents))
            {
                firedEvents.dict = contents;
            }
            return firedEvents;
#else
            Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            if(!localSettings.Values.ContainsKey(FIRED_EVENTS_KEY))
            {
                return new HashSet<string>();
            }
            return new HashSet<string>((string[])localSettings.Values[FIRED_EVENTS_KEY]);
#endif
        }

        public void SaveFiredEvents(HashSet<string> firedEvents)
        {
#if TEST_WINPHONE || WINDOWS_PHONE
            IsolatedStorageSettings.ApplicationSettings[FIRED_EVENTS_KEY] = firedEvents.dict;
            IsolatedStorageSettings.ApplicationSettings.Save();
#else
            Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            localSettings.Values[FIRED_EVENTS_KEY] = firedEvents.ToArray();
#endif
        }


        public string GetResolution()
        {
#if TEST_WINPHONE || WINDOWS_PHONE
            int w = (int)System.Windows.Application.Current.Host.Content.ActualWidth;
            int h = (int)System.Windows.Application.Current.Host.Content.ActualHeight;
            return String.Format("{0}x{1}", w, h);
#else
            return null;
#endif
        }
<commit_msg>Protect settings save with mutex on windows phone<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#if TEST_WINPHONE || WINDOWS_PHONE
using Microsoft.Phone.Reactive;
using Microsoft.Phone.Info;
using System.Threading;
using System.Net;
using System.IO;
using System.IO.IsolatedStorage;
using System.Reflection;
using System.Windows;
#else
using System.Threading.Tasks;
using System.Net.Http;
using Windows.Globalization;
using Windows.Storage.Streams;
using Windows.System.Profile;
using Windows.ApplicationModel;
#endif

namespace TapstreamMetrics.Sdk
{
    class PlatformImpl : Platform
    {
        private const string FIRED_EVENTS_KEY = "__tapstream_sdk_fired_events";
        private const string UUID_KEY = "__tapstream_sdk_uuid";

#if TEST_WINPHONE || WINDOWS_PHONE
        private static Mutex storageMutex = new Mutex(false, null);
#endif

        public string LoadUuid()
        {
#if TEST_WINPHONE || WINDOWS_PHONE
            string guid = null;

            storageMutex.WaitOne();
            try
            {
                if (IsolatedStorageSettings.ApplicationSettings.TryGetValue<string>(UUID_KEY, out guid))
                {
                    return guid;
                }
                guid = Guid.NewGuid().ToString();
                IsolatedStorageSettings.ApplicationSettings[UUID_KEY] = guid;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
            finally
            {
                storageMutex.ReleaseMutex();
            }
            return guid;
#else
            Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            if(localSettings.Values.ContainsKey(UUID_KEY))
            {
                return (string)localSettings.Values[UUID_KEY];
            }
            string guid = Guid.NewGuid().ToString();
            localSettings.Values[UUID_KEY] = guid;
            return guid;            
#endif
        }

        public HashSet<string> LoadFiredEvents()
        {
#if TEST_WINPHONE || WINDOWS_PHONE
            HashSet<string> firedEvents = new HashSet<string>();
            Dictionary<string, bool> contents;

            storageMutex.WaitOne();
            try
            {
                if (IsolatedStorageSettings.ApplicationSettings.TryGetValue<Dictionary<string, bool>>(FIRED_EVENTS_KEY, out contents))
                {
                    firedEvents.dict = contents;
                }
            }
            finally
            {
                storageMutex.ReleaseMutex();
            }
            return firedEvents;
#else
            Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            if(!localSettings.Values.ContainsKey(FIRED_EVENTS_KEY))
            {
                return new HashSet<string>();
            }
            return new HashSet<string>((string[])localSettings.Values[FIRED_EVENTS_KEY]);
#endif
        }

        public void SaveFiredEvents(HashSet<string> firedEvents)
        {
#if TEST_WINPHONE || WINDOWS_PHONE
            storageMutex.WaitOne();
            try
            {
                IsolatedStorageSettings.ApplicationSettings[FIRED_EVENTS_KEY] = firedEvents.dict;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
            finally
            {
                storageMutex.ReleaseMutex();
            }
#else
            Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            localSettings.Values[FIRED_EVENTS_KEY] = firedEvents.ToArray();
#endif
        }


        public string GetResolution()
        {
#if TEST_WINPHONE || WINDOWS_PHONE
            int w = (int)System.Windows.Application.Current.Host.Content.ActualWidth;
            int h = (int)System.Windows.Application.Current.Host.Content.ActualHeight;
            return String.Format("{0}x{1}", w, h);
#else
            return null;
#endif
        }

{'fraction_non_alphanumeric': 0.08632801945420157, 'fraction_numerical': 0.0005403944879762226, 'mean_word_length': 2.3558476881233, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '4484237', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1928, 'n_words': 356}