Skip to content

Commit

Permalink
embed snappy and sparkey into Ruby extension
Browse files Browse the repository at this point in the history
Use submodules to include dependency source code.
  • Loading branch information
wjordan committed Jul 27, 2018
1 parent 0eeab17 commit ef63a92
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ test/version_tmp
tmp

Makefile
extconf.h
mkmf.log

.DS_Store
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "ext/gnista/sparkey"]
path = ext/gnista/sparkey
url = https://github.com/spotify/sparkey.git
[submodule "ext/gnista/snappy"]
path = ext/gnista/snappy
url = https://github.com/google/snappy
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ Gnista is a wrapper for the database/hashstore [Sparkey](http://github.com/spoti

## Setup

You will need Sparkey, obviously. It's really easy to install. Head over to [http://github.com/spotify/sparkey](http://github.com/spotify/sparkey) and install it, I'll wait..

Now, add this line to your application's Gemfile:
Add this line to your application's Gemfile:

gem 'gnista'

Expand Down Expand Up @@ -88,7 +86,8 @@ Both ```Logreader```and ```Hash``` mixin [Enumerable](http://ruby-doc.org/core-2

__Step-by-step:__

1. Fork it
1. Fork it
1. Fetch submodules (`git submodule update`)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Write some code
4. Build and test
Expand Down
63 changes: 61 additions & 2 deletions ext/gnista/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,66 @@
extension_name = 'gnista'
dir_config(extension_name)

have_library('sparkey') or raise "Sparkey is missing"
have_header('sparkey/sparkey.h') or raise "sparkey.h could not be found"
$CFLAGS << ' -std=c99'
$INCFLAGS << ' -I$(srcdir)/sparkey/src'
$INCFLAGS << ' -I$(srcdir)/snappy'
$VPATH ||= []
$VPATH << '$(srcdir)/sparkey/src'
$VPATH << '$(srcdir)/snappy'
gnista_srcs = %w[
gnista.c
]

sparkey_srcs = %w[
buf.c
hashalgorithms.c
hashiter.c
hashwriter.c
logreader.c
returncodes.c
util.c
endiantools.c
hashheader.c
hashreader.c
logheader.c
logwriter.c
MurmurHash3.c
]

snappy_srcs = %w[
snappy.cc
snappy-c.cc
snappy-sinksource.cc
]

$srcs = gnista_srcs + sparkey_srcs + snappy_srcs

# Create macro definitions used by Snappy sources.
%w[
byteswap.h
stddef.h
stdint.h
sys/endian.h
sys/mman.h
sys/resource.h
sys/time.h
sys/uio.h
unistd.h
windows.h
].each { |header| have_header header }

if have_func 'mmap', 'sys/mman.h'
$defs << '-DHAVE_FUNC_MMAP'
end
if have_func 'sysconf', 'unistd.h'
$defs << '-DHAVE_FUNC_SYSCONF'
end
if try_compile('int main(void) { return __builtin_expect(0, 1); }')
$defs << '-DHAVE_BUILTIN_EXPECT'
end
if try_compile('int main(void) { return __builtin_ctzll(0); }')
$defs << '-DHAVE_BUILTIN_CTZ'
end

create_header
create_makefile(extension_name)
1 change: 1 addition & 0 deletions ext/gnista/snappy
Submodule snappy added at 4f7bd2
94 changes: 94 additions & 0 deletions ext/gnista/snappy-stubs-public.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2011 Google Inc. All Rights Reserved.
// Author: [email protected] (Steinar H. Gunderson)
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Various type stubs for the open-source version of Snappy.
//
// This file cannot include config.h, as it is included from snappy.h,
// which is a public header. Instead, snappy-stubs-public.h is generated by
// from snappy-stubs-public.h.in at configure time.

#ifndef THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_PUBLIC_H_
#define THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_PUBLIC_H_

#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif // HAVE_STDDEF_H

#ifdef HAVE_STDDEF_H
#include <stddef.h>
#endif // HAVE_STDDEF_H

#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif // HAVE_SYS_UIO_H

#define SNAPPY_MAJOR 1
#define SNAPPY_MINOR 1
#define SNAPPY_PATCHLEVEL 7
#define SNAPPY_VERSION \
((SNAPPY_MAJOR << 16) | (SNAPPY_MINOR << 8) | SNAPPY_PATCHLEVEL)

#include <string>

namespace snappy {

#ifdef HAVE_STDINT_H
typedef int8_t int8;
typedef uint8_t uint8;
typedef int16_t int16;
typedef uint16_t uint16;
typedef int32_t int32;
typedef uint32_t uint32;
typedef int64_t int64;
typedef uint64_t uint64;
#else
typedef signed char int8;
typedef unsigned char uint8;
typedef short int16;
typedef unsigned short uint16;
typedef int int32;
typedef unsigned int uint32;
typedef long long int64;
typedef unsigned long long uint64;
#endif // HAVE_STDINT_H

typedef std::string string;

#ifndef HAVE_SYS_UIO_H
// Windows does not have an iovec type, yet the concept is universally useful.
// It is simple to define it ourselves, so we put it inside our own namespace.
struct iovec {
void* iov_base;
size_t iov_len;
};
#endif // !HAVE_SYS_UIO_H

} // namespace snappy

#endif // THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_PUBLIC_H_
1 change: 1 addition & 0 deletions ext/gnista/sparkey
Submodule sparkey added at e3f71b
4 changes: 3 additions & 1 deletion gnista.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ Gem::Specification.new do |spec|
spec.files = %w[README.md LICENSE.txt HISTORY.md Rakefile
ext/gnista/extconf.rb ext/gnista/gnista.c
test/test_gnista.rb test/test_commands.rb
lib/gnista.rb lib/gnista/version.rb]
lib/gnista.rb lib/gnista/version.rb] +
# Add all files from all included submodules.
`git submodule --quiet foreach 'git ls-files | sed "s|^|$path/|"'`.split("\n")

spec.test_files = ['test/test_gnista.rb', 'test/test_commands.rb']
spec.require_paths = ["ext", "lib"]
Expand Down

0 comments on commit ef63a92

Please sign in to comment.