Storage package

ndn-python-repo supports 3 types of databases as backends. The Storage package provides a unified key-value storage API with the following features:

  • Supports MustBeFresh

  • Supports CanBePrefix

  • Batched writes with periodic writebacks to improve performance

The Storage class provides an interface, and is implemented by:

  • SqliteStorage

  • LevelDBStorage

  • MongoDBStorage

Note that the type Union[Iterable[Union[bytes, bytearray, memoryview, str]], str, bytes, bytearray, memoryview] in the documentation is equivalent to the ndn.name.NonStrictName type.

Reference

class ndn_python_repo.storage.Storage

Interface for a unified key-value storage API.

get_data_packet(name, can_be_prefix=False, must_be_fresh=False)

Get a data packet named name.

Parameters:
  • name (Union[Iterable[Union[bytes, bytearray, memoryview, str]], str, bytes, bytearray, memoryview]) – NonStrictName. The name of the data packet.

  • can_be_prefix (bool) – bool. If true, use prefix match instead of exact match.

  • must_be_fresh (bool) – bool. If true, ignore expired data.

Return type:

Optional[bytes]

Returns:

The value of the data packet.

put_data_packet(name, data)

Insert a data packet named name with value data. This method will parse data to get its freshnessPeriod, and compute its expiration time by adding the freshnessPeriod to the current time.

Parameters:
  • name (Union[Iterable[Union[bytes, bytearray, memoryview, str]], str, bytes, bytearray, memoryview]) – NonStrictName. The name of the data packet.

  • data (bytes) – bytes. The value of the data packet.

remove_data_packet(name)

Remove a data packet named name.

Parameters:

name (Union[Iterable[Union[bytes, bytearray, memoryview, str]], str, bytes, bytearray, memoryview]) – NonStrictName. The name of the data packet.

Return type:

bool

Returns:

True if a data packet is being removed.

class ndn_python_repo.storage.SqliteStorage(db_path)

Init table “data” with the attribute key being the primary key.

Parameters:

db_path (str) – str. Path to database file.

class ndn_python_repo.storage.LevelDBStorage(dir)

Creates a LevelDB storage instance at disk location str.

Parameters:

dir (str) – str. The disk location of the database directory.

class ndn_python_repo.storage.MongoDBStorage(db, collection)

Init a MongoDB storage with unique index on key.

Parameters:
  • db (str) – str. Database name.

  • collection (str) – str. Collection name.