TRI_doc_datafile_info_t* TRI_FindDatafileInfoPrimaryCollection (TRI_primary_collection_t* primary, TRI_voc_fid_t fid, bool create) { TRI_doc_datafile_info_t const* found; TRI_doc_datafile_info_t* dfi; found = TRI_LookupByKeyAssociativePointer(&primary->_datafileInfo, &fid); if (found != NULL) { return CONST_CAST(found); } if (! create) { return NULL; } // allocate and set to 0 dfi = TRI_Allocate(TRI_UNKNOWN_MEM_ZONE, sizeof(TRI_doc_datafile_info_t), true); if (dfi == NULL) { return NULL; } dfi->_fid = fid; TRI_InsertKeyAssociativePointer(&primary->_datafileInfo, &fid, dfi, true); return dfi; }
TRI_doc_datafile_info_t* TRI_FindDatafileInfoPrimaryCollection (TRI_primary_collection_t* primary, TRI_voc_fid_t fid) { TRI_doc_datafile_info_t const* found; TRI_doc_datafile_info_t* dfi; found = TRI_LookupByKeyAssociativePointer(&primary->_datafileInfo, &fid); if (found != NULL) { union { TRI_doc_datafile_info_t const* c; TRI_doc_datafile_info_t* v; } cnv; cnv.c = found; return cnv.v; } dfi = TRI_Allocate(TRI_UNKNOWN_MEM_ZONE, sizeof(TRI_doc_datafile_info_t), true); if (dfi == NULL) { return NULL; } dfi->_fid = fid; TRI_InsertKeyAssociativePointer(&primary->_datafileInfo, &fid, dfi, true); return dfi; }
bool TRI_VariableExistsScopeAql (TRI_aql_context_t* const context, const char* const name) { size_t n; if (name == NULL) { TRI_SetErrorContextAql(__FILE__, __LINE__, context, TRI_ERROR_OUT_OF_MEMORY, NULL); return false; } n = context->_currentScopes._length; assert(n > 0); while (n > 0) { TRI_aql_scope_t* scope = (TRI_aql_scope_t*) TRI_AtVectorPointer(&context->_currentScopes, --n); assert(scope); if (TRI_LookupByKeyAssociativePointer(&scope->_variables, (void*) name)) { // duplicate variable return true; } if (n == 0) { // reached the outermost scope break; } } return false; }
TRI_aql_function_t* TRI_GetFunctionAql (TRI_associative_pointer_t* functions, const char* const internalName) { TRI_aql_function_t* function; function = (TRI_aql_function_t*) TRI_LookupByKeyAssociativePointer(functions, (void*) internalName); assert(function); return function; }
char* TRI_GetMimetype (const char* extension) { mimetype_t* entry = static_cast<mimetype_t*>(TRI_LookupByKeyAssociativePointer(&Mimetypes, (void const*) extension)); if (entry == nullptr) { return nullptr; } return entry->_mimetype; }
static bool CalculateSize (TRI_df_marker_t const* marker, void* data, TRI_datafile_t* datafile, bool journal) { TRI_document_collection_t* document; TRI_primary_collection_t* primary; compaction_initial_context_t* context; TRI_voc_size_t alignedSize; context = data; document = context->_document; primary = &document->base; alignedSize = TRI_DF_ALIGN_BLOCK(marker->_size); // new or updated document if (marker->_type == TRI_DOC_MARKER_KEY_DOCUMENT || marker->_type == TRI_DOC_MARKER_KEY_EDGE) { TRI_doc_document_key_marker_t const* d; TRI_doc_mptr_t const* found; TRI_voc_key_t key; bool deleted; d = (TRI_doc_document_key_marker_t const*) marker; key = (char*) d + d->_offsetKey; // check if the document is still active TRI_READ_LOCK_DOCUMENTS_INDEXES_PRIMARY_COLLECTION(primary); found = TRI_LookupByKeyAssociativePointer(&primary->_primaryIndex, key); deleted = (found == NULL || found->_rid > d->_rid); TRI_READ_UNLOCK_DOCUMENTS_INDEXES_PRIMARY_COLLECTION(primary); if (deleted) { return true; } context->_keepDeletions = true; context->_targetSize += alignedSize; } else if (marker->_type == TRI_DOC_MARKER_KEY_DELETION && context->_keepDeletions) { context->_targetSize += alignedSize; } else if (marker->_type == TRI_DOC_MARKER_BEGIN_TRANSACTION || marker->_type == TRI_DOC_MARKER_COMMIT_TRANSACTION || marker->_type == TRI_DOC_MARKER_ABORT_TRANSACTION || marker->_type == TRI_DOC_MARKER_PREPARE_TRANSACTION) { context->_targetSize += alignedSize; } return true; }
void TRI_ReleaseGeneralCursor (TRI_general_cursor_t* cursor) { TRI_general_cursor_store_t* store = cursor->_store; TRI_LockSpin(&store->_lock); cursor = static_cast<TRI_general_cursor_t*>(TRI_LookupByKeyAssociativePointer(&store->_ids, &cursor->_id)); if (cursor != NULL) { --cursor->_usage._refCount; } TRI_UnlockSpin(&store->_lock); }
char* TRI_GetMimetype (const char* extension) { mimetype_t* entry; entry = TRI_LookupByKeyAssociativePointer(&Mimetypes, (void const*) extension); if (entry == NULL) { return NULL; } return entry->_mimetype; }
void TRI_MoveToBackLinkedArray (TRI_linked_array_t* array, void const* data) { TRI_linked_list_entry_t* found; found = TRI_LookupByKeyAssociativePointer(&array->_array, data); if (found) { if (found->_next != NULL) { TRI_RemoveLinkedList(&array->_list, found); TRI_AddLinkedList(&array->_list, found); } } }
bool TRI_ValidateBindParametersAql (TRI_aql_context_t* const context) { size_t i; size_t n; // iterate thru all parameter names used in the query n = context->_parameters._names._nrAlloc; for (i = 0; i < n; ++i) { char* name = (char*) context->_parameters._names._table[i]; if (!name) { continue; } if (!TRI_LookupByKeyAssociativePointer(&context->_parameters._values, name)) { TRI_SetErrorContextAql(context, TRI_ERROR_QUERY_BIND_PARAMETER_MISSING, name); return false; } } // iterate thru all parameters that we have values for 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); if (!TRI_LookupByKeyAssociativePointer(&context->_parameters._names, parameter->_name)) { TRI_SetErrorContextAql(context, TRI_ERROR_QUERY_BIND_PARAMETER_UNDECLARED, parameter->_name); return false; } } return true; }
TRI_general_cursor_t* TRI_FindGeneralCursor (TRI_vocbase_t* vocbase, TRI_voc_tick_t id) { TRI_general_cursor_store_t* store = vocbase->_cursors; TRI_LockSpin(&store->_lock); TRI_general_cursor_t* cursor = static_cast<TRI_general_cursor_t*>(TRI_LookupByKeyAssociativePointer(&store->_ids, &id)); if (cursor == NULL || cursor->_usage._isDeleted) { cursor = NULL; } TRI_UnlockSpin(&store->_lock); return cursor; }
static TRI_aql_node_t* InjectParameter (TRI_aql_statement_walker_t* const walker, TRI_aql_node_t* node) { TRI_aql_bind_parameter_t* bind; TRI_associative_pointer_t* bindValues; TRI_aql_context_t* context; char* name; if (node == NULL || node->_type != TRI_AQL_NODE_PARAMETER) { return node; } // we found a parameter node context = (TRI_aql_context_t*) walker->_data; assert(context); bindValues = (TRI_associative_pointer_t*) &context->_parameters._values; assert(bindValues); name = TRI_AQL_NODE_STRING(node); assert(name); bind = (TRI_aql_bind_parameter_t*) TRI_LookupByKeyAssociativePointer(bindValues, name); if (bind) { if (*name == '@') { // a collection name bind parameter if (TRI_IsStringJson(bind->_value)) { char* collectionName = TRI_RegisterStringAql(context, bind->_value->_value._string.data, bind->_value->_value._string.length - 1, false); node = TRI_CreateNodeCollectionAql(context, collectionName); } else { TRI_SetErrorContextAql(context, TRI_ERROR_QUERY_BIND_PARAMETER_TYPE, name); node = NULL; } } else { node = TRI_JsonNodeAql(context, bind->_value); } if (node == NULL) { TRI_SetErrorContextAql(context, TRI_ERROR_QUERY_BIND_PARAMETERS_INVALID, NULL); } } return node; }
void TRI_EndUsageDataShadowData (TRI_shadow_store_t* const store, const void* const data) { TRI_shadow_t* shadow; assert(store); TRI_LockMutex(&store->_lock); shadow = (TRI_shadow_t*) TRI_LookupByKeyAssociativePointer(&store->_pointers, data); if (shadow && ! shadow->_deleted) { DecreaseRefCount(store, shadow); // this might delete the shadow } TRI_UnlockMutex(&store->_lock); }
static TRI_shape_aid_t FindAttributeNameArrayShaper (TRI_shaper_t* shaper, char const* name) { array_shaper_t* s; void const* p; s = (array_shaper_t*) shaper; p = TRI_LookupByKeyAssociativePointer(&s->_attributeNames, name); if (p == NULL) { size_t n; attribute_2_id_t* a2i; void* f; int res; n = strlen(name) + 1; a2i = TRI_Allocate(shaper->_memoryZone, sizeof(attribute_2_id_t) + n, false); if (a2i == NULL) { return 0; } a2i->_aid = 1 + s->_attributes._length; a2i->_size = n; memcpy(((char*) a2i) + sizeof(attribute_2_id_t), name, n); f = TRI_InsertKeyAssociativePointer(&s->_attributeNames, name, a2i, false); if (f == NULL) { TRI_Free(shaper->_memoryZone, a2i); return 0; } res = TRI_PushBackVectorPointer(&s->_attributes, a2i); if (res != TRI_ERROR_NO_ERROR) { TRI_RemoveKeyAssociativePointer(&s->_attributeNames, name); TRI_Free(shaper->_memoryZone, a2i); return 0; } return a2i->_aid; } else { attribute_2_id_t const* a2i = (attribute_2_id_t const*) p; return a2i->_aid; } }
bool TRI_RemoveGeneralCursor (TRI_vocbase_t* vocbase, TRI_voc_tick_t id) { TRI_general_cursor_store_t* store = vocbase->_cursors; bool result; TRI_LockSpin(&store->_lock); TRI_general_cursor_t* cursor = static_cast<TRI_general_cursor_t*>(TRI_LookupByKeyAssociativePointer(&store->_ids, &id)); if (cursor == NULL || cursor->_usage._isDeleted) { result = false; } else { cursor->_usage._isDeleted = true; result = true; } TRI_UnlockSpin(&store->_lock); return result; }
bool TRI_DropGeneralCursor (TRI_general_cursor_t* cursor) { TRI_general_cursor_store_t* store = cursor->_store; bool result; TRI_LockSpin(&store->_lock); cursor = static_cast<TRI_general_cursor_t*>(TRI_LookupByKeyAssociativePointer(&store->_ids, &cursor->_id)); if (cursor != NULL && ! cursor->_usage._isDeleted) { cursor->_usage._isDeleted = true; result = true; } else { result = false; } TRI_UnlockSpin(&store->_lock); return result; }
TRI_general_cursor_t* TRI_UseGeneralCursor (TRI_general_cursor_t* cursor) { TRI_general_cursor_store_t* store = cursor->_store; TRI_LockSpin(&store->_lock); cursor = static_cast<TRI_general_cursor_t*>(TRI_LookupByKeyAssociativePointer(&store->_ids, &cursor->_id)); if (cursor != NULL) { if (cursor->_usage._isDeleted) { cursor = NULL; } else { ++cursor->_usage._refCount; } } TRI_UnlockSpin(&store->_lock); return cursor; }
void* TRI_BeginUsageIdShadowData (TRI_shadow_store_t* const store, const TRI_shadow_id id) { TRI_shadow_t* shadow; assert(store); TRI_LockMutex(&store->_lock); shadow = (TRI_shadow_t*) TRI_LookupByKeyAssociativePointer(&store->_ids, (void const*) &id); if (shadow && ! shadow->_deleted) { IncreaseRefCount(store, shadow); TRI_UnlockMutex(&store->_lock); return shadow->_data; } TRI_UnlockMutex(&store->_lock); return NULL; }
bool TRI_PersistDataShadowData (TRI_shadow_store_t* const store, const void* const data) { TRI_shadow_t* shadow; bool result = false; assert(store); TRI_LockMutex(&store->_lock); shadow = (TRI_shadow_t*) TRI_LookupByKeyAssociativePointer(&store->_pointers, data); if (shadow && ! shadow->_deleted) { PersistShadow(shadow); result = true; } TRI_UnlockMutex(&store->_lock); return result; }
bool TRI_DeleteIdShadowData (TRI_shadow_store_t* const store, const TRI_shadow_id id) { TRI_shadow_t* shadow; bool found = false; assert(store); TRI_LockMutex(&store->_lock); shadow = (TRI_shadow_t*) TRI_LookupByKeyAssociativePointer(&store->_ids, &id); if (shadow && ! shadow->_deleted) { DeleteShadow(store, shadow, false); found = true; } TRI_UnlockMutex(&store->_lock); return found; }
TRI_aql_function_t* TRI_GetByExternalNameFunctionAql (TRI_associative_pointer_t* functions, const char* const externalName) { TRI_aql_function_t* function; char* upperName; assert(functions); assert(externalName); // normalize the name by upper-casing it upperName = TRI_UpperAsciiString(externalName); if (!upperName) { return NULL; } function = (TRI_aql_function_t*) TRI_LookupByKeyAssociativePointer(functions, (void*) upperName); TRI_Free(TRI_CORE_MEM_ZONE, upperName); return function; }
TRI_aql_variable_t* TRI_GetVariableStatementWalkerAql (TRI_aql_statement_walker_t* const walker, const char* const name, size_t* scopeCount) { size_t n; // init scope counter to 0 *scopeCount = 0; assert(name != NULL); n = walker->_currentScopes._length; while (n > 0) { TRI_aql_scope_t* scope; TRI_aql_variable_t* variable; scope = (TRI_aql_scope_t*) TRI_AtVectorPointer(&walker->_currentScopes, --n); assert(scope); variable = TRI_LookupByKeyAssociativePointer(&scope->_variables, (void*) name); if (variable != NULL) { return variable; } if (n == 0 || scope->_type == TRI_AQL_SCOPE_SUBQUERY) { // reached the outermost scope if (scope->_type == TRI_AQL_SCOPE_SUBQUERY) { // variable not found but we reached the end of the scope // we must mark the scope as not self-contained so it is not moved to // some other position scope->_selfContained = false; } return NULL; } // increase the scope counter (*scopeCount)++; } // variable not found return NULL; }
TRI_shadow_id TRI_GetIdDataShadowData (TRI_shadow_store_t* const store, const void* const data) { TRI_shadow_t* shadow; TRI_shadow_id id = 0; assert(store); if (data) { TRI_LockMutex(&store->_lock); shadow = (TRI_shadow_t*) TRI_LookupByKeyAssociativePointer(&store->_pointers, data); if (shadow && ! shadow->_deleted) { id = shadow->_id; UpdateTimestampShadow(shadow); } TRI_UnlockMutex(&store->_lock); } return id; }
bool TRI_DeleteDataShadowData (TRI_shadow_store_t* const store, const void* const data) { bool found = false; assert(store); if (data) { TRI_shadow_t* shadow; TRI_LockMutex(&store->_lock); shadow = (TRI_shadow_t*) TRI_LookupByKeyAssociativePointer(&store->_pointers, data); if (shadow && ! shadow->_deleted) { DeleteShadow(store, shadow, true); found = true; } TRI_UnlockMutex(&store->_lock); } return found; }
void TRI_ConvertQueryJavascript (TRI_query_javascript_converter_t* converter, const TRI_query_node_t* const node, TRI_associative_pointer_t* bindParameters) { TRI_query_node_t* lhs; TRI_query_node_t* rhs; TRI_bind_parameter_t* parameter; char* escapedString; size_t outLength; assert(converter); assert(bindParameters); if (!node) { return; } lhs = node->_lhs; rhs = node->_rhs; switch (node->_type) { case TRI_QueryNodeValueUndefined: TRI_AppendStringStringBuffer(converter->_buffer, "undefined"); return; case TRI_QueryNodeValueNull: TRI_AppendStringStringBuffer(converter->_buffer, "null"); return; case TRI_QueryNodeValueBool: TRI_AppendStringStringBuffer(converter->_buffer, node->_value._boolValue ? "true" : "false"); return; case TRI_QueryNodeValueString: TRI_AppendCharStringBuffer(converter->_buffer, '"'); escapedString = TRI_EscapeUtf8String( node->_value._stringValue, strlen(node->_value._stringValue), false, &outLength ); if (escapedString) { TRI_AppendStringStringBuffer(converter->_buffer, escapedString); TRI_Free(escapedString); } TRI_AppendCharStringBuffer(converter->_buffer, '"'); return; case TRI_QueryNodeValueNumberInt: TRI_AppendInt64StringBuffer(converter->_buffer, node->_value._intValue); return; case TRI_QueryNodeValueNumberDouble: TRI_AppendDoubleStringBuffer(converter->_buffer, node->_value._doubleValue); return; case TRI_QueryNodeValueNumberDoubleString: TRI_AppendStringStringBuffer(converter->_buffer, node->_value._stringValue); return; case TRI_QueryNodeValueArray: TRI_AppendCharStringBuffer(converter->_buffer, '['); TRI_WalkListQueryJavascript(converter, rhs, bindParameters, ',', 0); TRI_AppendCharStringBuffer(converter->_buffer, ']'); return; case TRI_QueryNodeValueDocument: TRI_AppendCharStringBuffer(converter->_buffer, '{'); TRI_WalkListQueryJavascript(converter, rhs, bindParameters, ',', 0); TRI_AppendCharStringBuffer(converter->_buffer, '}'); return; case TRI_QueryNodeValueParameterNamed: parameter = (TRI_bind_parameter_t*) TRI_LookupByKeyAssociativePointer(bindParameters, node->_value._stringValue); assert(parameter); TRI_StringifyJson(converter->_buffer, parameter->_data); return; case TRI_QueryNodeValueIdentifier: TRI_AppendStringStringBuffer(converter->_buffer, node->_value._stringValue); return; case TRI_QueryNodeValueNamedValue: TRI_ConvertQueryJavascript(converter, lhs, bindParameters); TRI_AppendCharStringBuffer(converter->_buffer, ':'); TRI_ConvertQueryJavascript(converter, rhs, bindParameters); return; case TRI_QueryNodeReferenceCollectionAlias: if (!converter->_prefix) { TRI_AppendStringStringBuffer(converter->_buffer, "$['"); TRI_AppendStringStringBuffer(converter->_buffer, node->_value._stringValue); TRI_AppendStringStringBuffer(converter->_buffer, "']"); } else { TRI_AppendStringStringBuffer(converter->_buffer, "$['"); TRI_AppendStringStringBuffer(converter->_buffer, converter->_prefix); TRI_AppendStringStringBuffer(converter->_buffer, "']."); TRI_AppendStringStringBuffer(converter->_buffer, node->_value._stringValue); } return; case TRI_QueryNodeUnaryOperatorPlus: case TRI_QueryNodeUnaryOperatorMinus: case TRI_QueryNodeUnaryOperatorNot: TRI_AppendStringStringBuffer( converter->_buffer, TRI_QueryNodeGetUnaryOperatorString(node->_type) ); TRI_ConvertQueryJavascript(converter, lhs, bindParameters); return; case TRI_QueryNodeBinaryOperatorAnd: case TRI_QueryNodeBinaryOperatorOr: case TRI_QueryNodeBinaryOperatorIdentical: case TRI_QueryNodeBinaryOperatorUnidentical: case TRI_QueryNodeBinaryOperatorEqual: case TRI_QueryNodeBinaryOperatorUnequal: case TRI_QueryNodeBinaryOperatorLess: case TRI_QueryNodeBinaryOperatorGreater: case TRI_QueryNodeBinaryOperatorLessEqual: case TRI_QueryNodeBinaryOperatorGreaterEqual: case TRI_QueryNodeBinaryOperatorAdd: case TRI_QueryNodeBinaryOperatorSubtract: case TRI_QueryNodeBinaryOperatorMultiply: case TRI_QueryNodeBinaryOperatorDivide: case TRI_QueryNodeBinaryOperatorModulus: case TRI_QueryNodeBinaryOperatorIn: TRI_AppendCharStringBuffer(converter->_buffer, '('); TRI_ConvertQueryJavascript(converter, lhs, bindParameters); TRI_AppendStringStringBuffer( converter->_buffer, TRI_QueryNodeGetBinaryOperatorString(node->_type) ); TRI_ConvertQueryJavascript(converter, rhs, bindParameters); TRI_AppendCharStringBuffer(converter->_buffer, ')'); return; case TRI_QueryNodeContainerMemberAccess: TRI_ConvertQueryJavascript(converter, lhs, bindParameters); TRI_WalkListQueryJavascript(converter, rhs, bindParameters, '.', 1); return; case TRI_QueryNodeContainerTernarySwitch: TRI_ConvertQueryJavascript(converter, lhs, bindParameters); TRI_AppendCharStringBuffer(converter->_buffer, ':'); TRI_ConvertQueryJavascript(converter, rhs, bindParameters); return; case TRI_QueryNodeControlFunctionCall: TRI_ConvertQueryJavascript(converter, lhs, bindParameters); TRI_AppendCharStringBuffer(converter->_buffer, '('); TRI_WalkListQueryJavascript(converter, rhs, bindParameters, ',', 0); TRI_AppendCharStringBuffer(converter->_buffer, ')'); return; case TRI_QueryNodeControlTernary: TRI_AppendCharStringBuffer(converter->_buffer, '('); TRI_ConvertQueryJavascript(converter, lhs, bindParameters); TRI_AppendCharStringBuffer(converter->_buffer, '?'); TRI_ConvertQueryJavascript(converter, rhs, bindParameters); TRI_AppendCharStringBuffer(converter->_buffer, ')'); return; default: return; } }
static bool Compactifier (TRI_df_marker_t const* marker, void* data, TRI_datafile_t* datafile, bool journal) { TRI_df_marker_t* result; TRI_doc_mptr_t const* found; TRI_document_collection_t* document; TRI_primary_collection_t* primary; compaction_context_t* context; int res; context = data; document = context->_document; primary = &document->base; // new or updated document if (marker->_type == TRI_DOC_MARKER_KEY_DOCUMENT || marker->_type == TRI_DOC_MARKER_KEY_EDGE) { TRI_doc_document_key_marker_t const* d; TRI_doc_mptr_t* found2; TRI_voc_key_t key; bool deleted; d = (TRI_doc_document_key_marker_t const*) marker; key = (char*) d + d->_offsetKey; // check if the document is still active TRI_READ_LOCK_DOCUMENTS_INDEXES_PRIMARY_COLLECTION(primary); found = TRI_LookupByKeyAssociativePointer(&primary->_primaryIndex, key); deleted = (found == NULL || found->_rid > d->_rid); TRI_READ_UNLOCK_DOCUMENTS_INDEXES_PRIMARY_COLLECTION(primary); if (deleted) { LOG_TRACE("found a stale document: %s", key); return true; } context->_keepDeletions = true; // write to compactor files res = CopyMarker(document, context->_compactor, marker, &result); if (res != TRI_ERROR_NO_ERROR) { LOG_FATAL_AND_EXIT("cannot write compactor file: %s", TRI_last_error()); } // check if the document is still active TRI_WRITE_LOCK_DOCUMENTS_INDEXES_PRIMARY_COLLECTION(primary); found = TRI_LookupByKeyAssociativePointer(&primary->_primaryIndex, key); deleted = found == NULL; if (deleted) { context->_dfi._numberDead += 1; context->_dfi._sizeDead += (int64_t) marker->_size; TRI_WRITE_UNLOCK_DOCUMENTS_INDEXES_PRIMARY_COLLECTION(primary); LOG_DEBUG("found a stale document after copying: %s", key); return true; } found2 = CONST_CAST(found); assert(found2->_data != NULL); assert(((TRI_df_marker_t*) found2->_data)->_size > 0); // the fid might change if (found->_fid != context->_compactor->_fid) { // update old datafile's info TRI_doc_datafile_info_t* dfi = TRI_FindDatafileInfoPrimaryCollection(primary, found->_fid, false); if (dfi != NULL) { dfi->_numberDead += 1; dfi->_sizeDead += (int64_t) marker->_size; } found2->_fid = context->_compactor->_fid; } // let marker point to the new position found2->_data = result; // let _key point to the new key position found2->_key = ((char*) result) + (((TRI_doc_document_key_marker_t*) result)->_offsetKey); // update datafile info context->_dfi._numberAlive += 1; context->_dfi._sizeAlive += (int64_t) marker->_size; TRI_WRITE_UNLOCK_DOCUMENTS_INDEXES_PRIMARY_COLLECTION(primary); } // deletion else if (marker->_type == TRI_DOC_MARKER_KEY_DELETION && context->_keepDeletions) { // write to compactor files res = CopyMarker(document, context->_compactor, marker, &result); if (res != TRI_ERROR_NO_ERROR) { LOG_FATAL_AND_EXIT("cannot write compactor file: %s", TRI_last_error()); } // update datafile info context->_dfi._numberDeletion++; } else if (marker->_type == TRI_DOC_MARKER_BEGIN_TRANSACTION || marker->_type == TRI_DOC_MARKER_COMMIT_TRANSACTION || marker->_type == TRI_DOC_MARKER_ABORT_TRANSACTION || marker->_type == TRI_DOC_MARKER_PREPARE_TRANSACTION) { // write to compactor files res = CopyMarker(document, context->_compactor, marker, &result); if (res != TRI_ERROR_NO_ERROR) { LOG_FATAL_AND_EXIT("cannot write compactor file: %s", TRI_last_error()); } context->_dfi._numberTransaction++; context->_dfi._sizeTransaction += (int64_t) marker->_size; } return true; }
bool TRI_CheckAuthenticationAuthInfo (char const* username, char const* password) { TRI_vocbase_auth_t* auth; bool res; char* hex; char* sha256; size_t hexLen; size_t len; size_t sha256Len; assert(DefaultAuthInfo); // look up username TRI_ReadLockReadWriteLock(&DefaultAuthInfo->_authInfoLock); auth = TRI_LookupByKeyAssociativePointer(&DefaultAuthInfo->_authInfo, username); if (auth == NULL || ! auth->_active) { TRI_ReadUnlockReadWriteLock(&DefaultAuthInfo->_authInfoLock); return false; } // convert password res = false; if (TRI_IsPrefixString(auth->_password, "$1$")) { if (strlen(auth->_password) < 12 || auth->_password[11] != '$') { LOG_WARNING("found corrupted password for user '%s'", username); } else { char* salted; len = 8 + strlen(password); salted = TRI_Allocate(TRI_CORE_MEM_ZONE, len + 1, false); memcpy(salted, auth->_password + 3, 8); memcpy(salted + 8, password, len - 8); salted[len] = '\0'; sha256 = TRI_SHA256String(salted, len, &sha256Len); TRI_FreeString(TRI_CORE_MEM_ZONE, salted); hex = TRI_EncodeHexString(sha256, sha256Len, &hexLen); TRI_FreeString(TRI_CORE_MEM_ZONE, sha256); LOG_DEBUG("found active user '%s', expecting password '%s', got '%s'", username, auth->_password + 12, hex); res = TRI_EqualString(auth->_password + 12, hex); TRI_FreeString(TRI_CORE_MEM_ZONE, hex); } } else { len = strlen(password); sha256 = TRI_SHA256String(password, len, &sha256Len); hex = TRI_EncodeHexString(sha256, sha256Len, &hexLen); TRI_FreeString(TRI_CORE_MEM_ZONE, sha256); LOG_DEBUG("found active user '%s', expecting password '%s', got '%s'", username, auth->_password + 12, hex); res = TRI_EqualString(auth->_password, hex); TRI_FreeString(TRI_CORE_MEM_ZONE, hex); } TRI_ReadUnlockReadWriteLock(&DefaultAuthInfo->_authInfoLock); return res; }
static bool Compactifier (TRI_df_marker_t const* marker, void* data, TRI_datafile_t* datafile, bool journal) { union { TRI_doc_mptr_t const* c; TRI_doc_mptr_t* v; } cnv; TRI_df_marker_t* result; TRI_doc_datafile_info_t* dfi; TRI_doc_mptr_t const* found; TRI_sim_collection_t* sim; TRI_voc_fid_t fid; bool deleted; int res; sim = data; // new or updated document if (marker->_type == TRI_DOC_MARKER_DOCUMENT || marker->_type == TRI_DOC_MARKER_EDGE) { TRI_doc_document_marker_t const* d; size_t markerSize; if (marker->_type == TRI_DOC_MARKER_DOCUMENT) { markerSize = sizeof(TRI_doc_document_marker_t); } else if (marker->_type == TRI_DOC_MARKER_EDGE) { markerSize = sizeof(TRI_doc_edge_marker_t); } else { LOG_FATAL("unknown marker type %d", (int) marker->_type); exit(EXIT_FAILURE); } d = (TRI_doc_document_marker_t const*) marker; // check if the document is still active TRI_READ_LOCK_DOCUMENTS_INDEXES_SIM_COLLECTION(sim); found = TRI_LookupByKeyAssociativePointer(&sim->_primaryIndex, &d->_did); deleted = found == NULL || found->_deletion != 0; TRI_READ_UNLOCK_DOCUMENTS_INDEXES_SIM_COLLECTION(sim); if (deleted) { LOG_TRACE("found a stale document: %lu", d->_did); return true; } // write to compactor files res = CopyDocument(sim, marker, &result, &fid); if (res != TRI_ERROR_NO_ERROR) { LOG_FATAL("cannot write compactor file: ", TRI_last_error()); return false; } // check if the document is still active TRI_READ_LOCK_DOCUMENTS_INDEXES_SIM_COLLECTION(sim); found = TRI_LookupByKeyAssociativePointer(&sim->_primaryIndex, &d->_did); deleted = found == NULL || found->_deletion != 0; TRI_READ_UNLOCK_DOCUMENTS_INDEXES_SIM_COLLECTION(sim); // update datafile TRI_WRITE_LOCK_DATAFILES_SIM_COLLECTION(sim); dfi = TRI_FindDatafileInfoDocCollection(&sim->base, fid); if (deleted) { dfi->_numberDead += 1; dfi->_sizeDead += marker->_size - markerSize; LOG_DEBUG("found a stale document after copying: %lu", d->_did); TRI_WRITE_UNLOCK_DATAFILES_SIM_COLLECTION(sim); return true; } cnv.c = found; cnv.v->_fid = datafile->_fid; cnv.v->_data = result; cnv.v->_document._data.data = ((char*) cnv.v->_data) + markerSize; // update datafile info dfi->_numberAlive += 1; dfi->_sizeAlive += marker->_size - markerSize; TRI_WRITE_UNLOCK_DATAFILES_SIM_COLLECTION(sim); } // deletion else if (marker->_type == TRI_DOC_MARKER_DELETION) { // TODO: remove TRI_doc_deletion_marker_t from file // write to compactor files res = CopyDocument(sim, marker, &result, &fid); if (res != TRI_ERROR_NO_ERROR) { LOG_FATAL("cannot write compactor file: ", TRI_last_error()); return false; } // update datafile info TRI_WRITE_LOCK_DATAFILES_SIM_COLLECTION(sim); dfi = TRI_FindDatafileInfoDocCollection(&sim->base, fid); dfi->_numberDeletion += 1; TRI_WRITE_UNLOCK_DATAFILES_SIM_COLLECTION(sim); } return true; }