<commit_before>/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/chips/p9/procedures/hwp/initfiles/p9_cxa_scom.C $  */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2016,2019                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#include "p9_cxa_scom.H"
#include <stdint.h>
#include <stddef.h>
#include <fapi2.H>

using namespace fapi2;

constexpr auto literal_0b0000 = 0b0000;
constexpr auto literal_0b111 = 0b111;
constexpr auto literal_0b0010 = 0b0010;
constexpr auto literal_0b0001 = 0b0001;
constexpr auto literal_0x0B1C000104060 = 0x0B1C000104060;
constexpr auto literal_0x2B9C0001240E0 = 0x2B9C0001240E0;
constexpr auto literal_0x80031F98D8717 = 0x80031F98D8717;

fapi2::ReturnCode p9_cxa_scom(const fapi2::Target<fapi2::TARGET_TYPE_CAPP>& TGT0,
                              const fapi2::Target<fapi2::TARGET_TYPE_SYSTEM>& TGT1)
{
    fapi2::ReturnCode l_rc = 0;

    do
    {
        fapi2::ATTR_PROC_FABRIC_ADDR_BAR_MODE_Type l_TGT1_ATTR_PROC_FABRIC_ADDR_BAR_MODE;
        l_rc = FAPI_ATTR_GET(fapi2::ATTR_PROC_FABRIC_ADDR_BAR_MODE, TGT1, l_TGT1_ATTR_PROC_FABRIC_ADDR_BAR_MODE);

        if (l_rc)
        {
            FAPI_ERR("ERROR executing: FAPI_ATTR_GET (ATTR_PROC_FABRIC_ADDR_BAR_MODE)");
            break;
        }

        {
            fapi2::buffer<uint64_t> l_scom_buffer;
            {
                l_rc = fapi2::getScom( TGT0, 0x2010818ull, l_scom_buffer );

                if (l_rc)
                {
                    FAPI_ERR("ERROR executing: getScom (0x2010818ull)");
                    break;
                }

                {
                    if ((l_TGT1_ATTR_PROC_FABRIC_ADDR_BAR_MODE == fapi2::ENUM_ATTR_PROC_FABRIC_ADDR_BAR_MODE_SMALL_SYSTEM))
                    {
                        constexpr auto l_CAPP0_CXA_TOP_CXA_APC0_APCCTL_ADR_BAR_MODE_ON = 0x1;
                        l_scom_buffer.insert<uint64_t> (l_CAPP0_CXA_TOP_CXA_APC0_APCCTL_ADR_BAR_MODE_ON, 1, 1, 63 );
                    }
                    else if ((l_TGT1_ATTR_PROC_FABRIC_ADDR_BAR_MODE == fapi2::ENUM_ATTR_PROC_FABRIC_ADDR_BAR_MODE_LARGE_SYSTEM))
                    {
                        constexpr auto l_CAPP0_CXA_TOP_CXA_APC0_APCCTL_ADR_BAR_MODE_OFF = 0x0;
                        l_scom_buffer.insert<uint64_t> (l_CAPP0_CXA_TOP_CXA_APC0_APCCTL_ADR_BAR_MODE_OFF, 1, 1, 63 );
                    }
                }

                l_rc = fapi2::putScom(TGT0, 0x2010818ull, l_scom_buffer);

                if (l_rc)
                {
                    FAPI_ERR("ERROR executing: putScom (0x2010818ull)");
                    break;
                }
            }
        }

        fapi2::ATTR_PROC_FABRIC_PUMP_MODE_Type l_TGT1_ATTR_PROC_FABRIC_PUMP_MODE;
        l_rc = FAPI_ATTR_GET(fapi2::ATTR_PROC_FABRIC_PUMP_MODE, TGT1, l_TGT1_ATTR_PROC_FABRIC_PUMP_MODE);

        if (l_rc)
        {
            FAPI_ERR("ERROR executing: FAPI_ATTR_GET (ATTR_PROC_FABRIC_PUMP_MODE)");
            break;
        }

        {
            fapi2::buffer<uint64_t> l_scom_buffer;
            {
                l_rc = fapi2::getScom( TGT0, 0x2010818ull, l_scom_buffer );

                if (l_rc)
                {
                    FAPI_ERR("ERROR executing: getScom (0x2010818ull)");
                    break;
                }

                {
                    if ((l_TGT1_ATTR_PROC_FABRIC_PUMP_MODE == fapi2::ENUM_ATTR_PROC_FABRIC_PUMP_MODE_CHIP_IS_GROUP))
                    {
                        constexpr auto l_CAPP0_CXA_TOP_CXA_APC0_APCCTL_SKIP_G_ON = 0x1;
                        l_scom_buffer.insert<uint64_t> (l_CAPP0_CXA_TOP_CXA_APC0_APCCTL_SKIP_G_ON, 6, 1, 63 );
                    }
                    else if ((l_TGT1_ATTR_PROC_FABRIC_PUMP_MODE == fapi2::ENUM_ATTR_PROC_FABRIC_PUMP_MODE_CHIP_IS_NODE))
                    {
                        constexpr auto l_CAPP0_CXA_TOP_CXA_APC0_APCCTL_SKIP_G_OFF = 0x0;
                        l_scom_buffer.insert<uint64_t> (l_CAPP0_CXA_TOP_CXA_APC0_APCCTL_SKIP_G_OFF, 6, 1, 63 );
                    }
                }

                l_rc = fapi2::putScom(TGT0, 0x2010818ull, l_scom_buffer);

                if (l_rc)
                {
                    FAPI_ERR("ERROR executing: putScom (0x2010818ull)");
                    break;
                }
            }
        }

        {
            fapi2::buffer<uint64_t> l_scom_buffer;
            {
                l_rc = fapi2::getScom( TGT0, 0x2010819ull, l_scom_buffer );

                if (l_rc)
                {
                    FAPI_ERR("ERROR executing: getScom (0x2010819ull)");
                    break;
                }

                {
                    l_scom_buffer.insert<uint64_t> (literal_0b0000, 4, 4, 60 );
                }

                l_rc = fapi2::putScom(TGT0, 0x2010819ull, l_scom_buffer);

                if (l_rc)
                {
                    FAPI_ERR("ERROR executing: putScom (0x2010819ull)");
                    break;
                }
            }
        }

        {
            fapi2::buffer<uint64_t> l_scom_buffer;
            {
                l_rc = fapi2::getScom( TGT0, 0x201081bull, l_scom_buffer );

                if (l_rc)
                {
                    FAPI_ERR("ERROR executing: getScom (0x201081bull)");
                    break;
                }

                {
                    l_scom_buffer.insert<uint64_t> (literal_0b111, 45, 3, 61 );
                }

                l_rc = fapi2::putScom(TGT0, 0x201081bull, l_scom_buffer);

                if (l_rc)
                {
                    FAPI_ERR("ERROR executing: putScom (0x201081bull)");
                    break;
                }
            }
        }

        {
            fapi2::buffer<uint64_t> l_scom_buffer;
            {
                l_rc = fapi2::getScom( TGT0, 0x201081bull, l_scom_buffer );

                if (l_rc)
                {
                    FAPI_ERR("ERROR executing: getScom (0x201081bull)");
                    break;
                }

                {
                    l_scom_buffer.insert<uint64_t> (literal_0b0010, 48, 4, 60 );
                }

                l_rc = fapi2::putScom(TGT0, 0x201081bull, l_scom_buffer);

                if (l_rc)
                {
                    FAPI_ERR("ERROR executing: putScom (0x201081bull)");
                    break;
                }
            }
        }

        {
            fapi2::buffer<uint64_t> l_scom_buffer;
            {
                l_rc = fapi2::getScom( TGT0, 0x201081cull, l_scom_buffer );

                if (l_rc)
                {
                    FAPI_ERR("ERROR executing: getScom (0x201081cull)");
                    break;
                }

                {
                    l_scom_buffer.insert<uint64_t> (literal_0b0001, 18, 4, 60 );
                }

                l_rc = fapi2::putScom(TGT0, 0x201081cull, l_scom_buffer);

                if (l_rc)
                {
                    FAPI_ERR("ERROR executing: putScom (0x201081cull)");
                    break;
                }
            }
        }

        {
            fapi2::buffer<uint64_t> l_scom_buffer;
            {
                l_rc = fapi2::getScom( TGT0, 0x2010806ull, l_scom_buffer );

                if (l_rc)
                {
                    FAPI_ERR("ERROR executing: getScom (0x2010806ull)");
                    break;
                }

                {
                    l_scom_buffer.insert<uint64_t> (literal_0x0B1C000104060, 0, 52, 12 );
                }

                l_rc = fapi2::putScom(TGT0, 0x2010806ull, l_scom_buffer);

                if (l_rc)
                {
                    FAPI_ERR("ERROR executing: putScom (0x2010806ull)");
                    break;
                }
            }
        }

        {
            fapi2::buffer<uint64_t> l_scom_buffer;
            {
                l_rc = fapi2::getScom( TGT0, 0x2010807ull, l_scom_buffer );

                if (l_rc)
                {
                    FAPI_ERR("ERROR executing: getScom (0x2010807ull)");
                    break;
                }

                {
                    l_scom_buffer.insert<uint64_t> (literal_0x2B9C0001240E0, 0, 52, 12 );
                }

                l_rc = fapi2::putScom(TGT0, 0x2010807ull, l_scom_buffer);

                if (l_rc)
                {
                    FAPI_ERR("ERROR executing: putScom (0x2010807ull)");
                    break;
                }
            }
        }

        {
            fapi2::buffer<uint64_t> l_scom_buffer;
            {
                l_rc = fapi2::getScom( TGT0, 0x2010803ull, l_scom_buffer );

                if (l_rc)
                {
                    FAPI_ERR("ERROR executing: getScom (0x2010803ull)");
                    break;
                }

                {
                    l_scom_buffer.insert<uint64_t> (literal_0x80031F98D8717, 0, 52, 0 );
                }

                l_rc = fapi2::putScom(TGT0, 0x2010803ull, l_scom_buffer);

                if (l_rc)
                {
                    FAPI_ERR("ERROR executing: putScom (0x2010803ull)");
                    break;
                }
            }
        }

    }
    while(0);

    return l_rc;
}
<commit_msg>Update code to consolidate writes to same address in same putScom<commit_after>/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/import/chips/p9/procedures/hwp/initfiles/p9_cxa_scom.C $  */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2016,2019                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
#include "p9_cxa_scom.H"
#include <stdint.h>
#include <stddef.h>
#include <fapi2.H>

