/* * Copyright 2018, Oath Inc. Licensed under the terms of the * Apache License 2.0. See LICENSE file at the project root for terms. */ #include "cpc_sketch_c_adapter.h" extern "C" { #include } #include #include #include void* cpc_sketch_new(unsigned lg_k) { try { datasketches::cpc_init(&palloc, &pfree); return new (palloc(sizeof(datasketches::cpc_sketch))) datasketches::cpc_sketch(lg_k, datasketches::DEFAULT_SEED); } catch (std::exception& e) { elog(ERROR, e.what()); } } void cpc_sketch_delete(void* sketchptr) { try { static_cast(sketchptr)->~cpc_sketch(); pfree(sketchptr); } catch (std::exception& e) { elog(ERROR, e.what()); } } void cpc_sketch_update(void* sketchptr, const void* data, unsigned length) { try { static_cast(sketchptr)->update(data, length); } catch (std::exception& e) { elog(ERROR, e.what()); } } double cpc_sketch_get_estimate(const void* sketchptr) { try { return static_cast(sketchptr)->get_estimate(); } catch (std::exception& e) { elog(ERROR, e.what()); } } void cpc_sketch_to_string(const void* sketchptr, char* buffer, unsigned length) { try { std::stringstream s; s << *(static_cast(sketchptr)); snprintf(buffer, length, s.str().c_str()); } catch (std::exception& e) { elog(ERROR, e.what()); } } void* cpc_sketch_serialize(const void* sketchptr) { try { auto data = static_cast(sketchptr)->serialize(VARHDRSZ); bytea* buffer = (bytea*) data.first.release(); const size_t length = data.second; SET_VARSIZE(buffer, length); return buffer; } catch (std::exception& e) { elog(ERROR, e.what()); } } void* cpc_sketch_deserialize(const char* buffer, unsigned length) { try { datasketches::cpc_init(&palloc, &pfree); auto ptr = datasketches::cpc_sketch::deserialize(buffer, length, datasketches::DEFAULT_SEED); return ptr.release(); } catch (std::exception& e) { elog(ERROR, e.what()); } } void* cpc_union_new(unsigned lg_k) { try { datasketches::cpc_init(&palloc, &pfree); return new (palloc(sizeof(datasketches::cpc_union))) datasketches::cpc_union(lg_k, datasketches::DEFAULT_SEED); } catch (std::exception& e) { elog(ERROR, e.what()); } } void cpc_union_delete(void* unionptr) { try { static_cast(unionptr)->~cpc_union(); pfree(unionptr); } catch (std::exception& e) { elog(ERROR, e.what()); } } void cpc_union_update(void* unionptr, const void* sketchptr) { try { static_cast(unionptr)->update(*static_cast(sketchptr)); } catch (std::exception& e) { elog(ERROR, e.what()); } } void* cpc_union_get_result(void* unionptr) { try { auto ptr = static_cast(unionptr)->get_result(); return ptr.release(); } catch (std::exception& e) { elog(ERROR, e.what()); } }