size_t CSeqTable_sparse_index::x_GetFirstRowWithValue(void) const
{
    switch ( Which() ) {
    case e_Indexes:
    {
        const TIndexes& indexes = GetIndexes();
        return indexes.empty()? kInvalidRow: indexes.front();
    }
    case e_Indexes_delta:
    {
        const TIndexes_delta& deltas = GetIndexes_delta();
        return deltas.empty()? kInvalidRow: deltas.front();
    }
    case e_Bit_set:
    {
        const TBit_set& bytes = GetBit_set();
        size_t byte_index = sx_FindFirstNonZeroByte(bytes, 0);
        if ( byte_index == kInvalidRow ) {
            return kInvalidRow;
        }
        return byte_index*8 + sx_FindFirstNonZeroBit(Uint1(bytes[byte_index]));
    }
    case e_Bit_set_bvector:
        return GetBit_set_bvector().GetBitVector().get_first();
    default:
        return kInvalidRow;
    }
}
size_t CSeqTable_sparse_index::x_GetNextRowWithValue(size_t row,
                                                     size_t value_index) const
{
    switch ( Which() ) {
    case e_Indexes:
    {
        const TIndexes& indexes = GetIndexes();
        return ++value_index >= indexes.size()?
            kInvalidRow: indexes[value_index];
    }
    case e_Indexes_delta:
    {
        const TIndexes_delta& deltas = GetIndexes_delta();
        return ++value_index >= deltas.size()?
            kInvalidRow: row + deltas[value_index];
    }
    case e_Bit_set:
    {
        const TBit_set& bytes = GetBit_set();
        size_t byte_index = row / 8;
        size_t bit_index = row % 8;
        bit_index = sx_FindNextNonZeroBit(Uint1(bytes[byte_index]), bit_index);
        if ( bit_index != kInvalidRow ) {
            return byte_index*8 + bit_index;
        }
        byte_index = sx_FindFirstNonZeroByte(bytes, byte_index + 1);
        if ( byte_index == kInvalidRow ) {
            return kInvalidRow;
        }
        return byte_index*8 + sx_FindFirstNonZeroBit(Uint1(bytes[byte_index]));
    }
    case e_Bit_set_bvector:
    {
        row = GetBit_set_bvector().GetBitVector().get_next(row);
        return row == 0? kInvalidRow: row;
    }
    default:
        return kInvalidRow;
    };
}
static inline size_t sx_CalcBlockBitCount(const char* block, size_t size)
{
    const bm::word_t* word_block = reinterpret_cast<const bm::word_t*>(block);
    const bm::word_t* word_block_end = word_block + size/sizeof(bm::word_t);
    size_t ret = bm::bit_block_calc_count(word_block, word_block_end);

    const char* tail_ptr = reinterpret_cast<const char*>(word_block_end);
    const char* block_end = block + size;
    for ( ; tail_ptr != block_end; ++tail_ptr ) {
        ret += sx_CalcByteBitCount(Uint1(*tail_ptr));
    }

    return ret;
}
size_t CSeqTable_sparse_index::x_GetBitSetCache(size_t byte_count) const
{
    const TBit_set& bytes = GetBit_set();
    size_t size = bytes.size();
    CMutexGuard guard(sx_PrepareMutex_sparse_index);
    if ( !m_Cache ) {
        m_Cache.Reset(new SBitsInfo);
    }
    SBitsInfo& info= dynamic_cast<SBitsInfo&>(*m_Cache);
    static const size_t kBlockSize = SBitsInfo::kBlockSize;
    size_t block_index  = byte_count / kBlockSize;
    size_t block_offset = byte_count % kBlockSize;
    for ( ; block_index > info.m_BlocksFilled; ) {
        if ( !info.m_Blocks ) {
            size_t block_count = size / kBlockSize;
            info.m_Blocks.reset(new size_t[block_count]);
        }
        size_t next_index = info.m_BlocksFilled;
        size_t count = sx_CalcBlockBitCount(&bytes[next_index*kBlockSize],
                                            kBlockSize);
        if ( next_index > 0 ) {
            count += info.m_Blocks[next_index-1];
        }
        info.m_Blocks[next_index] = count;
        info.m_BlocksFilled = next_index+1;
    }
    size_t ret = block_index? info.m_Blocks[block_index-1]: 0;
    if ( block_offset ) {
        if ( block_index != info.m_CacheBlockIndex ) {
            if ( !info.m_CacheBlockInfo ) {
                info.m_CacheBlockInfo.reset(new size_t[kBlockSize]);
            }
            size_t count = 0;
            size_t block_pos = block_index*kBlockSize;
            size_t block_size = min(kBlockSize, size-block_pos);
            const char* block = &bytes[block_pos];
            for ( size_t i = 0; i < block_size; ++i ) {
                count += sx_CalcByteBitCount(Uint1(block[i]));
                info.m_CacheBlockInfo[i] = count;
            }
            info.m_CacheBlockIndex = block_index;
        }
        ret += info.m_CacheBlockInfo[block_offset-1];
    }
    return ret;
}
Example #5
0
CValueConvert<SSafeSqlCP, CDB_Object>::operator Uint1(void) const
{
    if (m_Value.IsNULL()) {
       return Uint1();
    }

    const EDB_Type cur_type = m_Value.GetType();

    switch (cur_type) {
        case eDB_TinyInt:
            return Convert(static_cast<const CDB_TinyInt&>(m_Value).Value());
        case eDB_Bit:
            // CDB_Bit is for some reason "Int4" ...
            // return Convert(static_cast<const CDB_Bit&>(m_Value).Value());
            return (static_cast<const CDB_Bit&>(m_Value).Value() == 0 ? 0 : 1);
        default:
            ReportTypeConvError(cur_type, "Uint1");
    }

    return 0;
}
Example #6
0
static CDB_Object* s_GetItem(I_Result::EGetItem policy,
                             EDB_Type    data_type,
                             CDB_Object* item_buff,
                             EDB_Type    b_type,
                             const char* d_ptr,
                             size_t      d_len)
{
    if ( !d_ptr )
        d_ptr = "";

    if (b_type == eDB_VarChar) {
        if ( !item_buff ) {
            item_buff = new CDB_VarChar;
        }
        if ( d_len ) {
            ((CDB_VarChar*) item_buff)->SetValue(d_ptr, d_len);
        } else {
            item_buff->AssignNULL();
        }
        return item_buff;
    }

    if (b_type == eDB_Image) {
        if ( !item_buff ) {
            item_buff = new CDB_Image;
        } else if (policy == I_Result::eAssignLOB) {
            // Explicitly truncate previous value ...
            static_cast<CDB_Image*>(item_buff)->Truncate();
        }

        if ( d_len ) {
            ((CDB_Image*) item_buff)->Append(d_ptr, d_len);
        } else {
            item_buff->AssignNULL();
        }

        return item_buff;
    }

    if (b_type == eDB_Text) {
        if ( !item_buff ) {
            item_buff = new CDB_Text;
        } else if (policy == I_Result::eAssignLOB) {
            // Explicitly truncate previous value ...
            static_cast<CDB_Text*>(item_buff)->Truncate();
        }

        if ( d_len ) {
            ((CDB_Text*) item_buff)->Append(d_ptr, d_len);
        } else {
            item_buff->AssignNULL();
        }

        return item_buff;
    }

    long   int_val = 0;
    Int8   int8_val;
    double double_val;

    switch ( data_type ) {
    case eDB_Bit:
    case eDB_TinyInt:
    case eDB_SmallInt:
    case eDB_Int:
        int_val = NStr::StringToLong(d_ptr);
        break;

    case eDB_BigInt:
        int8_val = NStr::StringToLong(d_ptr);
        break;

    case eDB_Float:
    case eDB_Double:
        double_val = NStr::StringToDouble(d_ptr);
        break;
    default:
        break;
    }

    switch ( b_type ) {
    case eDB_TinyInt: {
        if ( item_buff )
            *((CDB_TinyInt*) item_buff) = Uint1(int_val);
        else
            item_buff = new CDB_TinyInt(Uint1(int_val));
        break;
    }
    case eDB_Bit: {
        if ( item_buff )
            *((CDB_Bit*) item_buff) = int(int_val);
        else
            item_buff = new CDB_Bit(int(int_val));
        break;
    }
    case eDB_SmallInt: {
        if ( item_buff )
            *((CDB_SmallInt*) item_buff) = Int2(int_val);
        else
            item_buff = new CDB_SmallInt(Int2(int_val));
        break;
    }
    case eDB_Int: {
        if ( item_buff )
            *((CDB_Int*) item_buff) = Int4(int_val);
        else
            item_buff = new CDB_Int(Int4(int_val));
        break;
    }
    case eDB_BigInt: {
        if ( item_buff )
            *((CDB_BigInt*) item_buff) = int8_val;
        else
            item_buff = new CDB_BigInt(int8_val);
        break;
    }
    case eDB_Float: {
        if ( item_buff )
            *((CDB_Float*) item_buff) = float(double_val);
        else
            item_buff = new CDB_Float(float(double_val));
        break;
    }
    case eDB_Double: {
        if ( item_buff )
            *((CDB_Double*) item_buff) = double_val;
        else
            item_buff = new CDB_Double(double_val);
        break;
    }
    case eDB_DateTime: {
        CTime time;
        if ( d_len )
            time = CTime(d_ptr, "Y-M-D h:m:s");

        if ( item_buff )
            *(CDB_DateTime*) item_buff = time;
        else
            item_buff = new CDB_DateTime(time);
        break;
    }
    case eDB_SmallDateTime: {
        CTime time;
        if (d_len)
            time = CTime(d_ptr, "Y-M-D");

        if (item_buff)
            *(CDB_SmallDateTime*) item_buff = time;
        else
            item_buff = new CDB_SmallDateTime(time);
        break;
    }
    default:
        break;
    }

    if (d_len == 0  &&  item_buff) {
        item_buff->AssignNULL();
    }

    return item_buff;
}
Example #7
0
static void
s_ReadPerClientConfig(const CNcbiRegistry& reg)
{
    string spec_prty = reg.Get(kNCReg_ServerSection, kNCReg_SpecPriority);
    NStr::Tokenize(spec_prty, ", \t\r\n", s_SpecPriority, NStr::eMergeDelims);

    SNCSpecificParams* main_params = new SNCSpecificParams;
    main_params->disable         = false;
    //main_params->cmd_timeout     = 600;
    //main_params->conn_timeout    = 10;
    main_params->blob_ttl        = 3600;
    main_params->ver_ttl         = 3600;
    main_params->ttl_unit        = 300;
    main_params->prolong_on_read = true;
    main_params->srch_on_read    = false;
    main_params->pass_policy     = eNCBlobPassAny;
    main_params->quorum          = 2;
    main_params->fast_on_main    = true;
    s_ReadSpecificParams(reg, kNCReg_ServerSection, main_params);
    //s_DefConnTimeout = main_params->conn_timeout;
    s_DefBlobTTL = main_params->blob_ttl;
    SSpecParamsSet* params_set = new SSpecParamsSet();
    params_set->entries.push_back(SSpecParamsEntry(kEmptyStr, main_params));
    for (unsigned int i = 0; i < s_SpecPriority.size(); ++i) {
        SSpecParamsSet* next_set = new SSpecParamsSet();
        next_set->entries.push_back(SSpecParamsEntry(kEmptyStr, params_set));
        params_set = next_set;
    }
    s_SpecParams = params_set;

    if (s_SpecPriority.size() != 0) {
        list<string> conf_sections;
        reg.EnumerateSections(&conf_sections);
        ITERATE(list<string>, sec_it, conf_sections) {
            const string& section = *sec_it;
            if (!NStr::StartsWith(section, kNCReg_AppSetupPrefix, NStr::eNocase))
                continue;
            SSpecParamsSet* cur_set  = s_SpecParams;
            NON_CONST_REVERSE_ITERATE(TSpecKeysList, prty_it, s_SpecPriority) {
                const string& key_name = *prty_it;
                if (reg.HasEntry(section, key_name, IRegistry::fCountCleared)) {
                    const string& key_value = reg.Get(section, key_name);
                    unsigned int next_ind = 0;
                    SSpecParamsSet* next_set
                                = s_FindNextParamsSet(cur_set, key_value, next_ind);
                    if (!next_set) {
                        if (cur_set->entries.size() == 0) {
                            cur_set->entries.push_back(SSpecParamsEntry(kEmptyStr, new SSpecParamsSet()));
                            ++next_ind;
                        }
                        next_set = new SSpecParamsSet();
                        s_PutNewParams(cur_set, next_ind,
                                       SSpecParamsEntry(key_value, next_set));
                    }
                    cur_set = next_set;
                }
                else {
                    if (cur_set->entries.size() == 0) {
                        cur_set->entries.push_back(SSpecParamsEntry(kEmptyStr, new SSpecParamsSet()));
                    }
                    cur_set = (SSpecParamsSet*)cur_set->entries[0].value.GetPointer();
                }
            }
            if (cur_set->entries.size() != 0) {
                SRV_LOG(Error, "Section '" << section << "' in configuration file is "
                               "a duplicate of another section - ignoring it.");
                continue;
            }
            SNCSpecificParams* params = new SNCSpecificParams(*main_params);
            if (reg.HasEntry(section, kNCReg_AppSetupValue)) {
                s_ReadSpecificParams(reg, reg.Get(section, kNCReg_AppSetupValue),
                                     params);
            }
            cur_set->entries.push_back(SSpecParamsEntry(kEmptyStr, params));
        }

        s_CheckDefClientConfig(s_SpecParams, NULL, Uint1(s_SpecPriority.size()), NULL);
    }