Skip to content

Commit cf78b3d

Browse files
committed
Add a documentation site
1 parent f4484a2 commit cf78b3d

File tree

13 files changed

+380
-2
lines changed

13 files changed

+380
-2
lines changed

.github/workflows/pages.yml

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: GitHub Pages
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- docs # temporary for testing
8+
9+
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
10+
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
11+
concurrency:
12+
group: pages
13+
cancel-in-progress: false
14+
15+
jobs:
16+
build-docs:
17+
name: Build Documentation
18+
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@v4
23+
- name: Setup .NET
24+
uses: actions/setup-dotnet@v4
25+
with:
26+
dotnet-version: 9.0.x
27+
28+
- name: Install docfX
29+
run: dotnet tool update -g docfx
30+
- name: Build documentation
31+
run: docfx docfx.json
32+
33+
- name: Upload artifact
34+
uses: actions/upload-pages-artifact@v3
35+
with:
36+
path: 'artifacts/_site'
37+
38+
publish-docs:
39+
name: Publish Documentation
40+
needs: build-docs
41+
42+
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
43+
permissions:
44+
actions: read
45+
pages: write
46+
id-token: write
47+
48+
# Deploy to the github-pages environment
49+
environment:
50+
name: github-pages
51+
url: ${{ steps.deployment.outputs.page_url }}
52+
53+
runs-on: ubuntu-latest
54+
steps:
55+
- name: Deploy to GitHub Pages
56+
id: deployment
57+
uses: actions/deploy-pages@v4

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ BenchmarkDotNet.Artifacts/
2424
test-results/
2525
TestResults/
2626
.DS_Store
27+
/api/

COPYING.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2011-2020, Google, Inc. and Snappier Authors
1+
Copyright 2011-2024, Google, Inc. and Snappier Authors.
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without

Snappier/Snappier.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<PackageLicenseExpression>BSD-3-Clause</PackageLicenseExpression>
2727
<PackageReadmeFile>README.md</PackageReadmeFile>
2828
<PackageIcon>icon.png</PackageIcon>
29+
<PackageProjectUrl>https://brantburnett.github.io/Snappier/</PackageProjectUrl>
2930
<GenerateDocumentationFile>true</GenerateDocumentationFile>
3031
<PublishRepositoryUrl>true</PublishRepositoryUrl>
3132
<EmbedUntrackedSources>true</EmbedUntrackedSources>
@@ -44,7 +45,7 @@
4445

4546
<ItemGroup>
4647
<None Include="..\README.md" Pack="true" PackagePath="$(PackageReadmeFile)" />
47-
<None Include="..\icon.png" Pack="true" PackagePath="$(PackageIcon)" />
48+
<None Include="..\images\icon.png" Pack="true" PackagePath="$(PackageIcon)" />
4849
</ItemGroup>
4950

5051
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">

docfx.json

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/dotnet/docfx/main/schemas/docfx.schema.json",
3+
"metadata": [
4+
{
5+
"src": [
6+
{
7+
"src": "./Snappier",
8+
"files": [
9+
"**/*.csproj"
10+
]
11+
}
12+
],
13+
"dest": "api",
14+
"properties": {
15+
"TargetFramework": "net8.0"
16+
}
17+
}
18+
],
19+
"build": {
20+
"content": [
21+
{
22+
"files": [
23+
"**/*.{md,yml}"
24+
],
25+
"exclude": [
26+
"_site/**",
27+
"artifacts/**",
28+
"**/BenchmarkDotNet.Artifacts/**"
29+
]
30+
}
31+
],
32+
"resource": [
33+
{
34+
"files": [
35+
"images/**"
36+
]
37+
}
38+
],
39+
"output": "artifacts/_site",
40+
"template": [
41+
"default",
42+
"material/material"
43+
],
44+
"globalMetadata": {
45+
"_appName": "Snappier",
46+
"_appTitle": "Snappier",
47+
"_appLogoPath": "images/icon-48.png",
48+
"_disableContribution": true,
49+
"_enableSearch": true,
50+
"pdf": false
51+
},
52+
"postProcessors": ["ExtractSearchIndex"]
53+
}
54+
}

