Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use modern kernel api for create safe multifile model in super-safe language #53

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
47 changes: 31 additions & 16 deletions .github/workflows/CD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,23 @@ env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SCRIPTS_BASE_URL: https://raw.githubusercontent.com/linksplatform/Scripts/master/MultiProjectRepository


jobs:
testAndDeploy:
rust:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: true
- name: Rustup nightly
run: |
rustup toolchain install nightly
- name: Rust test
run: |
cd rust
cargo +nightly test --verbose -- --show-output

csharpTestAndDeploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
Expand Down Expand Up @@ -56,18 +71,18 @@ jobs:
wget "$SCRIPTS_BASE_URL/publish-release.sh"
bash ./publish-release.sh

pushCSharpNuGetToGitHubPackageRegistry:
needs: testAndDeploy
if: github.event_name == 'push'
runs-on: windows-latest
steps:
- uses: actions/checkout@v1
with:
submodules: true
- uses: nuget/setup-nuget@v1
- name: Publish CSharp NuGet to GitHub Package Registry
run: |
dotnet build -c Release
dotnet pack -c Release
nuget source Add -Name "GitHub" -Source "https://nuget.pkg.github.com/linksplatform/index.json" -UserName linksplatform -Password ${{ secrets.GITHUB_TOKEN }}
nuget push **/*.nupkg -Source "GitHub" -SkipDuplicate
pushCSharpNuGetToGitHubPackageRegistry:
needs: testAndDeploy
if: github.event_name == 'push'
runs-on: windows-latest
steps:
- uses: actions/checkout@v1
with:
submodules: true
- uses: nuget/setup-nuget@v1
- name: Publish CSharp NuGet to GitHub Package Registry
run: |
dotnet build -c Release
dotnet pack -c Release
nuget source Add -Name "GitHub" -Source "https://nuget.pkg.github.com/linksplatform/index.json" -UserName linksplatform -Password ${{ secrets.GITHUB_TOKEN }}
nuget push **/*.nupkg -Source "GitHub" -SkipDuplicate
2 changes: 2 additions & 0 deletions rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
10 changes: 10 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "triplets"
version = "0.1.0"
authors = ["uselessgoddess"]
edition = "2018"

build = "build.rs"

[dependencies]
libc = "0.1"
4 changes: 4 additions & 0 deletions rust/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("cargo:rustc-link-search=native=dylibs");
println!("cargo:rustc-link-lib=static=Platform.Data.Triplets.Kernel");
}
Binary file added rust/dylibs/libPlatform.Data.Triplets.Kernel.a
Binary file not shown.
65 changes: 65 additions & 0 deletions rust/src/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#[cfg(windows)]
use std::os::windows::raw::HANDLE;
use libc::c_void;
use libc::int64_t;
use libc::uint64_t;

pub type SignedInteger = libc::int64_t;
pub type UnsignedInteger = libc::uint64_t;
pub type LinkIndex = UnsignedInteger;

pub type Visitor = extern "C" fn(LinkIndex);
pub type StoppableVisitor = extern "C" fn(LinkIndex) -> SignedInteger;

pub const SUCCESS_RESULT: SignedInteger = 0;
pub const ERROR_RESULT: SignedInteger = 1;

#[repr(C)]
pub struct Link
{
pub SourceIndex: LinkIndex,
pub TargetIndex: LinkIndex,
pub LinkerIndex: LinkIndex,
pub Timestamp: SignedInteger,
pub BySourceRootIndex: LinkIndex,
pub BySourceLeftIndex: LinkIndex,
pub BySourceRightIndex: LinkIndex,
pub BySourceCount: UnsignedInteger,
pub ByTargetRootIndex: LinkIndex,
pub ByTargetLeftIndex: LinkIndex,
pub ByTargetRightIndex: LinkIndex,
pub ByTargetCount: UnsignedInteger,
pub ByLinkerRootIndex: LinkIndex,
pub ByLinkerLeftIndex: LinkIndex,
pub ByLinkerRightIndex: LinkIndex,
pub ByLinkerCount: UnsignedInteger,
}

#[repr(C)]
pub struct RawDB
{
#[cfg(windows)]
pub storageFileHandle: HANDLE,
#[cfg(windows)]
pub storageFileMappingHandle: HANDLE,
#[cfg(unix)]
storageFileHandle: int64_t,
pub storageFileSizeInBytes: int64_t,
pub pointerToMappedRegion: *mut c_void,

pub currentMemoryPageSizeInBytes: int64_t,
pub serviceBlockSizeInBytes: int64_t,
pub baseLinksSizeInBytes: int64_t,
pub baseBlockSizeInBytes: int64_t,
pub storageFileMinSizeInBytes: int64_t,

pub pointerToDataSeal: *mut int64_t,
pub pointerToLinkIndexSize: *mut int64_t,
pub pointerToMappingLinksMaxSize: *mut int64_t,
pub lpointerToPointerToMappingLinks: *mut LinkIndex,
pub lpointerToLinksMaxSize: *mut LinkIndex,
pub lpointerToLinksSize: *mut LinkIndex,
pub LpointerToLinks: *mut Link,

pub pointerToUnusedMarker: *mut Link,
}
110 changes: 110 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//#![feature(try_trait)]
#![feature(thread_local)]
#![feature(in_band_lifetimes)]

use common::{*};
use link_c::{*};
//use persistent_memory_manager::{*};

pub mod common;
mod link_c;
mod persistent_memory_manager_c;
pub mod persistent_memory_manager;


