Key

Designing key is an important design decision.

Database Key

An object store stores records which consists of key (primary key) and value. In addition to primary key, object store optionally index secondary keys. How these keys are extracted from the record value is defined in database schema. Keys are important for efficient querying and are carefully constructed.

Valid key is one of Number, String, Date object or Array consisting elements of valid key. When keys of different types are compared, Array is greater than String, which is greater than Date and Date is greater than Number.

KeyRange

Records can be retrieved from object stores and indexes using either keys or key ranges. A key range is a continuous segment of keys defined by interval by using lower and upper bound, or unbounded.

To retrieve all keys within a certain interval, you can construct a key range as follow:

Range Code
All keys ? x ydn.db.KeyRange.upperBound(x)
All keys < x ydn.db.KeyRange.upperBound(x, true)
All keys ? y ydn.db.KeyRange.lowerBound(y)
All keys > y ydn.db.KeyRange.lowerBound(y, true)
All keys ≥ xy ydn.db.KeyRange.bound(x, y)
All keys > x < y ydn.db.KeyRange.bound(x, y, true, true)
All keys > xy ydn.db.KeyRange.bound(x, y, true, false)
All keys ≤ x < y ydn.db.KeyRange.bound(x, y, false, true)
The key = z ydn.db.KeyRange.only(z)
All (string or array) keys start with a ydn.db.KeyRange.starts(a)

For example, the key range starts with [123] comprises all keys of Array value with first element is 123. For example [123], [123, -1], [123, 'a'], etc are part of the key range.

Curious reader will notice key range starts with integer has special interest. For example, ydn.db.KeyRange.starts(2) comprises not only 2 but a range bounded by 2 - Number.EPSILON and 2 + Number.EPSILON.

Nested set model

The nested set model is a particular technique for representing nested sets (also known as trees or hierarchies) in relational databases. Similarly Google appengine has hierarchy key structure and AWS DynamoDB has local secondary index key model. Key is constructed in such a way that parent key is in prefix resulting querying them becomes inexpensive. Parent-child and one-to-many relationship can be represent by such key structure.

For example, to store product in category:

var computer_123 = {
  id: 123,
  category: 'hardware/electronic/computer/laptop/'
};

We can query all computers by:

var key_range = ydn.db.KeyRange.starts('hardware/electronic/computer/');
db.values('product', 'category', key_range);

YDN-DB provides Key model that support hierarchical key structure.

Key of Array type are useful for creating such hierarchical key structure.

Suppose we have Address object store, which belong to Employee object store,

var employee_123 = {
  id: 123,
  name: 'Foo Bar'
};
var address_123_1 = {
  id: [employee_123.id, 1]
  street: 'Street 1'
}

Notice that we can efficiently retrieve all addresses belong to employee by following key range query:

var key_range = ydn.db.KeyRange.starts([employee_123.id]);
db.values('Address', key_range);

Authors

Kyaw Tun