100 Days of Code, Day 2: Building a NoSQL Database in Go

Today was a continuation on building the NoSQL database in Go that I started on Day 1 of the challenge. There are several areas that I needed to build out after setting up the bones of the project yesterday. I needed to structure the data, define the individual commands, and set up the socket to listen for the commands. Each of these areas utilized a different tool in Go: structs, for loops, switch statements, and sockets

Data Structure (Structs)

Snip20180515_2

Structs are special containers in Go that can hold data. For this project I needed a dbEntry struct to represent each item in the database, and a database struct to hold an array of items.

Define Commands (For Loops)

Snip20180515_3

For this early version of GoSQL, I am only implementing 4 commands: CREATE, GET, UPDATE, and DELETE. Each command requires looping through all of the entries in that database struct and manipulating the data.

Listening For Commands (Sockets and Switch Statements)

Snip20180515_4

The main function contains the logic on port 3333. The listen variable uses the net package to connect to the correct port on local host. I then began an infinite for loop with the listen variable accepting calls to the port and calling the processConnection function.

Snip20180515_5

Within the function, I utilized a switch statement to process the commands. The program is expecting a string with syntax for the NoSQL command. For example:

  • To GET an object with the ID “Car001”, the caller would submit the string “GET Car001”
  • To UPDATE the same object, the caller would submit “UPDATE Car001 Porsche”

The string.Fields function breaks strings at the white space. The program first checks the command to select the correct case statement, and then passes the associated value and key to the functions I built earlier.

What To Look For Tomorrow

Tomorrow I will set up the database to read from and write to a JSON file. This will be the physical store for all of the data. I will also test the database by using another Go program to simulate calls from a third party application. Check back in for the blog post tomorrow or check out the update code on GitHub!

One thought on “100 Days of Code, Day 2: Building a NoSQL Database in Go

Leave a comment