Databases Reference
In-Depth Information
put(' /images/my-image.png', $image-data)
get(' /images/my-image.png')
Key-value
store
delete(' /images/my-image.png')
Figure 4.6 The code and result of using the commands associated with a
key-value store. To add a new key, you use the put command, as shown on
the left; to remove, you use the delete command, as shown in the middle;
and to retrieve, you use the get command, as shown on the right.
Figure 4.6 shows how an application would store ( put ), retrieve ( get ), and remove
( delete ) an image from a key-value store.
Standards watch: REST API
Note that we use the verb put instead of add in a key-value store to align with the
standard Representational State Transfer (REST) protocol, a style of software archi-
tecture for distributed systems that uses clients to initiate requests and servers to
process requests and return responses. The use of as xs:string indicates that the
key can be any valid string of characters with the exception of binary structures. The
item() references a single structure that may be a binary file. The xs: prefix indi-
cates that the format follows the W3C definition of data types that's consistent with
the XML Schema and the closely related XPath and XQuery standards.
In addition to the put, get, and delete API , a key-value store has two rules: distinct keys
and no queries on values:
Distinct keys —You can never have two rows with the same key-value. This means
that all the keys in any given key-value store are unique.
1
No queries on values —You can't perform queries on the values of the table.
2
The first rule, distinct keys, is straightforward: if you can't uniquely identify a key-value
pair, you can't return a single result. The second rule requires some additional
thought if your knowledge base is grounded in traditional relational databases. In a
relational database, you can constrain a result set using the where clause, as shown in
figure 4.7.
A key-value store prohibits this type of operation, as you can't select a key-value
pair using the value. The key-value store resolves the issues of indexing and retrieval
in large datasets by transferring the association of the key with the value to the appli-
cation layer, allowing the key-value store to retain a simple and flexible structure. This
is an example of the trade-offs between application and database layer complexity we
discussed in chapter 2.
Search WWH ::




Custom Search