static int key_do_get(StoreHandle *pHandle, const char *full_key, \ const int full_key_len, char *key_list, int *value_len, \ char **key_array, int *key_count) { int result; *value_len = 4 + FDHT_KEY_LIST_MAX_SIZE - 1; result = g_func_get(pHandle, full_key, full_key_len, \ &key_list, value_len); if (result == ENOENT) { *value_len = 0; } else if (result != 0) { return result; } if (*value_len <= 4) { *value_len = 0; *(key_list + *value_len) = '\0'; *key_count = 0; return 0; } *(key_list + *value_len) = '\0'; *key_count = splitEx(key_list + 4, FDHT_FULL_KEY_SEPERATOR, \ key_array, *key_count); return 0; }
/*加载本地的所有ip*/ void load_local_host_ip_addrs() { #define STORAGE_MAX_ALIAS_PREFIX_COUNT 4 char ip_addresses[STORAGE_MAX_LOCAL_IP_ADDRS][IP_ADDRESS_SIZE]; int count; int k; char *if_alias_prefixes[STORAGE_MAX_ALIAS_PREFIX_COUNT]; int alias_count; insert_into_local_host_ip("127.0.0.1"); memset(if_alias_prefixes, 0, sizeof(if_alias_prefixes)); if (*g_if_alias_prefix == '\0') { alias_count = 0; } else { alias_count = splitEx(g_if_alias_prefix, ',', \ if_alias_prefixes, STORAGE_MAX_ALIAS_PREFIX_COUNT); for (k=0; k<alias_count; k++) { trim(if_alias_prefixes[k]); } } /*获取主机的ip地址*/ if (gethostaddrs(if_alias_prefixes, alias_count, ip_addresses, \ STORAGE_MAX_LOCAL_IP_ADDRS, &count) != 0) { return; } for (k=0; k<count; k++) { /*保存到g_local_host_ip_addrs*/ insert_into_local_host_ip(ip_addresses[k]); } //print_local_host_ip_addrs(); }
int trunk_binlog_read(TrunkBinLogReader *pReader, \ TrunkBinLogRecord *pRecord, int *record_length) { #define COL_COUNT 8 char line[TRUNK_BINLOG_LINE_SIZE]; char *cols[COL_COUNT]; int result; result = trunk_binlog_read_line(pReader, line, \ sizeof(line), record_length); if (result != 0) { return result; } if ((result=splitEx(line, ' ', cols, COL_COUNT)) < COL_COUNT) { logError("file: "__FILE__", line: %d, " \ "read data from binlog file \"%s\" fail, " \ "file offset: %"PRId64", " \ "read item count: %d < %d", \ __LINE__, get_binlog_readable_filename(pReader, NULL), \ pReader->binlog_offset, result, COL_COUNT); return ENOENT; } pRecord->timestamp = atoi(cols[0]); pRecord->op_type = *(cols[1]); pRecord->trunk.path.store_path_index = atoi(cols[2]); pRecord->trunk.path.sub_path_high = atoi(cols[3]); pRecord->trunk.path.sub_path_low = atoi(cols[4]); pRecord->trunk.file.id = atoi(cols[5]); pRecord->trunk.file.offset = atoi(cols[6]); pRecord->trunk.file.size = atoi(cols[7]); return 0; }
int main(int argc, char *argv[]) { char *conf_filename; int result; int expires; FDHTObjectInfo object_info; FDHTKeyValuePair key_list[32]; char sub_keys[16 * 1024]; char *keys[FDHT_KEY_LIST_MAX_COUNT]; int key_count; int success_count; int i; int conn_success_count; int conn_fail_count; int sub_key_count; printf("This is FastDHT client test program v%d.%d\n" \ "\nCopyright (C) 2008, Happy Fish / YuQing\n" \ "\nFastDHT may be copied only under the terms of the GNU General\n" \ "Public License V3, which may be found in the FastDHT source kit.\n" \ "Please visit the FastDHT Home Page http://www.csource.org/ \n" \ "for more detail.\n\n" \ , g_fdht_version.major, g_fdht_version.minor); if (argc < 2) { printf("Usage: %s <config_file>\n", argv[0]); return 1; } log_init(); conf_filename = argv[1]; g_log_context.log_level = LOG_DEBUG; if ((result=fdht_client_init(conf_filename)) != 0) { return result; } //g_keep_alive = true; if (g_keep_alive) { if ((result=fdht_connect_all_servers(&g_group_array, true, \ &conn_success_count, &conn_fail_count)) != 0) { printf("fdht_connect_all_servers fail, " \ "error code: %d, error info: %s\n", \ result, STRERROR(result)); return result; } } srand(time(NULL)); expires = FDHT_EXPIRES_NEVER; //expires = time(NULL) + 3600; memset(&object_info, 0, sizeof(object_info)); object_info.namespace_len = sprintf(object_info.szNameSpace, "user"); object_info.obj_id_len = sprintf(object_info.szObjectId, "happy_fish"); memset(key_list, 0, sizeof(key_list)); key_count = 4; key_list[0].key_len = sprintf(key_list[0].szKey, "login"); key_list[1].key_len = sprintf(key_list[1].szKey, "reg"); key_list[2].key_len = sprintf(key_list[2].szKey, "intl"); key_list[3].key_len = sprintf(key_list[3].szKey, "co"); do { key_list[0].pValue = "happyfish"; key_list[0].value_len = strlen(key_list[0].pValue); key_list[1].pValue = "1235277184"; key_list[1].value_len = strlen(key_list[1].pValue); key_list[2].pValue = "zh"; key_list[2].value_len = strlen(key_list[2].pValue); key_list[3].pValue = "cn"; key_list[3].value_len = strlen(key_list[3].pValue); if ((result=fdht_batch_set(&object_info, key_list, \ key_count, expires, &success_count)) != 0) { printf("fdht_batch_set result=%d\n", result); break; } printf("fdht_batch_set success count: %d\n", success_count); for (i=0; i<key_count; i++) { key_list[i].pValue = NULL; key_list[i].value_len = 0; } if ((result=fdht_batch_get_ex(&object_info, key_list, \ key_count, expires, &success_count)) != 0) { printf("fdht_batch_get_ex result=%d\n", result); break; } printf("fdht_batch_get_ex success count: %d\n", success_count); for (i=0; i<key_count; i++) { if (key_list[i].status == 0) { printf("key=%s, value=%s(%d)\n", \ key_list[i].szKey, key_list[i].pValue, \ key_list[i].value_len); } else { printf("key=%s, status=%d\n", \ key_list[i].szKey, key_list[i].status); } } for (i=0; i<key_count; i++) { if (key_list[i].pValue != NULL) { free(key_list[i].pValue); } } if ((result=fdht_get_sub_keys(&object_info, sub_keys, \ sizeof(sub_keys))) != 0) { printf("fdht_get_sub_keys fail, " \ "errno: %d, error info: %s\n", \ result, STRERROR(result)); } else { sub_key_count = splitEx(sub_keys, \ FDHT_FULL_KEY_SEPERATOR, keys, \ FDHT_KEY_LIST_MAX_COUNT); printf("sub keys after batch set: "); for (i=0; i<sub_key_count; i++) { if (i > 0) { printf(", "); } printf("%s", keys[i]); } printf("\n"); } /* if ((result=fdht_batch_delete(&object_info, key_list, \ key_count, &success_count)) != 0) { printf("fdht_batch_delete result=%d\n", result); break; } printf("fdht_batch_delete success count: %d\n", success_count); */ if ((result=fdht_get_sub_keys(&object_info, sub_keys, \ sizeof(sub_keys))) != 0) { printf("fdht_get_sub_keys fail, " \ "errno: %d, error info: %s\n", \ result, STRERROR(result)); } else { printf("sub keys after batch delete: %s\n", sub_keys); } } while(0); if (g_keep_alive) { fdht_disconnect_all_servers(&g_group_array); } fdht_client_destroy(); return result; }
static int storage_trunk_load() { #define TRUNK_DATA_NEW_FIELD_COUNT 8 // >= v5.01 #define TRUNK_DATA_OLD_FIELD_COUNT 6 // < V5.01 #define TRUNK_LINE_MAX_LENGHT 64 int64_t restore_offset; char trunk_data_filename[MAX_PATH_SIZE]; char buff[4 * 1024 + 1]; int line_count; int col_count; int index; char *pLineStart; char *pLineEnd; char *cols[TRUNK_DATA_NEW_FIELD_COUNT]; FDFSTrunkFullInfo trunkInfo; int result; int fd; int bytes; int len; storage_trunk_get_data_filename(trunk_data_filename); if (g_trunk_init_reload_from_binlog) { if (unlink(trunk_data_filename) != 0) { result = errno != 0 ? errno : ENOENT; if (result != ENOENT) { logError("file: "__FILE__", line: %d, " \ "unlink file %s fail, " \ "errno: %d, error info: %s", __LINE__, \ trunk_data_filename, result, \ STRERROR(result)); return result; } } restore_offset = 0; return storage_trunk_restore(restore_offset); } fd = open(trunk_data_filename, O_RDONLY); if (fd < 0) { result = errno != 0 ? errno : EIO; if (result == ENOENT) { restore_offset = 0; return storage_trunk_restore(restore_offset); } logError("file: "__FILE__", line: %d, " \ "open file %s fail, " \ "errno: %d, error info: %s", \ __LINE__, trunk_data_filename, \ result, STRERROR(result)); return result; } if ((bytes=read(fd, buff, sizeof(buff) - 1)) < 0) { result = errno != 0 ? errno : EIO; logError("file: "__FILE__", line: %d, " \ "read from file %s fail, " \ "errno: %d, error info: %s", \ __LINE__, trunk_data_filename, \ result, STRERROR(result)); close(fd); return result; } *(buff + bytes) = '\0'; pLineEnd = strchr(buff, '\n'); if (pLineEnd == NULL) { logError("file: "__FILE__", line: %d, " \ "read offset from file %s fail", \ __LINE__, trunk_data_filename); close(fd); return EINVAL; } *pLineEnd = '\0'; restore_offset = strtoll(buff, NULL, 10); pLineStart = pLineEnd + 1; //skip \n line_count = 0; while (1) { pLineEnd = strchr(pLineStart, '\n'); if (pLineEnd == NULL) { if (bytes < sizeof(buff) - 1) //EOF { break; } len = strlen(pLineStart); if (len > TRUNK_LINE_MAX_LENGHT) { logError("file: "__FILE__", line: %d, " \ "file %s, line length: %d too long", \ __LINE__, trunk_data_filename, len); close(fd); return EINVAL; } memcpy(buff, pLineStart, len); if ((bytes=read(fd, buff + len, sizeof(buff) \ - len - 1)) < 0) { result = errno != 0 ? errno : EIO; logError("file: "__FILE__", line: %d, " \ "read from file %s fail, " \ "errno: %d, error info: %s", \ __LINE__, trunk_data_filename, \ result, STRERROR(result)); close(fd); return result; } if (bytes == 0) { result = ENOENT; logError("file: "__FILE__", line: %d, " \ "file: %s, end of file, expect " \ "end line", __LINE__, \ trunk_data_filename); close(fd); return result; } bytes += len; *(buff + bytes) = '\0'; pLineStart = buff; continue; } ++line_count; *pLineEnd = '\0'; col_count = splitEx(pLineStart, ' ', cols, TRUNK_DATA_NEW_FIELD_COUNT); if (col_count != TRUNK_DATA_NEW_FIELD_COUNT && \ col_count != TRUNK_DATA_OLD_FIELD_COUNT) { logError("file: "__FILE__", line: %d, " \ "file %s, line: %d is invalid", \ __LINE__, trunk_data_filename, line_count); close(fd); return EINVAL; } if (col_count == TRUNK_DATA_OLD_FIELD_COUNT) { index = 0; } else { index = 2; } trunkInfo.path.store_path_index = atoi(cols[index++]); trunkInfo.path.sub_path_high = atoi(cols[index++]); trunkInfo.path.sub_path_low = atoi(cols[index++]); trunkInfo.file.id = atoi(cols[index++]); trunkInfo.file.offset = atoi(cols[index++]); trunkInfo.file.size = atoi(cols[index++]); if ((result=storage_trunk_do_add_space(&trunkInfo)) != 0) { close(fd); return result; } pLineStart = pLineEnd + 1; //next line } close(fd); if (*pLineStart != '\0') { logError("file: "__FILE__", line: %d, " \ "file %s does not end correctly", \ __LINE__, trunk_data_filename); return EINVAL; } logDebug("file: "__FILE__", line: %d, " \ "file %s, line count: %d", \ __LINE__, trunk_data_filename, line_count); return storage_trunk_restore(restore_offset); }
FDFSMetaData *fdfs_split_metadata_ex(char *meta_buff, \ const char recordSeperator, const char filedSeperator, \ int *meta_count, int *err_no) { char **rows; char **ppRow; char **ppEnd; FDFSMetaData *meta_list; FDFSMetaData *pMetadata; char *pSeperator; int nNameLen; int nValueLen; *meta_count = getOccurCount(meta_buff, recordSeperator) + 1; meta_list = (FDFSMetaData *)malloc(sizeof(FDFSMetaData) * \ (*meta_count)); if (meta_list == NULL) { *meta_count = 0; *err_no = ENOMEM; logError("file: "__FILE__", line: %d, " \ "malloc %d bytes fail", \ __LINE__, (int)sizeof(FDFSMetaData) * (*meta_count)); return NULL; } rows = (char **)malloc(sizeof(char *) * (*meta_count)); if (rows == NULL) { free(meta_list); *meta_count = 0; *err_no = ENOMEM; logError("file: "__FILE__", line: %d, " \ "malloc %d bytes fail", \ __LINE__, (int)sizeof(char *) * (*meta_count)); return NULL; } *meta_count = splitEx(meta_buff, recordSeperator, \ rows, *meta_count); ppEnd = rows + (*meta_count); pMetadata = meta_list; for (ppRow=rows; ppRow<ppEnd; ppRow++) { pSeperator = strchr(*ppRow, filedSeperator); if (pSeperator == NULL) { continue; } nNameLen = pSeperator - (*ppRow); nValueLen = strlen(pSeperator+1); if (nNameLen > FDFS_MAX_META_NAME_LEN) { nNameLen = FDFS_MAX_META_NAME_LEN; } if (nValueLen > FDFS_MAX_META_VALUE_LEN) { nValueLen = FDFS_MAX_META_VALUE_LEN; } memcpy(pMetadata->name, *ppRow, nNameLen); memcpy(pMetadata->value, pSeperator+1, nValueLen); pMetadata->name[nNameLen] = '\0'; pMetadata->value[nValueLen] = '\0'; pMetadata++; } *meta_count = pMetadata - meta_list; free(rows); *err_no = 0; return meta_list; }