Documentation
Table of Contents

Class FolderDocumentStore

Namespace
IntentSharp.Documents
Assembly
IntentSharp.dll

An IDocumentStore over a folder people edit: each document is a file in the working folder (its working copy), and the store keeps every version's text, the running pointers and the state values beside it, in .intentsharp inside the folder unless StateRoot names another folder. An edit made to a working copy, while the application runs or while it is stopped, becomes a version written by External, and runs only if the document's validators pass it.

public sealed class FolderDocumentStore : IDocumentStore, IDisposable
Inheritance
object
FolderDocumentStore
Implements
IDisposable
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.ReferenceEquals(object, object)
object.ToString()
Extension Methods

Remarks

Layout, with the default state folder:

<root>/rules/orders.rules.yaml                                       the working copy (people edit here)
<root>/.intentsharp/history/rules/orders.rules.yaml/000007-9f3a1c2e9b10.yaml   the text of version 7
<root>/.intentsharp/state.json                                       running pointers and state values
<root>/.intentsharp/lock                                             held only during a write

The running version is always read from the history, never from the working copy, so a half-typed edit is never read. When a version is activated through the store, the working copy is rewritten atomically (a temporary file, then a replace) to show it. A working copy that holds an edit the store has not read yet is first kept as a version of its own, so the rewrite loses nothing. A file that appears in the working folder with no history is imported as version 1 and runs; the readers of each document kind validate the running text themselves. Files and folders whose name starts with a dot (on every platform), hidden or system ones, folders reached through a link (a symbolic link or a junction), and those Include leaves out are not documents; each folder link the store does not enter is reported in Problems under the folder's path. A file link is read only when its target, every link followed, lies in the working folder and outside the state folder (as in a folder mounted from a configuration map); any other is reported in Problems and not read. A working copy larger than MaxDocumentBytes is reported and not read either. A document whose working copy would lie in the state folder (in any spelling that opens it), or be written through a link out of the working folder, cannot be saved or activated. On Windows a volume mounted in a folder counts as such a link, since its target names a volume and not a path the store can check: a working folder under one is read, but every save and activation through it is refused naming the mounted folder, and the store logs a warning when it opens. Open the store on the volume's drive letter instead.

What is an external edit. For each document the store remembers the hash of the text it last read from the working copy or last wrote to it (in state.json, so it holds across restarts). When the folder is read, a working copy that shows the running version is left alone, and one that holds a rejected version keeps that rejection reported. One that still has the remembered hash has not changed since the store last looked: the running version moved through the store and the working copy could not be rewritten, so nothing is read, the running version stays in effect, and the store reports the problem and tries the rewrite again each time it reads the folder. Any other text is an edit and becomes a new version, even when it equals an older version or a candidate: a person who puts such a text back has made an edit. An activation writes state.json before it rewrites the working copy and records the rewritten text in a second write after it, so a crash between the two leaves a working copy that still has the remembered hash, which the next read of the folder rewrites instead of taking it for an edit. A read-only store (IsReadOnly) never rewrites a working copy: a version saved or activated through it runs from memory while the file keeps its text, which is read again only once it changes.

An external edit is checked by the validators in Validators and those of every IntentClient built with this store. An edit that loads cleanly runs. One that does not is stored, marked rejected and reported in Problems and the log, while the running version stays in effect and the working copy is left for the person to fix (SI-6, SI-8). An edit to a document that no validator handles runs, since it is the person's own file; a library edit to such a document is refused instead (see IDocumentValidator). When a client is built after the store, the client's validators also check the versions that ran before it existed: a version they fail is rejected and the version that ran before it runs again, or, for a document with no earlier version, none runs until the file loads cleanly. Each built client's validators stay attached for the life of the store, so build the clients of one store once, not per request or per tenant. A validator may read the store during an assessment but not write to it: a save, activation, rejection, state write or scan it starts on the store it is checking throws InvalidOperationException, and the edit is rejected. The store's contract otherwise holds as IDocumentStore states it.

Several store instances, in one process or several, may share a folder: every write re-reads state.json while it holds the lock file, so a save based on a stale version and a state write with a stale etag conflict, and of several instances that create one state key with TryCreateStateAsync(string, JsonNode, CancellationToken) exactly one creates it. An instance that watches the folder also reads another instance's writes. Pruning keeps MaxVersionsPerDocument versions of each document, never removing the running version, the last good version or a candidate. Every write rewrites the one state.json, which suits folders of up to some thousands of documents.

Limits. Document ids compare ordinally, but on a file system that ignores case two ids that differ only by case share one working copy and one history folder. Changes to a document on a local folder are noticed; on a network share changes may be missed (the known gap of the folder catalog), so a fleet that shares documents implements IDocumentStore on shared storage instead. A history file changed by hand is refused, not served. Reading a version whose history file is missing or was changed throws IOException. When another instance holds the lock file for longer than LockTimeout while the folder is read, the first working copy that needs a write reports it, and the other edited working copies wait for the next read of the folder instead of each waiting out the timeout.

Constructors

FolderDocumentStore(string, FolderDocumentStoreOptions?, ILogger<FolderDocumentStore>?)

Opens the store over the working folder root: reads its state, starts watching (unless WatchForChanges is false) and reads the working folder once before returning, so edits made while the application was stopped are already handled. The constructor blocks on that file input and output.

Properties

IsReadOnly

True when the store keeps versions and state in memory only: ReadOnly asked for it, or the state folder could not be written. History and comparison across restarts are then off.

Problems

The problems the store found, with the document each concerns: working copies that could not be read, were deleted, are larger than MaxDocumentBytes or are links the store does not follow, folder links the store does not enter (under the folder's path, which names no document), files the Include filter threw for, a working folder that is missing, external edits that were rejected (with their validators' problems, for as long as the working copy holds the rejected text), and working copies that could not be rewritten to show the running version (for as long as they do not show it; a read-only store, which never rewrites them, does not report these). The list is replaced as a whole when the working folder is read or a write changes what it reports; each new problem is also logged at Warning.

Root

The full path of the working folder.

StateRoot

The full path of the folder that holds the history, state.json and the lock file (unused when IsReadOnly).

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.

Dispose()

Stops watching and cancels a write or read of the folder in progress. Reads keep answering from what the store holds; writes and ScanAsync(CancellationToken) throw ObjectDisposedException. Calling it more than once is safe.

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.

ScanAsync(CancellationToken)

Reads the working folder now: imports new files, hands edited working copies to the document coordinator, and replaces Problems. A watching store does this by itself once the folder is quiet after a change; call it when watching is off. It also takes in what other store instances wrote to the state, and rewrites working copies an earlier rewrite failed to update.

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).