The Logicwalk Database
LWOS contains an application called serve_db which provides database services to clients. serve_db stores databases in folders in the file system. The database tables are stored in files inside the database folders. The table file contains a map with two keys: "columns" and "rows". "columns" is a list of column names of the table and "rows" is a list of the rows of the table. Each row is also a list of column values.
For example, suppose that the following is the current state of LWOS:
- Hostname: "b.com"
- Command: "serve_db"
- Command Context:
- Home: 1 (/dbs)
- Database: 2 (/dbs/baseball)
- File System
- Data:
- Index: 0
- Path: '/'
- Folders: dbs: 1
- Index: 1
- Path: '/dbs'
- Folders: baseball: 2
- Index: 2
- Path: '/dbs/baseball'
- Files:
- players:
- 'columns': ['id', 'given_name', 'family_name']
- 'rows':
- [1, "Mike", "Trout", ]
- [2, "Max", "Scherzer", ]
- stats:
- 'columns': ['player_id', 'team', 'year', 'home_runs']
- 'rows':
- [1, 'Angels', 2015, 40]
- [1, 'Angels', 2016, 32]
- [1, 'Angels', 2017, 51]
- [2, 'Astros', 2015, 3]
- [2, 'Rangers', 2016, 5]
- players:
- Folders: -
- Index: 0
- Data:
/dbs contains 1 folder (database) called "baseball." And the baseball folder contains 2 files (tables) called players and stats. The table files are formatted in JSON.
The column names of the players table are:
[ "id", "given_name", "family_name" ]
And the table rows are:
[ [ 1, "Mike", "Trout" ], [ 2, "Max", "Scherzer" ] ]
In the first row, the id is 1, the given_name is Mike, and family_name is Trout.
The column names of the stats table are:
[ "player_id", "team", "year", "home_runs" ]
And the table rows are:
[ [ 1, "Angels", 2015, 40 ] [ 1, "Angels", 2016, 32 ] [ 1, "Angels", 2017, 51 ] [ 2, "Astros", 2015, 3 ] [ 2, "Rangers", 2016, 5 ] ]
When serve_db receives a query, the server loads the referenced table file and iterates over the table rows. If the table was updated during query processing, serve_db writes the table file back to the file system.
Querying a Database Inside a Program
Python programs can interact with serve_db by sending requests and receiving responses over the network. As with any network request, Python programs can use the send_message function to send database queries to serve_db. To receive a response from serve_db, Python programs can use the receive_message function and parse the response. The response is in JSON, so we can call the json_loads function to convert the message to a dictionary object. Alternatively, Logicwalk Python provides a function called receive_json_response which receives the message and converts it into a dictionary for you.
Here is an example Python client:
send_message('server.com', 'USE baseball') response = receive_json_response() if response['status'] == 'ok': send_message('server.com', 'SELECT family_name FROM players WHERE id="2"') response = receive_json_response() message = response['object'] if message['status'] == 'ok': for row in message['rows']: print(row) else: print("use command failed")
We will examine the serve_db command and the receive_json_response function in greater detail later in the chapter.
Please choose every statement that is true.
Please write a Python statement that sends the following message to the "sports-db.com" database server:
SELECT home_runs FROM players WHERE family_name="Ruth"
Comments
Please log in to add comments