Skip to content

Commit

Permalink
Migrate Ruby bindings from helix to rutie
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancisMurillo authored and rtyler committed Mar 20, 2021
1 parent 5b6e98f commit 0d8b23f
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 71 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/ruby_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ jobs:
- name: Cache Rust build
uses: actions/[email protected]
with:
path: target/debug/build
key: ruby-${{ runner.OS }}-target-build-${{ hashFiles('**/Cargo.lock') }}
path: target/release/build
key: ruby-${{ runner.OS }}-target-release-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
ruby-${{ runner.OS }}-target-build-
- name: Cache Rust incremental build
uses: actions/[email protected]
with:
path: target/debug/incremental
key: ruby-${{ runner.OS }}-target-incremental-${{ hashFiles('**/Cargo.lock') }}
path: target/release/incremental
key: ruby-${{ runner.OS }}-target-release-incremental-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
ruby-${{ runner.OS }}-target-incremental-
- name: Install minimal stable with clippy and rustfmt
Expand All @@ -34,6 +34,8 @@ jobs:
profile: default
toolchain: stable
override: true
- name: Build Release for Ruby
run: cargo build --release
- name: 'Set up Ruby'
uses: actions/setup-ruby@v1
- name: 'Install Ruby Dependencies'
Expand Down
78 changes: 50 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ruby/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ version = "0.1.0"
authors = ["R Tyler Croy <[email protected]>"]

[lib]

crate-type = ["cdylib"]

[dependencies]
helix = "*"
lazy_static = "1"
rutie = "0.8.2"
tokio = { version = "1", features = ["rt-multi-thread"] }

[dependencies.deltalake]
Expand Down
4 changes: 3 additions & 1 deletion ruby/Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gem 'helix_runtime'
gem 'colorize'
gem 'rutie', '~> 0.0.3'

group :development do
gem 'rake', '~> 12.0'
Expand Down
10 changes: 3 additions & 7 deletions ruby/Rakefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
# frozen_string_literal: true

require 'bundler/setup'
require 'helix_runtime/build_task'
require 'rspec/core/rake_task'

# For Windows
$stdout.sync = true

HelixRuntime::BuildTask.new do |t|
t.build_root = File.expand_path("../", __dir__)
end

RSpec::Core::RakeTask.new(:spec) do |t|
t.verbose = false
end

task :spec => :build
task :default => :spec
task default: :spec
8 changes: 6 additions & 2 deletions ruby/lib/deltalake.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
require 'helix_runtime'
require 'deltalake-ruby/native'
# frozen_string_literal: true

require 'deltalake/version'
require 'rutie'

module Deltalake
Rutie.new(:deltalake_ruby, lib_path: '../../target/release').init 'Init_table', __dir__

def self.open_table(table_path)
Table.new(table_path)
end
Expand Down
5 changes: 5 additions & 0 deletions ruby/lib/deltalake/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

module Deltalake
VERSION = '0.1.0'
end
12 changes: 12 additions & 0 deletions ruby/spec/deltalake_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'
require 'deltalake'

Expand All @@ -17,6 +19,16 @@

it { should be_instance_of Array }
its(:size) { should eq(5) }

it {
should contain_exactly(
'part-00000-2befed33-c358-4768-a43c-3eda0d2a499d-c000.snappy.parquet',
'part-00000-c1777d7d-89d9-4790-b38a-6ee7e24456b1-c000.snappy.parquet',
'part-00007-3a0e4727-de0d-41b6-81ef-5223cf40f025-c000.snappy.parquet',
'part-00001-7891c33d-cedc-47c3-88a6-abcfb049d3b4-c000.snappy.parquet',
'part-00004-315835fe-fb44-4562-98f6-5e6cfa3ae45d-c000.snappy.parquet'
)
}
end
end
end
102 changes: 75 additions & 27 deletions ruby/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,91 @@
#![recursion_limit = "1024"]

#[macro_use]
extern crate helix;
extern crate deltalake;

#[macro_use]
extern crate lazy_static;

#[macro_use]
extern crate rutie;

use deltalake::DeltaTable;
use rutie::{AnyObject, Array, Class, Integer, Object, RString};
use std::sync::Arc;

ruby! {
class Table {
struct {
table_path: String,
actual: Arc<DeltaTable>,
}
pub struct TableData {
table_path: String,
actual: Arc<DeltaTable>,
}

def initialize(helix, table_path: String) {
println!("initializing with {}", table_path);
impl TableData {
fn new(table_path: String) -> Self {
println!("initializing with {}", table_path);

let rt = tokio::runtime::Runtime::new().unwrap();
let table = rt.block_on(deltalake::open_table(&table_path)).unwrap();
let actual = Arc::new(table);
let rt = tokio::runtime::Runtime::new().unwrap();
let table = rt.block_on(deltalake::open_table(&table_path)).unwrap();
let actual = Arc::new(table);

Table {
helix,
table_path,
actual,
}
}
Self { table_path, actual }
}

def table_path(&self) -> String {
self.table_path.clone()
}
fn table_path(&self) -> &str {
&self.table_path
}

def version(&self) -> i64 {
self.actual.version
}
fn version(&self) -> i64 {
self.actual.version
}

def files(&self) -> Vec<String> {
self.actual.get_files().to_vec()
fn files(&self) -> &[String] {
self.actual.get_files().as_slice()
}
}

wrappable_struct!(TableData, TableDataWrapper, TABLE_DATA_WRAPPER);

class!(Table);

methods!(
Table,
rtself,
fn ruby_table_new(table_path: RString) -> AnyObject {
let table_data = TableData::new(table_path.unwrap().to_string());

Class::from_existing("Table").wrap_data(table_data, &*TABLE_DATA_WRAPPER)
},
fn ruby_table_path() -> RString {
let table_path = rtself.get_data(&*TABLE_DATA_WRAPPER).table_path();

RString::new_utf8(table_path)
},
fn ruby_version() -> Integer {
let version = rtself.get_data(&*TABLE_DATA_WRAPPER).version();

Integer::new(version)
},
fn ruby_files() -> Array {
let files = rtself.get_data(&*TABLE_DATA_WRAPPER).files();

let mut array = Array::with_capacity(files.len());

for file in files {
array.push(RString::new_utf8(file));
}

array
}
);

#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn Init_table() {
let data_class = Class::from_existing("Object");

Class::new("Table", Some(&data_class)).define(|klass| {
klass.def_self("new", ruby_table_new);

klass.def("table_path", ruby_table_path);
klass.def("version", ruby_version);
klass.def("files", ruby_files);
});
}

0 comments on commit 0d8b23f

Please sign in to comment.