Documentation
Table of Contents

Interface IDocumentStore

Namespace
IntentSharp.Documents
Assembly
IntentSharp.dll

Versioned text documents and a few state values: the store behind intent files, rule files, compiled companions, settings and the provider file. The store works in text, not formats; reading a document's content is the job of whoever owns that kind of document. Every save makes a new version, and each document has at most one running version, the one a read without a sequence returns. Implement it to keep documents in any storage; the library ships InMemoryDocumentStore.

public interface IDocumentStore
Extension Methods

Remarks

What every implementation must do:

  • Sequences start at 1 per document, increase by one per save and are never reused.
  • A save whose BasedOn is set and is not the running sequence throws DocumentConflictException before anything is stored; a state write whose expected etag is not the current one throws it too.
  • Of several writers, in one process or several, that create one state key with TryCreateStateAsync(string, JsonNode, CancellationToken) at once, exactly one stores its value. The default implementation reads and then writes, so it does not hold this rule; a store that several clients or processes share overrides it.
  • Once a document has been activated it has exactly one running version.
  • LastActivatedAt is strictly increasing within a document: an activation is stamped with the later of the clock and the document's previous activation plus one tick.
  • A rejected version is never activated, and the running version cannot be rejected.
  • The change token of a prefix fires when a document under it gets a new running version (an activating save, an activation, a revert), and not for a save with Activate false, a state write or a rejection.
  • Every member that takes a DocumentId throws ArgumentException for the default value, which names no document.

A store may surface a change callback's exception to the caller after the change is stored: InMemoryDocumentStore throws an AggregateException from SaveAsync(DocumentSave, CancellationToken) or ActivateAsync(DocumentId, long, CancellationToken) once every token has fired. Such an exception means the change was applied and a callback failed, not that nothing was stored; read the document to find its running version.

Validation is not the store's: it stores any text. The library validates edits before it saves them.

Some readers wait on a store synchronously: the rule file source, the settings document and the provider file wait on ListAsync(string, CancellationToken) and ReadAsync(DocumentId, long?, CancellationToken) in a synchronous member when they reload after a change, at the start of a call or a check. An implementation over real I/O should use ConfigureAwait(false) on every await, so that it never needs the caller's synchronization context (in a UI application or classic ASP.NET such a wait deadlocks otherwise), and should keep those reads cheap.

Methods

ActivateAsync(DocumentId, long, CancellationToken)

Makes a stored version the document's running version, as a revert to an older version or the promotion of a version saved without activation. Activating the running version changes nothing and returns it.

GetChangeToken(string)

A token that fires once when any document whose path starts with prefix (compared ordinally) gets a new running version. Ask for a new token after it fires.

GetHistoryAsync(DocumentId, CancellationToken)

Every stored version of a document, rejected ones included, in ascending sequence; nothing for a document that does not exist.

ListAsync(string, CancellationToken)

The running versions of every document whose path starts with prefix (compared ordinally), in path order; an empty prefix lists them all.

MarkRejectedAsync(DocumentId, long, string, CancellationToken)

Marks a stored version rejected, with the reason: a version that failed validation, such as an external edit the library refused. The version stays in the history and can still be read by its sequence, but it is never activated. Marking a rejected version again replaces its reason.

ReadAsync(DocumentId, long?, CancellationToken)

Reads a version of a document: the running version when sequence is null, otherwise the version with that sequence, rejected or not. Returns null when the document has no running version, or no version with that sequence.

ReadStateAsync(string, CancellationToken)

Reads a state value; null when the key has never been written.

SaveAsync(DocumentSave, CancellationToken)

Stores a new version of a document and, when Activate is true, makes it the running version. Returns the new version.

TryCreateStateAsync(string, JsonNode, CancellationToken)

Creates a state value only when the key has none: of several writers, in one process or several, that create the same key at once, exactly one stores its value. Returns the entry as written, with its etag; null when the key already has a value, and then nothing was written. Read the key to see the value that is there.

WriteStateAsync(string, JsonNode, string?, CancellationToken)

Writes a state value. With expectedEtag null the value is created or overwritten; otherwise it is written only when the key's current etag is expectedEtag. Returns the entry as written, with its new etag. To create a value only when the key has none, call TryCreateStateAsync(string, JsonNode, CancellationToken).