Пример #1
0
// Creates a copy of an event data item.
//
// source - The event data to copy.
// target - A reference to the new event data item.
//
// Returns 0 if successful, otherwise returns -1.
int sky_event_data_copy(sky_event_data *source, sky_event_data **target)
{
    check(source != NULL, "Event data source required for copy");

    switch(source->data_type) {
        case SKY_DATA_TYPE_STRING: 
            *target = sky_event_data_create_string(source->key, source->string_value);
            break;
        case SKY_DATA_TYPE_INT: 
            *target = sky_event_data_create_int(source->key, source->int_value);
            break;
        case SKY_DATA_TYPE_DOUBLE: 
            *target = sky_event_data_create_double(source->key, source->double_value);
            break;
        case SKY_DATA_TYPE_BOOLEAN:
            *target = sky_event_data_create_boolean(source->key, source->boolean_value);
            break;
        default:
            sentinel("Invalid data type (%d) for event data", source->data_type);
    }

    return 0;
    
error:
    if(*target) sky_event_data_free(*target);
    *target = NULL;
    
    return -1;
}
Пример #2
0
// Creates a reference to event data with no data type.
//
// key   - The property id used as the key for the data.
sky_event_data *sky_event_data_create(sky_property_id_t key)
{
    sky_event_data *data = calloc(1, sizeof(sky_event_data)); check_mem(data);
    data->key = key;
    data->data_type = SKY_DATA_TYPE_NONE;
    return data;
    
error:
    sky_event_data_free(data);
    return NULL;
}
Пример #3
0
// Creates a reference to event data representing a boolean.
//
// key   - The property id used as the key for the data.
// value - The boolean value of the data.
sky_event_data *sky_event_data_create_boolean(sky_property_id_t key, bool value)
{
    sky_event_data *data = calloc(1, sizeof(sky_event_data)); check_mem(data);
    data->key = key;
    data->data_type = SKY_DATA_TYPE_BOOLEAN;
    data->boolean_value = value;

    return data;
    
error:
    sky_event_data_free(data);
    return NULL;
}
Пример #4
0
// Creates a reference to event data representing a double.
//
// key   - The property id used as the key for the data.
// value - The double value of the data.
sky_event_data *sky_event_data_create_double(sky_property_id_t key, double value)
{
    sky_event_data *data = calloc(1, sizeof(sky_event_data)); check_mem(data);
    data->key = key;
    data->data_type = SKY_DATA_TYPE_DOUBLE;
    data->double_value = value;

    return data;
    
error:
    sky_event_data_free(data);
    return NULL;
}
Пример #5
0
// Creates a reference to event data representing a 64-bit integer.
//
// key   - The property id used as the key for the data.
// value - The integer value of the data.
sky_event_data *sky_event_data_create_int(sky_property_id_t key, int64_t value)
{
    sky_event_data *data = calloc(1, sizeof(sky_event_data)); check_mem(data);
    data->key = key;
    data->data_type = SKY_DATA_TYPE_INT;
    data->int_value = value;

    return data;
    
error:
    sky_event_data_free(data);
    return NULL;
}
Пример #6
0
// Creates a reference to event data.
//
// key   - The property id used as the key for the data.
// value - The string value of the data.
sky_event_data *sky_event_data_create_string(sky_property_id_t key, bstring value)
{
    sky_event_data *data = calloc(1, sizeof(sky_event_data)); check_mem(data);
    data->key = key;
    data->data_type = SKY_DATA_TYPE_STRING;
    data->string_value = bstrcpy(value);
    if(value) check_mem(data->string_value);

    return data;
    
error:
    sky_event_data_free(data);
    return NULL;
}
Пример #7
0
// Adds an event to the tablet.
//
// tablet - The tablet.
// event  - The event to add.
//
// Returns 0 if successful, otherwise returns -1.
int sky_tablet_add_event(sky_tablet *tablet, sky_event *event)
{
    int rc;
    char *errptr = NULL;
    void *new_data = NULL;
    void *data = NULL;
    sky_data_object *data_object = NULL;
    sky_data_descriptor *descriptor = NULL;
    sky_cursor cursor; memset(&cursor, 0, sizeof(cursor));
    assert(tablet != NULL);
    assert(event != NULL);

    // Make sure that this event is being added to the correct tablet.
    sky_tablet *target_tablet = NULL;
    rc = sky_table_get_target_tablet(tablet->table, event->object_id, &target_tablet);
    check(rc == 0, "Unable to determine target tablet");
    check(tablet == target_tablet, "Event added to invalid tablet; IDX:%d of %d, OID:%d", tablet->index, tablet->table->tablet_count, event->object_id);

    // Retrieve the existing value.
    size_t data_length;
    data = (void*)leveldb_get(tablet->leveldb_db, tablet->readoptions, (const char*)&event->object_id, sizeof(event->object_id), &data_length, &errptr);
    check(errptr == NULL, "LevelDB get error: %s", errptr);
    
    // Find the insertion point on the path.
    size_t insert_offset = 0;
    size_t event_length;
    
    // If the object doesn't exist yet then just set the single event. Easy peasy.
    if(data == NULL) {
        event_length = sky_event_sizeof(event);
        new_data = calloc(1, event_length); check_mem(new_data);
        insert_offset = 0;
    }
    // If the object does exist, we need to find where to insert the event data.
    // Also, we need to strip off any state which is redundant at the point of
    // insertion.
    else {
        void *insert_ptr = NULL;

        // Initialize data descriptor.
        descriptor = sky_data_descriptor_create(); check_mem(descriptor);
        rc = sky_data_descriptor_init_with_event(descriptor, event);
        check(rc == 0, "Unable to initialize data descriptor for event insert");
    
        // Initialize data object.
        data_object = calloc(1, descriptor->data_sz); check_mem(data);

        // Attach data & descriptor to the cursor.
        cursor.data_descriptor = descriptor;
        cursor.data = (void*)data_object;

        // Initialize the cursor.
        rc = sky_cursor_set_ptr(&cursor, data, data_length);
        check(rc == 0, "Unable to set pointer on cursor");
        
        // Loop over cursor until we reach the event insertion point.
        while(!cursor.eof) {
            // Retrieve event insertion pointer once the timestamp is reached.
            if(data_object->ts >= event->timestamp) {
                insert_ptr = cursor.ptr;
                break;
            }
            
            // Move to next event.
            check(sky_cursor_next_event(&cursor) == 0, "Unable to move to next event");
        }

        // If no insertion point was found then append the event to the
        // end of the path.
        if(insert_ptr == NULL) {
            insert_ptr = data + data_length;
        }
        insert_offset = insert_ptr - data;

        // Clear off any object data on the event that matches
        // what is the current state of the event in the database.
        uint32_t i;
        for(i=0; i<event->data_count; i++) {
            // Ignore any action properties.
            if(event->data[i]->key > 0) {
                sky_data_property_descriptor *property_descriptor = &descriptor->property_zero_descriptor[event->data[i]->key];

                // If the values match then splice this from the array.
                // Compare strings.
                void *a = &event->data[i]->value;
                void *b = ((void*)data_object)+property_descriptor->offset;
                size_t n = sky_data_type_sizeof(event->data[i]->data_type);
                
                bool is_equal = false;
                if(event->data[i]->data_type == SKY_DATA_TYPE_STRING) {
                    is_equal = sky_string_bequals((sky_string*)b, event->data[i]->string_value);
                }
                // Compare other types.
                else if(memcmp(a, b, n) == 0) {
                    is_equal = true;
                }

                // If the data is equal then remove it.
                if(is_equal) {
                    sky_event_data_free(event->data[i]);
                    if(i < event->data_count - 1) {
                        memmove(&event->data[i], &event->data[i+1], (event->data_count-i-1) * sizeof(*event->data));
                    }
                    i--;
                    event->data_count--;
                }
            }
        }

        // Determine the serialized size of the event. If the event is
        // completely redundant (e.g. it is a data-only event and the event
        // matches the current object state) then don't allocate space for a
        // new path value.
        event_length = sky_event_sizeof(event);
        if(event_length > 0) {
            // Allocate space for the existing data plus the new data.
            new_data = calloc(1, data_length + event_length); check_mem(new_data);

            // Copy in data before event.
            if(insert_offset > 0) {
                memmove(new_data, data, insert_ptr-data);
            }

            // Copy in data after event.
            if(insert_offset < data_length) {
                event_length = sky_event_sizeof(event);
                memmove(new_data+insert_offset+event_length, data+insert_offset, data_length-insert_offset);
            }
        }
    }
    
    // If no space was allocated then it means the event is redundant and
    // should be ignored.
    if(new_data != NULL) {
        // If the object doesn't exist then just set the event as the data.
        size_t event_sz;
        rc = sky_event_pack(event, new_data + insert_offset, &event_sz);
        check(rc == 0, "Unable to pack event");
        check(event_sz == event_length, "Expected event size (%ld) does not match actual event size (%ld)", event_length, event_sz);

        leveldb_put(tablet->leveldb_db, tablet->writeoptions, (const char*)&event->object_id, sizeof(event->object_id), new_data, data_length + event_length, &errptr);
        check(errptr == NULL, "LevelDB put error: %s", errptr);
    }
    
    free(data_object);
    sky_data_descriptor_free(descriptor);
    free(data);
    free(new_data);
    
    return 0;

error:
    if(errptr) leveldb_free(errptr);
    sky_data_descriptor_free(descriptor);
    if(data) free(data);
    if(new_data) free(new_data);
    if(data_object) free(data_object);
    return -1;
}