Skip to content

Commit

Permalink
better parameter checking for both the lua and C APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
mstump committed Jul 12, 2013
1 parent 94295b7 commit d8b479e
Show file tree
Hide file tree
Showing 13 changed files with 365 additions and 44 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ ext/include/
ext/share/
ext/src/
ext/tmp/
ext/bin/
ext/lib/
ext/ssl/

# editor
*.swp
Expand Down
26 changes: 25 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,30 @@ endif()
# UTF8-CPP
set(INCLUDES ${INCLUDES} ${PROJECT_SOURCE_DIR}/ext/utf8/source)

# OS specific lib stuff
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME ON)

include(OpenSSL)
include(ZLib)
set(LIBS ${LIBS} ${openssl_STATIC_LIBRARIES} )
set(LIBS ${LIBS} ${zlib_STATIC_LIBRARIES} )
else()
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_STATIC_RUNTIME OFF)

find_package(OpenSSL)
if (${OPENSSL_FOUND})
set(LIBS ${LIBS} ${OPENSSL_LIBRARIES} )
else()
message(FATAL_ERROR "couldn't find OpenSSL")
endif()
endif()

# Boost
set(Boost_USE_MULTITHREADED ON)
find_package(Boost 1.47.0 COMPONENTS system filesystem REQUIRED )
find_package(Boost 1.47.0 COMPONENTS system filesystem thread REQUIRED )
set(INCLUDES ${INCLUDES} ${Boost_INCLUDE_DIRS} )
set(LIBS ${LIBS} ${Boost_LIBRARIES} )

Expand All @@ -42,6 +63,9 @@ include(leveldb)
# RE2
include(re2)

# libcql
include(libcql)

# Lua 5.1
find_package(Lua51 REQUIRED)
set(INCLUDES ${INCLUDES} ${LUA_INCLUDE_DIR} )
Expand Down
51 changes: 51 additions & 0 deletions ext/libcql.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
#
# Install LIBCQL from source
#
if (NOT LIBCQL_NAME)

CMAKE_MINIMUM_REQUIRED(VERSION 2.8.7)

include (snappy)
include (ExternalProject)

set(ABBREV "LIBCQL")
set(${ABBREV}_NAME ${ABBREV})
set(${ABBREV}_INCLUDE_DIRS ${EXT_PREFIX}/src/LIBCQL/)
set(APP_DEPENDENCIES ${APP_DEPENDENCIES} ${ABBREV})

message("Installing ${LIBCQL_NAME} into ext build area: ${EXT_PREFIX} ...")

ExternalProject_Add(LIBCQL
PREFIX ${EXT_PREFIX}
URL https://github.com/mstump/libcql/archive/master.zip
URL_MD5 c511a6b9f89d56d0893f12a81d638ec2
CMAKE_ARGS "-DCMAKE_BUILD_TYPE=debug"
BUILD_IN_SOURCE 1
)

set(CXXFLAGS CMAKE_CXX_FLAGS)

set(${ABBREV}_LIBRARIES ${EXT_PREFIX}/src/libcql/lib/libcql.dylib)
set(${ABBREV}_STATIC_LIBRARIES ${EXT_PREFIX}/src/libcql/lib/libcql.a)

set_target_properties(${LIBCQL_NAME} PROPERTIES EXCLUDE_FROM_ALL ON)

endif (NOT LIBCQL_NAME)
else()
find_path(LIBCQL_INCLUDE_DIRS NAMES libcql/cql.hpp HINTS /usr/include /usr/local/include)
find_library(LIBCQL_LIBRARIES NAMES libcql.a libcql.lib libcql.dylib HINTS /usr/lib /usr/local/lib)
endif()

if(LIBCQL_INCLUDE_DIRS AND LIBCQL_LIBRARIES)
set(LIBCQL_FOUND TRUE)
endif(LIBCQL_INCLUDE_DIRS AND LIBCQL_LIBRARIES)

if(LIBCQL_FOUND)
message(STATUS "Found libcql: ${LIBCQL_LIBRARIES}")
else(LIBCQL_FOUND)
message(FATAL_ERROR "Could not find libcql library.")
endif(LIBCQL_FOUND)

set(INCLUDES ${INCLUDES} ${LIBCQL_INCLUDE_DIRS} )
set(LIBS ${LIBS} ${LIBCQL_STATIC_LIBRARIES} )
42 changes: 42 additions & 0 deletions ext/openssl.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#
# Install openssl from source
#

if (NOT openssl_NAME)

CMAKE_MINIMUM_REQUIRED(VERSION 2.8.7)

include(ExternalProject)
include(zlib)

if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
# On Mac, 64-bit builds must be manually requested.
set (SSL_CONFIGURE_COMMAND ./Configure darwin64-x86_64-cc )
else()
# The config script seems to auto-select the platform correctly on linux
# It calls ./Configure for us.
set (SSL_CONFIGURE_COMMAND ./config)
endif()

