In this article we are going to create our Key-Value Store DB
In previous articles we learned to use many data structures but we were not able to persist data, as soon as we closed our program, data is lost.
We learned to work with files so now, its time that we create our first persistent database.
The Task
Create a persistent database or a key-value store, that has features as follows,
1 - Create new databases
2 - Select a database
3 - Insert Data
There are many Databases/keyvalue stores available with much more features than our JokeDB but we wanted to create something of our own. so lets get started.
Just in case you want to use some of professional key-value stores, check these out
For people who want to use JokeDB lets first make it :)
Identifying Entities
- Database
- Data
In our example we are just creating a Database class, because that is enough for our purpose, but you can extend this jokeDB to create amazing features into it and use it as needed.
We start by creating a Class called Jokedb, which has three methods
- Initialize
- Insert
- Show_content
class Jokedb
attr_accessor :dbFile
def initialize(name)
_name = name+".dat"
if !File::exists?("jokedb/"+_name)
Dir.mkdir('jokedb') unless Dir.exist?('jokedb')
@dbFile = File.new("jokedb/"+_name, "w")
else
@dbFile = File.open("jokedb/"+_name)
end
#File.open(name) if File::exists?(name)
end
def show_content
IO.foreach(@dbFile){|block| puts block}
end
def insert(key, value)
open(@dbFile, 'a') { |f|
f << "[\"#{key}\",\"#{value}\"]\n"
puts "Data Inserted: [\"#{key}\",\"#{value}\"]"
}
end
end
1 - Initialize
Initialize method is used to Create a db directory, and inside that directory a new database file.
- If there exists a database file already, it will open that and use it
- If there exists no database file, with the specified name, It will create a new file and use that
2 - Insert
Second method opens database file, and appends it the text which is passed to it, as key and value.
You should also know that, while use this in code you can also pass a HASH to it as a value which is cool :)
but for our purpose, we will store a key, and a value
3 - show content
This method allows you to see the content of the database, e.g all the records
Later on we can implement more features to our JokeDB, so that it can also search for a record, it should also delete a record, it can also sort a record .. but for now we are going with just these few methods.
Lets now create main.rb file and use our JokeDB there
require_relative 'jokedb'
while $entry.to_i != 5
print "1 - Create New Database \n2 - Select Database\n3 - Insert Data\n4 - Show Data\n5 - Exit\nYour Choice: "
$entry = gets.chomp
if $entry.to_i == 1
print "Insert Database Name : "
_dbname = gets.chomp
_myDatabase = Jokedb.new(_dbname)
elsif $entry.to_i == 2
print "Select Database : "
_dbname = gets.chomp
$myDatabase = Jokedb.new(_dbname)
elsif $entry.to_i == 3
print "Insert key : "
_key = gets.chomp
print "Insert Value : "
_value = gets.chomp
$myDatabase.insert(_key, _value)
elsif $entry.to_i == 4
$myDatabase.show_content()
elsif $entry.to_i == 5
puts "Exiting .... "
else
puts "Invalid choice "
end
end
Create a Database
Notice that, after I typed Database name, it creates a jokedb folder, and puts a database file in it.
As soon as my Database is created I can then select it, and Start inserting data
Notice that I can also create another database, and insert data into it
You post nice contents. Can you contact me in email?
You can chat with me at discord :) link to my discord channel, you can find in my profile page. I am glad you like my posts.
Nice to see your Ruby posts doing well so fast Bilal_haider! Nice work man..!
I guess its been a month or maybe a little more.. since that post, where you commented.
I planned to write at least 2 articles per day... want to reach 1000 in total, very soon..
Yeah man...you got 12K+ followers and pretty much all on your own "steem"! You should probably start posting on how to grow your followship in a hurry! That seems really fast - that's awesome. You were wondering if there was interest...I guess you know now! Take care amigo...:)