rosti.cpp 3.1 KB
Newer Older
1 2 3 4 5 6 7 8
/*******************************************************************************
 *     ___                  _   ____  ____
 *    / _ \ _   _  ___  ___| |_|  _ \| __ )
 *   | | | | | | |/ _ \/ __| __| | | |  _ \
 *   | |_| | |_| |  __/\__ \ |_| |_| | |_) |
 *    \__\_\\__,_|\___||___/\__|____/|____/
 *
 *  Copyright (c) 2014-2019 Appsicle
9
 *  Copyright (c) 2019-2022 QuestDB
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 *  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 "rosti.h"
#include <jni.h>

rosti_t *alloc_rosti(const int32_t *column_types, const int32_t column_count, const uint64_t map_capacity) {
U
ugnls 已提交
29
    int32_t slot_key_size = 0;
30 31 32 33
    auto value_offsets = reinterpret_cast<int32_t *>(malloc(sizeof(int32_t) * (column_count + 1)));
    value_offsets[0] = 0;
    for (int32_t i = 0; i < column_count; i++) {
        switch (column_types[i]) {
34 35
            case 1: // BOOL
            case 2: // BYTE
36 37
                slot_key_size += 1;
                break;
38 39
            case 3: // SHORT
            case 4: // CHAR
40 41
                slot_key_size += 2;
                break;
42 43 44
            case 5: // INT
            case 9: // FLOAT
            case 12: // SYMBOL - store as INT
45 46
                slot_key_size += 4;
                break;
47 48 49 50 51
            case 6: // LONG (64 bit)
            case 7: // DATE
            case 8: // TIMESTAMP
            case 10: // DOUBLE
            case 11: // STRING - store reference only
52 53
                slot_key_size += 8;
                break;
54
            case 13: // LONG256
55 56 57 58 59 60 61
                slot_key_size += 64;
                break;
        }
        value_offsets[i + 1] = slot_key_size;
    }
    auto map = reinterpret_cast<rosti_t *>(malloc(sizeof(rosti_t)));
    map->slot_size_ = ceil_pow_2(slot_key_size);
62
    map->slot_size_shift_ = bit_scan_forward(map->slot_size_);
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    map->capacity_ = map_capacity;
    map->size_ = 0;
    map->value_offsets_ = value_offsets;
    initialize_slots(map);
    return map;
}

extern "C" {

JNIEXPORT jlong JNICALL
Java_io_questdb_std_Rosti_alloc(JNIEnv *env, jclass cl, jlong pKeyTypes, jint keyTypeCount, jlong capacity) {
    return reinterpret_cast<jlong>(alloc_rosti(reinterpret_cast<int32_t *>(pKeyTypes), keyTypeCount, capacity));
}

JNIEXPORT void JNICALL
Java_io_questdb_std_Rosti_free0(JNIEnv *env, jclass cl, jlong pRosti) {
    auto map = reinterpret_cast<rosti_t *>(pRosti);
    // initial values contains main arena pointer
    free(map->slot_initial_values_);
    free(map->value_offsets_);
    free(map);
}

JNIEXPORT void JNICALL
Java_io_questdb_std_Rosti_clear(JNIEnv *env, jclass cl, jlong pRosti) {
    clear(reinterpret_cast<rosti_t *>(pRosti));
}

}