
# High-level Design

Note:
: This is a working document and captures current design. We reserve the right to change the design at any time as prototype development moves forward.

## Information scale and performance

DMS3 is designed to enable web scale information management (storage and retrieval) and provides applications with an API that abstracts information as documents stored in index repositories managed by a search engine.

Consistent performance is achieved by enforcing configurable upper bound on the number of documents in one index repository. Information scale is achieved by supporting an arbitrary number of index repositories backed by a fault tolerant distributed storage layer.

### Key terms and concepts

Information is organized into index repository **_containers_** [^1] considering multiple criteria:

[^1]: container and repository is used interchangeably in this document to mean the set of files and folders the search engine uses to track volatile and persistent state of a repository.

1. A document **_kind_** suggests a certain structure and semantics context for a class of similar documents. Documents of a certain kind are said to conform to a common schema and contain information of a common theme.

2. An **_infostore_** is a set of containers that collects documents of similar kind. As content is added to a repository and as that repository grows and reaches a configurable document limit, the set is expanded with an additional repository to host additional documents of the same kind. The container-set or **_reposet_** [^2] may expand up to an architectural limit and provides a mechanism to physically shard information. The repository document limit and other life-cycle properties can be configured based on the kind of infospace. An infostore serves to limit search scope of a query target.

[^2]: container-set and reposet is used interchangeably in this document, it represents a generic term that refers to the set of index repositories that form an infostore.

3. A **_metastore_** is a set of index repositories that collects documents providing metadata for corresponding infostores of similar kind. A metastore serves to limit search scope of a query target. Each document in a metastore repository describes the kind of data contained in an associated infostore. A metastore also represents a manifest for hosted information, serves as a testament made by the endpoint user regarding hosted information.

Infostore and metastore life-cycle management is at the purview of a user managing a DMS3 endpoint node, also known as a **_source_** of information.

- A source may create infostores for personal use or to publish and share information with nodes in the peer-to-peer network.

- A metastore is defined by associating (or binding) a locally unique key name with a set of locally hosted infostores of similar kind. Key names are not required to be unique in the global DMS3 peer-to-peer network.

- Ideally, the community of sources shares information using commonly accepted key names for aggregating metadata representing similar content semantics into searchable metadata repositories. For example,

    - A source that may want to publish and share blogs, uses a metastore under the kind key "blog" to describe the infostores food-blog and product-brand-review-blog, each of which consists of a reposet.

    - Food and product review blogs can now be added to the respective infostores.

    - The source also creates a metastore under the kind key blog, associated with the food-blog and product-brand-review-blog inforstores. This metastore also consists of a reposet.

    - A document is added to the blog metastore to describe the food-blog infostore.

    - A separate document is added the blog metastore to describe the product-brand-review-blog.

- A source publishes information by announcing available metastores to the peer-to-peer network.
- Published information is routed through the peer-to-peer network to enable remote search and access.
- Published documents are always on the local endpoint node controlled by the source.
- Routing protocols enable periodic refresh and revocation of published information.
- Published information may represent a broad number of semantic themes, such as:
    - tweets, messages, blogs, podcasts, media, news, research, and other kinds of articles
    - applications that people can access and run on their client nodes
    - data produced by an application that others can search

Any source managing a dms3 node may decide independently to assume a role as **_curator_** [^3] providing information services on behalf of other peer-to-peer network nodes.

[^3]: curators provide compute and storage hosting resources for published information in return for income.


## Installing the software

Installation instructions will be provided at a later time. DMS3 builds on forked versions of key open source software components. A partial list of key components is provided below, additional credits will be provided at a later time.

