Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Security.Cryptography;
- namespace Kvdb
- {
- public class Kvdb
- {
- private const string DefaultDbLocation = "database/db";
- private const string DefaultDirName = "database/";
- public static void InitializeDataBase() // function to create all the necessary folders and files
- {
- if(!Directory.Exists("database")){
- Directory.CreateDirectory("database");
- }
- }
- public static string Hash(string text) // function to get name of a database file
- {
- if (String.IsNullOrEmpty(text))
- return String.Empty;
- var sha = new SHA256Managed(); // hash algorithm
- {
- byte[] textData = System.Text.Encoding.UTF8.GetBytes(text);
- byte[] hash = sha.ComputeHash(textData);
- return BitConverter.ToString(hash).Replace("-", String.Empty);
- }
- }
- private static string GetDbLocation(string? dbName) // function to get the location of a database
- {
- string currentDbLocation = "";
- if (dbName != null)
- {
- currentDbLocation = DefaultDirName + Hash(dbName);
- }
- else
- {
- currentDbLocation = DefaultDbLocation;
- }
- return currentDbLocation;
- }
- public static void Put(string? dbName, string key, string value) // put command
- {
- string currentDbLocation = GetDbLocation(dbName);
- Remove(dbName, key); // removes the key (because put always overrides current value). It also prints the value stored there before.
- string line = key + " " + value; // new line that is going to be placed under the key
- var streamWriter = new StreamWriter(currentDbLocation, true);
- streamWriter.WriteLine(line); // writing the new line to db
- streamWriter.Close();
- }
- private static void Get(string? dbName, string key) // get command
- {
- string currentDbLocation = GetDbLocation(dbName);
- var foundLine = ""; // initially the found string is empty
- foreach (string line in File.ReadLines(currentDbLocation)) // reading all lines
- {
- var arr = line.Split(" ");
- if (arr[0] == key)
- {
- foundLine = line.Substring(arr[0].Length + 1);
- }
- }
- Console.WriteLine(foundLine);
- }
- private static void Remove(string? dbName, string key) // remove command
- {
- string currentDbLocation = GetDbLocation(dbName);
- var lineToRemove = "";
- var tempFile = Path.GetTempFileName(); // creating a temporary file to store all the lines except for the one to remove
- var streamWriter = new StreamWriter(tempFile);
- if(File.Exists(currentDbLocation)){
- foreach (var line in File.ReadLines(currentDbLocation))
- {
- var arr = line.Split(" ");
- if (arr[0] != key)
- {
- streamWriter.WriteLine(line); // if it is any other line except for the one to remove
- }
- else
- {
- lineToRemove = line.Substring(arr[0].Length + 1); // if it is the line to remove
- }
- }
- Console.WriteLine(lineToRemove); // printing what was there before we remove it
- streamWriter.Close();
- File.Delete(currentDbLocation); // deleting the previous db file
- File.Move(tempFile, currentDbLocation); // moving contents of temporary file to the new database
- }
- }
- public static void Main(string[] args)
- {
- if (args.Length == 0)
- {
- Console.WriteLine("You didn't provide any arguments. That's sad. I'm quitting.");
- }
- switch (args[0])
- {
- case "put":
- if (args.Length == 3) // if db is not specicfied
- {
- Put(null, args[1], args[2]);
- }
- else if (args.Length == 4) // if db is specified
- {
- Put(args[1], args[2], args[3]);
- }
- else
- {
- Console.WriteLine("Wrong command format. Correct format is: put [dbname?] [key] [value]");
- }
- break;
- case "get":
- if (args.Length == 2)
- {
- Get(null, args[1]);
- }
- else if (args.Length == 3)
- {
- Get(args[1], args[2]);
- }
- else
- {
- Console.WriteLine("Wrong command format. Correct format is: get [dbname?] [key]");
- }
- break;
- case "remove":
- if (args.Length == 2)
- {
- Remove(null, args[1]);
- }
- else if (args.Length == 3)
- {
- Remove(args[1], args[2]);
- }
- else
- {
- Console.WriteLine("Wrong command format. Correct format is: remove [dbname?] [key]");
- }
- break;
- default:
- Console.WriteLine("I don't know this command. I suggest you read the documentation. That might help actually.");
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment