// Checks that the server is up and running. This function is synchronous and // does not use a worker. // // server - The server. // header - The message header. // table - The table the message is working against // input - The input file stream. // output - The output file stream. // // Returns 0 if successful, otherwise returns -1. int sky_ping_message_process(sky_server *server, sky_message_header *header, sky_table *table, FILE *input, FILE *output) { size_t sz; check(server != NULL, "Server required"); check(header != NULL, "Message header required"); check(input != NULL, "Input stream required"); check(output != NULL, "Output stream required"); UNUSED(table); struct tagbstring status_str = bsStatic("status"); struct tagbstring ok_str = bsStatic("ok"); // Return: // {status:"OK"} minipack_fwrite_map(output, 1, &sz); check(sz > 0, "Unable to write output"); check(sky_minipack_fwrite_bstring(output, &status_str) == 0, "Unable to write status key"); check(sky_minipack_fwrite_bstring(output, &ok_str) == 0, "Unable to write status value"); // Clean up. fclose(input); fclose(output); return 0; error: if(input) fclose(input); if(output) fclose(output); return -1; }
// Deletes a table to the server. This function is synchronous and does not use // a worker. // // server - The server. // header - The message header. // table - The table the message is working against // input - The input file stream. // output - The output file stream. // // Returns 0 if successful, otherwise returns -1. int sky_delete_table_message_process(sky_server *server, sky_message_header *header, sky_table *_table, FILE *input, FILE *output) { int rc = 0; size_t sz; sky_delete_table_message *message = NULL; sky_table *table = NULL; bstring path = NULL; check(server != NULL, "Server required"); check(header != NULL, "Message header required"); check(input != NULL, "Input stream required"); check(output != NULL, "Output stream required"); (void)_table; struct tagbstring status_str = bsStatic("status"); struct tagbstring ok_str = bsStatic("ok"); // Parse message. message = sky_delete_table_message_create(); check_mem(message); rc = sky_delete_table_message_unpack(message, input); check(rc == 0, "Unable to parse 'delete_table' message"); // Retrieve table reference from server. rc = sky_server_get_table(server, message->name, &table); check(rc == 0, "Unable to find table: %s", bdata(message->name)); check(table != NULL, "Table does not exist: %s", bdata(message->name)); // Detach table first. path = bstrcpy(table->path); check_mem(path); rc = sky_server_close_table(server, table); check(rc == 0, "Unable to close table before deletion"); // If the table exists then delete it. if(sky_file_exists(path)) { rc = sky_file_rm_r(path); check(rc == 0, "Unable to delete table: %s", bdata(path)); } // Return. // {status:"OK"} minipack_fwrite_map(output, 1, &sz); check(sz > 0, "Unable to write output"); check(sky_minipack_fwrite_bstring(output, &status_str) == 0, "Unable to write status key"); check(sky_minipack_fwrite_bstring(output, &ok_str) == 0, "Unable to write status value"); fclose(input); fclose(output); bdestroy(path); sky_delete_table_message_free(message); return 0; error: if(input) fclose(input); if(output) fclose(output); bdestroy(path); sky_delete_table_message_free(message); return -1; }
// Writes the results to an output stream. // // worker - The worker. // output - The output stream. // // Returns 0 if successful, otherwise returns -1. int sky_lua_map_reduce_message_worker_write(sky_worker *worker, FILE *output) { size_t sz; assert(worker != NULL); assert(output != NULL); // Ease-of-use references. sky_lua_map_reduce_message *message = (sky_lua_map_reduce_message*)worker->data; // Return. // {status:"ok", data:{<action_id>:{count:0}, ...}} check(minipack_fwrite_map(output, 2, &sz) == 0, "Unable to write root map"); check(sky_minipack_fwrite_bstring(output, &SKY_LUA_MAP_REDUCE_STATUS_STR) == 0, "Unable to write status key"); check(sky_minipack_fwrite_bstring(output, &SKY_LUA_MAP_REDUCE_OK_STR) == 0, "Unable to write status value"); check(sky_minipack_fwrite_bstring(output, &SKY_LUA_MAP_REDUCE_DATA_STR) == 0, "Unable to write data key"); check(minipack_fwrite_nil(output, &sz) == 0, "Unable to write data value"); // Write total number of events to log. printf("[lua::map_reduce] events: %lld\n", (long long int)message->event_count); return 0; error: return -1; }
// Retrieves a list of properties from the server for a table. This function is // synchronous and does not use a worker. // // server - The server. // header - The message header. // table - The table the message is working against // input - The input file stream. // output - The output file stream. // // Returns 0 if successful, otherwise returns -1. int sky_get_properties_message_process(sky_server *server, sky_message_header *header, sky_table *table, FILE *input, FILE *output) { int rc = 0; size_t sz; sky_get_properties_message *message = NULL; check(server != NULL, "Server required"); check(header != NULL, "Message header required"); check(table != NULL, "Table required"); check(input != NULL, "Input stream required"); check(output != NULL, "Output stream required"); struct tagbstring status_str = bsStatic("status"); struct tagbstring ok_str = bsStatic("ok"); struct tagbstring properties_str = bsStatic("properties"); // Parse message. message = sky_get_properties_message_create(); check_mem(message); rc = sky_get_properties_message_unpack(message, input); check(rc == 0, "Unable to parse 'get_properties' message"); // Return. // {status:"OK", properties:[{...}]} minipack_fwrite_map(output, 2, &sz); check(sz > 0, "Unable to write output"); check(sky_minipack_fwrite_bstring(output, &status_str) == 0, "Unable to write status key"); check(sky_minipack_fwrite_bstring(output, &ok_str) == 0, "Unable to write status value"); check(sky_minipack_fwrite_bstring(output, &properties_str) == 0, "Unable to write properties key"); // Loop over properties and serialize them. minipack_fwrite_array(output, table->property_file->property_count, &sz); check(sz > 0, "Unable to write properties array"); uint32_t i; for(i=0; i<table->property_file->property_count; i++) { sky_property *property = table->property_file->properties[i]; check(sky_property_pack(property, output) == 0, "Unable to write property"); } // Clean up. sky_get_properties_message_free(message); fclose(input); fclose(output); return 0; error: sky_get_properties_message_free(message); if(input) fclose(input); if(output) fclose(output); return -1; }
// Serializes a property to a file stream. // // property - The property. // file - The file stream to write to. // // Returns 0 if successful, otherwise returns -1. int sky_property_pack(sky_property *property, FILE *file) { int rc; size_t sz; check(property != NULL, "Property required"); check(file != NULL, "File stream required"); struct tagbstring id_str = bsStatic("id"); struct tagbstring type_str = bsStatic("type"); struct tagbstring data_type_str = bsStatic("dataType"); struct tagbstring name_str = bsStatic("name"); // Update the type just in case. rc = sky_property_update_type(property); check(rc == 0, "Unable to update property type"); // Map minipack_fwrite_map(file, 4, &sz); check(sz > 0, "Unable to write map"); // ID check(sky_minipack_fwrite_bstring(file, &id_str) == 0, "Unable to write id key"); minipack_fwrite_int(file, property->id, &sz); check(sz > 0, "Unable to write id value"); // Type check(sky_minipack_fwrite_bstring(file, &type_str) == 0, "Unable to write type key"); minipack_fwrite_uint(file, property->type, &sz); check(sz > 0, "Unable to write type value"); // Data Type check(sky_minipack_fwrite_bstring(file, &data_type_str) == 0, "Unable to write data type key"); bstring data_type_name = sky_data_type_to_str(property->data_type); check(sky_minipack_fwrite_bstring(file, data_type_name) == 0, "Unable to write data type value"); bdestroy(data_type_name); // Name check(sky_minipack_fwrite_bstring(file, &name_str) == 0, "Unable to write name key"); check(sky_minipack_fwrite_bstring(file, property->name) == 0, "Unable to write name value"); return 0; error: return -1; }
// Serializes an 'delete_table' message to a file stream. // // message - The message. // file - The file stream to write to. // // Returns 0 if successful, otherwise returns -1. int sky_delete_table_message_pack(sky_delete_table_message *message, FILE *file) { size_t sz; check(message != NULL, "Message required"); check(file != NULL, "File stream required"); // Map minipack_fwrite_map(file, SKY_DELETE_TABLE_MESSAGE_TABLE_KEY_COUNT, &sz); check(sz > 0, "Unable to write map"); // Name check(sky_minipack_fwrite_bstring(file, &SKY_DELETE_TABLE_MESSAGE_NAME_STR) == 0, "Unable to write table name key"); check(sky_minipack_fwrite_bstring(file, message->name) == 0, "Unable to write name value"); return 0; error: return -1; }
// Serializes a 'lua::aggregate' message to a file stream. // // message - The message. // file - The file stream to write to. // // Returns 0 if successful, otherwise returns -1. int sky_lua_aggregate_message_pack(sky_lua_aggregate_message *message, FILE *file) { size_t sz; assert(message != NULL); assert(file != NULL); // Map minipack_fwrite_map(file, SKY_LUA_AGGREGATE_KEY_COUNT, &sz); check(sz > 0, "Unable to write map"); // Source check(sky_minipack_fwrite_bstring(file, &SKY_LUA_AGGREGATE_KEY_SOURCE) == 0, "Unable to pack source key"); check(sky_minipack_fwrite_bstring(file, message->source) == 0, "Unable to pack source"); return 0; error: return -1; }
// Serializes an 'multi' message to a file stream. // // message - The message. // file - The file stream to write to. // // Returns 0 if successful, otherwise returns -1. int sky_multi_message_pack(sky_multi_message *message, FILE *file) { size_t sz; assert(message != NULL); assert(file != NULL); // Map minipack_fwrite_map(file, SKY_MULTI_KEY_COUNT, &sz); check(sz > 0, "Unable to write map"); // Object ID check(sky_minipack_fwrite_bstring(file, &SKY_MULTI_KEY_COUNT_STR) == 0, "Unable to pack count key"); minipack_fwrite_int(file, message->message_count, &sz); check(sz != 0, "Unable to pack count"); return 0; error: return -1; }
// Writes the results to an output stream. // // worker - The worker. // output - The output stream. // // Returns 0 if successful, otherwise returns -1. int sky_lua_aggregate_message_worker_write(sky_worker *worker, FILE *output) { size_t sz; assert(worker != NULL); assert(output != NULL); // Ease-of-use references. sky_lua_aggregate_message *message = (sky_lua_aggregate_message*)worker->data; // Return. // {status:"ok", data:{<action_id>:{count:0}, ...}} check(minipack_fwrite_map(output, 2, &sz) == 0, "Unable to write root map"); check(sky_minipack_fwrite_bstring(output, &SKY_LUA_AGGREGATE_STATUS_STR) == 0, "Unable to write status key"); check(sky_minipack_fwrite_bstring(output, &SKY_LUA_AGGREGATE_OK_STR) == 0, "Unable to write status value"); check(sky_minipack_fwrite_bstring(output, &SKY_LUA_AGGREGATE_DATA_STR) == 0, "Unable to write data key"); check(fwrite(bdatae(message->results, ""), blength(message->results), 1, output) == 1, "Unable to write data value"); return 0; error: return -1; }
// Serializes an action to a file stream. // // action - The action. // file - The file stream to write to. // // Returns 0 if successful, otherwise returns -1. int sky_action_pack(sky_action *action, FILE *file) { size_t sz; check(action != NULL, "Action required"); check(file != NULL, "File stream required"); // Map minipack_fwrite_map(file, SKY_ACTION_KEY_COUNT, &sz); check(sz > 0, "Unable to write map"); // ID check(sky_minipack_fwrite_bstring(file, &SKY_ACTION_ID_STR) == 0, "Unable to write id key"); minipack_fwrite_uint(file, action->id, &sz); check(sz > 0, "Unable to write id value"); // Name check(sky_minipack_fwrite_bstring(file, &SKY_ACTION_NAME_STR) == 0, "Unable to write name key"); check(sky_minipack_fwrite_bstring(file, action->name) == 0, "Unable to write name value"); return 0; error: return -1; }