<commit_before>﻿// Copyright (c) 2011 - <NAME> & <NAME>
//
// This file is provided to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file
// except in compliance with the License.  You may obtain
// a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using CorrugatedIron.Containers;
using CorrugatedIron.Extensions;
using CorrugatedIron.Messages;
using CorrugatedIron.Models.CommitHook;
using CorrugatedIron.Models.Rest;
using CorrugatedIron.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace CorrugatedIron.Models
{
    public class RiakBucketProperties
    {
        // At the moment, only the NVal and AllowMult can be set via the PBC
        // so if the request has any other value set, we can't use that interface.
        // We check those values and if they're missing we go with PBC as it's
        // substantially quicker.
        public bool CanUsePbc
        {
            get
            {
                return !LastWriteWins.HasValue
                    && RVal == null
                    && RwVal == null
                    && DwVal == null
                    && WVal == null
                    && string.IsNullOrEmpty(Backend);
            }
        }

        public bool? LastWriteWins { get; private set; }
        public uint? NVal { get; private set; }
        public bool? AllowMultiple { get; private set; }
        public string Backend { get; private set; }
        public bool? Search { get; private set; }
        public List<IRiakPreCommitHook> PreCommitHooks { get; private set; }
        public List<IRiakPostCommitHook> PostCommitHooks { get; private set; }

        public Either<uint, string> RVal { get; private set; }
        public Either<uint, string> RwVal { get; private set; }
        public Either<uint, string> DwVal { get; private set; }
        public Either<uint, string> WVal { get; private set; }

        public RiakBucketProperties SetAllowMultiple(bool value)
        {
            AllowMultiple = value;
            return this;
        }

        public RiakBucketProperties SetLastWriteWins(bool value)
        {
            LastWriteWins = value;
            return this;
        }

        public RiakBucketProperties SetNVal(uint value)
        {
            NVal = value;
            return this;
        }

        public RiakBucketProperties SetRVal(uint value)
        {
            return WriteQuorum(value, v => RVal = v);
        }

        public RiakBucketProperties SetRVal(string value)
        {
            return WriteQuorum(value, v => RVal = v);
        }

        public RiakBucketProperties SetRwVal(uint value)
        {
            return WriteQuorum(value, v => RwVal = v);
        }

        public RiakBucketProperties SetRwVal(string value)
        {
            return WriteQuorum(value, v => RwVal = v);
        }

        public RiakBucketProperties SetDwVal(uint value)
        {
            return WriteQuorum(value, v => DwVal = v);
        }

        public RiakBucketProperties SetDwVal(string value)
        {
            return WriteQuorum(value, v => DwVal = v);
        }

        public RiakBucketProperties SetWVal(uint value)
        {
            return WriteQuorum(value, v => WVal = v);
        }

        public RiakBucketProperties SetWVal(string value)
        {
            return WriteQuorum(value, v => WVal = v);
        }

        public RiakBucketProperties SetBackend(string backend)
        {
            Backend = backend;
            return this;
        }
        
        public RiakBucketProperties SetSearch(bool search)
        {
            Search = search;
            return this;
        }

        public RiakBucketProperties AddPreCommitHook(IRiakPreCommitHook commitHook)
        {
            (PreCommitHooks ?? (PreCommitHooks = new List<IRiakPreCommitHook>())).Add(commitHook);
            return this;
        }

        public RiakBucketProperties AddPostCommitHook(IRiakPostCommitHook commitHook)
        {
            (PostCommitHooks ?? (PostCommitHooks = new List<IRiakPostCommitHook>())).Add(commitHook);
            return this;
        }

        public RiakBucketProperties ClearPreCommitHooks()
        {
            (PreCommitHooks ?? (PreCommitHooks = new List<IRiakPreCommitHook>())).Clear();
            return this;
        }

        public RiakBucketProperties ClearPostCommitHooks()
        {
            (PostCommitHooks ?? (PostCommitHooks = new List<IRiakPostCommitHook>())).Clear();
            return this;
        }

        private RiakBucketProperties WriteQuorum(uint value, Action<Either<uint, string>> setter)
        {
            System.Diagnostics.Debug.Assert(value >= 1);
            setter(new Either<uint, string>(value));
            return this;
        }

        private RiakBucketProperties WriteQuorum(string value, Action<Either<uint, string>> setter)
        {
            System.Diagnostics.Debug.Assert(new HashSet<string> { "all", "quorum", "one" }.Contains(value), "Incorrect quorum value");
            setter(new Either<uint, string>(value));
            return this;
        }

        public RiakBucketProperties()
        {
        }

        public RiakBucketProperties(RiakRestResponse response)
        {
            System.Diagnostics.Debug.Assert(response.ContentType == RiakConstants.ContentTypes.ApplicationJson);

            var json = JObject.Parse(response.Body);
            var props = (JObject)json["props"];
            NVal = props.Value<uint?>("n_val");
            AllowMultiple = props.Value<bool?>("allow_mult");
            LastWriteWins = props.Value<bool?>("last_write_wins");
            Backend = props.Value<string>("backend");
            Search = props.Value<bool?>("search");

            ReadQuorum(props, "r", v => RVal = v);
            ReadQuorum(props, "rw", v => RwVal = v);
            ReadQuorum(props, "dw", v => DwVal = v);
            ReadQuorum(props, "w", v => WVal = v);

            var preCommitHooks = props.Value<JArray>("precommit");
            if (preCommitHooks.Count > 0)
            {
                PreCommitHooks = preCommitHooks.Cast<JObject>().Select(LoadPreCommitHook).ToList();
            }
<commit_msg>Added pr, pw, notfound_ok, and basic_quorum to bucket properties<commit_after>﻿// Copyright (c) 2011 - <NAME> & <NAME>
//
// This file is provided to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file
// except in compliance with the License.  You may obtain
// a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using CorrugatedIron.Containers;
using CorrugatedIron.Extensions;
using CorrugatedIron.Messages;
using CorrugatedIron.Models.CommitHook;
using CorrugatedIron.Models.Rest;
using CorrugatedIron.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace CorrugatedIron.Models
{
    public class RiakBucketProperties
    {
        // At the moment, only the NVal and AllowMult can be set via the PBC
        // so if the request has any other value set, we can't use that interface.
        // We check those values and if they're missing we go with PBC as it's
        // substantially quicker.
        public bool CanUsePbc
        {
            get
            {
                return !LastWriteWins.HasValue
                    && RVal == null
                    && RwVal == null
                    && DwVal == null
                    && WVal == null
                    && PrVal == null
                    && PwVal == null
                    && string.IsNullOrEmpty(Backend);
            }
        }

        public bool? LastWriteWins { get; private set; }
        public uint? NVal { get; private set; }
        public bool? AllowMultiple { get; private set; }
        public string Backend { get; private set; }
        public bool? Search { get; private set; }
        public List<IRiakPreCommitHook> PreCommitHooks { get; private set; }
        public List<IRiakPostCommitHook> PostCommitHooks { get; private set; }
        public bool? NotFoundOk { get; private set; }
        public bool? BasicQuorum { get; private set; }

        public Either<uint, string> RVal { get; private set; }
        public Either<uint, string> RwVal { get; private set; }
        public Either<uint, string> DwVal { get; private set; }
        public Either<uint, string> WVal { get; private set; }
        public Either<uint, string> PrVal { get; private set; }
        public Either<uint, string> PwVal { get; private set; }
        
        public RiakBucketProperties SetBasicQuorum(bool value)
        {
            BasicQuorum = value;
            return this;
        }
        
        public RiakBucketProperties SetNotFoundOk(bool value)
        {
            NotFoundOk = value;
            return this;
        }

        public RiakBucketProperties SetAllowMultiple(bool value)
        {
            AllowMultiple = value;
            return this;
        }

        public RiakBucketProperties SetLastWriteWins(bool value)
        {
            LastWriteWins = value;
            return this;
        }

        public RiakBucketProperties SetNVal(uint value)
        {
            NVal = value;
            return this;
        }

        public RiakBucketProperties SetRVal(uint value)
        {
            return WriteQuorum(value, v => RVal = v);
        }

        public RiakBucketProperties SetRVal(string value)
        {
            return WriteQuorum(value, v => RVal = v);
        }

        public RiakBucketProperties SetRwVal(uint value)
        {
            return WriteQuorum(value, v => RwVal = v);
        }

        public RiakBucketProperties SetRwVal(string value)
        {
            return WriteQuorum(value, v => RwVal = v);
        }

        public RiakBucketProperties SetDwVal(uint value)
        {
            return WriteQuorum(value, v => DwVal = v);
        }

        public RiakBucketProperties SetDwVal(string value)
        {
            return WriteQuorum(value, v => DwVal = v);
        }

        public RiakBucketProperties SetWVal(uint value)
        {
            return WriteQuorum(value, v => WVal = v);
        }

        public RiakBucketProperties SetWVal(string value)
        {
            return WriteQuorum(value, v => WVal = v);
        }
        
        public RiakBucketProperties SetPrVal(string value)
        {
            return WriteQuorum(value, v => PrVal = v);
        }
        
        public RiakBucketProperties SetPrVal(int value)
        {
            return WriteQuorum(value, v => PrVal = v);
        }
        
        public RiakBucketProperties SetPwVal(string value)
        {
            return WriteQuorum(value, var => PwVal = var);
        }
        
        public RiakBucketProperties SetPwVal(int value)
        {
            return WriteQuorum(value, var => PwVal = var);
        }

        public RiakBucketProperties SetBackend(string backend)
        {
            Backend = backend;
            return this;
        }
        
        public RiakBucketProperties SetSearch(bool search)
        {
            Search = search;
            return this;
        }

        public RiakBucketProperties AddPreCommitHook(IRiakPreCommitHook commitHook)
        {
            (PreCommitHooks ?? (PreCommitHooks = new List<IRiakPreCommitHook>())).Add(commitHook);
            return this;
        }

        public RiakBucketProperties AddPostCommitHook(IRiakPostCommitHook commitHook)
        {
            (PostCommitHooks ?? (PostCommitHooks = new List<IRiakPostCommitHook>())).Add(commitHook);
            return this;
        }

        public RiakBucketProperties ClearPreCommitHooks()
        {
            (PreCommitHooks ?? (PreCommitHooks = new List<IRiakPreCommitHook>())).Clear();
            return this;
        }

        public RiakBucketProperties ClearPostCommitHooks()
        {
            (PostCommitHooks ?? (PostCommitHooks = new List<IRiakPostCommitHook>())).Clear();
            return this;
        }

        private RiakBucketProperties WriteQuorum(uint value, Action<Either<uint, string>> setter)
        {
            System.Diagnostics.Debug.Assert(value >= 1);
            setter(new Either<uint, string>(value));
            return this;
        }

        private RiakBucketProperties WriteQuorum(string value, Action<Either<uint, string>> setter)
        {
            System.Diagnostics.Debug.Assert(new HashSet<string> { "all", "quorum", "one" }.Contains(value), "Incorrect quorum value");
            setter(new Either<uint, string>(value));
            return this;
        }

        public RiakBucketProperties()
        {
        }

        public RiakBucketProperties(RiakRestResponse response)
        {
            System.Diagnostics.Debug.Assert(response.ContentType == RiakConstants.ContentTypes.ApplicationJson);

            var json = JObject.Parse(response.Body);
            var props = (JObject)json["props"];
            NVal = props.Value<uint?>("n_val");
            AllowMultiple = props.Value<bool?>("allow_mult");
            LastWriteWins = props.Value<bool?>("last_write_wins");
            Backend = props.Value<string>("backend");
            Search = props.Value<bool?>("search");
            NotFoundOk = props.Value<bool?>("notfound_ok");
            BasicQuorum = props.Value<bool?>("basic_quorum");

            ReadQuorum(props, "r", v => RVal = v);
            ReadQuorum(props, "rw", v => RwVal = v);
            ReadQuorum(props, "dw", v => DwVal = v);
            ReadQuorum(props, "w", v => WVal = v);

            var preCommitHooks = props.Value<JArray>("precommit");
            if (preCommitHooks.Count > 0)
            {
                PreCommitHooks = preCommitHooks.Cast<JObject>().Select(LoadPreCommitHook).ToList();
            }

{'fraction_non_alphanumeric': 0.09311989100817439, 'fraction_numerical': 0.0013623978201634877, 'mean_word_length': 2.0214035809837414, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3823639', 'n_tokens_mistral': 4068, 'n_tokens_neox': 3963, 'n_words': 1064}