set(ABBREV "openssl")
set(${ABBREV}_NAME ${ABBREV})
set(${ABBREV}_INCLUDE_DIRS ${EXT_PREFIX}/include)
set(APP_DEPENDENCIES ${APP_DEPENDENCIES} ${ABBREV})

message("Installing ${openssl_NAME} into ext build area: ${EXT_PREFIX} ...")

ExternalProject_Add(openssl
DEPENDS zlib
PREFIX ${EXT_PREFIX}
URL http://openssl.org/source/openssl-1.0.0e.tar.gz
URL_MD5 "7040b89c4c58c7a1016c0dfa6e821c86"
PATCH_COMMAND ""
CONFIGURE_COMMAND ${SSL_CONFIGURE_COMMAND} zlib no-shared no-zlib-dynamic --prefix=${EXT_PREFIX}
BUILD_IN_SOURCE 1
)

set(${ABBREV}_STATIC_LIBRARIES ${EXT_PREFIX}/lib/libcrypto.a ${EXT_PREFIX}/lib/libssl.a)

set_target_properties(${openssl_NAME} PROPERTIES EXCLUDE_FROM_ALL ON)

endif(NOT openssl_NAME)
4 changes: 2 additions & 2 deletions ext/re2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")

endif (NOT RE2_NAME)
else()
find_path(RE2_INCLUDE_DIRS NAMES leveldb/db.h HINTS /usr/include /usr/local/include)
find_library(RE2_LIBRARIES NAMES libleveldb.a libleveldb.lib libleveldb.dylib HINTS /usr/lib /usr/local/lib)
find_path(RE2_INCLUDE_DIRS NAMES re2/re2.h HINTS /usr/include /usr/local/include)
find_library(RE2_LIBRARIES NAMES libre2.a libre2.lib libre2.dylib HINTS /usr/lib /usr/local/lib)
endif()

if(RE2_INCLUDE_DIRS AND RE2_LIBRARIES)
Expand Down
33 changes: 33 additions & 0 deletions ext/zlib.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# Install zlib from source
#

if (NOT zlib_NAME)

CMAKE_MINIMUM_REQUIRED(VERSION 2.8.7)

include (ExternalProject)

set(ABBREV "zlib")
set(${ABBREV}_NAME ${ABBREV})
set(${ABBREV}_INCLUDE_DIRS ${EXT_PREFIX}/include)
set(APP_DEPENDENCIES ${APP_DEPENDENCIES} ${ABBREV})

message("Installing ${zlib_NAME} into ext build area: ${EXT_PREFIX} ...")

ExternalProject_Add(zlib
PREFIX ${EXT_PREFIX}
URL http://zlib.net/zlib-1.2.8.tar.gz
URL_MD5 "44d667c142d7cda120332623eab69f40"
PATCH_COMMAND ""
CONFIGURE_COMMAND ./configure -p=${EXT_PREFIX} --static --64
BUILD_COMMAND make
INSTALL_COMMAND make install prefix=${EXT_PREFIX}
BUILD_IN_SOURCE 1
)

set(${ABBREV}_STATIC_LIBRARIES ${EXT_PREFIX}/lib/libz.a)

set_target_properties(${zlib_NAME} PROPERTIES EXCLUDE_FROM_ALL ON)

endif (NOT zlib_NAME)
26 changes: 25 additions & 1 deletion include/libmutton/mutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ typedef __uint128_t uint128_t;
typedef uint16_t mtn_index_partition_t;
typedef uint128_t mtn_index_address_t;

/* 1MB max string size */
#define MTN_MAX_STRING_SIZE 1048576

/* The configuration options */
#define MTN_OPT_DB_PATH 1 /* the path to store the DB files */
#define MTN_OPT_LUA_PATH 2 /* the search path for lua packages */
Expand All @@ -70,9 +73,10 @@ typedef uint128_t mtn_index_address_t;
#define MTN_ERROR_INDEX_OPERATION 2
#define MTN_ERROR_NOT_FOUND 3
#define MTN_ERROR_BAD_REGEX 4
#define MTN_ERROR_BAD_OPTION 4
#define MTN_ERROR_BAD_CONFIGURATION 4
#define MTN_ERROR_SCRIPT 5
#define MTN_ERROR_UNKOWN_EVENT_TYPE 6
#define MTN_ERROR_BAD_PARAM 7

/**
* Allocate a new libmutton context.
Expand Down Expand Up @@ -360,6 +364,26 @@ mutton_process_event_bucketed(
size_t buffer_size,
void** status);

/**
* Proccess an event
*
* @param context allocated mutton context
* @param partition partition, used to create logical seperation between indexes and other data
* @param query the adhoc query string
* @param query_size size of the query string
* @param future query result future
* @param status output pointer to status if error is encountered, NULL otherwise. If input value of status is not NULL it will be freed prior to being set.
*
* @return true if successfull
*/
MUTTON_EXPORT bool
mutton_persistence_query(
void* context,
void* query,
size_t query_size,
void** future,
void** status);