using namespace fapi2;

constexpr auto literal_0x80031F98D8717 = 0x80031F98D8717;
constexpr auto literal_0x0B1C000104060 = 0x0B1C000104060;
constexpr auto literal_0x2B9C0001240E0 = 0x2B9C0001240E0;
constexpr auto literal_0b0000 = 0b0000;
constexpr auto literal_0b111 = 0b111;
constexpr auto literal_0b0010 = 0b0010;
constexpr auto literal_0b0001 = 0b0001;

fapi2::ReturnCode p9_cxa_scom(const fapi2::Target<fapi2::TARGET_TYPE_CAPP>& TGT0,
                              const fapi2::Target<fapi2::TARGET_TYPE_SYSTEM>& TGT1)
{
    fapi2::ReturnCode l_rc = 0;

    do
    {
        fapi2::ATTR_PROC_FABRIC_ADDR_BAR_MODE_Type l_TGT1_ATTR_PROC_FABRIC_ADDR_BAR_MODE;
        l_rc = FAPI_ATTR_GET(fapi2::ATTR_PROC_FABRIC_ADDR_BAR_MODE, TGT1, l_TGT1_ATTR_PROC_FABRIC_ADDR_BAR_MODE);

        if (l_rc)
        {
            FAPI_ERR("ERROR executing: FAPI_ATTR_GET (ATTR_PROC_FABRIC_ADDR_BAR_MODE)");
            break;
        }

        fapi2::ATTR_PROC_FABRIC_PUMP_MODE_Type l_TGT1_ATTR_PROC_FABRIC_PUMP_MODE;
        l_rc = FAPI_ATTR_GET(fapi2::ATTR_PROC_FABRIC_PUMP_MODE, TGT1, l_TGT1_ATTR_PROC_FABRIC_PUMP_MODE);

        if (l_rc)
        {
            FAPI_ERR("ERROR executing: FAPI_ATTR_GET (ATTR_PROC_FABRIC_PUMP_MODE)");
            break;
        }

        fapi2::buffer<uint64_t> l_scom_buffer;
        {
            l_rc = fapi2::getScom( TGT0, 0x2010803ull, l_scom_buffer );

            if (l_rc)
            {
                FAPI_ERR("ERROR executing: getScom (0x2010803ull)");
                break;
            }

            l_scom_buffer.insert<uint64_t> (literal_0x80031F98D8717, 0, 52, 0 );
            l_rc = fapi2::putScom(TGT0, 0x2010803ull, l_scom_buffer);

            if (l_rc)
            {
                FAPI_ERR("ERROR executing: putScom (0x2010803ull)");
                break;
            }
        }
        {
            l_rc = fapi2::getScom( TGT0, 0x2010806ull, l_scom_buffer );

            if (l_rc)
            {
                FAPI_ERR("ERROR executing: getScom (0x2010806ull)");
                break;
            }

            l_scom_buffer.insert<uint64_t> (literal_0x0B1C000104060, 0, 52, 12 );
            l_rc = fapi2::putScom(TGT0, 0x2010806ull, l_scom_buffer);

            if (l_rc)
            {
                FAPI_ERR("ERROR executing: putScom (0x2010806ull)");
                break;
            }
        }
        {
            l_rc = fapi2::getScom( TGT0, 0x2010807ull, l_scom_buffer );

            if (l_rc)
            {
                FAPI_ERR("ERROR executing: getScom (0x2010807ull)");
                break;
            }

            l_scom_buffer.insert<uint64_t> (literal_0x2B9C0001240E0, 0, 52, 12 );
            l_rc = fapi2::putScom(TGT0, 0x2010807ull, l_scom_buffer);

            if (l_rc)
            {
                FAPI_ERR("ERROR executing: putScom (0x2010807ull)");
                break;
            }
        }
        {
            l_rc = fapi2::getScom( TGT0, 0x2010818ull, l_scom_buffer );

            if (l_rc)
            {
                FAPI_ERR("ERROR executing: getScom (0x2010818ull)");
                break;
            }

            if ((l_TGT1_ATTR_PROC_FABRIC_ADDR_BAR_MODE == fapi2::ENUM_ATTR_PROC_FABRIC_ADDR_BAR_MODE_SMALL_SYSTEM))
            {
                constexpr auto l_CAPP0_CXA_TOP_CXA_APC0_APCCTL_ADR_BAR_MODE_ON = 0x1;
                l_scom_buffer.insert<uint64_t> (l_CAPP0_CXA_TOP_CXA_APC0_APCCTL_ADR_BAR_MODE_ON, 1, 1, 63 );
            }
            else if ((l_TGT1_ATTR_PROC_FABRIC_ADDR_BAR_MODE == fapi2::ENUM_ATTR_PROC_FABRIC_ADDR_BAR_MODE_LARGE_SYSTEM))
            {
                constexpr auto l_CAPP0_CXA_TOP_CXA_APC0_APCCTL_ADR_BAR_MODE_OFF = 0x0;
                l_scom_buffer.insert<uint64_t> (l_CAPP0_CXA_TOP_CXA_APC0_APCCTL_ADR_BAR_MODE_OFF, 1, 1, 63 );
            }

            if ((l_TGT1_ATTR_PROC_FABRIC_PUMP_MODE == fapi2::ENUM_ATTR_PROC_FABRIC_PUMP_MODE_CHIP_IS_GROUP))
            {
                constexpr auto l_CAPP0_CXA_TOP_CXA_APC0_APCCTL_SKIP_G_ON = 0x1;
                l_scom_buffer.insert<uint64_t> (l_CAPP0_CXA_TOP_CXA_APC0_APCCTL_SKIP_G_ON, 6, 1, 63 );
            }
            else if ((l_TGT1_ATTR_PROC_FABRIC_PUMP_MODE == fapi2::ENUM_ATTR_PROC_FABRIC_PUMP_MODE_CHIP_IS_NODE))
            {
                constexpr auto l_CAPP0_CXA_TOP_CXA_APC0_APCCTL_SKIP_G_OFF = 0x0;
                l_scom_buffer.insert<uint64_t> (l_CAPP0_CXA_TOP_CXA_APC0_APCCTL_SKIP_G_OFF, 6, 1, 63 );
            }

            l_rc = fapi2::putScom(TGT0, 0x2010818ull, l_scom_buffer);

            if (l_rc)
            {
                FAPI_ERR("ERROR executing: putScom (0x2010818ull)");
                break;
            }
        }
        {
            l_rc = fapi2::getScom( TGT0, 0x2010819ull, l_scom_buffer );

            if (l_rc)
            {
                FAPI_ERR("ERROR executing: getScom (0x2010819ull)");
                break;
            }

            l_scom_buffer.insert<uint64_t> (literal_0b0000, 4, 4, 60 );
            l_rc = fapi2::putScom(TGT0, 0x2010819ull, l_scom_buffer);

            if (l_rc)
            {
                FAPI_ERR("ERROR executing: putScom (0x2010819ull)");
                break;
            }
        }
        {
            l_rc = fapi2::getScom( TGT0, 0x201081bull, l_scom_buffer );

            if (l_rc)
            {
                FAPI_ERR("ERROR executing: getScom (0x201081bull)");
                break;
            }

            l_scom_buffer.insert<uint64_t> (literal_0b111, 45, 3, 61 );
            l_scom_buffer.insert<uint64_t> (literal_0b0010, 48, 4, 60 );
            l_rc = fapi2::putScom(TGT0, 0x201081bull, l_scom_buffer);

            if (l_rc)
            {
                FAPI_ERR("ERROR executing: putScom (0x201081bull)");
                break;
            }
        }
        {
            l_rc = fapi2::getScom( TGT0, 0x201081cull, l_scom_buffer );

            if (l_rc)
            {
                FAPI_ERR("ERROR executing: getScom (0x201081cull)");
                break;
            }

            l_scom_buffer.insert<uint64_t> (literal_0b0001, 18, 4, 60 );
            l_rc = fapi2::putScom(TGT0, 0x201081cull, l_scom_buffer);

            if (l_rc)
            {
                FAPI_ERR("ERROR executing: putScom (0x201081cull)");
                break;
            }
        }

    }
    while(0);

    return l_rc;
}
<|endoftext|>


<commit_before>#include "erlv8.hh"

