/** @brief Parse all events. * * @param cur Pointer to the \<events\> node. */ static void parser_events(xmlNodePtr cur) { xmlChar *name; int repeats; tupleinfo *dest=NULL; int n; while(cur!=NULL) { if(!xmlStrcmp(cur->name, XMLCHAR "event")) { name=parser_getprop_str(cur, XMLCHAR "name"); repeats=parser_getprop_int(cur, XMLCHAR "repeats"); if (repeats<=0) INVPROP("repeats", cur); for(n=0; n<repeats; n++) { dest=tuple_new(CHAR name); if(dest==NULL) { fatal(_("Can't allocate memory")); } parser_event(cur, dest); } parser_newprop_int(cur, XMLCHAR "tupleid", dest->tupleid-repeats+1); xmlFree(name); } cur=cur->next; } }
extern Tuple *tuple_join(Tuple *l, Tuple *r, int lpos[], int rpos[], int len) { Value res[len]; for (int i = 0; i < len; ++i) res[i] = rpos[i] < 0 ? tuple_attr(l, lpos[i]) : tuple_attr(r, rpos[i]); return tuple_new(res, len); }
static int blackhole_space_execute_replace(struct space *space, struct txn *txn, struct request *request, struct tuple **result) { struct txn_stmt *stmt = txn_current_stmt(txn); stmt->new_tuple = tuple_new(space->format, request->tuple, request->tuple_end); if (stmt->new_tuple == NULL) return -1; tuple_ref(stmt->new_tuple); *result = stmt->new_tuple; return 0; }
void zpl_add_parameter(const char* def) { const char* warning = "--- Warning 175: Illegal syntax for command line define \"%s\" -- ignored\n"; Set* set; Symbol* sym; Numb* numb; Tuple* tuple; Entry* entry; char* name; char* value; assert(def != NULL); name = strdup(def); value = strchr(name, '='); if (value == NULL) { fprintf(stderr, warning, def); free(name); return; } *value = '\0'; value++; if (strlen(name) == 0 || strlen(value) == 0 || !is_valid_identifier(name)) { if (verbose > VERB_QUIET) fprintf(stderr, warning, def); free(name); return; } set = set_pseudo_new(); sym = symbol_new(str_new(name), SYM_ERR, set, 1, ENTRY_NULL); tuple = tuple_new(0); if (!numb_is_number(value)) entry = entry_new_strg(tuple, str_new(value)); else { numb = numb_new_ascii(value); entry = entry_new_numb(tuple, numb); numb_free(numb); } symbol_add_entry(sym, entry); tuple_free(tuple); set_free(set); free(name); }
extern Value tuple_attr(Tuple *t, int pos) { void *mem = t; Value res = {.size = t->v.size[pos], .data = mem + t->v.off[pos]}; return res; } extern Tuple *tuple_reord(Tuple *t, int pos[], int len) { Value res[len]; for (int i = 0; i < len; ++i) res[i] = tuple_attr(t, pos[i]); return tuple_new(res, len); }
void blokenv_call(STATE, cpu c, OBJECT self, int num_args) { OBJECT t1, t2, t3; int j; c->blockargs = num_args; t3 = tuple_new(state, num_args); for(j = 0; j < num_args; j++) { t1 = stack_pop(); tuple_put(state, t3, j, t1); } stack_push(t3); cpu_flush_sp(c); t2 = cpu_create_block_context(state, c, self, c->sp); cpu_activate_context(state, c, t2, blokenv_get_home(self), 1); }
static void test_attr() { Head *h = gen_head(); int i = 1; char *s = "string_1"; Value vals[2]; vals[0] = val_new_int(&i); vals[1] = val_new_str(s); Tuple *t = tuple_new(vals, 2); int pos; Type type; head_attr(h, "c", &pos, &type); Expr *e = expr_eq(expr_str("string_1"), expr_attr(pos, type)); if (expr_bool_val(e, t, NULL) == 0) fail(); expr_free(e); tuple_free(t); mem_free(h); }
static PyObject * tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *tmp, *newobj, *item; Py_ssize_t i, n; assert(PyType_IsSubtype(type, &PyTuple_Type)); tmp = tuple_new(&PyTuple_Type, args, kwds); if (tmp == NULL) return NULL; assert(PyTuple_Check(tmp)); newobj = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp)); if (newobj == NULL) return NULL; for (i = 0; i < n; i++) { item = PyTuple_GET_ITEM(tmp, i); Py_INCREF(item); PyTuple_SET_ITEM(newobj, i, item); } Py_DECREF(tmp); return newobj; }
static void test_compound() { char *names[] = {"a", "b", "c"}; Type types[] = {Int, Real, String}; int a, b, c; Type ta, tb, tc; Head *h = head_new(names, types, 3); head_attr(h, "a", &a, &ta); head_attr(h, "b", &b, &tb); head_attr(h, "c", &c, &tc); int v_int = 3; double v_real = 1.01; char *v_str = "bbb"; Value vals[3]; vals[0] = val_new_int(&v_int); vals[1] = val_new_real(&v_real); vals[2] = val_new_str(v_str); Tuple *t = tuple_new(vals, 3); Expr *expr = expr_or(expr_gt(expr_attr(b, Real), expr_real(4.00)), expr_lt(expr_attr(a, Int), expr_int(2))); expr = expr_or(expr, expr_lt(expr_attr(c, String), expr_str("aab"))); if (expr_bool_val(expr, t, NULL)) fail(); expr_free(expr); tuple_free(t); mem_free(h); }
static void xspf_add_file (xmlNode * track, const gchar * filename, const gchar * base, Index * filenames, Index * tuples) { xmlNode *nptr; gchar *location = NULL; Tuple * tuple = NULL; for (nptr = track->children; nptr != NULL; nptr = nptr->next) { if (nptr->type == XML_ELEMENT_NODE) { if (!xmlStrcmp(nptr->name, (xmlChar *)"location")) { /* Location is a special case */ gchar *str = (gchar *)xmlNodeGetContent(nptr); if (strstr (str, "://") != NULL) location = str_get (str); else if (str[0] == '/' && base != NULL) { const gchar * colon = strstr (base, "://"); if (colon != NULL) location = str_printf ("%.*s%s", (gint) (colon + 3 - base), base, str); } else if (base != NULL) { const gchar * slash = strrchr (base, '/'); if (slash != NULL) location = str_printf ("%.*s%s", (gint) (slash + 1 - base), base, str); } xmlFree(str); } else { /* Rest of the nodes are handled here */ gint i; gboolean isMeta; xmlChar *findName; if (!xmlStrcmp(nptr->name, (xmlChar *)"meta")) { isMeta = TRUE; findName = xmlGetProp(nptr, (xmlChar *)"rel"); } else { isMeta = FALSE; findName = xmlStrdup(nptr->name); } for (i = 0; i < xspf_nentries; i++) if ((xspf_entries[i].isMeta == isMeta) && !xmlStrcmp(findName, (xmlChar *)xspf_entries[i].xspfName)) { xmlChar *str = xmlNodeGetContent(nptr); switch (xspf_entries[i].type) { case TUPLE_STRING: if (! tuple) tuple = tuple_new (); tuple_set_str(tuple, xspf_entries[i].tupleField, NULL, (gchar *)str); break; case TUPLE_INT: if (! tuple) tuple = tuple_new (); tuple_set_int(tuple, xspf_entries[i].tupleField, NULL, atol((char *)str)); break; default: break; } xmlFree(str); break; } xmlFree(findName); } } } if (location != NULL) { if (tuple) tuple_set_filename (tuple, location); index_append(filenames, location); index_append(tuples, tuple); } else if (tuple) tuple_unref (tuple); }
/** * Tuple transforming function. * * Remove the fields designated by 'offset' and 'len' from an tuple, * and replace them with the elements of supplied data fields, * if any. * * Function returns newly allocated tuple. * It does not change any parent tuple data. */ static int lbox_tuple_transform(struct lua_State *L) { struct tuple *tuple = lua_checktuple(L, 1); int argc = lua_gettop(L); if (argc < 3) luaL_error(L, "tuple.transform(): bad arguments"); lua_Integer offset = lua_tointeger(L, 2); /* Can be negative and can be > INT_MAX */ lua_Integer len = lua_tointeger(L, 3); lua_Integer field_count = box_tuple_field_count(tuple); /* validate offset and len */ if (offset == 0) { luaL_error(L, "tuple.transform(): offset is out of bound"); } else if (offset < 0) { if (-offset > field_count) luaL_error(L, "tuple.transform(): offset is out of bound"); offset += field_count + 1; } else if (offset > field_count) { offset = field_count + 1; } if (len < 0) luaL_error(L, "tuple.transform(): len is negative"); if (len > field_count + 1 - offset) len = field_count + 1 - offset; assert(offset + len <= field_count + 1); /* * Calculate the number of operations and length of UPDATE expression */ uint32_t op_cnt = 0; if (offset < field_count + 1 && len > 0) op_cnt++; if (argc > 3) op_cnt += argc - 3; if (op_cnt == 0) { /* tuple_update() does not accept an empty operation list. */ luaT_pushtuple(L, tuple); return 1; } struct ibuf *buf = tarantool_lua_ibuf; ibuf_reset(buf); struct mpstream stream; mpstream_init(&stream, buf, ibuf_reserve_cb, ibuf_alloc_cb, luamp_error, L); /* * Prepare UPDATE expression */ mpstream_encode_array(&stream, op_cnt); if (len > 0) { mpstream_encode_array(&stream, 3); mpstream_encode_str(&stream, "#"); mpstream_encode_uint(&stream, offset); mpstream_encode_uint(&stream, len); } for (int i = argc ; i > 3; i--) { mpstream_encode_array(&stream, 3); mpstream_encode_str(&stream, "!"); mpstream_encode_uint(&stream, offset); luamp_encode(L, luaL_msgpack_default, &stream, i); } mpstream_flush(&stream); uint32_t new_size = 0, bsize; const char *old_data = tuple_data_range(tuple, &bsize); struct region *region = &fiber()->gc; size_t used = region_used(region); struct tuple *new_tuple = NULL; /* * Can't use box_tuple_update() since transform must reset * the tuple format to default. The new tuple most likely * won't coerce into the original space format, so we have * to use the default one with no restrictions on field * count or types. */ const char *new_data = tuple_update_execute(region_aligned_alloc_cb, region, buf->buf, buf->buf + ibuf_used(buf), old_data, old_data + bsize, &new_size, 1, NULL); if (new_data != NULL) new_tuple = tuple_new(box_tuple_format_default(), new_data, new_data + new_size); region_truncate(region, used); if (new_tuple == NULL) luaT_error(L); luaT_pushtuple(L, new_tuple); ibuf_reset(buf); return 1; }
int main() { res.defi = -1; res.defd = -0.999; res.defl = -123; res.add_int = 0; res.add_real = 0.0; res.add_long = 0; int ints[MAX]; double reals[MAX]; long long longs[MAX]; Value val_ints[MAX]; Value val_reals[MAX]; Value val_longs[MAX]; Tuple *tuples[MAX]; for (int i = 0; i < MAX; ++i) { ints[i] = i + 1; reals[i] = i + 0.5; longs[i] = 0x7000ffffffffffff; val_ints[i] = val_new_int(&ints[i]); val_reals[i] = val_new_real(&reals[i]); val_longs[i] = val_new_long(&longs[i]); if (i == 0 || ints[i] > res.max_int) res.max_int = ints[i]; if (i == 0 || reals[i] > res.max_real) res.max_real = reals[i]; if (i == 0 || longs[i] > res.max_long) res.max_long = longs[i]; if (i == 0 || ints[i] < res.min_int) res.min_int = ints[i]; if (i == 0 || reals[1] < res.min_real) res.min_real = reals[i]; if (i == 0 || longs[i] < res.min_long) res.min_long = longs[i]; res.add_int += ints[i]; res.add_real += reals[i]; res.add_long += longs[i]; } res.avg_int = (double) res.add_int / MAX; res.avg_real = res.add_real / MAX; res.avg_long = (double) res.add_long / MAX; Value vals[3]; for (int i = 0; i < MAX; ++i) { vals[0] = val_ints[i]; vals[1] = val_reals[i]; vals[2] = val_longs[i]; tuples[i] = tuple_new(vals, 3); } test_cnt(tuples); test_min(tuples); test_max(tuples); test_avg(tuples); test_add(tuples); for (int i = 0; i < MAX; ++i) tuple_free(tuples[i]); return 0; }