docs/block.md

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Block Compression
2+
3+
Block compression is ideal for data up to 64KB, though it may be used for data of any size. It does not include any stream
4+
framing or CRC validation. It also doesn't automatically revert to uncompressed data in the event of data size growth.
5+
6+
## Block compression/decompression using a buffer you already own
7+
8+
```cs
9+
using Snappier;
10+
11+
public class Program
12+
{
13+
private static byte[] Data = {0, 1, 2}; // Wherever you get the data from
14+
15+
public static void Main()
16+
{
17+
// This option assumes that you are managing buffers yourself in an efficient way.
18+
// In this example, we're using heap allocated byte arrays, however in most cases
19+
// you would get these buffers from a buffer pool like ArrayPool<byte> or MemoryPool<byte>.
20+
21+
// If the output buffer is too small, an ArgumentException is thrown. This will not
22+
// occur in this example because a sufficient buffer is always allocated via
23+
// Snappy.GetMaxCompressedLength or Snappy.GetUncompressedLength. There are TryCompress
24+
// and TryDecompress overloads that return false if the output buffer is too small
25+
// rather than throwing an exception.
26+
27+
// Compression
28+
byte[] buffer = new byte[Snappy.GetMaxCompressedLength(Data)];
29+
int compressedLength = Snappy.Compress(Data, buffer);
30+
Span<byte> compressed = buffer.AsSpan(0, compressedLength);
31+
32+
// Decompression
33+
byte[] outputBuffer = new byte[Snappy.GetUncompressedLength(compressed)];
34+
int decompressedLength = Snappy.Decompress(compressed, outputBuffer);
35+
36+
for (var i = 0; i < decompressedLength; i++)
37+
{
38+
// Do something with the data
39+
}
40+
}
41+
}
42+
```
43+
44+
## Block compression/decompression using a memory pool buffer
45+
46+
```cs
47+
using Snappier;
48+
49+
public class Program
50+
{
51+
private static byte[] Data = {0, 1, 2}; // Wherever you get the data from
52+
53+
public static void Main()
54+
{
55+
// This option uses `MemoryPool<byte>.Shared`. However, if you fail to
56+
// dispose of the returned buffers correctly it can result in inefficient garbage collection.
57+
// It is important to either call .Dispose() or use a using statement.
58+
59+
// Compression
60+
using (IMemoryOwner<byte> compressed = Snappy.CompressToMemory(Data))
61+
{
62+
// Decompression
63+
using (IMemoryOwner<byte> decompressed = Snappy.DecompressToMemory(compressed.Memory.Span))
64+
{
65+
// Do something with the data
66+
}
67+
}
68+
}
69+
}
70+
```
71+
72+
## Block compression/decompression using a buffer writter
73+
74+
```cs
75+
using Snappier;
76+
using System.Buffers;
77+
78+
public class Program
79+
{
80+
private static byte[] Data = {0, 1, 2}; // Wherever you get the data from
81+
82+
public static void Main()
83+
{
84+
// This option uses `IBufferWriter<byte>`. In .NET 6 you can get a simple
85+
// implementation such as `ArrayBufferWriter<byte>` but it may also be a `PipeWriter<byte>`
86+
// or any other more advanced implementation of `IBufferWriter<byte>`.
87+
88+
// These overloads also accept a `ReadOnlySequence<byte>` which allows the source data
89+
// to be made up of buffer segments rather than one large buffer. However, segment size
90+
// may be a factor in performance. For compression, segments that are some multiple of
91+
// 64KB are recommended. For decompression, simply avoid small segments.
92+
93+
// Compression
94+
var compressedBufferWriter = new ArrayBufferWriter<byte>();
95+
Snappy.Compress(new ReadOnlySequence<byte>(Data), compressedBufferWriter);
96+
var compressedData = compressedBufferWriter.WrittenMemory;
97+
98+
// Decompression
99+
var decompressedBufferWriter = new ArrayBufferWriter<byte>();
100+
Snappy.Decompress(new ReadOnlySequence<byte>(compressedData), decompressedBufferWriter);
101+
var decompressedData = decompressedBufferWriter.WrittenMemory;
102+
103+
// Do something with the data
104+
}
105+
}
106+
```
107+
108+
## Block compression/decompression using heap allocated byte[]
109+
110+
```cs
111+
using Snappier;
112+
113+
public class Program
114+
{
115+
private static byte[] Data = {0, 1, 2}; // Wherever you get the data from
116+
117+
public static void Main()
118+
{
119+
// This is generally the least efficient option,
120+
// but in some cases may be the simplest to implement.
121+
122+
// Compression
123+
byte[] compressed = Snappy.CompressToArray(Data);
124+
125+
// Decompression
126+
byte[] decompressed = Snappy.DecompressToArray(compressed);
127+
}
128+
}
129+
```

docs/getting-started.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Getting Started
2+
3+
## Installing
4+
5+
Simply add a NuGet package reference to the latest version of Snappier.
6+
7+
```xml
8+
<PackageReference Include="Snappier" Version="1.1.6" />
9+
```
10+
11+
or
12+
13+
```sh
14+
dotnet add package Snappier
15+
```

docs/stream.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Stream Compression
2+
3+
Stream compression reads or writes the [Snappy framing format](https://github.com/google/snappy/blob/master/framing_format.txt) designed for streaming.
4+
It is ideal for data being sent over a network stream, and includes additional framing data and CRC validation.
5+
It also recognizes when an individual block in the stream compresses poorly and will include it in uncompressed form.
6+
7+
## Stream compression/decompression
8+
9+
Compressing or decompressing a stream follows the same paradigm as other compression streams in .NET. `SnappyStream` wraps an inner stream. If decompressing you read from the `SnappyStream`, if compressing you write to the `SnappyStream`
10+
11+
```cs
12+
using System.IO;
13+
using System.IO.Compression;
14+
using Snappier;
15+
16+
public class Program
17+
{
18+
public static async Task Main()
19+
{
20+
using var fileStream = File.OpenRead("somefile.txt");
21+
22+
// First, compression
23+
using var compressed = new MemoryStream();
24+
25+
using (var compressor = new SnappyStream(compressed, CompressionMode.Compress, leaveOpen: true))
26+
{
27+
await fileStream.CopyToAsync(compressor);
28+
29+
// Disposing the compressor also flushes the buffers to the inner stream
30+
// We pass true to the constructor above so that it doesn't close/dispose the inner stream
31+
// Alternatively, we could call compressor.Flush()
32+
}
33+
34+
// Then, decompression
35+
36+
compressed.Position = 0; // Reset to beginning of the stream so we can read
37+
using var decompressor = new SnappyStream(compressed, CompressionMode.Decompress);
38+
39+
var buffer = new byte[65536];
40+
var bytesRead = decompressor.Read(buffer, 0, buffer.Length);
41+
while (bytesRead > 0)
42+
{
43+
// Do something with the data
44+
45+
bytesRead = decompressor.Read(buffer, 0, buffer.Length)
46+
}
47+
}
48+
}
49+
```

docs/toc.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
- name: Getting Started
2+
href: getting-started.md
3+
- name: Block Compression
4+
href: block.md
5+
- name: Stream Compression
6+
href: stream.md

images/icon-48.png

6.23 KB
Loading
File renamed without changes.

0 commit comments

Comments
 (0)