#[cfg(test)]
mod test {
use crate::common::{RawDB, LinkIndex};
use libc::malloc;
use crate::link_c::{RawDB_new, CreateLink, UpdateLink, DeleteLink};
use crate::persistent_memory_manager_c::{OpenLinks, CloseLinks, GetLinksCount};
use std::ffi::CString;
use crate::persistent_memory_manager::Links;

#[test]
fn it_works() {
std::fs::remove_file("db.links");
{
let mem = Links::create("db.links");
println!("{:?}", mem);
// close db file
}

let mem = Links::open("db.links").unwrap();
println!("{:?}", mem);
mem.close().unwrap();
}

#[test]
fn c_like_CrUD() {
std::fs::remove_file("юникод_data_base.links");

unsafe {
let db = RawDB_new();

let name = CString::new("юникод_data_base.links").unwrap();
OpenLinks(db, name.as_ptr());

let itself = 0;

let is_a = CreateLink(db, itself, itself, itself);
let is_not_a = CreateLink(db, itself, itself, is_a);
let link = CreateLink(db, itself, is_a, itself);
let thing = CreateLink(db, itself, is_not_a, link);

UpdateLink(db, is_a, is_a, is_a, link);

DeleteLink(db, is_a);
DeleteLink(db, thing);

println!("Links count: {}", GetLinksCount(db));

CloseLinks(db);

libc::free(db as *mut libc::c_void);
}}

#[test]
fn CrUD() {
std::fs::remove_file("db.links");
let mem = Links::create("db.links").unwrap();

let itself = 0 as LinkIndex /* optional */;

let mut is_a = mem.create(itself, itself, itself);
let is_not_a = mem.create(itself, itself, &is_a);
let link = mem.create(itself, &is_a, itself);
let thing = mem.create(itself, &is_not_a, &link);

let clone = is_a.clone();
is_a.update(&clone, &clone, &link);
// or
// is_a.update(is_a.index(), is_a.index(), &link);

is_a.delete(); // delete all
thing.delete();

println!("Links count: {:?}", mem.count());
}

#[test]
fn multi_file() {
std::fs::remove_file("1.links");
std::fs::remove_file("2.links");

let mem1 = Links::create("1.links").unwrap();
let mem2 = Links::create("2.links").unwrap();

mem1.create(1, 0, 2);
mem2.create(2, 0, 3);

println!("mem1: {:?}", mem1);
println!("mem1 links count: {}", mem1.count());
println!("{:-<10}", '-');
println!("mem2: {:?}", mem2);
println!("mem2 links count: {}", mem1.count());
println!("{:-<10}", '-');
println!("mem1 close result: {:?}", mem1.close());
println!("mem2 close result: {:?}", mem2.close());
}
}
124 changes: 124 additions & 0 deletions rust/src/link_c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
use crate::common::{*};

extern "C" {
pub fn RawDB_new() -> *mut RawDB;

pub fn GetSourceIndex(
db: *mut RawDB,
index: LinkIndex,
) -> LinkIndex;

pub fn GetLinkerIndex(
db: *mut RawDB,
index: LinkIndex,
) -> LinkIndex;

pub fn GetTargetIndex(
db: *mut RawDB,
index: LinkIndex,
) -> LinkIndex;

pub fn GetTime(
db: *mut RawDB,
index: LinkIndex,
) -> SignedInteger;

pub fn CreateLink(
db: *mut RawDB,
source: LinkIndex,
linker: LinkIndex,
target: LinkIndex,
) -> LinkIndex;

pub fn SearchLink(
db: *mut RawDB,
source: LinkIndex,
linker: LinkIndex,
target: LinkIndex,
) -> LinkIndex;

pub fn ReplaceLink(
db: *mut RawDB,
index: LinkIndex,
replacement: LinkIndex,
) -> LinkIndex;

pub fn UpdateLink(
db: *mut RawDB,
index: LinkIndex,
source: LinkIndex,
linker: LinkIndex,
target: LinkIndex,
) -> LinkIndex;

pub fn DeleteLink(
db: *mut RawDB,
index: LinkIndex,
);

pub fn GetFirstRefererBySourceIndex(
db: *mut RawDB,
index: LinkIndex,
) -> LinkIndex;

pub fn GetFirstRefererByLinkerIndex(
db: *mut RawDB,
index: LinkIndex,
) -> LinkIndex;

pub fn GetFirstRefererByTargetIndex(
db: *mut RawDB,
index: LinkIndex,
) -> LinkIndex;

pub fn GetLinkNumberOfReferersBySource(
db: *mut RawDB,
index: LinkIndex,
) -> UnsignedInteger;

pub fn GetLinkNumberOfReferersByLinker(
db: *mut RawDB,
index: LinkIndex,
) -> UnsignedInteger;

pub fn GetLinkNumberOfReferersByTarget(
db: *mut RawDB,
index: LinkIndex,
) -> UnsignedInteger;

pub fn WalkThroughAllReferersBySource(
db: *mut RawDB,
root: LinkIndex,
visitor: Visitor,
);

pub fn WalkThroughReferersBySource(
db: *mut RawDB,
root: LinkIndex,
visitor: StoppableVisitor,
) -> SignedInteger;

pub fn WalkThroughAllReferersByLinker(
db: *mut RawDB,
root: LinkIndex,
visitor: Visitor,
);

pub fn WalkThroughReferersByLinker(
db: *mut RawDB,
root: LinkIndex,
visitor: StoppableVisitor,
) -> SignedInteger;

pub fn WalkThroughAllReferersByTarget(
db: *mut RawDB,
root: LinkIndex,
visitor: Visitor,
);

pub fn WalkThroughReferersByTarget(
db: *mut RawDB,
root: LinkIndex,
visitor: StoppableVisitor,
) -> SignedInteger;
}
Loading