NHyDBrun4
R
u
v
Version of 1
lane
NLOverview

Protection and operation

An NHyDB database consists of a few files that you protect and back up like other important files. In addition there are locks, limits and tools for day-to-day operation.

Three files belong together

Every database consists of the data file (.nhydb), the encrypted layout plan (.layout.enc) and its key (.layout.key). Always back them up and move them together, and do so while the database is closed. Without the key the database will not open – a lost key cannot be recovered.

Encrypted layout plan

The description of how the data is organised – realms, record shapes, layouts, indexes – is stored encrypted and cryptographically secured in a separate file. If data file, layout plan and key do not match, NHyDB aborts the opening instead of continuing with a wrong structure. The payload data in the .nhydb file is not encrypted; protect it with file permissions or disk encryption.

One writer, many readers

Whoever opens a database for writing gets an exclusive lock. A second writer is rejected or, if requested, waits. Readers need no lock and see the committed state.

import os
import nhydb

with nhydb.create("warehouse.nhydb") as db:
    db.create_realm(1, "warehouse")
    db.put_cell(1, 1, 1, "Shelf A", type="utf8")

with nhydb.open("warehouse.nhydb") as writer:         # takes the writer lock
    try:
        nhydb.open("warehouse.nhydb")                 # a second writer
    except nhydb.DatabaseLockedError:
        print("locked")
    with nhydb.open("warehouse.nhydb", readonly=True) as reader:   # readers need no lock
        print(reader.get_value(1, 1, 1))

os.rename("warehouse.nhydb.layout.key", "gone.key")   # key is missing
try:
    nhydb.open("warehouse.nhydb")
except OSError:
    print("no key, no opening")

Output:

locked
Shelf A
no key, no opening

Limits for size and memory

Every database has a size limit that you can set and change. Queries that return their whole result at once have an upper memory limit. A memory limiter watches the working memory of the process and, when memory runs short, slows down large operations instead of overloading the system. It writes its statistics to the file limiter.txt.

Backing up, cleaning up, moving

  • Backing up: copy the three files of the closed database. Restore under the same file name – the folder may be a different one.
  • Cleaning up: since NHyDB only appends, files grow with every change. Compaction rewrites the current state and removes old versions and deleted data.
  • Renaming: a renamed data file does not open. Compaction creates a copy under a new name.

Tools for operation

The nhydb command line checks, repairs and compacts databases, shows statistics, converts addresses and reads individual cells. Reading commands never change the file. The same functions are available from Python.

# Protection and operation An NHyDB database consists of a few files that you protect and back up like other important files. In addition there are locks, limits and tools for day-to-day operation. ## Three files belong together Every database consists of the data file (`.nhydb`), the encrypted layout plan (`.layout.enc`) and its key (`.layout.key`). Always back them up and move them together, and do so while the database is closed. Without the key the database will not open – a lost key cannot be recovered. ## Encrypted layout plan The description of how the data is organised – realms, record shapes, layouts, indexes – is stored encrypted and cryptographically secured in a separate file. If data file, layout plan and key do not match, NHyDB aborts the opening instead of continuing with a wrong structure. The payload data in the `.nhydb` file is **not** encrypted; protect it with file permissions or disk encryption. ## One writer, many readers Whoever opens a database for writing gets an exclusive lock. A second writer is rejected or, if requested, waits. Readers need no lock and see the committed state. ```pythonimport osimport nhydb with nhydb.create("warehouse.nhydb") as db: db.create_realm(1, "warehouse") db.put_cell(1, 1, 1, "Shelf A", type="utf8") with nhydb.open("warehouse.nhydb") as writer: # takes the writer lock try: nhydb.open("warehouse.nhydb") # a second writer except nhydb.DatabaseLockedError: print("locked") with nhydb.open("warehouse.nhydb", readonly=True) as reader: # readers need no lock print(reader.get_value(1, 1, 1)) os.rename("warehouse.nhydb.layout.key", "gone.key") # key is missingtry: nhydb.open("warehouse.nhydb")except OSError: print("no key, no opening")``` Output: ```textlockedShelf Ano key, no opening``` ## Limits for size and memory Every database has a size limit that you can set and change. Queries that return their whole result at once have an upper memory limit. A memory limiter watches the working memory of the process and, when memory runs short, slows down large operations instead of overloading the system. It writes its statistics to the file `limiter.txt`. ## Backing up, cleaning up, moving - **Backing up:** copy the three files of the closed database. Restore under the same file name – the folder may be a different one.- **Cleaning up:** since NHyDB only appends, files grow with every change. Compaction rewrites the current state and removes old versions and deleted data.- **Renaming:** a renamed data file does not open. Compaction creates a copy under a new name. ## Tools for operation The `nhydb` command line checks, repairs and compacts databases, shows statistics, converts addresses and reads individual cells. Reading commands never change the file. The same functions are available from Python.