- [IPFS](https://ipfs.io/) with modifications to support new capabilities. In particular dms3 adds the ability to manage document index repositories and dis-intermediated search.

- [Indri Search Engine](https://sourceforge.net/p/lemur/wiki/Home/) as its underlying Search Engine (**_SE_**) with some modifications that allow extensible non-text data type support.

## Configuring the software

When you initialize dms3 with the _dms3 init_ command, dms3 uses a repository in the local file system.

By default, the repo is located at ~/.dms3. To change the repo location, set the $DMS3_PATH environment variable:

```bash
    export DMS3_PATH=/path/to/dms3repo
```

dms3 expands the use of local file system by using the _index_ sub-folder within it for managing index repositories.


## Decentralized applications (DApps)

### Base command line interface (CLI)

#### Summary of index management commands

A summary of _index_ repository life-cycle commands are listed below.

- use _config_ to get or set index parameters
- use _mkidx_ to create an index repository
- use _mkdoc_ to generate a document template to be indexed
- use _addoc_ to index a document
- use _rmdoc_ to delete a document added to an index repository
- use _publish_ to share, allowing p2p notes to find, documents in an index
- use _ls_ to display a list of index repositories
- use _show_ to display a document in an index repository
- use _stat_ to display index repository statistics
- use _start_ to run in daemon mode
- use _stop_ to exit daemon mode
- use _restart_ to restart the daemon
- use _recover_ to destructively rebuild a corrupted index repository

When not running in daemon mode, each command affecting an index, first performs an integrity check on the index and may detect that the index has been corrupted. Some corruptions may be healed, in other cases the _recover_ command must be used to destroy the corrupted index and rebuild the index from redundant storage. In daemon mode, index integrity checks are performed when the daemon first opens and index repository.


### Base graphic user interface (GUI)

To-Be-Specified

## Search Engine

DMS3 uses a forked version of the open source [Indri Search Engine](https://sourceforge.net/p/lemur/wiki/Home/) as its underlying Search Engine (**_SE_**) with some modifications that allow extensible non-text data type support.

Refer to the following page for [a background on the Indri search engine](https://sourceforge.net/p/lemur/wiki/Language%20Modeling%20and%20Information%20Retrieval%20Background/).

Refer to [Indri repository structure](https://sourceforge.net/p/lemur/wiki/Indri%20Repository%20Structure/) for a discussion of the repository on disk structure.

Refer to [Indri parameters file](https://sourceforge.net/p/lemur/wiki/IndriBuildIndex%20Parameters/) for a discussion of search engine indexer configuration parameters.

## Application programming interface (API)

DMS3 provides an API to simplify dapp developments that target the platform. Details of the dms3api will be provided at a later time.

The API helps dapps create or connect to multiple repositories. Each repository has an active in-memory index where new documents are added to the index dynamically. In-memory indexes are written out to disk for durability, and when the number of indexes grows a background process merges repository indexes consolidating them on disk. All this dynamic  index management is performed by the search engine for each of the connected repository contexts.

dms3 treats each index repository as a container, and manages the growth in information using multiple kinds of reposets to physically chard information in bounded containers.

### Index repository state

dms3 uses a path naming convention for each index repository on the local file system within the _index_ sub-folder. In the following example:
```bash
ls ~/.dms3/index/reposet/blog/myblog20/w1543348319-a1-c1-o0/
```
where path components have the following implied semantics

~/.dms3
: initialized local file system dms3 root

/index
: initialized local file system index repository root

/reposet
: root folder for all resposets on the local file system

/blog
: _kind_ name of the reposet, shared by all repos in the set.

/myblog20
: root folder name for a physical repo instance

/w1543348319-a1-c1-o0
: root folder for a logical grouping repo instance

    with name components encoding index metadata:

    w
    : window start time value, seconds since epoch

    a
    : area value associated with documents

    c
    : category value associated with documents

    o
    : offset value used to encode absolute times
    area/category designations provide applications with a logical sharding mechanism.


### Index metadata and fields

The indexer defines metadata as non-searchable _fields_ and provides forward and reverse document lookup by metadata value. Whereas, fields can be used to qualify query searches.

### Document structure and schema

Fields are used to convey document structure and are used to influence document queries.

### Supported document parsers

The preconfigured file class environment and parsers [indexer file formats](https://sourceforge.net/p/lemur/wiki/Indexer%20File%20Formats/) exhibit the metadata extraction behavior discussed here.

The metadata fields for an index is defined by the parameters supplied
when the index is created. The only metadata required is _docno_, used to
prevent indexing duplicate documents. The metadata field _docno_ also  represents the external name or id of the document.

As of this writing, for a document being indexed (using the addFile interface), the parser always adds the following metadata for **html, xml, text, pdf** files. For example:

path
: /home/username/temp/data/sitemap.xml

docno
: /home/username/temp/data/sitemap.xml

For **html, xml, text** documents, the parser additionally adds the metadata. For example:

filetype
: _TEXT_

For **html, pdf** documents, the parser additionally adds the metadata. For example:

url
: /

For **pdf** documents, the parser additionally adds the metadata. For example:

title
: <from file, ex: timeoutcdf.eps>

author
: <from file, ex: ongardie>

The parser additionally adds the metadata listed within a document, regardless of position in the document tag hierarchy (i.e. whether the metadata fields are wrapped within a <meta> tag or not). the metadata associated with a document is not required to include all the metadata keys specified during index creation.

A file may contain multiple documents wrapped within a <document> tag, in the absence of such a tag, the file represents a single document.

The indexer requires one metadata field for documents: _docno_, the value
of this field must be unique within the index, otherwise the document is
parsed but not indexed.

When an application generated (using the addString interface) document includes a _docno_ field, the index will contain two _docno_ fields. The
first added by the parser, and the second added by the application. The
parser will use the last occurrence as the authoritative _docno_ record.


## Network protocol stack

DMS3 offers two classes of fault tolerant services.

1. Centralized practical byzantine fault tolerant (PBFT) services provide protection for personal private data.

    For personal information not publicly shared, DMS3 offers centralized PBFT services for maintaining immutable data integrity. The immutable state for a repository consists of the initial configuration parameters, documents added to the repository corpus, and other repository management state.

2. Decentralized p2p protocol services to provide distribution and access services for shared public data.

    For shared public information, this service defrays the hosting and access costs of dedicated compute and storage resources.

The following sub-sections describe these services.

### Practical byzantine fault tolerant (PBFT) services

DMS3 allows participants to offer centralized data protection services to protect the users' personal information.

These services protect persisted information by storing dapp consistent redundant copies on multiple endpoints dedicated to one user. The set of endpoints so configured by a user form a user private network that enables access to data from any node within the defined set.

The set of nodes in the private network may be owned by the user or leased from a centralized service provider. Network services encrypt data with the user private key prior to storing on leased equipment.

Nodes and storage scaling capacity of private networks may be restricted to enable reasonable performance expectations. Private networks may be configured to asymmetrically distribute redundant information to compensate for variations of hardware configurations within the defined set of endpoints.

dms3 implements two PBFS services.

1. Information storage and retrieval, providing index and query services.
2. Network file system (NFS) service, providing backing for node storage.

DMS3 PBFS services automatically recover from a configured number of arbitrary simultaneous faults.


#### Index fault recovery

The search engine can heal and recover an index from certain faults on its own, for other faults external recovery support using redundant data is required.

There are two options to recover an index repository.

1. Redundant repository state stored in dms3. When an index is created, its configuration state is saved external to the search engine. When a document is added to an index, the document is also saved external to the search engine. The _recover_ command can destructively recreate the index from this externally save information. This recovery option does not rely on PBFT.

2. Redundant copies the local file system index repository root. Optional PBFT services automatically keep redundant copies of information in sync. Recovery for simultaneous fault counts exceeding the configured protection level must be handled manually as an exception case.


### Peer to peer (p2p) network services

#### Publishing a repository

Use the _index_ _publish_ command to enable other users on the p2p network to search and access the contents of a repository.

### Published information distribution and protection services

High demand for published information can overburden participant's compute resources, dms3 enables optional paid p2p services to offload compute and storage onto other p2p nodes.

p2p protocol network enables participants to contribute compute and storage resources to gain income in return to the use of their resources.

#### Information curators

Any source managing a dms3 node may decide independently to assume a role as **_curator_** providing information services to the peer-to-peer network. These services are independent from locally published information and relate to information published by remote peer sources.

- A curator may choose to operate in limited **_roles_**, offering one or more hosting services to the peer-to-peer network:
    - A **_directory_** role indicates a hosting service that enables metastore metadata queries.
    - A **_replica_** role indicates a hosting service that enables infostore data queries.
    - Each curator peer node decides what information and role, if any, is offered.
    - Curators offer source authors information distribution and access services.
- Hosting services are periodically announced to the peer-to-peer network via the routing protocol.

The dms3 peer-to-peer network will offer **_cryptocurrency_** as incentive gas to drive the information flow economy.
- A Curator may earn coins/tokens for hosting services
- An Author may pay transaction fees for distribution on the network
- An Author may earn tokens for published content in the information flow economy
    - Paid content is structured as a pair of documents
    - Free promotional (excerpt, abstract) short form content with terms references
    - Paid long form content with term enforced by a smart contract
    - Smart contracts enforce terms for a variety of payment plans
