NHyDBrun4
R
u
v
Version of 1
lane
NLOverview

Records and cells

NHyDB stores values in two ways: as records with a fixed, typed field structure and as single cells that you write freely. Both can be mixed in the same database.

Records with a fixed shape

A record shape (shape) describes the fields: each field has a position v, a name and a type. In the CLASSIC layout a record is a row u, and its fields sit side by side at v = 1, 2, 3 …

You write a record individually or many consecutive ones in one call. Types are checked on writing: a value that does not fit is rejected.

Single cells

Every cell (R, u, v, lane) can also be written directly – without a record shape and on both sides NL and NR. If the cell does not belong to a field, you specify its type when writing. This suits notes, scattered measurements or values at very large coordinates.

Extension fields

If only some records need an additional field, you create an extension field (extension): a named, typed field above the fixed fields. It only takes up space in the records that have a value there.

An example

A customer with two fixed fields, one extension field and a free note:

import nhydb

with nhydb.create("customers.nhydb") as db:
    db.create_realm(1, "customers")
    db.create_shape(1, 1, "customer", [{"v": 1, "name": "name", "type": "utf8"},
                                       {"v": 2, "name": "city", "type": "utf8"}])
    db.create_classic_layout(1, 1, "customers", shape_id=1)
    db.define_extension(1, 10, "u8", name="dunning_level")     # extension field at v = 10
    db.put_record(1, 1, {"name": "Miller", "city": "Giessen", "dunning_level": 2})
    db.put_cell(1, 1, 50, "please call back", type="utf8")     # free single cell
    db.commit()

    print({k: w for k, w in db.get(1, 1).items() if not k.startswith("_")})
    print(db.get_value(1, 1, 50))
    db.put_cell(1, 1, 2, "Marburg", commit=True)               # change one field only
    print(db.get(1, 1)["city"])
    print(db.delete_record(1, 1, commit=True), db.get(1, 1))   # delete everything

Output:

{'name': 'Miller', 'city': 'Giessen', 'dunning_level': 2, 'v50': 'please call back'}
please call back
Marburg
4 None

The record shows fixed fields, extension field and note together. delete_record deletes all four live cells of the customer at once.

Overwriting and deleting

Writing the same cell again replaces the old value. A whole record is replaced with put, a single cell is changed with put_cell. There are three ways to delete: one cell, a rectangular area or a whole record.

The most recently written value always applies – whether it was written as a record or as a single cell.

New data is appended to the file; old data stays where it is. Overwritten and deleted values therefore remain in the file as older blocks until a compaction removes them (see Protection and operation).

# Records and cells NHyDB stores values in two ways: as records with a fixed, typed field structure and as single cells that you write freely. Both can be mixed in the same database. ## Records with a fixed shape A **record shape** (shape) describes the fields: each field has a position v, a name and a type. In the CLASSIC layout a record is a row u, and its fields sit side by side at v = 1, 2, 3 … You write a record individually or many consecutive ones in one call. Types are checked on writing: a value that does not fit is rejected. ## Single cells Every cell (R, u, v, lane) can also be written directly – without a record shape and on both sides NL and NR. If the cell does not belong to a field, you specify its type when writing. This suits notes, scattered measurements or values at very large coordinates. ## Extension fields If only some records need an additional field, you create an **extension field** (extension): a named, typed field above the fixed fields. It only takes up space in the records that have a value there. ## An example A customer with two fixed fields, one extension field and a free note: ```pythonimport nhydb with nhydb.create("customers.nhydb") as db: db.create_realm(1, "customers") db.create_shape(1, 1, "customer", [{"v": 1, "name": "name", "type": "utf8"}, {"v": 2, "name": "city", "type": "utf8"}]) db.create_classic_layout(1, 1, "customers", shape_id=1) db.define_extension(1, 10, "u8", name="dunning_level") # extension field at v = 10 db.put_record(1, 1, {"name": "Miller", "city": "Giessen", "dunning_level": 2}) db.put_cell(1, 1, 50, "please call back", type="utf8") # free single cell db.commit() print({k: w for k, w in db.get(1, 1).items() if not k.startswith("_")}) print(db.get_value(1, 1, 50)) db.put_cell(1, 1, 2, "Marburg", commit=True) # change one field only print(db.get(1, 1)["city"]) print(db.delete_record(1, 1, commit=True), db.get(1, 1)) # delete everything``` Output: ```text{'name': 'Miller', 'city': 'Giessen', 'dunning_level': 2, 'v50': 'please call back'}please call backMarburg4 None``` The record shows fixed fields, extension field and note together. `delete_record` deletes all four live cells of the customer at once. ## Overwriting and deleting Writing the same cell again replaces the old value. A whole record is replaced with `put`, a single cell is changed with `put_cell`. There are three ways to delete: one cell, a rectangular area or a whole record. > The most recently written value always applies – whether it was written as a record or as a single cell. New data is appended to the file; old data stays where it is. Overwritten and deleted values therefore remain in the file as older blocks until a compaction removes them (see [Protection and operation](/en/protection-and-operation/)).