Ruby Programming Tutorial - Lesson 33 - Creating Key Value Store DB - JokeDB

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@bilal-haider·
0.000 HBD
Ruby Programming Tutorial - Lesson 33 - Creating Key Value Store DB - JokeDB
## 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 
![Screenshot from 2018-03-22 17-48-28.png](https://steemitimages.com/DQmbEvw6XaHCJ5UKxMssk5MQsBMtoD4HTeiNF4EXubD3a59/Screenshot%20from%202018-03-22%2017-48-28.png)

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

![Screenshot from 2018-03-22 20-56-34.png](https://steemitimages.com/DQmc172Bmj5d3DnMH9Abf6jKpxjA1QCo5N3ynfDkZufgseQ/Screenshot%20from%202018-03-22%2020-56-34.png)

## 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 
![Screenshot from 2018-03-22 20-35-36.png](https://steemitimages.com/DQmNMSxfxTmryasTRYLgrBkihyk86SxQQdyipjUXfu5hvJr/Screenshot%20from%202018-03-22%2020-35-36.png)

Notice that, after I typed Database name, it creates a jokedb folder, and puts a database file in it. 
![Screenshot from 2018-03-22 20-36-11.png](https://steemitimages.com/DQmQ7wSFJD7MGKpnXTRyeqpZAQZpYyfs6qFGdEdAhhRvkd8/Screenshot%20from%202018-03-22%2020-36-11.png)

As soon as my Database is created I can then select it, and Start inserting data
![Screenshot from 2018-03-22 20-37-29.png](https://steemitimages.com/DQmXUKWjcL4vzoyJgugsbdTPMibbfsTp2Nvw7zDoVaMhA2Z/Screenshot%20from%202018-03-22%2020-37-29.png)


Notice that I can also create another database, and insert data into it
![Screenshot from 2018-03-22 21-04-42.png](https://steemitimages.com/DQmTRKzf95fm2VbBrqmH43RPz4FhiPyWarSLm8su66v6mvL/Screenshot%20from%202018-03-22%2021-04-42.png)

#### In this article we learned to create a Key-value Store that can help us persist our data, its not ideal because if you delete the file your data is gone.. as long as you can protect this file your data is safe
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,