<commit_before>﻿using System;
using Microsoft.Azure.Documents.Client;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Azure.Documents;
using System.Threading.Tasks;
using CommandTool.DocumentDb;
using Newtonsoft.Json;

namespace CommandTool
{
    public class PatientStore : IDisposable
    {
        static PatientStore()
        {
            JsonConvert.DefaultSettings = () =>
            {
                return new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                };
            };
        }

        public PatientStore(IConfiguration configuration)
        {
            _client = new DocumentClient(
                new Uri(configuration.Get("DbUri")),
                configuration.Get("DbToken"));
        }

        public async Task<DocumentCollection> Setup()
        {
            Console.WriteLine("Steup");
            var db = new Database() { Id = "PatientDb" };
            var collection = new DocumentCollection { Id = "Patients" };

            _database = await _client.ReadDatabaseAsync(db.SelfLink);
            if (_database == null)
            {
                _database = await _client.CreateDatabaseAsync(new Database { Id = "PatientDb" });
            }
            _collection = await _client.ReadDocumentCollectionAsync(_database.CollectionsLink);
            if (_collection == null)
            {
                _collection = await _client.CreateDocumentCollectionAsync(_database.CollectionsLink, collection);
            }

            Console.WriteLine("Setup complete");
            return _collection;

        }

        public async Task<Database> TearDown()
        {
            var result = await _client.DeleteDatabaseAsync(_database.SelfLink);
            return result;
        }

        public void Dispose()
        {
            if (_client != null)
            {
                _client.Dispose();
            }
        }

        public async Task<long> Save(Patient patient)
        {
            var response = await _client.CreateDocumentAsync(_collection.DocumentsLink, patient);
            return response.DocumentUsage;
        }
<commit_msg>read or create database and collection<commit_after>﻿using System;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Azure.Documents;
using System.Threading.Tasks;
using CommandTool.DocumentDb;
using Newtonsoft.Json;
using System.Linq;

namespace CommandTool
{
    public class PatientStore : IDisposable
    {
        static PatientStore()
        {
            JsonConvert.DefaultSettings = () =>
            {
                return new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                };
            };
        }

        public PatientStore(IConfiguration configuration)
        {
            _client = new DocumentClient(
                new Uri(configuration.Get("DbUri")),
                configuration.Get("DbToken"));
        }

        public async Task<DocumentCollection> Setup()
        {
            Console.WriteLine("Steup");

            var databaseId = "PatientDb";
            var collectionId = "Patients";

            _database = _client.CreateDatabaseQuery()
                               .Where(d => d.Id == databaseId)
                               .AsEnumerable()
                               .FirstOrDefault();         
            if (_database == null)
            {
                _database = await _client.CreateDatabaseAsync(new Database { Id = databaseId });
            }
   
            _collection = _client.CreateDocumentCollectionQuery(_database.CollectionsLink)
                                 .Where(c => c.Id == collectionId)
                                 .AsEnumerable()
                                 .FirstOrDefault();           
            if (_collection == null)
            {
                _collection = await _client.CreateDocumentCollectionAsync(_database.CollectionsLink, new DocumentCollection { Id = collectionId } );
            }

            Console.WriteLine("Setup complete");
            return _collection;

        }

        public async Task<Database> TearDown()
        {
            var result = await _client.DeleteDatabaseAsync(_database.SelfLink);
            return result;
        }

        public void Dispose()
        {
            if (_client != null)
            {
                _client.Dispose();
            }
        }

        public async Task<long> Save(Patient patient)
        {
            var response = await _client.CreateDocumentAsync(_collection.DocumentsLink, patient);
            return response.DocumentUsage;
        }

{'fraction_non_alphanumeric': 0.08145363408521303, 'fraction_numerical': 0.0, 'mean_word_length': 1.635663181067694, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4660763', 'n_tokens_mistral': 1108, 'n_tokens_neox': 1052, 'n_words': 232}