static TRI_json_t* MergeRecursive (TRI_memory_zone_t* zone, const TRI_json_t* const lhs, const TRI_json_t* const rhs, const bool nullMeansRemove) { size_t i, n; TRI_json_t* result = TRI_CopyJson(zone, lhs); if (result == NULL) { return NULL; } n = rhs->_value._objects._length; for (i = 0; i < n; i += 2) { // enumerate all the replacement values TRI_json_t* key = TRI_AtVector(&rhs->_value._objects, i); TRI_json_t* value = TRI_AtVector(&rhs->_value._objects, i + 1); if (value->_type == TRI_JSON_NULL && nullMeansRemove) { // replacement value is a null and we don't want to store nulls => delete attribute from the result TRI_DeleteArrayJson(zone, result, key->_value._string.data); } else { // replacement value is not a null or we want to store nulls TRI_json_t* lhsValue = TRI_LookupArrayJson(lhs, key->_value._string.data); if (lhsValue == NULL) { // existing array does not have the attribute => append new attribute if (value->_type == TRI_JSON_ARRAY) { TRI_json_t* empty = TRI_CreateArrayJson(zone); TRI_json_t* merged = MergeRecursive(zone, empty, value, nullMeansRemove); TRI_Insert3ArrayJson(zone, result, key->_value._string.data, merged); TRI_FreeJson(zone, empty); } else { TRI_Insert3ArrayJson(zone, result, key->_value._string.data, TRI_CopyJson(zone, value)); } } else { // existing array already has the attribute => replace attribute if (lhsValue->_type == TRI_JSON_ARRAY && value->_type == TRI_JSON_ARRAY) { TRI_json_t* merged = MergeRecursive(zone, lhsValue, value, nullMeansRemove); TRI_ReplaceArrayJson(zone, result, key->_value._string.data, merged); TRI_FreeJson(zone, merged); } else { TRI_ReplaceArrayJson(zone, result, key->_value._string.data, value); } } } } return result; }
int TRI_SaveConfigurationReplicationApplier (TRI_vocbase_t* vocbase, TRI_replication_applier_configuration_t const* config, bool doSync) { TRI_json_t* json; char* filename; int res; json = JsonConfiguration(config, true); if (json == NULL) { return TRI_ERROR_OUT_OF_MEMORY; } filename = GetConfigurationFilename(vocbase); if (! TRI_SaveJson(filename, json, doSync)) { res = TRI_errno(); } else { res = TRI_ERROR_NO_ERROR; } TRI_FreeString(TRI_CORE_MEM_ZONE, filename); TRI_FreeJson(TRI_CORE_MEM_ZONE, json); return res; }
static void FreeData (TRI_general_cursor_result_t* result) { TRI_json_t* json = (TRI_json_t*) result->_data; TRI_ASSERT(json); TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, json); }
int TRI_SaveStateReplicationApplier (TRI_vocbase_t* vocbase, TRI_replication_applier_state_t const* state, bool doSync) { TRI_json_t* json; char* filename; int res; if (vocbase->_type == TRI_VOCBASE_TYPE_COORDINATOR) { return TRI_ERROR_CLUSTER_UNSUPPORTED; } json = JsonApplyState(state); if (json == nullptr) { return TRI_ERROR_OUT_OF_MEMORY; } filename = GetStateFilename(vocbase); LOG_TRACE("saving replication applier state to file '%s'", filename); if (! TRI_SaveJson(filename, json, doSync)) { res = TRI_errno(); } else { res = TRI_ERROR_NO_ERROR; } TRI_FreeString(TRI_CORE_MEM_ZONE, filename); TRI_FreeJson(TRI_CORE_MEM_ZONE, json); return res; }
int TRI_SaveStateReplicationApplier (TRI_vocbase_t* vocbase, TRI_replication_applier_state_t const* state, bool doSync) { TRI_json_t* json; char* filename; int res; json = JsonApplyState(state); if (json == NULL) { return TRI_ERROR_OUT_OF_MEMORY; } filename = GetStateFilename(vocbase); LOG_TRACE("saving replication applier state to file '%s'", filename); if (! TRI_SaveJson(filename, json, doSync)) { res = TRI_errno(); } else { res = TRI_ERROR_NO_ERROR; } TRI_FreeString(TRI_CORE_MEM_ZONE, filename); TRI_FreeJson(TRI_CORE_MEM_ZONE, json); return res; }
static char* ExtractStringShapedJson (TRI_shaper_t* shaper, TRI_shaped_json_t const* document, char const* path) { TRI_json_t* json; TRI_shape_pid_t pid; TRI_shape_t const* shape; TRI_shaped_json_t shaped; bool ok; char* result; pid = shaper->findAttributePathByName(shaper, path); ok = TRI_ExtractShapedJsonVocShaper(shaper, document, 0, pid, &shaped, &shape); if (! ok || shape == NULL) { return NULL; } json = TRI_JsonShapedJson(shaper, &shaped); if (json == NULL) { return NULL; } if (json->_type != TRI_JSON_STRING) { return NULL; } result = TRI_DuplicateString2(json->_value._string.data, json->_value._string.length); TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, json); return result; }
void TRI_ClearIndexOperator(TRI_index_operator_t* indexOperator) { TRI_logical_index_operator_t* logicalOperator; TRI_relation_index_operator_t* relationOperator; if (indexOperator == NULL) { return; } switch (indexOperator->_type) { case TRI_AND_INDEX_OPERATOR: case TRI_NOT_INDEX_OPERATOR: case TRI_OR_INDEX_OPERATOR: { logicalOperator = (TRI_logical_index_operator_t*)(indexOperator); TRI_ClearIndexOperator(logicalOperator->_left); TRI_ClearIndexOperator(logicalOperator->_right); TRI_Free(TRI_UNKNOWN_MEM_ZONE, logicalOperator); break; } case TRI_EQ_INDEX_OPERATOR: case TRI_GE_INDEX_OPERATOR: case TRI_GT_INDEX_OPERATOR: case TRI_NE_INDEX_OPERATOR: case TRI_LE_INDEX_OPERATOR: case TRI_LT_INDEX_OPERATOR: case TRI_IN_INDEX_OPERATOR: { relationOperator = (TRI_relation_index_operator_t*)(indexOperator); if (relationOperator->_parameters != NULL) { TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, relationOperator->_parameters); } if (relationOperator->_fields != NULL) { size_t i; // relationOperator->_fields contains _numFields shapedJson objects for (i = 0; i < relationOperator->_numFields; ++i) { // destroy each individual shapedJson object TRI_shaped_json_t* shaped = relationOperator->_fields + i; TRI_DestroyShapedJson(relationOperator->_base._shaper, shaped); } // free the memory pointer TRI_Free(TRI_UNKNOWN_MEM_ZONE, relationOperator->_fields); } TRI_Free(TRI_UNKNOWN_MEM_ZONE, relationOperator); break; } } // end of switch statement }
void TRI_FreeGeneralCursor (TRI_general_cursor_t* cursor) { if (cursor->_extra != NULL) { TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, cursor->_extra); } TRI_FreeCursorResult(cursor->_result); TRI_DestroySpin(&cursor->_lock); TRI_Free(TRI_UNKNOWN_MEM_ZONE, cursor); LOG_TRACE("destroyed general cursor"); }
void ImportHelper::handleResult (SimpleHttpResult* result) { if (! result) { return; } stringstream& r = result->getBody(); TRI_json_t* json = TRI_JsonString(TRI_UNKNOWN_MEM_ZONE, r.str().c_str()); if (json) { // get the "error" flag. This returns a pointer, not a copy TRI_json_t* error = TRI_LookupArrayJson(json, "error"); if (error) { if (error->_type == TRI_JSON_BOOLEAN && error->_value._boolean) { _hasError = true; // get the error message. This returns a pointer, not a copy TRI_json_t* errorMessage = TRI_LookupArrayJson(json, "errorMessage"); if (TRI_IsStringJson(errorMessage)) { _errorMessage = string(errorMessage->_value._string.data, errorMessage->_value._string.length); } } } TRI_json_t* importResult; // look up the "created" flag. This returns a pointer, not a copy importResult= TRI_LookupArrayJson(json, "created"); if (importResult) { if (importResult->_type == TRI_JSON_NUMBER) { _numberOk += (size_t) importResult->_value._number; } } // look up the "errors" flag. This returns a pointer, not a copy importResult= TRI_LookupArrayJson(json, "errors"); if (importResult) { if (importResult->_type == TRI_JSON_NUMBER) { _numberError += (size_t) importResult->_value._number; } } // this will free the json struct will a sub-elements TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, json); } delete result; }
static TRI_json_t* GetMergedKeyList (const TRI_json_t* const lhs, const TRI_json_t* const rhs) { TRI_json_t* keys; TRI_json_t* unique; size_t i, n; TRI_ASSERT(lhs->_type == TRI_JSON_ARRAY); TRI_ASSERT(rhs->_type == TRI_JSON_ARRAY); keys = TRI_CreateList2Json(TRI_UNKNOWN_MEM_ZONE, lhs->_value._objects._length + rhs->_value._objects._length); if (keys == NULL) { return NULL; } n = lhs->_value._objects._length; for (i = 0 ; i < n; i += 2) { TRI_json_t* key = TRI_AtVector(&lhs->_value._objects, i); TRI_ASSERT(TRI_IsStringJson(key)); TRI_PushBackListJson(TRI_UNKNOWN_MEM_ZONE, keys, key); } n = rhs->_value._objects._length; for (i = 0 ; i < n; i += 2) { TRI_json_t* key = TRI_AtVector(&rhs->_value._objects, i); TRI_ASSERT(TRI_IsStringJson(key)); TRI_PushBackListJson(TRI_UNKNOWN_MEM_ZONE, keys, key); } // sort the key list in place TRI_SortListJson(keys); // list is now sorted unique = TRI_UniquifyListJson(keys); TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, keys); return unique; // might be NULL }
int TRI_SaveCollectionInfo (char const* path, const TRI_col_info_t* const info) { TRI_json_t* json; char* filename; bool ok; filename = TRI_Concatenate2File(path, TRI_COL_PARAMETER_FILE); // create a json info object json = TRI_CreateArrayJson(TRI_UNKNOWN_MEM_ZONE); if (json == NULL) { // out of memory LOG_ERROR("cannot save info block '%s': out of memory", filename); TRI_FreeString(TRI_CORE_MEM_ZONE, filename); return TRI_ERROR_OUT_OF_MEMORY; } TRI_Insert3ArrayJson(TRI_UNKNOWN_MEM_ZONE, json, "version", TRI_CreateNumberJson(TRI_UNKNOWN_MEM_ZONE, info->_version)); TRI_Insert3ArrayJson(TRI_UNKNOWN_MEM_ZONE, json, "type", TRI_CreateNumberJson(TRI_UNKNOWN_MEM_ZONE, info->_type)); TRI_Insert3ArrayJson(TRI_UNKNOWN_MEM_ZONE, json, "cid", TRI_CreateNumberJson(TRI_UNKNOWN_MEM_ZONE, info->_cid)); TRI_Insert3ArrayJson(TRI_UNKNOWN_MEM_ZONE, json, "deleted", TRI_CreateBooleanJson(TRI_UNKNOWN_MEM_ZONE, info->_deleted)); TRI_Insert3ArrayJson(TRI_UNKNOWN_MEM_ZONE, json, "maximalSize", TRI_CreateNumberJson(TRI_UNKNOWN_MEM_ZONE, info->_maximalSize)); TRI_Insert3ArrayJson(TRI_UNKNOWN_MEM_ZONE, json, "name", TRI_CreateStringCopyJson(TRI_UNKNOWN_MEM_ZONE, info->_name)); TRI_Insert3ArrayJson(TRI_UNKNOWN_MEM_ZONE, json, "isVolatile", TRI_CreateBooleanJson(TRI_UNKNOWN_MEM_ZONE, info->_isVolatile)); TRI_Insert3ArrayJson(TRI_UNKNOWN_MEM_ZONE, json, "waitForSync", TRI_CreateBooleanJson(TRI_UNKNOWN_MEM_ZONE, info->_waitForSync)); if (info->_keyOptions) { TRI_Insert3ArrayJson(TRI_UNKNOWN_MEM_ZONE, json, "keyOptions", TRI_CopyJson(TRI_UNKNOWN_MEM_ZONE, info->_keyOptions)); } // save json info to file ok = TRI_SaveJson(filename, json); TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, json); if (! ok) { LOG_ERROR("cannot save info block '%s': '%s'", filename, TRI_last_error()); TRI_FreeString(TRI_CORE_MEM_ZONE, filename); return TRI_errno(); } TRI_FreeString(TRI_CORE_MEM_ZONE, filename); return TRI_ERROR_NO_ERROR; }
static bool ExtractBooleanShapedJson (TRI_shaper_t* shaper, TRI_shaped_json_t const* document, char const* path, bool* found) { TRI_json_t* json; TRI_shape_pid_t pid; TRI_shape_t const* shape; TRI_shaped_json_t shaped; bool result; bool ok; if (found != NULL) { *found = false; } pid = shaper->findAttributePathByName(shaper, path); ok = TRI_ExtractShapedJsonVocShaper(shaper, document, 0, pid, &shaped, &shape); if (! ok || shape == NULL) { return false; } json = TRI_JsonShapedJson(shaper, &shaped); if (json == NULL) { return false; } if (json->_type != TRI_JSON_BOOLEAN) { return false; } if (found != NULL) { *found = true; } result = json->_value._boolean; TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, json); return result; }
void TRI_FreeBindParametersAql (TRI_aql_context_t* const context) { size_t i; size_t n; // iterate thru all parameters allocated n = context->_parameters._values._nrAlloc; for (i = 0; i < n; ++i) { TRI_aql_bind_parameter_t* parameter; parameter = (TRI_aql_bind_parameter_t*) context->_parameters._values._table[i]; if (!parameter) { continue; } assert(parameter->_name); assert(parameter->_value); TRI_FreeString(TRI_UNKNOWN_MEM_ZONE, parameter->_name); TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, parameter->_value); TRI_Free(TRI_UNKNOWN_MEM_ZONE, parameter); } }
int TRI_LoadStateReplicationApplier (TRI_vocbase_t* vocbase, TRI_replication_applier_state_t* state) { TRI_json_t* json; TRI_json_t* serverId; char* filename; int res; TRI_InitStateReplicationApplier(state); filename = GetStateFilename(vocbase); if (filename == NULL) { return TRI_ERROR_OUT_OF_MEMORY; } LOG_TRACE("looking for replication state file '%s'", filename); if (! TRI_ExistsFile(filename)) { TRI_FreeString(TRI_CORE_MEM_ZONE, filename); return TRI_ERROR_FILE_NOT_FOUND; } LOG_TRACE("replication state file '%s' found", filename); json = TRI_JsonFile(TRI_CORE_MEM_ZONE, filename, NULL); TRI_FreeString(TRI_CORE_MEM_ZONE, filename); if (! TRI_IsArrayJson(json)) { if (json != NULL) { TRI_FreeJson(TRI_CORE_MEM_ZONE, json); } return TRI_ERROR_REPLICATION_INVALID_APPLIER_STATE; } res = TRI_ERROR_NO_ERROR; // read the server id serverId = TRI_LookupArrayJson(json, "serverId"); if (! TRI_IsStringJson(serverId)) { res = TRI_ERROR_REPLICATION_INVALID_APPLIER_STATE; } else { state->_serverId = TRI_UInt64String2(serverId->_value._string.data, serverId->_value._string.length - 1); } if (res == TRI_ERROR_NO_ERROR) { // read the ticks res |= ReadTick(json, "lastAppliedContinuousTick", &state->_lastAppliedContinuousTick); // set processed = applied state->_lastProcessedContinuousTick = state->_lastAppliedContinuousTick; } TRI_FreeJson(TRI_CORE_MEM_ZONE, json); LOG_TRACE("replication state file read successfully"); return res; }
static TRI_json_t* MergeRecursive (TRI_memory_zone_t* zone, TRI_json_t const* lhs, TRI_json_t const* rhs, bool nullMeansRemove, bool mergeObjects) { TRI_ASSERT(lhs != nullptr); std::unique_ptr<TRI_json_t> result(TRI_CopyJson(zone, lhs)); if (result == nullptr) { return nullptr; } auto r = result.get(); // shortcut variable size_t const n = TRI_LengthVector(&rhs->_value._objects); for (size_t i = 0; i < n; i += 2) { // enumerate all the replacement values auto key = static_cast<TRI_json_t const*>(TRI_AtVector(&rhs->_value._objects, i)); auto value = static_cast<TRI_json_t const*>(TRI_AtVector(&rhs->_value._objects, i + 1)); if (value->_type == TRI_JSON_NULL && nullMeansRemove) { // replacement value is a null and we don't want to store nulls => delete attribute from the result TRI_DeleteObjectJson(zone, r, key->_value._string.data); } else { // replacement value is not a null or we want to store nulls TRI_json_t const* lhsValue = TRI_LookupObjectJson(lhs, key->_value._string.data); if (lhsValue == nullptr) { // existing array does not have the attribute => append new attribute if (value->_type == TRI_JSON_OBJECT && nullMeansRemove) { TRI_json_t empty; TRI_InitObjectJson(TRI_UNKNOWN_MEM_ZONE, &empty); TRI_json_t* merged = MergeRecursive(zone, &empty, value, nullMeansRemove, mergeObjects); if (merged == nullptr) { return nullptr; } TRI_json_t* copy = TRI_CopyJson(zone, value); if (copy == nullptr) { return nullptr; } TRI_Insert3ObjectJson(zone, r, key->_value._string.data, copy); } else { TRI_Insert3ObjectJson(zone, r, key->_value._string.data, TRI_CopyJson(zone, value)); } } else { // existing array already has the attribute => replace attribute if (lhsValue->_type == TRI_JSON_OBJECT && value->_type == TRI_JSON_OBJECT && mergeObjects) { TRI_json_t* merged = MergeRecursive(zone, lhsValue, value, nullMeansRemove, mergeObjects); if (merged == nullptr) { return nullptr; } TRI_ReplaceObjectJson(zone, r, key->_value._string.data, merged); TRI_FreeJson(zone, merged); } else { TRI_ReplaceObjectJson(zone, r, key->_value._string.data, value); } } } } return result.release(); }
int BitarrayIndex_generateInsertBitMask (BitarrayIndex* baIndex, const TRI_bitarray_index_key_t* element, TRI_bitarray_mask_t* mask) { TRI_shaper_t* shaper; int j; int shiftLeft; int result; // ........................................................................... // some safety checks first // ........................................................................... if (baIndex == NULL || element == NULL) { return TRI_ERROR_INTERNAL; } if (element->collection == NULL) { return TRI_ERROR_INTERNAL; } // ........................................................................... // We could be trying to store an 'undefined' document into the bitarray // We determine this implicitly. If element->numFields b == 0, then we // assume that the document did not have any matching attributes, yet since // we are here we wish to store this fact. // ........................................................................... if (!baIndex->_supportUndef && (element->numFields == 0 || element->fields == NULL)) { return TRI_ERROR_INTERNAL; } if (baIndex->_supportUndef && element->numFields == 0) { mask->_mask = 1; mask->_ignoreMask = 0; return TRI_ERROR_NO_ERROR; } // ........................................................................... // attempt to convert the stored TRI_shaped_json_t into TRI_Json_t so that // we can make a comparison between what values the bitarray index requires // and what values the document has sent. // ........................................................................... shaper = ((TRI_primary_collection_t*)(element->collection))->_shaper; mask->_mask = 0; shiftLeft = 0; for (j = 0; j < baIndex->_values._length; ++j) { TRI_json_t* valueList; TRI_json_t* value; uint64_t tempMask; value = TRI_JsonShapedJson(shaper, &(element->fields[j])); // from shaped json to simple json valueList = (TRI_json_t*)(TRI_AtVector(&(baIndex->_values),j)); tempMask = 0; // ......................................................................... // value is now the shaped json converted into plain json for comparison // ........................................................................ result = BitarrayIndex_generateEqualBitMaskHelper(valueList, value, &tempMask); // ............................................................................ // remove the json entry created from the shaped json // ............................................................................ TRI_FreeJson(shaper->_memoryZone, value); if (result != TRI_ERROR_NO_ERROR) { return result; } mask->_mask = mask->_mask | (tempMask << shiftLeft); shiftLeft += valueList->_value._objects._length; } return TRI_ERROR_NO_ERROR; }
int TRI_LoadCollectionInfo (char const* path, TRI_col_info_t* parameter) { TRI_json_t* json; char* filename; char* error = NULL; size_t i; size_t n; memset(parameter, 0, sizeof(TRI_col_info_t)); // find parameter file filename = TRI_Concatenate2File(path, TRI_COL_PARAMETER_FILE); if (filename == NULL) { LOG_ERROR("cannot load parameter info for collection '%s', out of memory", path); return TRI_set_errno(TRI_ERROR_OUT_OF_MEMORY); } if (! TRI_ExistsFile(filename)) { TRI_FreeString(TRI_CORE_MEM_ZONE, filename); return TRI_set_errno(TRI_ERROR_ARANGO_ILLEGAL_PARAMETER_FILE); } json = TRI_JsonFile(TRI_UNKNOWN_MEM_ZONE, filename, &error); if (json == NULL) { if (error != NULL) { LOG_ERROR("cannot open '%s', parameter block not readable: %s", filename, error); TRI_FreeString(TRI_CORE_MEM_ZONE, error); } else { LOG_ERROR("cannot open '%s', parameter block not readable", filename); } TRI_FreeString(TRI_CORE_MEM_ZONE, filename); return TRI_set_errno(TRI_ERROR_ARANGO_ILLEGAL_PARAMETER_FILE); } if (json->_type != TRI_JSON_ARRAY) { LOG_ERROR("cannot open '%s', file does not contain a json array", filename); TRI_FreeString(TRI_CORE_MEM_ZONE, filename); return TRI_set_errno(TRI_ERROR_ARANGO_ILLEGAL_PARAMETER_FILE); } TRI_FreeString(TRI_CORE_MEM_ZONE, filename); // convert json n = json->_value._objects._length; for (i = 0; i < n; i += 2) { TRI_json_t* key; TRI_json_t* value; key = TRI_AtVector(&json->_value._objects, i); value = TRI_AtVector(&json->_value._objects, i + 1); if (key->_type == TRI_JSON_STRING && value->_type == TRI_JSON_NUMBER) { if (TRI_EqualString(key->_value._string.data, "version")) { parameter->_version = value->_value._number; } else if (TRI_EqualString(key->_value._string.data, "type")) { parameter->_type = value->_value._number; } else if (TRI_EqualString(key->_value._string.data, "cid")) { parameter->_cid = value->_value._number; } else if (TRI_EqualString(key->_value._string.data, "maximalSize")) { parameter->_maximalSize = value->_value._number; } } else if (key->_type == TRI_JSON_STRING && value->_type == TRI_JSON_STRING) { if (TRI_EqualString(key->_value._string.data, "name")) { TRI_CopyString(parameter->_name, value->_value._string.data, sizeof(parameter->_name)); parameter->_isSystem = TRI_IsSystemCollectionName(parameter->_name); } } else if (key->_type == TRI_JSON_STRING && value->_type == TRI_JSON_BOOLEAN) { if (TRI_EqualString(key->_value._string.data, "deleted")) { parameter->_deleted = value->_value._boolean; } else if (TRI_EqualString(key->_value._string.data, "isVolatile")) { parameter->_isVolatile = value->_value._boolean; } else if (TRI_EqualString(key->_value._string.data, "waitForSync")) { parameter->_waitForSync = value->_value._boolean; } } else if (key->_type == TRI_JSON_STRING && value->_type == TRI_JSON_ARRAY) { if (TRI_EqualString(key->_value._string.data, "keyOptions")) { parameter->_keyOptions = TRI_CopyJson(TRI_UNKNOWN_MEM_ZONE, value); } } } TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, json); return TRI_ERROR_NO_ERROR; }
void TRI_FreeCollectionInfoOptions (TRI_col_info_t* parameter) { if (parameter->_keyOptions) { TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, parameter->_keyOptions); parameter->_keyOptions = NULL; } }
int TRI_CompareValuesJson (const TRI_json_t* const lhs, const TRI_json_t* const rhs) { // note: both lhs and rhs may be NULL! int lWeight = TypeWeight(lhs); int rWeight = TypeWeight(rhs); if (lWeight < rWeight) { return -1; } if (lWeight > rWeight) { return 1; } // lhs and rhs have equal weights if (lhs == NULL) { // both lhs and rhs are NULL, so they are equal return 0; } switch (lhs->_type) { case TRI_JSON_UNUSED: case TRI_JSON_NULL: return 0; // null == null; case TRI_JSON_BOOLEAN: if (lhs->_value._boolean == rhs->_value._boolean) { return 0; } if (!lhs->_value._boolean && rhs->_value._boolean) { return -1; } return 1; case TRI_JSON_NUMBER: if (lhs->_value._number == rhs->_value._number) { return 0; } if (lhs->_value._number < rhs->_value._number) { return -1; } return 1; case TRI_JSON_STRING: return strcmp(lhs->_value._string.data, rhs->_value._string.data); case TRI_JSON_LIST: { size_t nl = lhs->_value._objects._length; size_t nr = rhs->_value._objects._length; size_t n; size_t i; if (nl > nr) { n = nl; } else { n = nr; } for (i = 0; i < n; ++i) { TRI_json_t* lhsValue; TRI_json_t* rhsValue; int result; lhsValue = (i >= nl) ? NULL : TRI_AtVector(&lhs->_value._objects, i); rhsValue = (i >= nr) ? NULL : TRI_AtVector(&rhs->_value._objects, i); result = TRI_CompareValuesJson(lhsValue, rhsValue); if (result != 0) { return result; } } return 0; } case TRI_JSON_ARRAY: { TRI_json_t* keys; assert(lhs->_type == TRI_JSON_ARRAY); assert(rhs->_type == TRI_JSON_ARRAY); keys = GetMergedKeyList(lhs, rhs); if (keys != NULL) { size_t i, n; n = keys->_value._objects._length; for (i = 0; i < n; ++i) { TRI_json_t* keyElement; TRI_json_t* lhsValue; TRI_json_t* rhsValue; int result; keyElement = TRI_AtVector(&keys->_value._objects, i); assert(keyElement->_type == TRI_JSON_STRING); assert(keyElement->_value._string.data); lhsValue = TRI_LookupArrayJson((TRI_json_t*) lhs, keyElement->_value._string.data); // may be NULL rhsValue = TRI_LookupArrayJson((TRI_json_t*) rhs, keyElement->_value._string.data); // may be NULL result = TRI_CompareValuesJson(lhsValue, rhsValue); if (result != 0) { TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, keys); return result; } } TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, keys); } return 0; } default: return 0; } }
void ImportHelper::handleResult (SimpleHttpResult* result) { if (result == 0) { return; } TRI_json_t* json = TRI_JsonString(TRI_UNKNOWN_MEM_ZONE, result->getBody().c_str()); if (json != 0) { // error details TRI_json_t const* details = TRI_LookupArrayJson(json, "details"); if (TRI_IsListJson(details)) { const size_t n = details->_value._objects._length; for (size_t i = 0; i < n; ++i) { TRI_json_t const* detail = (TRI_json_t const*) TRI_AtVector(&details->_value._objects, i); if (TRI_IsStringJson(detail)) { LOG_WARNING("%s", detail->_value._string.data); } } } // get the "error" flag. This returns a pointer, not a copy TRI_json_t const* error = TRI_LookupArrayJson(json, "error"); if (TRI_IsBooleanJson(error) && error->_value._boolean) { _hasError = true; // get the error message. This returns a pointer, not a copy TRI_json_t const* errorMessage = TRI_LookupArrayJson(json, "errorMessage"); if (TRI_IsStringJson(errorMessage)) { _errorMessage = string(errorMessage->_value._string.data, errorMessage->_value._string.length - 1); } } TRI_json_t const* importResult; // look up the "created" flag. This returns a pointer, not a copy importResult = TRI_LookupArrayJson(json, "created"); if (TRI_IsNumberJson(importResult)) { _numberOk += (size_t) importResult->_value._number; } // look up the "errors" flag. This returns a pointer, not a copy importResult = TRI_LookupArrayJson(json, "errors"); if (TRI_IsNumberJson(importResult)) { _numberError += (size_t) importResult->_value._number; } // this will free the json struct will a sub-elements TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, json); } delete result; }
static TRI_aql_node_t* OptimiseBinaryRelationalOperation (TRI_aql_context_t* const context, TRI_aql_node_t* node) { TRI_aql_node_t* lhs = TRI_AQL_NODE_MEMBER(node, 0); TRI_aql_node_t* rhs = TRI_AQL_NODE_MEMBER(node, 1); TRI_js_exec_context_t* execContext; TRI_string_buffer_t* code; TRI_json_t* json; char* func; if (!lhs || !TRI_IsConstantValueNodeAql(lhs) || !rhs || !TRI_IsConstantValueNodeAql(rhs)) { return node; } if (node->_type == TRI_AQL_NODE_OPERATOR_BINARY_EQ) { func = "EQUAL"; } else if (node->_type == TRI_AQL_NODE_OPERATOR_BINARY_NE) { func = "UNEQUAL"; } else if (node->_type == TRI_AQL_NODE_OPERATOR_BINARY_GT) { func = "GREATER"; } else if (node->_type == TRI_AQL_NODE_OPERATOR_BINARY_GE) { func = "GREATEREQUAL"; } else if (node->_type == TRI_AQL_NODE_OPERATOR_BINARY_LT) { func = "LESS"; } else if (node->_type == TRI_AQL_NODE_OPERATOR_BINARY_LE) { func = "LESSEQUAL"; } else if (node->_type == TRI_AQL_NODE_OPERATOR_BINARY_IN) { func = "IN"; } else { // not what we expected, however, simply continue return node; } code = RelationCode(func, lhs, rhs); if (!code) { TRI_SetErrorContextAql(context, TRI_ERROR_OUT_OF_MEMORY, NULL); return node; } // execute the function code execContext = TRI_CreateExecutionContext(code->_buffer); TRI_FreeStringBuffer(TRI_UNKNOWN_MEM_ZONE, code); if (!execContext) { TRI_SetErrorContextAql(context, TRI_ERROR_OUT_OF_MEMORY, NULL); return node; } json = TRI_ExecuteResultContext(execContext); TRI_FreeExecutionContext(execContext); if (!json) { TRI_SetErrorContextAql(context, TRI_ERROR_QUERY_SCRIPT, NULL); return NULL; } // use the constant values instead of the function call node node = TRI_JsonNodeAql(context, json); if (!node) { TRI_SetErrorContextAql(context, TRI_ERROR_OUT_OF_MEMORY, NULL); } LOG_TRACE("optimised away binary relational operation"); TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, json); return node; }
static int LoadConfiguration (TRI_vocbase_t* vocbase, TRI_replication_applier_configuration_t* config) { TRI_json_t* json; TRI_json_t* value; char* filename; int res; TRI_DestroyConfigurationReplicationApplier(config); TRI_InitConfigurationReplicationApplier(config); filename = GetConfigurationFilename(vocbase); if (! TRI_ExistsFile(filename)) { TRI_FreeString(TRI_CORE_MEM_ZONE, filename); return TRI_ERROR_FILE_NOT_FOUND; } json = TRI_JsonFile(TRI_CORE_MEM_ZONE, filename, NULL); TRI_FreeString(TRI_CORE_MEM_ZONE, filename); if (! TRI_IsArrayJson(json)) { if (json != NULL) { TRI_FreeJson(TRI_CORE_MEM_ZONE, json); } return TRI_ERROR_REPLICATION_INVALID_APPLIER_CONFIGURATION; } res = TRI_ERROR_NO_ERROR; if (config->_endpoint != NULL) { TRI_FreeString(TRI_CORE_MEM_ZONE, config->_endpoint); config->_endpoint = NULL; } if (config->_database != NULL) { TRI_FreeString(TRI_CORE_MEM_ZONE, config->_database); config->_database = NULL; } if (config->_username != NULL) { TRI_FreeString(TRI_CORE_MEM_ZONE, config->_username); config->_username = NULL; } if (config->_password != NULL) { TRI_FreeString(TRI_CORE_MEM_ZONE, config->_password); config->_password = NULL; } // read the endpoint value = TRI_LookupArrayJson(json, "endpoint"); if (! TRI_IsStringJson(value)) { res = TRI_ERROR_REPLICATION_INVALID_APPLIER_CONFIGURATION; } else { config->_endpoint = TRI_DuplicateString2Z(TRI_CORE_MEM_ZONE, value->_value._string.data, value->_value._string.length - 1); } // read the database name value = TRI_LookupArrayJson(json, "database"); if (! TRI_IsStringJson(value)) { config->_database = TRI_DuplicateStringZ(TRI_CORE_MEM_ZONE, vocbase->_name); } else { config->_database = TRI_DuplicateString2Z(TRI_CORE_MEM_ZONE, value->_value._string.data, value->_value._string.length - 1); } // read username / password value = TRI_LookupArrayJson(json, "username"); if (TRI_IsStringJson(value)) { config->_username = TRI_DuplicateString2Z(TRI_CORE_MEM_ZONE, value->_value._string.data, value->_value._string.length - 1); } value = TRI_LookupArrayJson(json, "password"); if (TRI_IsStringJson(value)) { config->_password = TRI_DuplicateString2Z(TRI_CORE_MEM_ZONE, value->_value._string.data, value->_value._string.length - 1); } value = TRI_LookupArrayJson(json, "requestTimeout"); if (TRI_IsNumberJson(value)) { config->_requestTimeout = value->_value._number; } value = TRI_LookupArrayJson(json, "connectTimeout"); if (TRI_IsNumberJson(value)) { config->_connectTimeout = value->_value._number; } value = TRI_LookupArrayJson(json, "maxConnectRetries"); if (TRI_IsNumberJson(value)) { config->_maxConnectRetries = (uint64_t) value->_value._number; } value = TRI_LookupArrayJson(json, "chunkSize"); if (TRI_IsNumberJson(value)) { config->_chunkSize = (uint64_t) value->_value._number; } value = TRI_LookupArrayJson(json, "autoStart"); if (TRI_IsBooleanJson(value)) { config->_autoStart = value->_value._boolean; } value = TRI_LookupArrayJson(json, "adaptivePolling"); if (TRI_IsBooleanJson(value)) { config->_adaptivePolling = value->_value._boolean; } TRI_FreeJson(TRI_CORE_MEM_ZONE, json); return res; }
static TRI_aql_node_t* OptimiseFcall (TRI_aql_context_t* const context, TRI_aql_node_t* node) { TRI_aql_node_t* args = TRI_AQL_NODE_MEMBER(node, 0); TRI_aql_function_t* function; TRI_js_exec_context_t* execContext; TRI_string_buffer_t* code; TRI_json_t* json; size_t i; size_t n; function = (TRI_aql_function_t*) TRI_AQL_NODE_DATA(node); assert(function); // check if function is deterministic if (!function->_isDeterministic) { return node; } // check if function call arguments are deterministic n = args->_members._length; for (i = 0; i < n; ++i) { TRI_aql_node_t* arg = (TRI_aql_node_t*) args->_members._buffer[i]; if (!arg || !TRI_IsConstantValueNodeAql(arg)) { return node; } } // all arguments are constants // create the function code code = FcallCode(function->_internalName, args); if (!code) { TRI_SetErrorContextAql(context, TRI_ERROR_OUT_OF_MEMORY, NULL); return node; } // execute the function code execContext = TRI_CreateExecutionContext(code->_buffer); TRI_FreeStringBuffer(TRI_UNKNOWN_MEM_ZONE, code); if (!execContext) { TRI_SetErrorContextAql(context, TRI_ERROR_OUT_OF_MEMORY, NULL); return node; } json = TRI_ExecuteResultContext(execContext); TRI_FreeExecutionContext(execContext); if (!json) { // cannot optimise the function call due to an internal error // TODO: check whether we can validate the arguments here already and return an error early // TRI_SetErrorContextAql(context, TRI_ERROR_QUERY_SCRIPT, "function optimisation"); return node; } // use the constant values instead of the function call node node = TRI_JsonNodeAql(context, json); if (!node) { TRI_SetErrorContextAql(context, TRI_ERROR_OUT_OF_MEMORY, NULL); } TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, json); LOG_TRACE("optimised function call"); return node; }