Skip to content

Commit

Permalink
支持net45和netstandard2.0,投入使用
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Aug 3, 2024
1 parent 3e56ec7 commit bae4f5f
Show file tree
Hide file tree
Showing 15 changed files with 374 additions and 364 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/publish-beta.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: publish-beta

on:
push:
branches: [ master ]
paths:
- 'NewLife.NoDb/**'
pull_request:
branches: [ master ]
paths:
- 'NewLife.NoDb/**'
workflow_dispatch:

jobs:
build-publish:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Setup dotNET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
- name: Get Version
run: echo "VERSION=$(date '+%Y.%m%d-beta%H%M')" >> $GITHUB_ENV
- name: Build
run: |
dotnet pack --version-suffix ${{ env.VERSION }} -c Release -o out NewLife.NoDb/NewLife.NoDb.csproj
- name: Publish
run: |
dotnet nuget push ./out/*.nupkg --skip-duplicate --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.nugetKey }}
dotnet nuget push ./out/*.nupkg --skip-duplicate --source https://nuget.pkg.github.com/NewLifeX/index.json --api-key ${{ github.token }}
27 changes: 27 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: publish

on:
push:
tags: [ v* ]
workflow_dispatch:

jobs:
build-publish:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Setup dotNET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
- name: Get Version
run: echo "VERSION=$(date '+%Y.%m%d')" >> $GITHUB_ENV
- name: Build
run: |
dotnet pack --version-suffix ${{ env.VERSION }} -c Release -o out NewLife.NoDb/NewLife.NoDb.csproj
- name: Publish
run: |
dotnet nuget push ./out/*.nupkg --skip-duplicate --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.nugetKey }}
dotnet nuget push ./out/*.nupkg --skip-duplicate --source https://nuget.pkg.github.com/NewLifeX/index.json --api-key ${{ github.token }}
25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: test

on:
push:
branches: [ '*' ]
pull_request:
branches: [ '*' ]
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Setup dotNET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
- name: Build
run: |
dotnet build -c Release
- name: Test
run: |
dotnet test
Binary file added Doc/leaf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Doc/newlife.snk
Binary file not shown.
7 changes: 5 additions & 2 deletions NewLife.NoDb.sln
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.3
# Visual Studio Version 17
VisualStudioVersion = 17.10.35122.118
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NewLife.NoDb", "NewLife.NoDb\NewLife.NoDb.csproj", "{10BBCB6A-0FC3-4145-B39F-AD63E328A585}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{C4BB2B56-969C-42EC-BB57-C65AE5191AA3}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Others", "Others", "{840AC1A4-6CB0-4F03-83E6-3005E520FA22}"
ProjectSection(SolutionItems) = preProject
.github\workflows\publish-beta.yml = .github\workflows\publish-beta.yml
.github\workflows\publish.yml = .github\workflows\publish.yml
Readme.MD = Readme.MD
.github\workflows\test.yml = .github\workflows\test.yml
EndProjectSection
EndProject
Global
Expand Down
209 changes: 103 additions & 106 deletions NewLife.NoDb/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,115 +1,112 @@
using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.IO.MemoryMappedFiles;
using NewLife.NoDb.Storage;

namespace NewLife.NoDb
namespace NewLife.NoDb;

/// <summary>帮助类</summary>
public static class Helper
{
/// <summary>帮助类</summary>
public static class Helper
/// <summary></summary>
/// <param name="file"></param>
/// <returns></returns>
public static MemoryMappedFile CreateFromFile(String file)
{
/// <summary></summary>
/// <param name="file"></param>
/// <returns></returns>
public static MemoryMappedFile CreateFromFile(String file)
{
file = file.GetFullPath();
var name = Path.GetFileNameWithoutExtension(file);

//var capacity = 4 * 1024 * 1024;
//if (file.AsFile().Exists) capacity = 0;
//_mmf = MemoryMappedFile.CreateFromFile(file, FileMode.OpenOrCreate, name, capacity, MemoryMappedFileAccess.ReadWrite);

// 使用文件流可以控制共享读写,让别的进程也可以读写文件
var fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, 4096, FileOptions.RandomAccess);
if (fs.Length == 0) fs.SetLength(1024);
#if __CORE__
var _mmf = MemoryMappedFile.CreateFromFile(fs, name, 0, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false);
file = file.GetFullPath();
var name = Path.GetFileNameWithoutExtension(file);

//var capacity = 4 * 1024 * 1024;
//if (file.AsFile().Exists) capacity = 0;
//_mmf = MemoryMappedFile.CreateFromFile(file, FileMode.OpenOrCreate, name, capacity, MemoryMappedFileAccess.ReadWrite);

// 使用文件流可以控制共享读写,让别的进程也可以读写文件
var fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, 4096, FileOptions.RandomAccess);
if (fs.Length == 0) fs.SetLength(1024);
#if NETFRAMEWORK
var _mmf = MemoryMappedFile.CreateFromFile(fs, name, 0, MemoryMappedFileAccess.ReadWrite, null, HandleInheritability.None, false);
#else
var _mmf = MemoryMappedFile.CreateFromFile(fs, name, 0, MemoryMappedFileAccess.ReadWrite, null, HandleInheritability.None, false);
var _mmf = MemoryMappedFile.CreateFromFile(fs, name, 0, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false);
#endif

return _mmf;
}

/// <summary></summary>
/// <param name="mmf"></param>
/// <param name="block"></param>
/// <returns></returns>
public static Stream CreateStream(this MemoryMappedFile mmf, Block block) => mmf.CreateViewStream(block.Position, block.Size);

/// <summary></summary>
/// <param name="mmf"></param>
/// <param name="block"></param>
/// <returns></returns>
public static UnmanagedMemoryAccessor CreateAccessor(this MemoryMappedFile mmf, Block block) => mmf.CreateViewAccessor(block.Position, block.Size);

/// <summary></summary>
/// <param name="accessor"></param>
/// <param name="position"></param>
/// <param name="count"></param>
/// <returns></returns>
public static Byte[] ReadArray(this UnmanagedMemoryAccessor accessor, Int32 position, Int32 count)
{
var buf = new Byte[count];
var n = accessor.ReadArray(position, buf, 0, buf.Length);
if (n <= buf.Length) buf = buf.ReadBytes(0, n);

return buf;
}

/// <summary></summary>
/// <param name="accessor"></param>
/// <param name="position"></param>
/// <returns></returns>
public static Block ReadBlock(this UnmanagedMemoryAccessor accessor, Int32 position) => Block.Null;

///// <summary></summary>
///// <param name="mmf"></param>
//public static void CheckAccessControl(this MemoryMappedFile mmf)
//{
// var user = $"{Environment.MachineName}\\{Environment.UserName}";
// var rule = new AccessRule<MemoryMappedFileRights>(user, MemoryMappedFileRights.FullControl, AccessControlType.Allow);

// var msc = mmf.GetAccessControl();
// foreach (AccessRule<MemoryMappedFileRights> ar in msc.GetAccessRules(true, true, typeof(NTAccount)))
// {
// if (ar.IdentityReference == rule.IdentityReference && ar.AccessControlType == rule.AccessControlType && ar.Rights == rule.Rights) return;
// }

// msc.AddAccessRule(rule);
// mmf.SetAccessControl(msc);
//}

//public static unsafe Byte[] ReadBytes(this MemoryMappedViewAccessor view, Int64 offset, Int32 num)
//{
// var ptr = (Byte*)0;
// view.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr);

// var p = new IntPtr(ptr);
// p = new IntPtr(p.ToInt64() + offset);
// var arr = new Byte[num];
// Marshal.Copy(p, arr, 0, num);

// view.SafeMemoryMappedViewHandle.ReleasePointer();
// return arr;

// //var arr = new Byte[num];
// //view.ReadArray(offset, arr, 0, num);

// //return arr;
//}

//public static unsafe void WriteBytes(this MemoryMappedViewAccessor accessor, Int64 offset, Byte[] data)
//{
// var ptr = (Byte*)0;
// accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr);

// var p = new IntPtr(ptr);
// p = new IntPtr(p.ToInt64() + offset);
// Marshal.Copy(data, 0, p, data.Length);

// accessor.SafeMemoryMappedViewHandle.ReleasePointer();
//}
return _mmf;
}

/// <summary></summary>
/// <param name="mmf"></param>
/// <param name="block"></param>
/// <returns></returns>
public static Stream CreateStream(this MemoryMappedFile mmf, Block block) => mmf.CreateViewStream(block.Position, block.Size);

/// <summary></summary>
/// <param name="mmf"></param>
/// <param name="block"></param>
/// <returns></returns>
public static UnmanagedMemoryAccessor CreateAccessor(this MemoryMappedFile mmf, Block block) => mmf.CreateViewAccessor(block.Position, block.Size);

/// <summary></summary>
/// <param name="accessor"></param>
/// <param name="position"></param>
/// <param name="count"></param>
/// <returns></returns>
public static Byte[] ReadArray(this UnmanagedMemoryAccessor accessor, Int32 position, Int32 count)
{
var buf = new Byte[count];
var n = accessor.ReadArray(position, buf, 0, buf.Length);
if (n <= buf.Length) buf = buf.ReadBytes(0, n);

return buf;
}

/// <summary></summary>
/// <param name="accessor"></param>
/// <param name="position"></param>
/// <returns></returns>
public static Block ReadBlock(this UnmanagedMemoryAccessor accessor, Int32 position) => Block.Null;

///// <summary></summary>
///// <param name="mmf"></param>
//public static void CheckAccessControl(this MemoryMappedFile mmf)
//{
// var user = $"{Environment.MachineName}\\{Environment.UserName}";
// var rule = new AccessRule<MemoryMappedFileRights>(user, MemoryMappedFileRights.FullControl, AccessControlType.Allow);

// var msc = mmf.GetAccessControl();
// foreach (AccessRule<MemoryMappedFileRights> ar in msc.GetAccessRules(true, true, typeof(NTAccount)))
// {
// if (ar.IdentityReference == rule.IdentityReference && ar.AccessControlType == rule.AccessControlType && ar.Rights == rule.Rights) return;
// }

// msc.AddAccessRule(rule);
// mmf.SetAccessControl(msc);
//}

//public static unsafe Byte[] ReadBytes(this MemoryMappedViewAccessor view, Int64 offset, Int32 num)
//{
// var ptr = (Byte*)0;
// view.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr);

// var p = new IntPtr(ptr);
// p = new IntPtr(p.ToInt64() + offset);
// var arr = new Byte[num];
// Marshal.Copy(p, arr, 0, num);

// view.SafeMemoryMappedViewHandle.ReleasePointer();
// return arr;

// //var arr = new Byte[num];
// //view.ReadArray(offset, arr, 0, num);

// //return arr;
//}

//public static unsafe void WriteBytes(this MemoryMappedViewAccessor accessor, Int64 offset, Byte[] data)
//{
// var ptr = (Byte*)0;
// accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr);

// var p = new IntPtr(ptr);
// p = new IntPtr(p.ToInt64() + offset);
// Marshal.Copy(data, 0, p, data.Length);

// accessor.SafeMemoryMappedViewHandle.ReleasePointer();
//}
}
Loading

0 comments on commit bae4f5f

Please sign in to comment.