TickHandler(ProplistTickHandler) {
  ErlNifEnv *ref_env = enif_alloc_env();
  ERL_NIF_TERM proplist_ref = enif_make_copy(ref_env, tick_ref);

  val_res_t *res;
  if (enif_get_resource(vm->env,array[1],val_resource,(void **)(&res))) {
    LHCS(vm->isolate, res->ctx);

    v8::Handle<v8::Array> keys = res->val->ToObject()->GetPropertyNames();

    ERL_NIF_TERM *arr = (ERL_NIF_TERM *) malloc(sizeof(ERL_NIF_TERM) * keys->Length());

    for (unsigned int i=0;i<keys->Length();i++) {
      v8::Handle<v8::Value> key = keys->Get(v8::Integer::New(i));
      arr[i] = enif_make_tuple2(vm->env,
                                js_to_term(res->ctx, vm->isolate, vm->env,v8::Handle<v8::String>::Cast(key)),
                                js_to_term(res->ctx, vm->isolate, vm->env,res->val->ToObject()->Get(key)));
    }
    ERL_NIF_TERM list = enif_make_list_from_array(vm->env,arr,keys->Length());
    free(arr);
    SEND(vm->server,
         enif_make_tuple3(env,
                          enif_make_atom(env,"result"),
                          enif_make_copy(env,proplist_ref),
                          enif_make_copy(env,list)));
  }

  enif_free_env(ref_env);
  TickHandlerResolution result;
  result.type = DONE;
  return result;
}
<commit_msg>fix #66<commit_after>#include "erlv8.hh"

TickHandler(ProplistTickHandler) {
  ErlNifEnv *ref_env = enif_alloc_env();
  ERL_NIF_TERM proplist_ref = enif_make_copy(ref_env, tick_ref);

  val_res_t *res;
  if (enif_get_resource(vm->env,array[1],val_resource,(void **)(&res))) {
    LHCS(vm->isolate, res->ctx);

    v8::Handle<v8::Array> keys;
    if (res->val->IsNativeError()) { //workaround for V8 bug #1595
        keys = v8::Array::New(2);
            keys->Set(v8::String::New("0"), v8::String::New("name"));
            keys->Set(v8::String::New("1"), v8::String::New("message"));
    } else {
        keys = res->val->ToObject()->GetPropertyNames();
    }

    ERL_NIF_TERM *arr = (ERL_NIF_TERM *) malloc(sizeof(ERL_NIF_TERM) * keys->Length());

    for (unsigned int i=0;i<keys->Length();i++) {
      v8::Handle<v8::Value> key = keys->Get(v8::Integer::New(i));
      arr[i] = enif_make_tuple2(vm->env,
                                js_to_term(res->ctx, vm->isolate, vm->env,v8::Handle<v8::String>::Cast(key)),
                                js_to_term(res->ctx, vm->isolate, vm->env,res->val->ToObject()->Get(key)));
    }
    ERL_NIF_TERM list = enif_make_list_from_array(vm->env,arr,keys->Length());
    free(arr);
    SEND(vm->server,
         enif_make_tuple3(env,
                          enif_make_atom(env,"result"),
                          enif_make_copy(env,proplist_ref),
                          enif_make_copy(env,list)));
  }

  enif_free_env(ref_env);
  TickHandlerResolution result;
  result.type = DONE;
  return result;
}
<|endoftext|>


<commit_before>/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2003 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. 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.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Xerces" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact apache\@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 APACHE SOFTWARE FOUNDATION OR
 * ITS 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.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation, and was
 * originally based on software copyright (c) 1999, International
 * Business Machines, Inc., http://www.ibm.com .  For more information
 * on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

/*
 * $Log$
 * Revision 1.6  2003/12/02 17:31:42  neilg
 * since there are certain things, such as schemaLocation attributes, that have a datatype and which we nonetheless do not validate, make canonical-value production dependent on validity being valid
 *
 * Revision 1.5  2003/11/28 22:41:04  neilg
 * fix compilation error
 *
 * Revision 1.4  2003/11/28 20:20:54  neilg
 * make use of canonical representation in PSVIAttribute implementation
 *
 * Revision 1.3  2003/11/27 06:10:32  neilg
 * PSVIAttribute implementation
 *
 * Revision 1.2  2003/11/06 21:50:33  neilg
 * fix compilation errors under gcc 3.3.
 *
 * Revision 1.1  2003/09/16 14:33:36  neilg
 * PSVI/schema component model classes, with Makefile/configuration changes necessary to build them
 *
 */

#include <xercesc/framework/psvi/PSVIAttribute.hpp>

XERCES_CPP_NAMESPACE_BEGIN

PSVIAttribute::PSVIAttribute( MemoryManager* const manager ):
        PSVIItem(manager),
        fAttributeDecl(0)
{
}

void PSVIAttribute::reset(
            const XMLCh * const         valContext
            , PSVIItem::VALIDITY_STATE  state
            , PSVIItem::ASSESSMENT_TYPE assessmentType
            , const XMLCh * const       normalizedValue
            , XSSimpleTypeDefinition *  validatingType
            , XSSimpleTypeDefinition *  memberType
            , const XMLCh * const       defaultValue
            , const bool                isSpecified
            , XSAttributeDeclaration *  attrDecl
            , DatatypeValidator *dv
        )
{
    fValidationContext = valContext;
    fValidityState = state;
    fAssessmentType = assessmentType;
    fNormalizedValue = normalizedValue;
    fType = validatingType;
    fMemberType = memberType;
    fDefaultValue = defaultValue;
    fIsSpecified = isSpecified;
    fMemoryManager->deallocate((void *)fCanonicalValue);
    if(normalizedValue && dv && fValidityState == VALIDITY_STATE::VALID)
        fCanonicalValue = (XMLCh *)dv->getCanonicalRepresentation(normalizedValue, fMemoryManager);
    else
        fCanonicalValue = 0;
    fAttributeDecl = attrDecl;
}

XERCES_CPP_NAMESPACE_END

<commit_msg>fix compilation problem<commit_after>/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2003 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. 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.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Xerces" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact apache\@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 APACHE SOFTWARE FOUNDATION OR
 * ITS 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.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation, and was
 * originally based on software copyright (c) 1999, International
 * Business Machines, Inc., http://www.ibm.com .  For more information
 * on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

/*
 * $Log$
 * Revision 1.7  2003/12/02 17:37:03  neilg
 * fix compilation problem
 *
 * Revision 1.6  2003/12/02 17:31:42  neilg
 * since there are certain things, such as schemaLocation attributes, that have a datatype and which we nonetheless do not validate, make canonical-value production dependent on validity being valid
 *
 * Revision 1.5  2003/11/28 22:41:04  neilg
 * fix compilation error
 *
 * Revision 1.4  2003/11/28 20:20:54  neilg
 * make use of canonical representation in PSVIAttribute implementation
 *
 * Revision 1.3  2003/11/27 06:10:32  neilg
 * PSVIAttribute implementation
 *
 * Revision 1.2  2003/11/06 21:50:33  neilg
 * fix compilation errors under gcc 3.3.
 *
 * Revision 1.1  2003/09/16 14:33:36  neilg
 * PSVI/schema component model classes, with Makefile/configuration changes necessary to build them
 *
 */

#include <xercesc/framework/psvi/PSVIAttribute.hpp>

XERCES_CPP_NAMESPACE_BEGIN

PSVIAttribute::PSVIAttribute( MemoryManager* const manager ):
        PSVIItem(manager),
        fAttributeDecl(0)
{
}

void PSVIAttribute::reset(
            const XMLCh * const         valContext
            , PSVIItem::VALIDITY_STATE  state
            , PSVIItem::ASSESSMENT_TYPE assessmentType
            , const XMLCh * const       normalizedValue
            , XSSimpleTypeDefinition *  validatingType
            , XSSimpleTypeDefinition *  memberType
            , const XMLCh * const       defaultValue
            , const bool                isSpecified
            , XSAttributeDeclaration *  attrDecl
            , DatatypeValidator *dv
        )
{
    fValidationContext = valContext;
    fValidityState = state;
    fAssessmentType = assessmentType;
    fNormalizedValue = normalizedValue;
    fType = validatingType;
    fMemberType = memberType;
    fDefaultValue = defaultValue;
    fIsSpecified = isSpecified;
    fMemoryManager->deallocate((void *)fCanonicalValue);
    if(normalizedValue && dv && fValidityState == PSVIItem::VALIDITY_VALID)
        fCanonicalValue = (XMLCh *)dv->getCanonicalRepresentation(normalizedValue, fMemoryManager);
    else
        fCanonicalValue = 0;
    fAttributeDecl = attrDecl;
}

XERCES_CPP_NAMESPACE_END

<|endoftext|>


