-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
18 changed files
with
283 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ | |
/src/Debug | ||
/src/Grpc/NativeGrpc/NativeGrpc.vcxproj.user | ||
**/coverage.net5.0.xml | ||
/src/Tests/Coverage/coverage.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2021 James Courtney | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Samples.WriteThrough | ||
{ | ||
using System; | ||
using System.Diagnostics; | ||
using FlatSharp; | ||
|
||
/// <summary> | ||
/// FlatSharp supports Write-Through in limited cases: | ||
/// - Serialization method is VectorCacheMutable | ||
/// - Struct field has been opted into write-through. | ||
/// | ||
/// Write Through allows you to make updates to an already-serialized FlatBuffer in-place without a full parse or re-serialize. | ||
/// This is extremely performant, especially for large buffers as it avoids series of copies and allows in-place updates. | ||
/// </summary> | ||
public class WriteThroughSample | ||
{ | ||
public static void Run() | ||
{ | ||
Roster roster = new Roster | ||
{ | ||
Walkers = new[] | ||
{ | ||
new DogWalker { Name = "Beth", CurrentDog = new DogReference { Id = 7 }, Position = new Location { Latitude = 1.2, Longitude = 2.1, NoWriteThrough = 10 } }, | ||
new DogWalker { Name = "Jerry", CurrentDog = new DogReference { Id = 127 }, Position = new Location { Latitude = 12.7, Longitude = 128 } }, | ||
new DogWalker { Name = "Summer", CurrentDog = new DogReference { Id = 22 }, Position = new Location { Latitude = 36.4, Longitude = 32.1 } }, | ||
} | ||
}; | ||
|
||
// Write the data to the buffer the first time. | ||
byte[] data = new byte[1024]; | ||
Roster.Serializer.Write(data, roster); | ||
|
||
// Parsed now refers to the buffer. Any changes we make to the fs_writeThrough fields will be reflected back. | ||
Roster parsed = Roster.Serializer.Parse(data); | ||
var walker = parsed.Walkers![0]; | ||
|
||
// Walking dog 8 in Tokyo now. | ||
walker.CurrentDog!.Id = 8; | ||
walker.Position!.Latitude = 35.683; | ||
walker.Position.Longitude = 139.808; | ||
|
||
// we can update this field, but since writethrough is disabled, the changes won't be reflected in the buffer. | ||
walker.Position.NoWriteThrough = 1024; | ||
|
||
// Now let's parse again and see our changes. | ||
// Notice that we never invoked .Write to rewrite the whole structure back. | ||
// We only mutated a few fields. | ||
parsed = Roster.Serializer.Parse(data); | ||
|
||
walker = parsed.Walkers![0]; | ||
|
||
Debug.Assert(walker.CurrentDog!.Id == 8, "Dog has been updated. Was 7 originally."); | ||
Debug.Assert(walker.Position!.Latitude == 35.683, "Latitude has been updated. Was 1.2 originally."); | ||
Debug.Assert(walker.Position!.Longitude == 139.808, "Longitude has been updated. Was 2.1 originally."); | ||
Debug.Assert(walker.Position!.NoWriteThrough == 10, "NoWriteThrough was unchanged."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2021 James Courtney | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
|
||
// WriteThrough is great when you only need to make small changes to a FlatBuffer and don't want to pay the cost | ||
// of a complete read-modify-write sequence. With WriteThrough enabled, you can make select updates directly back to the buffer. | ||
|
||
// WriteThrough is only applicable to structs, and is only available when using "VectorCacheMutable" as the serializer. | ||
|
||
namespace Samples.WriteThrough; | ||
|
||
// In this contrived example, imagine an animal shelter. There are lots of volunteers walking different dogs. | ||
// Each volunteer only has one dog at a time, but they can change dogs quite frequently. We'd like to | ||
// be able to update the current dog that each volunteer is walking without doing a full parse and re-serialize | ||
// of the entire list of volunteers. There are also lots of dog-nappers around, so we need to be able to update | ||
// the last known location of a given volunteer so we can call the police if they go missing. | ||
|
||
table Roster (fs_serializer:"VectorCacheMutable") { | ||
Walkers : [DogWalker]; | ||
} | ||
|
||
table DogWalker { | ||
Name : string; | ||
CurrentDog : DogReference; | ||
Position : Location; | ||
} | ||
|
||
struct DogReference { Id : int (fs_writeThrough); } | ||
|
||
// Can be applied to all items in a struct. | ||
struct Location (fs_writeThrough) { | ||
Latitude : double; | ||
Longitude : double; | ||
NoWriteThrough : int (fs_writeThrough:"false"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="..\..\common.props" /> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
<AssemblyName>FlatSharpCompilerTests</AssemblyName> | ||
<RootNamespace>FlatSharpTests</RootNamespace> | ||
<DelaySign>false</DelaySign> | ||
<SignAssembly>true</SignAssembly> | ||
<Nullable>annotations</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\FlatSharpTests\**\*.cs"> | ||
<Link>FS\%(RecursiveDir)%(Filename)%(Extension)</Link> | ||
</Compile> | ||
<Compile Include="..\FlatSharpCompilerTests\**\*.cs"> | ||
<Link>Compiler\%(RecursiveDir)%(Filename)%(Extension)</Link> | ||
</Compile> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AltCover" Version="6.7.750" /> | ||
<PackageReference Include="Grpc.Core" Version="2.27.0" /> | ||
<PackageReference Include="Grpc.Core.Api" Version="2.35.0" /> | ||
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" /> | ||
<PackageReference Include="ReportGenerator" Version="4.1.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\FlatSharp.Compiler\FlatSharp.Compiler.csproj" /> | ||
<ProjectReference Include="..\..\FlatSharp.Runtime\FlatSharp.Runtime.csproj" /> | ||
<ProjectReference Include="..\..\FlatSharp.Unsafe\FlatSharp.Unsafe.csproj" /> | ||
<ProjectReference Include="..\..\FlatSharp\FlatSharp.csproj" /> | ||
<ProjectReference Include="..\..\Google.FlatBuffers\Google.FlatBuffers.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dotnet test /p:AltCoverForce=true /p:AltCover=true /p:AltCoverAttributeFilter=ExcludeFromCodeCoverage /p:AltCoverStrongNameKey=..\..\..\misc\strongname.snk -v d -f net5.0 | ||
dotnet %UserProfile%\.nuget\packages\reportgenerator\4.1.2\tools\netcoreapp2.1\ReportGenerator.dll -reports:coverage.xml -targetdir:.coverage\ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.