<commit_before>﻿namespace SharpMongo.Core
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    public class Collection
    {
        private string name;
        private IList<DynamicDocument> documents = new List<DynamicDocument>();
        private IDictionary<object, DynamicDocument> documentsbyid = new Dictionary<object, DynamicDocument>();

        public Collection(string name)
        {
            this.name = name;
        }

        public string Name { get { return this.name; } }

        public void Insert(DynamicDocument document)
        {
            Guid id = Guid.NewGuid();
            document.Id = id;
            document.Seal();
            this.documents.Add(document);
            this.documentsbyid[id] = document;
        }

        public void Save(DynamicDocument document)
        {
            DynamicDocument original = null;

            if (document.Id != null && this.documentsbyid.ContainsKey(document.Id))
                original = this.documentsbyid[document.Id];

            if (original == null)
            {
                if (document.Id == null) 
                {
                    Guid id = Guid.NewGuid();
                    document.Id = id;
                }

                document.Seal();
                this.documents.Add(document);
                this.documentsbyid[document.Id] = document;
            }
            else
            {
                this.documents.Remove(original);
                this.documents.Add(document);
                this.documentsbyid[document.Id] = document;
            }
        }

        public DynamicDocument GetDocument(Guid id)
        {
            if (!this.documentsbyid.ContainsKey(id))
                return null;

            return this.documentsbyid[id];
        }

        public DynamicDocument FindOne(DynamicObject query = null, DynamicObject projection = null)
        {
            return this.Find(query, projection).FirstOrDefault();
        }

        public IEnumerable<DynamicDocument> Find(DynamicObject query = null, DynamicObject projection = null)
        {
            if (projection != null)
            {
                if (query == null)
                    foreach (var document in this.documents)
                        yield return (DynamicDocument)document.Project(projection);
                else
                    foreach (var document in this.documents)
                        if (query.Match(document))
                            yield return (DynamicDocument)document.Project(projection);
            }
            else
            {
                if (query == null)
                    foreach (var document in this.documents)
                        yield return document;
                else
                    foreach (var document in this.documents)
                        if (query.Match(document))
                            yield return document;
            }
        }

        public IEnumerable<DynamicObject> Aggregate(DynamicObject spec = null)
        {
            DynamicObject query = null;
            DynamicObject projection = null;

            if (spec != null && spec.Exists("$match"))
                query = (DynamicObject)spec.GetMember("$match");

            if (spec != null && spec.Exists("$project"))
                projection = (DynamicObject)spec.GetMember("$project");

            IEnumerable<DynamicObject> result = this.Find(query, projection);

            if (spec != null && spec.Exists("$skip"))
            {
                int n = (int)spec.GetMember("$skip");

                result = result.Skip(n);
            }

            if (spec != null && spec.Exists("$limit"))
            {
                int n = (int)spec.GetMember("$limit");

                result = result.Take(n);
            }

            return result;
        }

        public void Update(DynamicObject query, DynamicObject update, bool multi = false)
        {
            foreach (var document in this.Find(query))
            {
                document.Update(update, true);

                if (!multi)
                    return;
            }
        }

        public void Remove(DynamicObject query = null, bool justone = false)
        {
            IList<object> toremove = new List<object>();

            foreach (var document in this.Find(query))
            {
                toremove.Add(document.Id);

                if (justone)
                    break;
            }

            foreach (var id in toremove)
            {
                var document = this.documentsbyid[id];
                this.documents.Remove(document);
                this.documentsbyid.Remove(id);
            }
        }

        public int Count(DynamicObject query = null)
        {
            if (query != null)
                return this.Find(query).Count();

            return this.documents.Count;
        }

        public IEnumerable<object> Distinct(string field, DynamicObject query = null)
        {
            return this.Find(query).Select(d => d.GetMember(field)).Distinct();
        }
    }
}
<commit_msg>Sort by one ascending field<commit_after>﻿namespace SharpMongo.Core
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    public class Collection
    {
        private string name;
        private IList<DynamicDocument> documents = new List<DynamicDocument>();
        private IDictionary<object, DynamicDocument> documentsbyid = new Dictionary<object, DynamicDocument>();

        public Collection(string name)
        {
            this.name = name;
        }

        public string Name { get { return this.name; } }

        public void Insert(DynamicDocument document)
        {
            Guid id = Guid.NewGuid();
            document.Id = id;
            document.Seal();
            this.documents.Add(document);
            this.documentsbyid[id] = document;
        }

        public void Save(DynamicDocument document)
        {
            DynamicDocument original = null;

            if (document.Id != null && this.documentsbyid.ContainsKey(document.Id))
                original = this.documentsbyid[document.Id];

            if (original == null)
            {
                if (document.Id == null) 
                {
                    Guid id = Guid.NewGuid();
                    document.Id = id;
                }

                document.Seal();
                this.documents.Add(document);
                this.documentsbyid[document.Id] = document;
            }
            else
            {
                this.documents.Remove(original);
                this.documents.Add(document);
                this.documentsbyid[document.Id] = document;
            }
        }

        public DynamicDocument GetDocument(Guid id)
        {
            if (!this.documentsbyid.ContainsKey(id))
                return null;

            return this.documentsbyid[id];
        }

        public DynamicDocument FindOne(DynamicObject query = null, DynamicObject projection = null)
        {
            return this.Find(query, projection).FirstOrDefault();
        }

        public IEnumerable<DynamicDocument> Find(DynamicObject query = null, DynamicObject projection = null)
        {
            if (projection != null)
            {
                if (query == null)
                    foreach (var document in this.documents)
                        yield return (DynamicDocument)document.Project(projection);
                else
                    foreach (var document in this.documents)
                        if (query.Match(document))
                            yield return (DynamicDocument)document.Project(projection);
            }
            else
            {
                if (query == null)
                    foreach (var document in this.documents)
                        yield return document;
                else
                    foreach (var document in this.documents)
                        if (query.Match(document))
                            yield return document;
            }
        }

        public IEnumerable<DynamicObject> Aggregate(DynamicObject spec = null)
        {
            DynamicObject query = null;
            DynamicObject projection = null;

            if (spec != null && spec.Exists("$match"))
                query = (DynamicObject)spec.GetMember("$match");

            if (spec != null && spec.Exists("$project"))
                projection = (DynamicObject)spec.GetMember("$project");

            IEnumerable<DynamicObject> result = this.Find(query, projection);

            if (spec != null && spec.Exists("$skip"))
            {
                int n = (int)spec.GetMember("$skip");

                result = result.Skip(n);
            }

            if (spec != null && spec.Exists("$limit"))
            {
                int n = (int)spec.GetMember("$limit");

                result = result.Take(n);
            }

            if (spec != null && spec.Exists("$sort"))
            {
                DynamicObject sspec = (DynamicObject)spec.GetMember("$sort");
                String fldname = sspec.GetMemberNames().First();

                return result.OrderBy(dobj => dobj.GetMember(fldname));
            }

            return result;
        }

        public void Update(DynamicObject query, DynamicObject update, bool multi = false)
        {
            foreach (var document in this.Find(query))
            {
                document.Update(update, true);

                if (!multi)
                    return;
            }
        }

        public void Remove(DynamicObject query = null, bool justone = false)
        {
            IList<object> toremove = new List<object>();

            foreach (var document in this.Find(query))
            {
                toremove.Add(document.Id);

                if (justone)
                    break;
            }

            foreach (var id in toremove)
            {
                var document = this.documentsbyid[id];
                this.documents.Remove(document);
                this.documentsbyid.Remove(id);
            }
        }

        public int Count(DynamicObject query = null)
        {
            if (query != null)
                return this.Find(query).Count();

            return this.documents.Count;
        }

        public IEnumerable<object> Distinct(string field, DynamicObject query = null)
        {
            return this.Find(query).Select(d => d.GetMember(field)).Distinct();
        }
    }
}

{'fraction_non_alphanumeric': 0.09145618674204431, 'fraction_numerical': 0.0, 'mean_word_length': 1.2912053478170045, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 26, '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': '4311412', 'n_tokens_mistral': 2739, 'n_tokens_neox': 2440, 'n_words': 579}