Document Storage Management

Each storage can have additional settings at least each storage has unique name. Besides it there is support of the indexes. Indexes support the efficient execution of queries. Without indexes the storage must scan every document in a collection, to select those documents that match the query statement. If an appropriate index exists for a query, the storage can use the index to limit the number of documents it must inspect.

Indexes are special data structures that store a small portion of the storage’s data set in an easy to traverse form. The index stores the value of a specific field or set of fields, ordered by the value of the field. The ordering of the index entries supports efficient equality matches and range-based query operations.

Default Indexes

The IDocumentStorage<TDocument> interface implementation works with descendants of the Document class which defines two mandatory fields: _id and _header, respectively, the unique identifier and the system information of the document. By default the _id is Guid but it is possible to use any other type. The _header is described using DocumentHeader class.

The DocumentHeader class contains system information which is used by an implementation of the storage to organize and control access to the documents. For example, the _tenant field helps to organize multitenancy and that field is used upon each query execution as an additional filter. Thus each document contains at least two indexes:

  • _id - for primary key
  • _header._tenant and _header._deleted - for multitenancy

Note

The IDocumentStorage interface implementation works the same but instead of the Document it uses DynamicDocument.

Additional Indexes

To define additional indexes you must create an instance of the DocumentMetadata class and set the Indexes property. Next example defines index by UserName field.

var userNameIndex = new DocumentIndex
                    {
                        Key = new Dictionary<string, DocumentIndexKeyType>
                              {
                                  { "UserName", DocumentIndexKeyType.Asc }
                              }
                    };

var userStoreMetadata = new DocumentMetadata
                        {
                            Type = "UserStore",
                            Indexes = new[] { userNameIndex }
                        };

After that you must get IDocumentStorageManager and invoke the CreateStorageAsync() method:

IDocumentStorageManager_ storageManager;

// ...

storageManager.CreateStorageAsync(userStoreMetadata);

Storage Provider

The IDocumentStorage<TDocument> and IDocumentStorage are used to access documents of the storage but besides data access they do additional logic. For example, providing interception mechanism and organizing multitenancy. And this logic can be a barrier to realize some system procedures such as data migration. To direct access to the storage without additional logic (interception, multitenancy and etc.), use IDocumentStorageProvider<TDocument> or IDocumentStorageProvider. To get them, use IDocumentStorageProviderFactory.