-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathState.cs
More file actions
64 lines (55 loc) · 2.44 KB
/
State.cs
File metadata and controls
64 lines (55 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections.Immutable;
using System.IO;
using MessagePack;
using MessagePack.ImmutableCollection;
using MessagePack.Resolvers;
namespace ExampleProject;
/// <summary>
/// The immutable state of the application: a library of documents and an index
/// to find documents containing a value.
/// </summary>
public sealed class State(
ImmutableDictionary<string, int> index,
ImmutableList<ImmutableList<int>> documentLists,
ImmutableList<State.Document> documents)
{
/// <summary> A document. </summary>
[MessagePackObject]
public sealed class Document(int id, string path, string contents)
{
/// <summary>
/// The identifier of the document (its position in the documents
/// array).
/// </summary>
[Key(0)]
public int Id { get; } = id;
/// <summary> The path from which the document was imported. </summary>
[Key(1)]
public string Path { get; } = path ?? throw new ArgumentNullException(nameof(path));
/// <summary> The contents of the document. </summary>
[Key(2)]
public string Contents { get; } = contents ?? throw new ArgumentNullException(nameof(contents));
}
/// <summary>
/// For each word, the position (in <see cref="DocumentLists"/>) of the list of all documents
/// where it appears, case-insensitive.
/// </summary>
public ImmutableDictionary<string,int> Index { get; } = index ?? throw new ArgumentNullException(nameof(index));
/// <summary> The table of document lists. </summary>
/// <remarks>
/// Documents are indexed by their position in <see cref="Documents"/>.
/// </remarks>
public ImmutableList<ImmutableList<int>> DocumentLists { get; } = documentLists ?? throw new ArgumentNullException(nameof(documentLists));
/// <summary> All known documents. </summary>
public ImmutableList<Document> Documents { get; } = documents ?? throw new ArgumentNullException(nameof(documents));
/// <summary>
/// The MessagePack options to use to serialize and deserialize the contents
/// of the LargeImmutableList tables.
/// </summary>
public static MessagePackSerializerOptions MessagePackOptions { get; } =
MessagePackSerializerOptions.Standard.WithResolver(
CompositeResolver.Create(
ImmutableCollectionResolver.Instance,
StandardResolver.Instance));
}