#undef MUTTON_EXPORT

Expand Down
5 changes: 4 additions & 1 deletion src/base_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ namespace mtn {

struct index_address_comparator_t
{
bool operator()(mtn_index_address_t a, mtn_index_address_t b)
bool
operator()(
const mtn_index_address_t a,
const mtn_index_address_t b) const
{
uint64_t high_a = (uint64_t) (a >> 64);
uint64_t high_b = (uint64_t) (b >> 64);
Expand Down
68 changes: 64 additions & 4 deletions src/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,47 @@
#ifndef __MUTTON_CONTEXT_HPP_INCLUDED__
#define __MUTTON_CONTEXT_HPP_INCLUDED__

#include <boost/thread/future.hpp>
#include <boost/asio.hpp>
#include <boost/ptr_container/ptr_map.hpp>
#include <boost/unordered_map.hpp>

#include <libcql/cql_future_connection.hpp>
#include <libcql/cql_future_result.hpp>
#include <libcql/cql_client.hpp>
#include <libcql/cql_client_factory.hpp>
#include <libcql/cql_client_pool.hpp>
#include <libcql/cql_client_pool_factory.hpp>


#include "base_types.hpp"
#include "index.hpp"
#include "index_reader_writer.hpp"
#include "status.hpp"
#include "lua.hpp"

struct client_functor_t
{

public:

client_functor_t(boost::asio::io_service& service,
cql::cql_client_t::cql_log_callback_t log_callback) :
_io_service(service),
_log_callback(log_callback)
{}

cql::cql_client_t*
operator()()
{
return cql::cql_client_factory_t::create_cql_client_t(_io_service, _log_callback);
}

private:
boost::asio::io_service& _io_service;
cql::cql_client_t::cql_log_callback_t _log_callback;
};

namespace mtn {

class context_t {
Expand All @@ -40,7 +72,9 @@ namespace mtn {
typedef boost::unordered_map<std::string, lua_state_t> lua_state_container_t;

context_t(mtn::index_reader_writer_t* rw) :
_rw(rw)
_rw(rw),
_work(_io),
_io_thread(boost::bind(&boost::asio::io_service::run, &_io))
{}

inline mtn::status_t
Expand Down Expand Up @@ -70,6 +104,16 @@ namespace mtn {
init()
{
return _rw->init(*this);

cql::cql_client_pool_t::cql_client_callback_t client_factory;
// if (argc > 1) {
// client_factory = client_ssl_functor_t(io_service, ctx, &log_callback);
// }
// else {
client_factory = client_functor_t(_io, NULL);
// }
_cql_pool.reset(cql::cql_client_pool_factory_t::create_client_pool_t(client_factory, NULL, NULL));

}

inline mtn::index_reader_writer_t&
Expand Down Expand Up @@ -245,18 +289,34 @@ namespace mtn {
}

inline bool
get_lua_script(const char* event_name,
size_t event_name_size,
lua_state_t& output)
get_lua_script(
const char* event_name,
size_t event_name_size,
lua_state_t& output)
{
return get_lua_script(std::string(event_name, event_name_size), output);
}

inline bool
cql_pool(
cql::cql_client_pool_t** output)
{
if (_cql_pool.get()) {
*output = _cql_pool.get();
return true;
}
return false;
}

private:
std::auto_ptr<mtn::index_reader_writer_t> _rw;
lua_state_container_t _lua_state;
index_container_t _indexes;
options_container_t _options;
boost::asio::io_service _io;
boost::asio::io_service::work _work;
boost::thread _io_thread;
std::auto_ptr<cql::cql_client_pool_t> _cql_pool;
};

} // namespace mtn
Expand Down
9 changes: 9 additions & 0 deletions src/demo/lua_scripts/basic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,14 @@ then
error("should reject because context isn't light userdata")
end

if pcall(
function()
mutton_index_value_trigram(mutton.context, 1, mutton.bucket, "a_field", nil, 1, true)
end)
then
error("should reject because value is nil")
end



print("\nTHE END");
2 changes: 1 addition & 1 deletion src/index_reader_writer_leveldb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mtn::index_reader_writer_leveldb_t::init(mtn::context_t& context)
{
std::string path;
if (!context.get_opt(MTN_OPT_DB_PATH, path)) {
return mtn::status_t(MTN_ERROR_BAD_OPTION, "no database path was specified");
return mtn::status_t(MTN_ERROR_BAD_CONFIGURATION, "no database path was specified");
}

leveldb::Options options;
Expand Down
Loading

0 comments on commit d8b479e

Please sign in to comment.