RAID Levels

RAID stands for Redundant Array of Independent Disks

We have 4 RAID levels
RAID 0
RAID 1
RAID 5
RAID 10

RAID 0 is not even useful as we do not maintain a copy of data, we simply save 50% data in one disk and the other 50% in another disk.

RAID 1, we maintain a copy of data in another disk and it is useful in case of single point failure.

RAID 5 requires mininimum 3 disks, what happens in RAID 5 is we store data along with a parity and when there is a disk failure we use this to reconstruct the data. The only didsadvantage here is parity will take up large space.

RAID 10 is combination of RAID0 and RAID1. We need minimum of 4 disks for RAID10.It takes advantages from both RAID0 speed and RAID1 fault tolerance but the disadvantage is we can only use 50% of data.

Changing jenkins slack notifications to a different channel

If in your project jenkins app is configured to slack to send notifications to a particular channel say “channel1” and now you want to move these notifications to “channel2”, you need to to the following steps.

Open jenkins and go to manage jenkins and then configure system.

Here go to Global Slack Notifier Settings where you can see the “channel1” in the channel or slack Id field, replace channel1 with the new channel “channel2”. This completes work on jenkins side.

Goto slack and then manage apps and select jenkins ci and then select configuration .Here you should see your old configuration, click edit and then replace channel1 in the post to channel field with new channel “channel2”. Save configuration. This completes the work on Slack side.

Now you are good to recieve notifications from jenkins in the new channel.

Using Task in C#


I had a requirement where i need to wait for a particular file to be available and the need to make operations on the file.
I thought of using Task to wait for this action to complete and throw an error if file is not available after certain amount of time. I made use of cancellation token service to make this happen. The following is the sample code for this.

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace TaskWaitSample
{
    public class Program
    {
        static void Main(string[] args)
        {
            var fileName = @"C:\TestFileDir\Narendra.txt";

            CancellationTokenSource ts = new CancellationTokenSource(120000);

            var task = Task.Factory.StartNew(() =>  CheckForFile(fileName, ts.Token), ts.Token);


            if (task.Result)
            {
                Console.WriteLine("Got result");
            }
            else
            {
                Console.WriteLine("Exiting after waiting");
            }


            Console.WriteLine("Exited task");

            Console.Read();
        }


        public static bool CheckForFile(string fileName, CancellationToken ts)
        {
           
            
          
            while (!ts.IsCancellationRequested)
            {
                //Console.WriteLine("Checking at {0}", DateTime.Now.TimeOfDay);

                if (File.Exists(fileName))
                {
                    return true;
                }

            }


            return false;
        }
    }
}

Running Total using excel

I had an issue trying to use excel to calculate the running total and display in a new cell in the adjacent column like below

The trick here is select the column you want to calculate the running total, assign a name and format it as a table, and in the adjacent column ,here C2 use the formula

=SUM(B$1:[@Number]), ola you have the running total .

Algorithm

An algorithm is a sequence of steps we perform to solve any given problem. An algorithm should also satisfy the following.

  • Each step should be unique and cannot be broken down into further smal steps
  • The order of steps is also important
  • Algorithm should return a result
  • Algorithm should not run for an infinite time

Accessing a 2d array in C#

We generally use a for loop or foeach to iterate through a normal array but how do we do it when we have a 2d array in C#. The following way can help.

var array = new int[,] { {1, 5},{7, 3},{3, 5} };
for (int j = 0; j < array.Length; j += 1) {
for (int k = 0; k < array[j].Length; k += 1) {
Console.Write(array[j][k]);
}
}

C# Encrypt and Decrypt a field with setter and getter

Sometimes we need to encrypt a field while saving to database instead of original value but while reading it and using it in our application we need to decrypt again.

This can be implemented at class level by making use of setter and getter and another property to back the property we are trying to encrypt.

Consider the following class Customer with Name and SSN

public class Customer { public string Name { get; set; } public string SSN {get;set;} }
Code language: JavaScript (javascript)

In this class if we want to encrypt the SSN property, this can be done as follows.

public class Customer { public string Name { get; set; } private string _SSN; [JsonIgnore] public string SSN { //make sure this property is not saved to database get { return Decrypt(EncryptedSSN); } set { _SSN = value; EncryptedSSN = Encrypt(value); } } private string _encryptedSSN; public string EncryptedSSN { get; private set; } public string Decrypt(string encryptedSSN) { return "originialvalue"; } public static string Encrypt(string originalSSN) { return "encryptedValue"; } }
Code language: JavaScript (javascript)

Here the encryptedSSN is saved to database and we can get and set the original SSN field like normal field and the encryption and decryption takes place at the class level and we don’t have to worry about making changes else where.
Also the private set on the “EncryptedSSN” ensures we don’t set it from anywhere in the code.Just make sure you don’t save the field “SSN” to the database.

Given a string ,find if its permutation can be a palindrome

public class Solution { public bool CanPermutationPalindrome(string s) { var ht=new Hashtable(); for(int i=0;i<s.Length;i++){ if(ht.Contains(s[i])){ ht[s[i]]=(int)ht[s[i]]+ 1; } else{ ht.Add(s[i],1); } } if(ht.Keys.Count==1){ return true; } var count=0; var oddCount=0; foreach (var val in ht.Values) { var value=(int)val; if(value==1){ count++; } if(value%2==1 && value !=1){ oddCount++; } } if(count==0 && oddCount==0){ return true; } else if(count ==1 && oddCount==0){ return true; } else if(count ==0 && oddCount==1){ return true; } return false; } }
Code language: PHP (php)

C# program to take “n” number of items from a List with N items

sometimes we need to pick “n” number of items from a list of “N” items and perform some operation on the “n” items and repeat the same operation on the remaining items, i encountered this kind of scenario and the following code in C# can be used.

Consider a list of items with 23 items and you need 4 each time and perform operation on those 4.

var listOfItems = new List<int>(); for (int i = 1; i <= 23; i++) { listOfItems.Add(i); } for (int i = 0; i < listOfItems.Count; i = i + 4) { var items = listOfItems.Skip(i/4 * 4).Take(4); foreach (var item in items) { Console.WriteLine(item); } Console.WriteLine($"Completed taking required {items.ToList().Count}"); } Console.Read();
Code language: PHP (php)

the output of the above code will look like this

1
2
3
4
Completed taking required 4
5
6
7
8
Completed taking required 4
9
10
11
12
Completed taking required 4
13
14
15
16
Completed taking required 4
17
18
19
20
Completed taking required 4
21
22
23
Completed taking required 3

Here first we insert into listOfItems 23 values and later iterate over the list to take 4 each item and in the last iteration we are only left with 3 and those 3 are taken

How to clone a new branch from tag

Sometimes we need to create a new branch and check it out from the tag of a git repository, the first step is to fetch all tags from the remote
git fetch –all –tags

once we have all the tags,the following command is used to create a new brach and switch to it

git checkout tags/<tag name> -b <branch name>

git checkout tags/1.7.14 -b branch1.7.14