<commit_before>/**
 * This file is part of the "tsdb" project
 *   Copyright (c) 2015 Paul Asmuth, FnordCorp B.V.
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#include <zbase/core/StaticPartitionReplication.h>
#include <zbase/core/ReplicationScheme.h>
#include <zbase/core/TSDBClient.h>
#include <stx/logging.h>
#include <stx/io/fileutil.h>
#include <stx/protobuf/msg.h>

using namespace stx;

namespace zbase {

StaticPartitionReplication::StaticPartitionReplication(
    RefPtr<Partition> partition,
    RefPtr<ReplicationScheme> repl_scheme,
    http::HTTPConnectionPool* http) :
    PartitionReplication(partition, repl_scheme, http) {}

bool StaticPartitionReplication::needsReplication() const {
  auto replicas = repl_scheme_->replicasFor(snap_->key);
  if (replicas.size() == 0) {
    return false;
  }

  auto repl_state = fetchReplicationState();
  auto head_version = snap_->state.cstable_version();
  for (const auto& r : replicas) {
    const auto& replica_version = replicatedVersionFor(repl_state, r.unique_id);
    if (replica_version < head_version) {
      return true;
    }
  }

  return false;
}

void StaticPartitionReplication::replicateTo(
    const ReplicaRef& replica,
    uint64_t head_version) {
  auto tsdb_url = StringUtil::format("http://$0/tsdb", replica.addr.hostAndPort());
  zbase::TSDBClient tsdb_client(tsdb_url, http_);

  auto pinfo = tsdb_client.partitionInfo(
      snap_->state.tsdb_namespace(),
      snap_->state.table_key(),
      snap_->key);

  if (!pinfo.isEmpty() && pinfo.get().cstable_version() >= head_version) {
    return;
  }

  auto reader = partition_->getReader();
  auto cstable_file = reader->cstableFilename();

  if (cstable_file.isEmpty()) {
    return;
  }

  auto uri = URI(
      StringUtil::format(
          "$0/update_cstable?namespace=$1&table=$2&partition=$3&version=$4",
          tsdb_url,
          URI::urlEncode(snap_->state.tsdb_namespace()),
          URI::urlEncode(snap_->state.table_key()),
          snap_->key.toString(),
          head_version));

  http::HTTPRequest req(http::HTTPMessage::M_POST, uri.pathAndQuery());
  req.addHeader("Host", uri.hostAndPort());
  req.addBody(FileUtil::read(cstable_file.get())); // FIXME use FileUpload

  auto res = http_->executeRequest(req);
  res.wait();

  const auto& r = res.get();
  if (r.statusCode() != 201) {
    RAISEF(kRuntimeError, "received non-201 response: $0", r.body().toString());
  }
}

bool StaticPartitionReplication::replicate() {
  auto replicas = repl_scheme_->replicasFor(snap_->key);
  if (replicas.size() == 0) {
    return true;
  }

  auto repl_state = fetchReplicationState();
  auto head_version = snap_->state.cstable_version();
  bool dirty = false;
  bool success = true;

  for (const auto& r : replicas) {
    const auto& replica_version = replicatedVersionFor(repl_state, r.unique_id);

    if (replica_version < head_version) {
      logTrace(
          "tsdb",
          "Replicating partition $0/$1/$2 to $3",
          snap_->state.tsdb_namespace(),
          snap_->state.table_key(),
          snap_->key.toString(),
          r.addr.hostAndPort());

      try {
        replicateTo(r, head_version);
        setReplicatedVersionFor(&repl_state, r.unique_id, head_version);
        dirty = true;
      } catch (const std::exception& e) {
        success = false;

        stx::logError(
          "tsdb",
          e,
          "Error while replicating partition $0/$1/$2 to $3",
          snap_->state.tsdb_namespace(),
          snap_->state.table_key(),
          snap_->key.toString(),
          r.addr.hostAndPort());
      }
    }
  }

  if (dirty) {
    commitReplicationState(repl_state);
  }

  return success;
}

} // namespace tdsb

<commit_msg>logging<commit_after>/**
 * This file is part of the "tsdb" project
 *   Copyright (c) 2015 Paul Asmuth, FnordCorp B.V.
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#include <zbase/core/StaticPartitionReplication.h>
#include <zbase/core/ReplicationScheme.h>
#include <zbase/core/TSDBClient.h>
#include <stx/logging.h>
#include <stx/io/fileutil.h>
#include <stx/protobuf/msg.h>

using namespace stx;

namespace zbase {

StaticPartitionReplication::StaticPartitionReplication(
    RefPtr<Partition> partition,
    RefPtr<ReplicationScheme> repl_scheme,
    http::HTTPConnectionPool* http) :
    PartitionReplication(partition, repl_scheme, http) {}

bool StaticPartitionReplication::needsReplication() const {
  auto replicas = repl_scheme_->replicasFor(snap_->key);
  if (replicas.size() == 0) {
    return false;
  }

  auto repl_state = fetchReplicationState();
  auto head_version = snap_->state.cstable_version();
  for (const auto& r : replicas) {
    const auto& replica_version = replicatedVersionFor(repl_state, r.unique_id);
    if (replica_version < head_version) {
      return true;
    }
  }

  return false;
}

void StaticPartitionReplication::replicateTo(
    const ReplicaRef& replica,
    uint64_t head_version) {
  auto tsdb_url = StringUtil::format("http://$0/tsdb", replica.addr.hostAndPort());
  zbase::TSDBClient tsdb_client(tsdb_url, http_);

  auto pinfo = tsdb_client.partitionInfo(
      snap_->state.tsdb_namespace(),
      snap_->state.table_key(),
      snap_->key);

  if (!pinfo.isEmpty() && pinfo.get().cstable_version() >= head_version) {
    return;
  }

  auto reader = partition_->getReader();
  auto cstable_file = reader->cstableFilename();

  if (cstable_file.isEmpty()) {
    return;
  }

  auto uri = URI(
      StringUtil::format(
          "$0/update_cstable?namespace=$1&table=$2&partition=$3&version=$4",
          tsdb_url,
          URI::urlEncode(snap_->state.tsdb_namespace()),
          URI::urlEncode(snap_->state.table_key()),
          snap_->key.toString(),
          head_version));

  http::HTTPRequest req(http::HTTPMessage::M_POST, uri.pathAndQuery());
  req.addHeader("Host", uri.hostAndPort());
  req.addBody(FileUtil::read(cstable_file.get())); // FIXME use FileUpload

  auto res = http_->executeRequest(req);
  res.wait();

  const auto& r = res.get();
  if (r.statusCode() != 201) {
    RAISEF(kRuntimeError, "received non-201 response: $0", r.body().toString());
  }
}

bool StaticPartitionReplication::replicate() {
  auto replicas = repl_scheme_->replicasFor(snap_->key);
  if (replicas.size() == 0) {
    return true;
  }

  auto repl_state = fetchReplicationState();
  auto head_version = snap_->state.cstable_version();
  bool dirty = false;
  bool success = true;

  for (const auto& r : replicas) {
    const auto& replica_version = replicatedVersionFor(repl_state, r.unique_id);

    if (replica_version < head_version) {
      logTrace(
          "tsdb",
          "Replicating partition $0/$1/$2 to $3",
          snap_->state.tsdb_namespace(),
          snap_->state.table_key(),
          snap_->key.toString(),
          r.addr.hostAndPort());

      try {
        replicateTo(r, head_version);
        setReplicatedVersionFor(&repl_state, r.unique_id, head_version);
        dirty = true;

        logTrace(
            "tsdb",
            "Replicating done: $0/$1/$2 to $3",
            snap_->state.tsdb_namespace(),
            snap_->state.table_key(),
            snap_->key.toString(),
            r.addr.hostAndPort());
      } catch (const std::exception& e) {
        success = false;

        stx::logError(
          "tsdb",
          e,
          "Error while replicating partition $0/$1/$2 to $3",
          snap_->state.tsdb_namespace(),
          snap_->state.table_key(),
          snap_->key.toString(),
          r.addr.hostAndPort());
      }
    }
  }

  if (dirty) {
    commitReplicationState(repl_state);
  }

  return success;
}

} // namespace tdsb

<|endoftext|>


<commit_before>// Copyright 2014 Alessio Sclocco <a.sclocco@vu.nl>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <string>
#include <vector>
#include <map>
#include <fstream>

#include <utils.hpp>
#include <Observation.hpp>

#ifndef FOLDING_HPP
#define FOLDING_HPP

namespace PulsarSearch {

class FoldingConf {
public:
  FoldingConf();
  ~FoldingConf();

  // Get
  unsigned int getNrDMsPerBlock() const;
  unsigned int getNrDMsPerThread() const;
  unsigned int getNrPeriodsPerBlock() const;
  unsigned int getNrPeriodsPerThread() const;
  unsigned int getNrBinsPerBlock() const;
  unsigned int getNrBinsPerThread() const;
  unsigned int getVector() const;
  // Set
  void setNrDMsPerBlock(unsigned int dms);
  void setNrDMsPerThread(unsigned int dms);
  void setNrPeriodsPerBlock(unsigned int periods);
  void setNrPeriodsPerThread(unsigned int periods);
  void setNrBinsPerBlock(unsigned int bins);
  void setNrBinsPerThread(unsigned int bins);
  void setVector(unsigned int vector);
  // utils
  std::string print() const;

private:
  unsigned int nrDMsPerBlock;
  unsigned int nrDMsPerThread;
  unsigned int nrPeriodsPerBlock;
  unsigned int nrPeriodsPerThread;
  unsigned int nrBinsPerBlock;
  unsigned int nrBinsPerThread;
  unsigned int vector;
};

typename std::map< std::string, std::map< unsigned int, std::map< unsigned int, PulsarSearch::FoldingConf > > > tunedFoldingConf;

// Sequential folding
template< typename T > void folding(const unsigned int second, const AstroData::Observation & observation, const std::vector< T > & samples, std::vector< T > & bins, std::vector< unsigned int > & counters);
// OpenCL folding algorithm
std::string * getFoldingOpenCL(const FoldingConf & conf, const std::string & dataType, const AstroData::Observation & observation);
// Read configuration files
void readTunedFoldingConf(tunedFoldingConf & tunedFolding, const std::string & foldingFilename);

// Implementations
inline unsigned int FoldingConf::getNrDMsPerBlock() const {
  return nrDMsPerBlock;
}

inline unsigned int FoldingConf::getNrDMsPerThread() const {
  return nrDMsPerThread;
}

inline unsigned int FoldingConf::getNrPeriodsPerBlock() const {
  return nrPeriodsPerBlock;
}

inline unsigned int FoldingConf::getNrPeriodsPerThread() const {
  return nrPeriodsPerThread;
}

inline unsigned int FoldingConf::getNrBinsPerBlock() const {
  return nrBinsPerBlock;
}

inline unsigned int FoldingConf::getNrBinsPerThread() const {
  return nrBinsPerThread;
}

inline unsigned int FoldingConf::getVector() const {
  return vector;
}

inline void FoldingConf::setNrDMsPerBlock(unsigned int dms) {
  nrDMsPerBlock = dms;
}

inline void FoldingConf::setNrDMsPerThread(unsigned int dms) {
  nrDMsPerThread = dms;
}

inline void FoldingConf::setNrPeriodsPerBlock(unsigned int periods) {
  nrPeriodsPerBlock = periods;
}

inline void FoldingConf::setNrPeriodsPerThread(unsigned int periods) {
  nrPeriodsPerThread = periods;
}

inline void FoldingConf::setNrBinsPerBlock(unsigned int bins) {
  nrBinsPerBlock = bins;
}

inline void FoldingConf::setNrBinsPerThread(unsigned int bins) {
  nrBinsPerThread = bins;
}

inline void FoldingConf::setVector(unsigned int vector) {
  this->vector = vector;
}

template< typename T > void folding(const unsigned int second, const AstroData::Observation & observation, const std::vector< T > & samples, std::vector< T > & bins, std::vector< unsigned int > & counters) {
  for ( unsigned int dm = 0; dm < observation.getNrDMs(); dm++ ) {
    for ( unsigned int periodIndex = 0; periodIndex < observation.getNrPeriods(); periodIndex++ ) {
      const unsigned int periodValue = observation.getFirstPeriod() + (periodIndex * observation.getPeriodStep());

      for ( unsigned int globalSample = 0; globalSample < observation.getNrSamplesPerSecond(); globalSample++ ) {
        const unsigned int sample = (second * observation.getNrSamplesPerSecond()) + globalSample;
        const float phase = (sample / static_cast< float >(periodValue)) - (sample / periodValue);
        const unsigned int bin = static_cast< unsigned int >(phase * static_cast< float >(observation.getNrBins()));
        const unsigned int globalItem = (dm * observation.getNrPeriods() * observation.getNrPaddedBins()) + (periodIndex * observation.getNrPaddedBins()) + bin;

        const T pValue = bins[globalItem];
        const unsigned int pCounter = counters[globalItem];
        T cValue = samples[(dm * observation.getNrSamplesPerPaddedSecond()) + globalSample];
        unsigned int cCounter = pCounter + 1;

        if ( pCounter != 0 ) {
          cValue = pValue + ((cValue - pValue) / cCounter);
        }
        bins[globalItem] = cValue;
        counters[globalItem] = cCounter;
      }
    }
  }
}

} // PulsarSearch

#endif // FOLDING_HPP
<commit_msg>Typo.<commit_after>// Copyright 2014 Alessio Sclocco <a.sclocco@vu.nl>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <string>
#include <vector>
#include <map>
#include <fstream>

#include <utils.hpp>
#include <Observation.hpp>

#ifndef FOLDING_HPP
#define FOLDING_HPP

namespace PulsarSearch {

class FoldingConf {
public:
  FoldingConf();
  ~FoldingConf();

  // Get
  unsigned int getNrDMsPerBlock() const;
  unsigned int getNrDMsPerThread() const;
  unsigned int getNrPeriodsPerBlock() const;
  unsigned int getNrPeriodsPerThread() const;
  unsigned int getNrBinsPerBlock() const;
  unsigned int getNrBinsPerThread() const;
  unsigned int getVector() const;
  // Set
  void setNrDMsPerBlock(unsigned int dms);
  void setNrDMsPerThread(unsigned int dms);
  void setNrPeriodsPerBlock(unsigned int periods);
  void setNrPeriodsPerThread(unsigned int periods);
  void setNrBinsPerBlock(unsigned int bins);
  void setNrBinsPerThread(unsigned int bins);
  void setVector(unsigned int vector);
  // utils
  std::string print() const;

private:
  unsigned int nrDMsPerBlock;
  unsigned int nrDMsPerThread;
  unsigned int nrPeriodsPerBlock;
  unsigned int nrPeriodsPerThread;
  unsigned int nrBinsPerBlock;
  unsigned int nrBinsPerThread;
  unsigned int vector;
};

typedef std::map< std::string, std::map< unsigned int, std::map< unsigned int, PulsarSearch::FoldingConf > > > tunedFoldingConf;

// Sequential folding
template< typename T > void folding(const unsigned int second, const AstroData::Observation & observation, const std::vector< T > & samples, std::vector< T > & bins, std::vector< unsigned int > & counters);
// OpenCL folding algorithm
std::string * getFoldingOpenCL(const FoldingConf & conf, const std::string & dataType, const AstroData::Observation & observation);
// Read configuration files
void readTunedFoldingConf(tunedFoldingConf & tunedFolding, const std::string & foldingFilename);

// Implementations
inline unsigned int FoldingConf::getNrDMsPerBlock() const {
  return nrDMsPerBlock;
}

inline unsigned int FoldingConf::getNrDMsPerThread() const {
  return nrDMsPerThread;
}

inline unsigned int FoldingConf::getNrPeriodsPerBlock() const {
  return nrPeriodsPerBlock;
}

inline unsigned int FoldingConf::getNrPeriodsPerThread() const {
  return nrPeriodsPerThread;
}

inline unsigned int FoldingConf::getNrBinsPerBlock() const {
  return nrBinsPerBlock;
}

inline unsigned int FoldingConf::getNrBinsPerThread() const {
  return nrBinsPerThread;
}

inline unsigned int FoldingConf::getVector() const {
  return vector;
}

inline void FoldingConf::setNrDMsPerBlock(unsigned int dms) {
  nrDMsPerBlock = dms;
}

inline void FoldingConf::setNrDMsPerThread(unsigned int dms) {
  nrDMsPerThread = dms;
}

inline void FoldingConf::setNrPeriodsPerBlock(unsigned int periods) {
  nrPeriodsPerBlock = periods;
}

inline void FoldingConf::setNrPeriodsPerThread(unsigned int periods) {
  nrPeriodsPerThread = periods;
}

inline void FoldingConf::setNrBinsPerBlock(unsigned int bins) {
  nrBinsPerBlock = bins;
}

inline void FoldingConf::setNrBinsPerThread(unsigned int bins) {
  nrBinsPerThread = bins;
}

inline void FoldingConf::setVector(unsigned int vector) {
  this->vector = vector;
}

template< typename T > void folding(const unsigned int second, const AstroData::Observation & observation, const std::vector< T > & samples, std::vector< T > & bins, std::vector< unsigned int > & counters) {
  for ( unsigned int dm = 0; dm < observation.getNrDMs(); dm++ ) {
    for ( unsigned int periodIndex = 0; periodIndex < observation.getNrPeriods(); periodIndex++ ) {
      const unsigned int periodValue = observation.getFirstPeriod() + (periodIndex * observation.getPeriodStep());

      for ( unsigned int globalSample = 0; globalSample < observation.getNrSamplesPerSecond(); globalSample++ ) {
        const unsigned int sample = (second * observation.getNrSamplesPerSecond()) + globalSample;
        const float phase = (sample / static_cast< float >(periodValue)) - (sample / periodValue);
        const unsigned int bin = static_cast< unsigned int >(phase * static_cast< float >(observation.getNrBins()));
        const unsigned int globalItem = (dm * observation.getNrPeriods() * observation.getNrPaddedBins()) + (periodIndex * observation.getNrPaddedBins()) + bin;

        const T pValue = bins[globalItem];
        const unsigned int pCounter = counters[globalItem];
        T cValue = samples[(dm * observation.getNrSamplesPerPaddedSecond()) + globalSample];
        unsigned int cCounter = pCounter + 1;

        if ( pCounter != 0 ) {
          cValue = pValue + ((cValue - pValue) / cCounter);
        }
        bins[globalItem] = cValue;
        counters[globalItem] = cCounter;
      }
    }
  }
}

} // PulsarSearch

#endif // FOLDING_HPP
<|endoftext|>


<commit_before>//===- lib/wxWidgets/StringConversion.cpp ---------------------------------===//
//
//                                    SeeC
//
// This file is distributed under The MIT License (MIT). See LICENSE.TXT for
// details.
//
//===----------------------------------------------------------------------===//
///
/// \file
///
//===----------------------------------------------------------------------===//

#include "seec/wxWidgets/StringConversion.hpp"

#include "llvm/Support/raw_ostream.h"

#include <string>

namespace seec {

wxString towxString(UnicodeString icu) {
  std::string Buffer;
  icu.toUTF8String(Buffer);
  return wxString(Buffer.c_str(), wxConvUTF8);
}

wxString getwxStringEx(ResourceBundle const &Bundle,
                       char const *Key,
                       UErrorCode &Status) {
  auto Str = Bundle.getStringEx(Key, Status);

  if (U_SUCCESS(Status)) {
    return towxString(Str);
  }

  return wxString{};
}

wxString getwxStringExOrDie(ResourceBundle const &Bundle,
                            char const *Key) {
  UErrorCode Status = U_ZERO_ERROR;

  auto Str = Bundle.getStringEx(Key, Status);

  if (U_FAILURE(Status)) {
    llvm::errs() << "Couldn't get string for '" << Key
                 << "' from '" << Bundle.getKey()
                 << "' in " << Bundle.getName() << "\n";
    std::exit(EXIT_FAILURE);
  }

  return towxString(Str);
}

wxString getwxStringExOrEmpty(ResourceBundle const &Bundle,
                              char const *Key) {
  UErrorCode Status = U_ZERO_ERROR;

  auto Str = Bundle.getStringEx(Key, Status);

  if (U_FAILURE(Status)) {
    return wxString();
  }

  return towxString(Str);
}

} // namespace seec
<commit_msg>Include <cstdlib> for std::exit().<commit_after>//===- lib/wxWidgets/StringConversion.cpp ---------------------------------===//
//
//                                    SeeC
//
// This file is distributed under The MIT License (MIT). See LICENSE.TXT for
// details.
//
//===----------------------------------------------------------------------===//
///
/// \file
///
//===----------------------------------------------------------------------===//

#include "seec/wxWidgets/StringConversion.hpp"

#include "llvm/Support/raw_ostream.h"

#include <cstdlib>
#include <string>

namespace seec {

wxString towxString(UnicodeString icu) {
  std::string Buffer;
  icu.toUTF8String(Buffer);
  return wxString(Buffer.c_str(), wxConvUTF8);
}

wxString getwxStringEx(ResourceBundle const &Bundle,
                       char const *Key,
                       UErrorCode &Status) {
  auto Str = Bundle.getStringEx(Key, Status);

  if (U_SUCCESS(Status)) {
    return towxString(Str);
  }

  return wxString{};
}

wxString getwxStringExOrDie(ResourceBundle const &Bundle,
                            char const *Key) {
  UErrorCode Status = U_ZERO_ERROR;

  auto Str = Bundle.getStringEx(Key, Status);

  if (U_FAILURE(Status)) {
    llvm::errs() << "Couldn't get string for '" << Key
                 << "' from '" << Bundle.getKey()
                 << "' in " << Bundle.getName() << "\n";
    std::exit(EXIT_FAILURE);
  }

  return towxString(Str);
}

wxString getwxStringExOrEmpty(ResourceBundle const &Bundle,
                              char const *Key) {
  UErrorCode Status = U_ZERO_ERROR;

  auto Str = Bundle.getStringEx(Key, Status);

  if (U_FAILURE(Status)) {
    return wxString();
  }

  return towxString(Str);
}

} // namespace seec
<|endoftext|>


<commit_before>#ifdef MSVC
#define _CRT_SECURE_NO_WARNINGS // disable msvc /sdl warning on fopen call.
#endif
#include <libdariadb/flags.h>
#include <libdariadb/storage/bloom_filter.h>
#include <libdariadb/storage/callbacks.h>
#include <libdariadb/storage/cursors.h>
#include <libdariadb/storage/manifest.h>
#include <libdariadb/storage/settings.h>
#include <libdariadb/storage/wal/walfile.h>
#include <libdariadb/utils/fs.h>
#include <libdariadb/utils/utils.h>

#include <algorithm>

#include <cstdio>
#include <cstring>
#include <fstream>
#include <mutex>
#include <sstream>

using namespace dariadb;
using namespace dariadb::storage;

class WALFile::Private {
public:
  Private(const EngineEnvironment_ptr env, dariadb::Id id) {
    _env = env;
    _settings = _env->getResourceObject<Settings>(EngineEnvironment::Resource::SETTINGS);
    _writed = 0;
    _is_readonly = false;
    auto rnd_fname = utils::fs::random_file_name(WAL_FILE_EXT);
    _filename = utils::fs::append_path(_settings->raw_path.value(), rnd_fname);
    _env->getResourceObject<Manifest>(EngineEnvironment::Resource::MANIFEST)
        ->wal_append(rnd_fname, id);
    _file = nullptr;
    _idBloom = bloom_empty<Id>();
  }

  Private(const EngineEnvironment_ptr env, const std::string &fname, bool readonly) {
    _env = env;
    _settings = _env->getResourceObject<Settings>(EngineEnvironment::Resource::SETTINGS);
    _writed = WALFile::writed(fname);
    _is_readonly = readonly;
    _filename = fname;
    _file = nullptr;
  }

  ~Private() {
    this->flush();
    if (_file != nullptr) {
      std::fclose(_file);
      _file = nullptr;
    }
  }

  void close() {
	  if (_file != nullptr) {
		  std::fclose(_file);
	  }
  }

  void open_to_append() {
    if (_file != nullptr) {
      return;
    }

    _file = std::fopen(_filename.c_str(), "ab");
    if (_file == nullptr) {
      std::stringstream ss;
      ss << "WALFile: open_to_append error. file:" << _filename
         << " errno:" << std::string(std::strerror(errno));
      logger_fatal(ss.str());
      throw MAKE_EXCEPTION(ss.str());
    }
  }

  void open_to_read() {
    if (_file != nullptr) {
      std::fclose(_file);
      _file = nullptr;
    }
    _file = std::fopen(_filename.c_str(), "rb");
    if (_file == nullptr) {
      throw_open_error_exception();
    }
  }

  Status append(const Meas &value) {
    ENSURE(!_is_readonly);

    if (_writed > _settings->wal_file_size.value()) {
      return Status(1, APPEND_ERROR::wal_file_limit);
    }
    open_to_append();
    std::fwrite(&value, sizeof(Meas), size_t(1), _file);
    std::fflush(_file);
    _minTime = std::min(_minTime, value.time);
    _maxTime = std::max(_maxTime, value.time);
    _writed++;
    _idBloom = bloom_add<Id>(_idBloom, value.id);
	close();
    return Status(1);
  }

  Status append(const MeasArray::const_iterator &begin,
                const MeasArray::const_iterator &end) {
    ENSURE(!_is_readonly);

    auto sz = std::distance(begin, end);
    open_to_append();
    auto max_size = _settings->wal_file_size.value();
    auto write_size = (sz + _writed) > max_size ? (max_size - _writed) : sz;
    if (write_size == size_t()) {
      Status result;
      result.ignored = sz;
      result.error = APPEND_ERROR::wal_file_limit;
      return result;
    }
    std::fwrite(&(*begin), sizeof(Meas), write_size, _file);
    std::fflush(_file);
    for (auto it = begin; it != begin + write_size; ++it) {
      auto value = *it;
      _minTime = std::min(_minTime, value.time);
      _maxTime = std::max(_maxTime, value.time);
      _idBloom = bloom_add<Id>(_idBloom, value.id);
    }
    _writed += write_size;
	close();
    return Status(write_size);
  }

  Statistic stat(const Id id, Time from, Time to) {
    Statistic result;

    IdArray ids{id};
    ENSURE(ids[0] == id);
    auto all = readAll();
    for (size_t i = 0; i < all->size(); ++i) {
      auto val = all->at(i);
      if (val.inQuery(ids, Flag(0), from, to)) {
        result.update(val);
      }
    }

    return result;
  }

  Id2Cursor intervalReader(const QueryInterval &q) {
    Id2MSet subresult;
    auto all = readAll();
    for (size_t i = 0; i < all->size(); ++i) {
      auto val = all->at(i);
      if (val.inQuery(q.ids, q.flag, q.from, q.to)) {
        subresult[val.id].insert(val);
      }
    }

    if (subresult.empty()) {
      return Id2Cursor();
    }

    Id2Cursor result;
    for (auto kv : subresult) {
      MeasArray ma(kv.second.begin(), kv.second.end());
      std::sort(ma.begin(), ma.end(), meas_time_compare_less());
      ENSURE(ma.front().time <= ma.back().time);
      FullCursor *fr = new FullCursor(ma);
      Cursor_Ptr reader{fr};
      result[kv.first] = reader;
    }
    return result;
  }

  void foreach (const QueryInterval &q, IReadCallback * clbk) {
    auto readers = intervalReader(q);

    for (auto kv : readers) {
      kv.second->apply(clbk);
    }
  }

  Id2Meas readTimePoint(const QueryTimePoint &q) {
    dariadb::IdSet readed_ids;
    dariadb::Id2Meas sub_res;

    auto all = readAll();
    for (size_t i = 0; i < all->size(); ++i) {
      auto val = all->at(i);
      if (val.inQuery(q.ids, q.flag) && (val.time <= q.time_point)) {
        replace_if_older(sub_res, val);
        readed_ids.insert(val.id);
      }
    }

    if (!q.ids.empty() && readed_ids.size() != q.ids.size()) {
      for (auto id : q.ids) {
        if (readed_ids.find(id) == readed_ids.end()) {
          auto e = Meas(id);
          e.flag = FLAGS::_NO_DATA;
          e.time = q.time_point;
          sub_res[id] = e;
        }
      }
    }

    return sub_res;
  }

  void replace_if_older(dariadb::Id2Meas &s, const dariadb::Meas &m) const {
    auto fres = s.find(m.id);
    if (fres == s.end()) {
      s.emplace(std::make_pair(m.id, m));
    } else {
      if (fres->second.time < m.time) {
        s.emplace(std::make_pair(m.id, m));
      }
    }
  }

  Id2Meas currentValue(const IdArray &ids, const Flag &flag) {
    dariadb::Id2Meas sub_res;
    dariadb::IdSet readed_ids;

    auto all = readAll();
    for (size_t i = 0; i < all->size(); ++i) {
      auto val = all->at(i);
      if (val.inFlag(flag) && val.inIds(ids)) {
        replace_if_older(sub_res, val);
        readed_ids.emplace(val.id);
      }
    }

    if (!ids.empty() && readed_ids.size() != ids.size()) {
      for (auto id : ids) {
        if (readed_ids.find(id) == readed_ids.end()) {
          auto e = Meas(id);
          e.flag = FLAGS::_NO_DATA;
          e.time = dariadb::Time(0);
          sub_res[id] = e;
        }
      }
    }

    return sub_res;
  }

  void updateBloom() {
    _idBloom = bloom_empty<Id>();
    auto all = this->readAll();
    for (auto v : *all) {
      _idBloom = bloom_add<Id>(_idBloom, v.id);
    }
  }

  dariadb::Id _idBloom = MIN_ID;
  Id id_bloom() {
    if (_idBloom == MIN_ID) {
      updateBloom();
    }
    return _idBloom;
  }

  dariadb::Time _minTime = MAX_TIME;
  dariadb::Time _maxTime = MIN_TIME;

  dariadb::Time minTime() {
    dariadb::Time result = dariadb::MAX_TIME;
    auto all = readAll();
    for (size_t i = 0; i < all->size(); ++i) {
      auto val = all->at(i);
      result = std::min(val.time, result);
    }
    return result;
  }

  dariadb::Time maxTime() {
    dariadb::Time result = dariadb::MIN_TIME;

    auto all = readAll();
    for (size_t i = 0; i < all->size(); ++i) {
      auto val = all->at(i);
      result = std::max(val.time, result);
    }
    return result;
  }

  bool minMaxTime(dariadb::Id id, dariadb::Time *minResult, dariadb::Time *maxResult) {
    *minResult = dariadb::MAX_TIME;
    *maxResult = dariadb::MIN_TIME;

    bool result = false;
    auto all = readAll();
    for (size_t i = 0; i < all->size(); ++i) {
      auto val = all->at(i);
      if (val.id == id) {
        result = true;
        *minResult = std::min(*minResult, val.time);
        *maxResult = std::max(*maxResult, val.time);
      }
    }
    return result;
  }

  void flush() {}

  std::string filename() const { return _filename; }

  std::shared_ptr<MeasArray> readAll() {
    open_to_read();

    auto ma = std::make_shared<MeasArray>(_writed);
    auto raw = ma.get();
    auto result = fread(raw->data(), sizeof(Meas), _writed, _file);
    if (result < _writed) {
      THROW_EXCEPTION("result < _writed");
    }
	close();
    _file = nullptr;
    return ma;
  }

  [[noreturn]] void throw_open_error_exception() const {
    std::stringstream ss;
    ss << "wal: file open error " << _filename;
    auto wals_manifest =
        _env->getResourceObject<Manifest>(EngineEnvironment::Resource::MANIFEST)
            ->wal_list();
    ss << "Manifest:";
    for (auto f : wals_manifest) {
      ss << f.fname << std::endl;
    }
    auto wals_exists = utils::fs::ls(_settings->raw_path.value(), WAL_FILE_EXT);
    for (auto f : wals_exists) {
      ss << f << std::endl;
    }
    throw MAKE_EXCEPTION(ss.str());
  }

  Id2MinMax_Ptr loadMinMax() {

    Id2MinMax_Ptr result = std::make_shared<Id2MinMax>();
    auto all = readAll();
    for (size_t i = 0; i < all->size(); ++i) {
      auto val = all->at(i);
      auto fres = result->find_bucket(val.id);

      fres.v->second.updateMax(val);
      fres.v->second.updateMin(val);
    }
    return result;
  }

  Id id_from_first() {
    auto all = readAll();
    if (all->empty()) {
      return MAX_ID;
    } else {
      return all->front().id;
    }
  }

  size_t writed() const { return _writed; }

protected:
  std::string _filename;
  bool _is_readonly;
  size_t _writed;
  EngineEnvironment_ptr _env;
  Settings *_settings;
  FILE *_file;
};

WALFile_Ptr WALFile::create(const EngineEnvironment_ptr env, dariadb::Id id) {
  return WALFile_Ptr{new WALFile(env, id)};
}

WALFile_Ptr WALFile::open(const EngineEnvironment_ptr env, const std::string &fname,
                          bool readonly) {
  return WALFile_Ptr{new WALFile(env, fname, readonly)};
}

WALFile::~WALFile() {}

WALFile::WALFile(const EngineEnvironment_ptr env, dariadb::Id id)
    : _Impl(new WALFile::Private(env, id)) {}

WALFile::WALFile(const EngineEnvironment_ptr env, const std::string &fname, bool readonly)
    : _Impl(new WALFile::Private(env, fname, readonly)) {}

dariadb::Id WALFile::id_bloom() {
  return _Impl->id_bloom();
}

dariadb::Time WALFile::minTime() {
  return _Impl->minTime();
}

dariadb::Time WALFile::maxTime() {
  return _Impl->maxTime();
}

bool WALFile::minMaxTime(dariadb::Id id, dariadb::Time *minResult,
                         dariadb::Time *maxResult) {
  return _Impl->minMaxTime(id, minResult, maxResult);
}
void WALFile::flush() { // write all to storage;
  _Impl->flush();
}

Status WALFile::append(const Meas &value) {
  return _Impl->append(value);
}
Status WALFile::append(const MeasArray::const_iterator &begin,
                       const MeasArray::const_iterator &end) {
  return _Impl->append(begin, end);
}

Id2Cursor WALFile::intervalReader(const QueryInterval &q) {
  return _Impl->intervalReader(q);
}

Statistic WALFile::stat(const Id id, Time from, Time to) {
  return _Impl->stat(id, from, to);
}

void WALFile::foreach (const QueryInterval &q, IReadCallback * clbk) {
  return _Impl->foreach (q, clbk);
}

Id2Meas WALFile::readTimePoint(const QueryTimePoint &q) {
  return _Impl->readTimePoint(q);
}

Id2Meas WALFile::currentValue(const IdArray &ids, const Flag &flag) {
  return _Impl->currentValue(ids, flag);
}

std::string WALFile::filename() const {
  return _Impl->filename();
}

std::shared_ptr<MeasArray> WALFile::readAll() {
  return _Impl->readAll();
}

size_t WALFile::writed(std::string fname) {
  std::ifstream in(fname, std::ifstream::ate | std::ifstream::binary);
  return in.tellg() / sizeof(Meas);
}

Id2MinMax_Ptr WALFile::loadMinMax() {
  return _Impl->loadMinMax();
}

Id WALFile::id_from_first() {
  return _Impl->id_from_first();
}

size_t WALFile::writed() const {
  return _Impl->writed();
}<commit_msg>wal: unsetted pointer.<commit_after>#ifdef MSVC
#define _CRT_SECURE_NO_WARNINGS // disable msvc /sdl warning on fopen call.
#endif
#include <libdariadb/flags.h>
#include <libdariadb/storage/bloom_filter.h>
#include <libdariadb/storage/callbacks.h>
#include <libdariadb/storage/cursors.h>
#include <libdariadb/storage/manifest.h>
#include <libdariadb/storage/settings.h>
#include <libdariadb/storage/wal/walfile.h>
#include <libdariadb/utils/fs.h>
#include <libdariadb/utils/utils.h>

#include <algorithm>

#include <cstdio>
#include <cstring>
#include <fstream>
#include <mutex>
#include <sstream>

using namespace dariadb;
using namespace dariadb::storage;

class WALFile::Private {
public:
  Private(const EngineEnvironment_ptr env, dariadb::Id id) {
    _env = env;
    _settings = _env->getResourceObject<Settings>(EngineEnvironment::Resource::SETTINGS);
    _writed = 0;
    _is_readonly = false;
    auto rnd_fname = utils::fs::random_file_name(WAL_FILE_EXT);
    _filename = utils::fs::append_path(_settings->raw_path.value(), rnd_fname);
    _env->getResourceObject<Manifest>(EngineEnvironment::Resource::MANIFEST)
        ->wal_append(rnd_fname, id);
    _file = nullptr;
    _idBloom = bloom_empty<Id>();
  }

  Private(const EngineEnvironment_ptr env, const std::string &fname, bool readonly) {
    _env = env;
    _settings = _env->getResourceObject<Settings>(EngineEnvironment::Resource::SETTINGS);
    _writed = WALFile::writed(fname);
    _is_readonly = readonly;
    _filename = fname;
    _file = nullptr;
  }

  ~Private() {
    this->flush();
    if (_file != nullptr) {
      std::fclose(_file);
      _file = nullptr;
    }
  }

  void close() {
	  if (_file != nullptr) {
		  std::fclose(_file);
		  _file = nullptr;
	  }
  }

  void open_to_append() {
    if (_file != nullptr) {
      return;
    }

    _file = std::fopen(_filename.c_str(), "ab");
    if (_file == nullptr) {
      std::stringstream ss;
      ss << "WALFile: open_to_append error. file:" << _filename
         << " errno:" << std::string(std::strerror(errno));
      logger_fatal(ss.str());
      throw MAKE_EXCEPTION(ss.str());
    }
  }

  void open_to_read() {
    if (_file != nullptr) {
      std::fclose(_file);
      _file = nullptr;
    }
    _file = std::fopen(_filename.c_str(), "rb");
    if (_file == nullptr) {
      throw_open_error_exception();
    }
  }

  Status append(const Meas &value) {
    ENSURE(!_is_readonly);

    if (_writed > _settings->wal_file_size.value()) {
      return Status(1, APPEND_ERROR::wal_file_limit);
    }
    open_to_append();
    std::fwrite(&value, sizeof(Meas), size_t(1), _file);
    std::fflush(_file);
    _minTime = std::min(_minTime, value.time);
    _maxTime = std::max(_maxTime, value.time);
    _writed++;
    _idBloom = bloom_add<Id>(_idBloom, value.id);
	close();
    return Status(1);
  }

  Status append(const MeasArray::const_iterator &begin,
                const MeasArray::const_iterator &end) {
    ENSURE(!_is_readonly);

    auto sz = std::distance(begin, end);
    open_to_append();
    auto max_size = _settings->wal_file_size.value();
    auto write_size = (sz + _writed) > max_size ? (max_size - _writed) : sz;
    if (write_size == size_t()) {
      Status result;
      result.ignored = sz;
      result.error = APPEND_ERROR::wal_file_limit;
      return result;
    }
    std::fwrite(&(*begin), sizeof(Meas), write_size, _file);
    std::fflush(_file);
    for (auto it = begin; it != begin + write_size; ++it) {
      auto value = *it;
      _minTime = std::min(_minTime, value.time);
      _maxTime = std::max(_maxTime, value.time);
      _idBloom = bloom_add<Id>(_idBloom, value.id);
    }
    _writed += write_size;
	close();
    return Status(write_size);
  }

  Statistic stat(const Id id, Time from, Time to) {
    Statistic result;

    IdArray ids{id};
    ENSURE(ids[0] == id);
    auto all = readAll();
    for (size_t i = 0; i < all->size(); ++i) {
      auto val = all->at(i);
      if (val.inQuery(ids, Flag(0), from, to)) {
        result.update(val);
      }
    }

    return result;
  }

  Id2Cursor intervalReader(const QueryInterval &q) {
    Id2MSet subresult;
    auto all = readAll();
    for (size_t i = 0; i < all->size(); ++i) {
      auto val = all->at(i);
      if (val.inQuery(q.ids, q.flag, q.from, q.to)) {
        subresult[val.id].insert(val);
      }
    }

    if (subresult.empty()) {
      return Id2Cursor();
    }

    Id2Cursor result;
    for (auto kv : subresult) {
      MeasArray ma(kv.second.begin(), kv.second.end());
      std::sort(ma.begin(), ma.end(), meas_time_compare_less());
      ENSURE(ma.front().time <= ma.back().time);
      FullCursor *fr = new FullCursor(ma);
      Cursor_Ptr reader{fr};
      result[kv.first] = reader;
    }
    return result;
  }

  void foreach (const QueryInterval &q, IReadCallback * clbk) {
    auto readers = intervalReader(q);

    for (auto kv : readers) {
      kv.second->apply(clbk);
    }
  }

  Id2Meas readTimePoint(const QueryTimePoint &q) {
    dariadb::IdSet readed_ids;
    dariadb::Id2Meas sub_res;

    auto all = readAll();
    for (size_t i = 0; i < all->size(); ++i) {
      auto val = all->at(i);
      if (val.inQuery(q.ids, q.flag) && (val.time <= q.time_point)) {
        replace_if_older(sub_res, val);
        readed_ids.insert(val.id);
      }
    }

    if (!q.ids.empty() && readed_ids.size() != q.ids.size()) {
      for (auto id : q.ids) {
        if (readed_ids.find(id) == readed_ids.end()) {
          auto e = Meas(id);
          e.flag = FLAGS::_NO_DATA;
          e.time = q.time_point;
          sub_res[id] = e;
        }
      }
    }

    return sub_res;
  }

  void replace_if_older(dariadb::Id2Meas &s, const dariadb::Meas &m) const {
    auto fres = s.find(m.id);
    if (fres == s.end()) {
      s.emplace(std::make_pair(m.id, m));
    } else {
      if (fres->second.time < m.time) {
        s.emplace(std::make_pair(m.id, m));
      }
    }
  }

  Id2Meas currentValue(const IdArray &ids, const Flag &flag) {
    dariadb::Id2Meas sub_res;
    dariadb::IdSet readed_ids;

    auto all = readAll();
    for (size_t i = 0; i < all->size(); ++i) {
      auto val = all->at(i);
      if (val.inFlag(flag) && val.inIds(ids)) {
        replace_if_older(sub_res, val);
        readed_ids.emplace(val.id);
      }
    }

    if (!ids.empty() && readed_ids.size() != ids.size()) {
      for (auto id : ids) {
        if (readed_ids.find(id) == readed_ids.end()) {
          auto e = Meas(id);
          e.flag = FLAGS::_NO_DATA;
          e.time = dariadb::Time(0);
          sub_res[id] = e;
        }
      }
    }

    return sub_res;
  }

  void updateBloom() {
    _idBloom = bloom_empty<Id>();
    auto all = this->readAll();
    for (auto v : *all) {
      _idBloom = bloom_add<Id>(_idBloom, v.id);
    }
  }

  dariadb::Id _idBloom = MIN_ID;
  Id id_bloom() {
    if (_idBloom == MIN_ID) {
      updateBloom();
    }
    return _idBloom;
  }

  dariadb::Time _minTime = MAX_TIME;
  dariadb::Time _maxTime = MIN_TIME;

  dariadb::Time minTime() {
    dariadb::Time result = dariadb::MAX_TIME;
    auto all = readAll();
    for (size_t i = 0; i < all->size(); ++i) {
      auto val = all->at(i);
      result = std::min(val.time, result);
    }
    return result;
  }

  dariadb::Time maxTime() {
    dariadb::Time result = dariadb::MIN_TIME;

    auto all = readAll();
    for (size_t i = 0; i < all->size(); ++i) {
      auto val = all->at(i);
      result = std::max(val.time, result);
    }
    return result;
  }

  bool minMaxTime(dariadb::Id id, dariadb::Time *minResult, dariadb::Time *maxResult) {
    *minResult = dariadb::MAX_TIME;
    *maxResult = dariadb::MIN_TIME;

    bool result = false;
    auto all = readAll();
    for (size_t i = 0; i < all->size(); ++i) {
      auto val = all->at(i);
      if (val.id == id) {
        result = true;
        *minResult = std::min(*minResult, val.time);
        *maxResult = std::max(*maxResult, val.time);
      }
    }
    return result;
  }

  void flush() {}

  std::string filename() const { return _filename; }

  std::shared_ptr<MeasArray> readAll() {
    open_to_read();

    auto ma = std::make_shared<MeasArray>(_writed);
    auto raw = ma.get();
    auto result = fread(raw->data(), sizeof(Meas), _writed, _file);
    if (result < _writed) {
      THROW_EXCEPTION("result < _writed");
    }
	close();
    _file = nullptr;
    return ma;
  }

  [[noreturn]] void throw_open_error_exception() const {
    std::stringstream ss;
    ss << "wal: file open error " << _filename;
    auto wals_manifest =
        _env->getResourceObject<Manifest>(EngineEnvironment::Resource::MANIFEST)
            ->wal_list();
    ss << "Manifest:";
    for (auto f : wals_manifest) {
      ss << f.fname << std::endl;
    }
    auto wals_exists = utils::fs::ls(_settings->raw_path.value(), WAL_FILE_EXT);
    for (auto f : wals_exists) {
      ss << f << std::endl;
    }
    throw MAKE_EXCEPTION(ss.str());
  }

  Id2MinMax_Ptr loadMinMax() {

    Id2MinMax_Ptr result = std::make_shared<Id2MinMax>();
    auto all = readAll();
    for (size_t i = 0; i < all->size(); ++i) {
      auto val = all->at(i);
      auto fres = result->find_bucket(val.id);

      fres.v->second.updateMax(val);
      fres.v->second.updateMin(val);
    }
    return result;
  }

  Id id_from_first() {
    auto all = readAll();
    if (all->empty()) {
      return MAX_ID;
    } else {
      return all->front().id;
    }
  }

  size_t writed() const { return _writed; }

protected:
  std::string _filename;
  bool _is_readonly;
  size_t _writed;
  EngineEnvironment_ptr _env;
  Settings *_settings;
  FILE *_file;
};

WALFile_Ptr WALFile::create(const EngineEnvironment_ptr env, dariadb::Id id) {
  return WALFile_Ptr{new WALFile(env, id)};
}

WALFile_Ptr WALFile::open(const EngineEnvironment_ptr env, const std::string &fname,
                          bool readonly) {
  return WALFile_Ptr{new WALFile(env, fname, readonly)};
}

WALFile::~WALFile() {}

WALFile::WALFile(const EngineEnvironment_ptr env, dariadb::Id id)
    : _Impl(new WALFile::Private(env, id)) {}

WALFile::WALFile(const EngineEnvironment_ptr env, const std::string &fname, bool readonly)
    : _Impl(new WALFile::Private(env, fname, readonly)) {}

dariadb::Id WALFile::id_bloom() {
  return _Impl->id_bloom();
}

dariadb::Time WALFile::minTime() {
  return _Impl->minTime();
}

dariadb::Time WALFile::maxTime() {
  return _Impl->maxTime();
}

bool WALFile::minMaxTime(dariadb::Id id, dariadb::Time *minResult,
                         dariadb::Time *maxResult) {
  return _Impl->minMaxTime(id, minResult, maxResult);
}
void WALFile::flush() { // write all to storage;
  _Impl->flush();
}

Status WALFile::append(const Meas &value) {
  return _Impl->append(value);
}
Status WALFile::append(const MeasArray::const_iterator &begin,
                       const MeasArray::const_iterator &end) {
  return _Impl->append(begin, end);
}

Id2Cursor WALFile::intervalReader(const QueryInterval &q) {
  return _Impl->intervalReader(q);
}

Statistic WALFile::stat(const Id id, Time from, Time to) {
  return _Impl->stat(id, from, to);
}

void WALFile::foreach (const QueryInterval &q, IReadCallback * clbk) {
  return _Impl->foreach (q, clbk);
}

Id2Meas WALFile::readTimePoint(const QueryTimePoint &q) {
  return _Impl->readTimePoint(q);
}

Id2Meas WALFile::currentValue(const IdArray &ids, const Flag &flag) {
  return _Impl->currentValue(ids, flag);
}

std::string WALFile::filename() const {
  return _Impl->filename();
}

std::shared_ptr<MeasArray> WALFile::readAll() {
  return _Impl->readAll();
}

size_t WALFile::writed(std::string fname) {
  std::ifstream in(fname, std::ifstream::ate | std::ifstream::binary);
  return in.tellg() / sizeof(Meas);
}

Id2MinMax_Ptr WALFile::loadMinMax() {
  return _Impl->loadMinMax();
}

Id WALFile::id_from_first() {
  return _Impl->id_from_first();
}

size_t WALFile::writed() const {
  return _Impl->writed();
}<|endoftext|>