liartie

Untitled

Sep 30th, 2022
1,038
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.01 KB | None | 0 0
  1. using System.Security.Cryptography;
  2.  
  3. namespace Kvdb
  4. {
  5.     public class Kvdb
  6.     {
  7.         private const string DefaultDbLocation = "database/db";
  8.         private const string DefaultDirName = "database/";
  9.         public static void InitializeDataBase() // function to create all the necessary folders and files
  10.         {
  11.             if(!Directory.Exists("database")){
  12.                 Directory.CreateDirectory("database");
  13.             }
  14.         }
  15.        
  16.         public static string Hash(string text) // function to get name of a database file
  17.         {
  18.             if (String.IsNullOrEmpty(text))
  19.                 return String.Empty;
  20.  
  21.             var sha = new SHA256Managed(); // hash algorithm
  22.             {
  23.                 byte[] textData = System.Text.Encoding.UTF8.GetBytes(text);
  24.                 byte[] hash = sha.ComputeHash(textData);
  25.                 return BitConverter.ToString(hash).Replace("-", String.Empty);
  26.             }
  27.         }
  28.  
  29.         private static string GetDbLocation(string? dbName) // function to get the location of a database
  30.         {
  31.             string currentDbLocation = "";
  32.             if (dbName != null)
  33.             {
  34.                 currentDbLocation = DefaultDirName + Hash(dbName);
  35.             }
  36.             else
  37.             {
  38.                 currentDbLocation = DefaultDbLocation;
  39.             }
  40.  
  41.             return currentDbLocation;
  42.         }
  43.  
  44.         public static void Put(string? dbName, string key, string value) // put command
  45.         {
  46.             string currentDbLocation = GetDbLocation(dbName);
  47.             Remove(dbName, key); // removes the key (because put always overrides current value). It also prints the value stored there before.
  48.             string line = key + " " + value; // new line that is going to be placed under the key
  49.             var streamWriter = new StreamWriter(currentDbLocation, true);
  50.             streamWriter.WriteLine(line); // writing the new line to db
  51.             streamWriter.Close();
  52.         }
  53.  
  54.         private static void Get(string? dbName, string key) // get command
  55.         {
  56.             string currentDbLocation = GetDbLocation(dbName);
  57.            
  58.             var foundLine = ""; // initially the found string is empty
  59.             foreach (string line in File.ReadLines(currentDbLocation)) // reading all lines
  60.             {
  61.                 var arr = line.Split(" ");
  62.                 if (arr[0] == key)
  63.                 {
  64.                     foundLine = line.Substring(arr[0].Length + 1);
  65.                 }
  66.             }
  67.             Console.WriteLine(foundLine);
  68.         }
  69.  
  70.         private static void Remove(string? dbName, string key) // remove command
  71.         {
  72.             string currentDbLocation = GetDbLocation(dbName);
  73.            
  74.             var lineToRemove = "";
  75.             var tempFile = Path.GetTempFileName(); // creating a temporary file to store all the lines except for the one to remove
  76.             var streamWriter = new StreamWriter(tempFile);
  77.             if(File.Exists(currentDbLocation)){
  78.                 foreach (var line in File.ReadLines(currentDbLocation))
  79.                 {
  80.                     var arr = line.Split(" ");
  81.                     if (arr[0] != key)
  82.                     {
  83.                         streamWriter.WriteLine(line); // if it is any other line except for the one to remove
  84.                     }
  85.                     else
  86.                     {
  87.                         lineToRemove = line.Substring(arr[0].Length + 1); // if it is the line to remove
  88.                     }
  89.                 }
  90.  
  91.                 Console.WriteLine(lineToRemove); // printing what was there before we remove it
  92.                 streamWriter.Close();
  93.                 File.Delete(currentDbLocation); // deleting the previous db file
  94.                 File.Move(tempFile, currentDbLocation); // moving contents of temporary file to the new database
  95.             }
  96.         }
  97.  
  98.         public static void Main(string[] args)
  99.         {
  100.             if (args.Length == 0)
  101.             {
  102.                 Console.WriteLine("You didn't provide any arguments. That's sad. I'm quitting.");
  103.             }
  104.             switch (args[0])
  105.             {
  106.                 case "put":
  107.                     if (args.Length == 3) // if db is not specicfied
  108.                     {
  109.                         Put(null, args[1], args[2]);
  110.                     }
  111.                     else if (args.Length == 4) // if db is specified
  112.                     {
  113.                         Put(args[1], args[2], args[3]);
  114.                     }
  115.                     else
  116.                     {
  117.                         Console.WriteLine("Wrong command format. Correct format is: put [dbname?] [key] [value]");
  118.                     }
  119.                     break;
  120.                
  121.                 case "get":
  122.                     if (args.Length == 2)
  123.                     {
  124.                         Get(null, args[1]);
  125.                     }
  126.                     else if (args.Length == 3)
  127.                     {
  128.                         Get(args[1], args[2]);
  129.                     }
  130.                     else
  131.                     {
  132.                         Console.WriteLine("Wrong command format. Correct format is: get [dbname?] [key]");
  133.                     }
  134.                     break;
  135.                
  136.                 case "remove":
  137.                     if (args.Length == 2)
  138.                     {
  139.                         Remove(null, args[1]);
  140.                     }
  141.                     else if (args.Length == 3)
  142.                     {
  143.                         Remove(args[1], args[2]);
  144.                     }
  145.                     else
  146.                     {
  147.                         Console.WriteLine("Wrong command format. Correct format is: remove [dbname?] [key]");
  148.                     }
  149.                     break;
  150.                
  151.                 default:
  152.                     Console.WriteLine("I don't know this command. I suggest you read the documentation. That might help actually.");
  153.                     break;
  154.             }
  155.         }
  156.     }
  157. }
  158.  
Advertisement
Add Comment
Please, Sign In to add comment