NHyDBrun4
R
u
v
Version of 1
lane
NLOverview

Layouts

A layout is a rule for how records and their fields are mapped to cells. Data is always stored in the same cells (R, u, v, lane) – the layout only determines which cell belongs to which field.

Organisation as a set of rules of its own

An application can keep its entire organisational logic as its own set of rules: storage rules, address formulas, meanings and views. These rules can be documented, tested, versioned and shared outside the database – or stored as data themselves.

A new data model therefore does not require extending the database. Tables are just as possible as hierarchies, networks, event sequences, version models and forms of organisation nobody has thought of yet. The built-in layouts are predefined orders for typical tasks.

The built-in layouts are an offer, not a limit.

CLASSIC: one record per row

The familiar table form: each record occupies a row u, its fields sit side by side at v = 1, 2, 3 … The fields are described by a record shape (see Records and cells). Indexes and NHySQL work on CLASSIC records.

COLUMN: one field per row

Here it is the other way round: each field has a fixed row u, and a record is a position v. All details of a customer are at the same position in every row.

Position 4710
Position 4711
Position 4712
Row 1 · Name
…
Miller
…
Row 2 · Street
…
8 High Street
…
Row 3 · Postal code
…
35390
…
Row 4 · City
…
Giessen
…
Row 7 · Dunning level
…
5
…
The same position connects all details of a customer (sample data).

Exactly this customer example as a COLUMN layout:

import nhydb

with nhydb.create("customers.nhydb") as db:
    db.create_realm(1, "customers")
    # COLUMN: one row u per field, the customer is the position v
    db.create_column_layout(1, 1, "customers", fields=[
        {"u": 1, "name": "name", "type": "utf8"},
        {"u": 2, "name": "street", "type": "utf8"},
        {"u": 3, "name": "postal_code", "type": "utf8"},
        {"u": 4, "name": "city", "type": "utf8"},
        {"u": 7, "name": "dunning_level", "type": "u8"},
    ])
    db.put_object(1, 4711, {"name": "Miller", "street": "8 High Street",
                            "postal_code": "35390", "city": "Giessen", "dunning_level": 5}, commit=True)

    print({k: w for k, w in db.get_object(1, 4711).items() if not k.startswith("_")})
    print(db.get_value(1, 4, 4711))                     # row 4, position 4711
    adr = db.get_object(1, 4711, include_addresses=True)["_addresses"]["dunning_level"]
    print(adr["u"], adr["v"], adr["N"])

Output:

{'name': 'Miller', 'street': '8 High Street', 'postal_code': '35390', 'city': 'Giessen', 'dunning_level': 5}
Giessen
7 4711 N22259517

If you know position 4711, you know the addresses of all details of this customer – the dunning level is in row 7, and its number N follows from that.

PACKED: many small records in one row

PACKED places many small records densely one after another in a single row u. Each record gets an equally sized section of positions, with the fields at fixed places inside it. This suits large numbers of small, uniform entries.

CUSTOM: your own rule

If none of the built-in forms is enough, you write the mapping yourself – as a small Python class that computes the cell for a record and a field. NHyDB stores only the name of the rule; you register the class again after every opening.

To place values directly by formula, use MATH_MAP.

Several layouts in one realm

A realm can have several layouts for different ranges of u, for example CLASSIC for most rows and a second layout for a special range. If ranges overlap, a fixed order of precedence decides which layout applies. Layouts can be changed later; NHyDB rejects changes that would assign already stored data to a different layout.

# Layouts A **layout** is a rule for how records and their fields are mapped to cells. Data is always stored in the same cells (R, u, v, lane) – the layout only determines which cell belongs to which field. ## Organisation as a set of rules of its own An application can keep its entire organisational logic as its own set of rules: storage rules, address formulas, meanings and views. These rules can be documented, tested, versioned and shared outside the database – or stored as data themselves. A new data model therefore does not require extending the database. Tables are just as possible as hierarchies, networks, event sequences, version models and forms of organisation nobody has thought of yet. The built-in layouts are predefined orders for typical tasks. > The built-in layouts are an offer, not a limit. ## CLASSIC: one record per row The familiar table form: each record occupies a row u, its fields sit side by side at v = 1, 2, 3 … The fields are described by a record shape (see [Records and cells](/en/records-and-cells/)). Indexes and NHySQL work on CLASSIC records. ## COLUMN: one field per row Here it is the other way round: each field has a fixed row u, and a record is a position v. All details of a customer are at the same position in every row. {{kundenraster}} Exactly this customer example as a COLUMN layout: ```pythonimport nhydb with nhydb.create("customers.nhydb") as db: db.create_realm(1, "customers") # COLUMN: one row u per field, the customer is the position v db.create_column_layout(1, 1, "customers", fields=[ {"u": 1, "name": "name", "type": "utf8"}, {"u": 2, "name": "street", "type": "utf8"}, {"u": 3, "name": "postal_code", "type": "utf8"}, {"u": 4, "name": "city", "type": "utf8"}, {"u": 7, "name": "dunning_level", "type": "u8"}, ]) db.put_object(1, 4711, {"name": "Miller", "street": "8 High Street", "postal_code": "35390", "city": "Giessen", "dunning_level": 5}, commit=True) print({k: w for k, w in db.get_object(1, 4711).items() if not k.startswith("_")}) print(db.get_value(1, 4, 4711)) # row 4, position 4711 adr = db.get_object(1, 4711, include_addresses=True)["_addresses"]["dunning_level"] print(adr["u"], adr["v"], adr["N"])``` Output: ```text{'name': 'Miller', 'street': '8 High Street', 'postal_code': '35390', 'city': 'Giessen', 'dunning_level': 5}Giessen7 4711 N22259517``` If you know position 4711, you know the addresses of all details of this customer – the dunning level is in row 7, and its number N follows from that. ## PACKED: many small records in one row PACKED places many small records densely one after another in a single row u. Each record gets an equally sized section of positions, with the fields at fixed places inside it. This suits large numbers of small, uniform entries. ## CUSTOM: your own rule If none of the built-in forms is enough, you write the mapping yourself – as a small Python class that computes the cell for a record and a field. NHyDB stores only the name of the rule; you register the class again after every opening. To place values directly by formula, use [MATH_MAP](/en/math-map/). ## Several layouts in one realm A realm can have several layouts for different ranges of u, for example CLASSIC for most rows and a second layout for a special range. If ranges overlap, a fixed order of precedence decides which layout applies. Layouts can be changed later; NHyDB rejects changes that would assign already stored data to a different layout.