Synchronization 5: Databases

Overview

In this lecture, we start building a networked database.

Database

Key-value database

Databases and networks

Representing networking in system calls

Clients and servers

WeensyDB protocol

kohler@unshare$ telnet cs61 6162
Trying 34.193.34.22...
Connected to cs61.seas.harvard.edu.
Escape character is '^]'.
$GET hello
VALUE hello 5 #1
world
END
$SET hello 3          [3 = size of value]
$Hi
STORED #1
$GET hello
VALUE hello 3 #1
Hi
END
$DELETE hello
DELETED #1
$SET hello 9
$Hi again
STORED #2
$Q
Connection closed by foreign host.

System calls?

Unix networking system calls

WeensyDB server loop

int fd = open_listen_socket(port);
// `fd` doesn’t receive *data*—it receives *new connections*

while (true) {
    struct sockaddr addr;
    socklen_t addrlen = sizeof(addr);

    // Accept connection on listening socket
    int cfd = accept(fd, &addr, &addrlen);
    // `cfd` is a new connection from the computer at `addr`

    // Handle connection
    handle_connection(cfd, unparse_sockaddr(&addr, addrlen));
    // `handle_connection` closes `cfd` when done
}

WeensyDB internals

dbmap API

Synchronization issues