static gpointer _row_to_bean(const struct bean_descriptor_s *descr, sqlite3_stmt *stmt) { const struct field_descriptor_s *fd; gpointer res; int col, s; res = _bean_create(descr); for (fd=descr->fields; fd->name ;fd++) { col = fd->position; #if 0 GRID_TRACE2("SQL column[%d,%d/%s,%s] field[%d,%ld,%d/%s,%s]", col, sqlite3_column_type(stmt, col), sqlite_strtype(sqlite3_column_type(stmt, col)), sqlite3_column_name(stmt, col), fd->position, fd->offset, fd->type, bean_strtype(fd->type), fd->name); #endif if (sqlite3_column_type(stmt, col) == SQLITE_NULL) { _bean_del_field(res, fd->position); continue; } _bean_set_field(res, fd->position); gpointer pf = ((guint8*)res) + descr->offset_fields + fd->offset; switch (fd->type) { case FT_BOOL: *((gboolean*)pf) = sqlite3_column_int(stmt, col); continue; case FT_INT: *((gint64*)pf) = sqlite3_column_int64(stmt, col); continue; case FT_REAL: *((gdouble*)pf) = sqlite3_column_double(stmt, col); continue; case FT_TEXT: s = sqlite3_column_bytes(stmt, col); g_string_append_len(GSTR(pf), (const gchar*)sqlite3_column_text(stmt, col), s); continue; case FT_BLOB: s = sqlite3_column_bytes(stmt, col); g_byte_array_append(GBA(pf), (guint8*)sqlite3_column_blob(stmt, col), s); continue; default: g_assert_not_reached(); continue; } } return res; }
GError* m2v2_json_load_single_header (struct json_object *j, gpointer *pbean) { GError *err = NULL; GByteArray *id = NULL, *hash = NULL; struct bean_CONTENTS_HEADERS_s *header = NULL; struct json_object *jid, *jhash, *jsize, *jctime, *jmtime, *jmethod, *jtype; struct oio_ext_json_mapping_s mapping[] = { {"id", &jid, json_type_string, 1}, {"hash", &jhash, json_type_string, 1}, {"size", &jsize, json_type_int, 1}, {"ctime", &jctime, json_type_int, 0}, {"mtime", &jmtime, json_type_int, 0}, {"chunk-method", &jmethod, json_type_string, 1}, {"mime-type", &jtype, json_type_string, 1}, {NULL, NULL, 0, 0} }; *pbean = NULL; if (NULL != (err = oio_ext_extract_json (j, mapping))) return err; id = metautils_gba_from_hexstring(json_object_get_string(jid)); if (!id) { err = NEWERROR(CODE_BAD_REQUEST, "Invalid header, not hexa id"); goto exit; } hash = metautils_gba_from_hexstring(json_object_get_string(jhash)); if (!hash || hash->len != 16) { err = NEWERROR(CODE_BAD_REQUEST, "Invalid header, not hexa16 hash"); goto exit; } header = _bean_create (&descr_struct_CONTENTS_HEADERS); CONTENTS_HEADERS_set2_id (header, id->data, id->len); CONTENTS_HEADERS_set2_hash (header, hash->data, hash->len); CONTENTS_HEADERS_set_size (header, json_object_get_int64(jsize)); if (jctime) CONTENTS_HEADERS_set_ctime (header, json_object_get_int64(jctime)); if (jmtime) CONTENTS_HEADERS_set_mtime (header, json_object_get_int64(jmtime)); CONTENTS_HEADERS_set2_chunk_method (header, json_object_get_string(jmethod)); CONTENTS_HEADERS_set2_mime_type (header, json_object_get_string(jtype)); *pbean = header; header = NULL; exit: metautils_gba_unref (id); metautils_gba_unref (hash); _bean_clean (header); return err; }
GError* m2v2_json_load_single_chunk (struct json_object *j, gpointer *pbean) { GError *err = NULL; GByteArray *hid = NULL, *hash = NULL; struct bean_CHUNKS_s *chunk = NULL; struct json_object *jid, *jcontent, *jhash, *jsize, *jctime, *jpos; struct oio_ext_json_mapping_s mapping[] = { {"id", &jid, json_type_string, 1}, {"hash", &jhash, json_type_string, 1}, {"size", &jsize, json_type_int, 1}, {"ctime", &jctime, json_type_int, 0}, {"content", &jcontent, json_type_string, 1}, {"pos", &jpos, json_type_string, 1}, {NULL, NULL, 0, 0} }; *pbean = NULL; if (NULL != (err = oio_ext_extract_json (j, mapping))) return err; hid = metautils_gba_from_hexstring(json_object_get_string(jid)); if (!hid) { err = NEWERROR(CODE_BAD_REQUEST, "Invalid header, not hexa id"); goto exit; } hash = metautils_gba_from_hexstring(json_object_get_string(jhash)); if (!hash) { err = NEWERROR(CODE_BAD_REQUEST, "Invalid chunk, not hexa header id"); goto exit; } chunk = _bean_create (&descr_struct_CHUNKS); CHUNKS_set2_id (chunk, json_object_get_string(jid)); CHUNKS_set_hash (chunk, hash); CHUNKS_set_size (chunk, json_object_get_int64(jsize)); CHUNKS_set_ctime (chunk, !jctime ? g_get_real_time() / G_TIME_SPAN_SECOND : json_object_get_int64(jctime)); CHUNKS_set_content (chunk, hid); CHUNKS_set2_position (chunk, json_object_get_string (jpos)); *pbean = chunk; chunk = NULL; exit: metautils_gba_unref (hid); metautils_gba_unref (hash); _bean_clean (chunk); return err; }
// TODO: export? static gpointer _gen_chunk_bean(struct service_info_s *si) { gchar straddr[STRLEN_ADDRINFO], strid[STRLEN_CHUNKID]; gchar *chunkid = NULL; struct bean_CHUNKS_s *chunk = NULL; grid_addrinfo_to_string(&(si->addr), straddr, sizeof(straddr)); SHA256_randomized_string(strid, sizeof(strid)); chunk = _bean_create(&descr_struct_CHUNKS); chunkid = m2v2_build_chunk_url (straddr, strid); CHUNKS_set2_id(chunk, chunkid); g_free(chunkid); return (gpointer)chunk; }
// TODO: factorize with _gen_chunk() from meta2_utils.c static gpointer _gen_chunk_bean(const char *straddr) { guint8 binid[32]; gchar strid[65]; gchar *chunkid = NULL; struct bean_CHUNKS_s *chunk = NULL; oio_buf_randomize (binid, sizeof(binid)); oio_str_bin2hex (binid, sizeof(binid), strid, sizeof(strid)); chunk = _bean_create(&descr_struct_CHUNKS); chunkid = m2v2_build_chunk_url (straddr, strid); CHUNKS_set2_id(chunk, chunkid); g_free(chunkid); return (gpointer)chunk; }
static GSList * _props_generate(struct oio_url_s *url, gint64 v, guint count) { GSList *result = NULL; while (count-- > 0) { gchar name[32]; g_snprintf(name, sizeof(name), "prop-%u", count); struct bean_PROPERTIES_s *prop = _bean_create(&descr_struct_PROPERTIES); PROPERTIES_set2_alias(prop, oio_url_get(url, OIOURL_PATH)); PROPERTIES_set_version(prop, v); PROPERTIES_set2_key(prop, name); PROPERTIES_set2_value(prop, (guint8*)"value", sizeof("value")); result = g_slist_prepend(result, prop); } _debug_beans_list(result); return result; }
GError* m2v2_json_load_single_alias (struct json_object *j, gpointer *pbean) { GError *err = NULL; GByteArray *hid = NULL; struct bean_ALIASES_s *alias = NULL; struct json_object *jname, *jversion, *jctime, *jmd, *jheader; struct metautils_json_mapping_s m[] = { {"name", &jname, json_type_string, 1}, {"ver", &jversion, json_type_int, 1}, {"ctime", &jctime, json_type_int, 1}, {"header", &jheader, json_type_string, 1}, {"system_metadata", &jmd, json_type_string, 1}, {NULL, NULL, 0, 0} }; *pbean = NULL; if (NULL != (err = metautils_extract_json(j, m))) goto exit; hid = metautils_gba_from_hexstring(json_object_get_string(jheader)); if (!hid) { err = NEWERROR(CODE_BAD_REQUEST, "Invalid alias, not hexadecimal header_id"); goto exit; } alias = _bean_create (&descr_struct_ALIASES); ALIASES_set_deleted (alias, FALSE); ALIASES_set_container_version (alias, 0); ALIASES_set_ctime (alias, 0); ALIASES_set2_alias (alias, json_object_get_string(jname)); ALIASES_set_version (alias, json_object_get_int64(jversion)); ALIASES_set2_mdsys (alias, json_object_get_string(jmd)); ALIASES_set2_content_id (alias, hid->data, hid->len); *pbean = alias; alias = NULL; exit: metautils_gba_unref (hid); _bean_clean (alias); return err; }
GError* m2v2_json_load_single_chunk (struct json_object *j, gpointer *pbean) { GError *err = NULL; GByteArray *hash = NULL; struct bean_CHUNKS_s *chunk = NULL; struct json_object *jid, *jhash, *jsize; struct metautils_json_mapping_s mapping[] = { {"id", &jid, json_type_string, 1}, {"hash", &jhash, json_type_string, 1}, {"size", &jsize, json_type_int, 1}, {NULL, NULL, 0, 0} }; *pbean = NULL; if (NULL != (err = metautils_extract_json (j, mapping))) return err; hash = metautils_gba_from_hexstring(json_object_get_string(jhash)); if (!hash) { err = NEWERROR(CODE_BAD_REQUEST, "Invalid chunk, not hexa header id"); goto exit; } chunk = _bean_create (&descr_struct_CHUNKS); CHUNKS_set2_id (chunk, json_object_get_string(jid)); CHUNKS_set2_hash (chunk, hash->data, hash->len); CHUNKS_set_size (chunk, json_object_get_int64(jsize)); CHUNKS_set_ctime (chunk, 0); *pbean = chunk; chunk = NULL; exit: metautils_gba_unref (hash); _bean_clean (chunk); return err; }
GError* m2v2_json_load_single_content (struct json_object *j, gpointer *pbean) { GError *err = NULL; GByteArray *hid = NULL; struct bean_CONTENTS_s *content = NULL; struct json_object *jhid, *jcid, *jpos; struct metautils_json_mapping_s mapping[] = { {"hdr", &jhid, json_type_string, 1}, {"chunk", &jcid, json_type_string, 1}, {"pos", &jpos, json_type_string, 1}, {NULL, NULL, 0, 0} }; *pbean = NULL; if (NULL != (err = metautils_extract_json (j, mapping))) return err; hid = metautils_gba_from_hexstring(json_object_get_string(jhid)); if (!hid) { err = NEWERROR(CODE_BAD_REQUEST, "Invalid content, not hexa header id"); goto exit; } content = _bean_create (&descr_struct_CONTENTS); CONTENTS_set2_content_id (content, hid->data, hid->len); CONTENTS_set2_chunk_id (content, json_object_get_string (jcid)); CONTENTS_set2_position (content, json_object_get_string (jpos)); *pbean = content; content = NULL; exit: metautils_gba_unref (hid); _bean_clean (content); return err; }