Position DocumentStorage::findField(StringData requested) const { int reqSize = requested.size(); // get size calculation out of the way if needed if (_numFields >= HASH_TAB_MIN) { // hash lookup const unsigned bucket = bucketForKey(requested); Position pos = _hashTab[bucket]; while (pos.found()) { const ValueElement& elem = getField(pos); if (elem.nameLen == reqSize && memcmp(requested.rawData(), elem._name, reqSize) == 0) { return pos; } // possible collision pos = elem.nextCollision; } } else { // linear scan for (DocumentStorageIterator it = iteratorAll(); !it.atEnd(); it.advance()) { if (it->nameLen == reqSize && memcmp(requested.rawData(), it->_name, reqSize) == 0) { return it.position(); } } } // if we got here, there's no such field return Position(); }
void Document::hash_combine(size_t &seed) const { for (DocumentStorageIterator it = storage().iterator(); !it.atEnd(); it.advance()) { StringData name = it->nameSD(); boost::hash_range(seed, name.rawData(), name.rawData() + name.size()); it->val.hash_combine(seed); } }
// static Status WiredTigerUtil::checkTableCreationOptions(const BSONElement& configElem) { invariant(configElem.fieldNameStringData() == "configString"); if (configElem.type() != String) { return {ErrorCodes::TypeMismatch, "'configString' must be a string."}; } std::vector<std::string> errors; ErrorAccumulator eventHandler(&errors); StringData config = configElem.valueStringData(); // Do NOT allow embedded null characters if (config.size() != strlen(config.rawData())) { return {ErrorCodes::FailedToParse, "malformed 'configString' value."}; } Status status = wtRCToStatus( wiredtiger_config_validate(nullptr, &eventHandler, "WT_SESSION.create", config.rawData())); if (!status.isOK()) { StringBuilder errorMsg; errorMsg << status.reason(); for (std::string error : errors) { errorMsg << ". " << error; } errorMsg << "."; return status.withReason(errorMsg.stringData()); } return Status::OK(); }
void Logstream::logLockless( const StringData& s ) { if ( s.size() == 0 ) return; if ( doneSetup == 1717 ) { #if defined(_WIN32) int fd = fileno( logfile ); if ( _isatty( fd ) ) { fflush( logfile ); writeUtf8ToWindowsConsole( s.rawData(), s.size() ); return; } #else if ( isSyslog ) { syslog( LOG_INFO , "%s" , s.rawData() ); return; } #endif if (fwrite(s.rawData(), s.size(), 1, logfile)) { fflush(logfile); } else { int x = errno; cout << "Failed to write to logfile: " << errnoWithDescription(x) << endl; } } else { cout << s; cout.flush(); } }
std::string createPasswordDigest(const StringData& username, const StringData& clearTextPassword) { md5digest d; { md5_state_t st; md5_init(&st); md5_append(&st, (const md5_byte_t*)username.rawData(), username.size()); md5_append(&st, (const md5_byte_t*)":mongo:", 7); md5_append(&st, (const md5_byte_t*)clearTextPassword.rawData(), clearTextPassword.size()); md5_finish(&st, d); } return digestToString(d); }
int versionCmp(const StringData rhs, const StringData lhs) { if (rhs == lhs) return 0; // handle "1.2.3-" and "1.2.3-pre" if (rhs.size() < lhs.size()) { if (strncmp(rhs.rawData(), lhs.rawData(), rhs.size()) == 0 && lhs[rhs.size()] == '-') return +1; } else if (rhs.size() > lhs.size()) { if (strncmp(rhs.rawData(), lhs.rawData(), lhs.size()) == 0 && rhs[lhs.size()] == '-') return -1; } return LexNumCmp::cmp(rhs, lhs, false); }
void DataFileMgr::deleteRecord(NamespaceDetails* d, const StringData& ns, Record *todelete, const DiskLoc& dl, bool cappedOK, bool noWarn, bool doLog ) { dassert( todelete == dl.rec() ); if ( d->isCapped() && !cappedOK ) { out() << "failing remove on a capped ns " << ns << endl; uassert( 10089 , "can't remove from a capped collection" , 0 ); return; } BSONObj obj = BSONObj::make( todelete ); Collection* collection = cc().database()->getCollection( ns ); verify( collection ); BSONObj toDelete; collection->deleteDocument( dl, cappedOK, noWarn, doLog ? &toDelete : NULL ); if ( ! toDelete.isEmpty() ) { // TODO: this is crazy, need to fix logOp const char* raw = ns.rawData(); if ( strlen(raw) == ns.size() ) { logOp( "d", raw, toDelete ); } else { string temp = ns.toString(); logOp( "d", temp.c_str(), toDelete ); } } }
/** * scratch must be empty when passed in. It will be used if there is a NUL byte in the * output string. In that case the returned StringData will point into scratch, otherwise * it will point directly into the input buffer. */ StringData readCStringWithNuls(BufReader* reader, std::string* scratch) { const StringData initial = readCString(reader); if (reader->peek<unsigned char>() != 0xFF) return initial; // Don't alloc or copy for simple case with no NUL bytes. scratch->append(initial.rawData(), initial.size()); while (reader->peek<unsigned char>() == 0xFF) { // Each time we enter this loop it means we hit a NUL byte encoded as "\x00\xFF". *scratch += '\0'; reader->skip(1); const StringData nextPart = readCString(reader); scratch->append(nextPart.rawData(), nextPart.size()); } return *scratch; }
/* resets the client for the current thread */ void Client::resetThread( const StringData& origThreadName ) { verify( currentClient.get() != 0 ); // Detach all client info from thread mongo::lastError.reset(NULL); currentClient.get()->shutdown(); currentClient.reset(NULL); setThreadName( origThreadName.rawData() ); }
void FieldRef::reserialize() const { std::string nextDotted; // Reserve some space in the string. We know we will have, at minimum, a character for // each component we are writing, and a dot for each component, less one. We don't want // to reserve more, since we don't want to forfeit the SSO if it is applicable. nextDotted.reserve((_size > 0) ? (_size * 2) - 1 : 0); // Concatenate the fields to a new string for (size_t i = 0; i != _size; ++i) { if (i > 0) nextDotted.append(1, '.'); const StringData part = getPart(i); nextDotted.append(part.rawData(), part.size()); } // Make the new string our contents _dotted.swap(nextDotted); // Before we reserialize, it's possible that _cachedSize != _size because parts were added or // removed. This reserialization process reconciles the components in our cached string // (_dotted) with the modified path. _cachedSize = _size; // Fixup the parts to refer to the new string std::string::const_iterator where = _dotted.begin(); const std::string::const_iterator end = _dotted.end(); for (size_t i = 0; i != _size; ++i) { boost::optional<StringView>& part = (i < kReserveAhead) ? _fixed[i] : _variable[getIndex(i)]; const size_t size = part ? part->len : _replacements[i].size(); // There is one case where we expect to see the "where" iterator to be at "end" here: we // are at the last part of the FieldRef and that part is the empty string. In that case, we // need to make sure we do not dereference the "where" iterator. invariant(where != end || (size == 0 && i == _size - 1)); if (!size) { part = StringView{}; } else { std::size_t offset = where - _dotted.begin(); part = StringView{offset, size}; } where += size; // skip over '.' unless we are at the end. if (where != end) { dassert(*where == '.'); ++where; } } // Drop any replacements _replacements.clear(); }
// Iterate the hash function to generate SaltedPassword void generateSaltedPassword(StringData hashedPassword, const unsigned char* salt, const int saltLen, const int iterationCount, unsigned char saltedPassword[hashSize]) { // saltedPassword = Hi(hashedPassword, salt) HMACIteration(reinterpret_cast<const unsigned char*>(hashedPassword.rawData()), hashedPassword.size(), salt, saltLen, iterationCount, saltedPassword); }
std::string FieldRef::dottedField() const { std::string res; if (_size == 0) { return res; } res.append(_fixed[0].rawData(), _fixed[0].size()); for (size_t i=1; i<_size; i++) { res.append(1, '.'); StringData part = getPart(i); res.append(part.rawData(), part.size()); } return res; }
string Stemmer::stem( const StringData& word ) const { if ( !_stemmer ) return word.toString(); const sb_symbol* sb_sym = sb_stemmer_stem( _stemmer, (const sb_symbol*)word.rawData(), word.size() ); if ( sb_sym == NULL ) { // out of memory abort(); } return string( (const char*)(sb_sym), sb_stemmer_length( _stemmer ) ); }
bool MobileKVEngine::hasIdent(OperationContext* opCtx, StringData ident) const { MobileSession* session = MobileRecoveryUnit::get(opCtx)->getSession(opCtx); std::string findTableQuery = "SELECT * FROM sqlite_master WHERE type='table' AND name = ?;"; SqliteStatement findTableStmt(*session, findTableQuery); findTableStmt.bindText(0, ident.rawData(), ident.size()); int status = findTableStmt.step(); if (status == SQLITE_DONE) { return false; } checkStatus(status, SQLITE_ROW, "sqlite3_step"); return true; }
std::string FieldRef::dottedField( size_t offset ) const { std::string res; if (_size == 0 || offset >= numParts() ) { return res; } for (size_t i=offset; i<_size; i++) { if ( i > offset ) res.append(1, '.'); StringData part = getPart(i); res.append(part.rawData(), part.size()); } return res; }
void FTSSpec::_scoreStringV2(FTSTokenizer* tokenizer, StringData raw, TermFrequencyMap* docScores, double weight) const { ScoreHelperMap terms; unsigned numTokens = 0; tokenizer->reset(raw.rawData(), FTSTokenizer::kFilterStopWords); while (tokenizer->moveNext()) { StringData term = tokenizer->get(); ScoreHelperStruct& data = terms[term]; if (data.exp) { data.exp *= 2; } else { data.exp = 1; } data.count += 1; data.freq += (1 / data.exp); numTokens++; } for (ScoreHelperMap::const_iterator i = terms.begin(); i != terms.end(); ++i) { const string& term = i->first; const ScoreHelperStruct& data = i->second; // in order to adjust weights as a function of term count as it // relates to total field length. ie. is this the only word or // a frequently occuring term? or does it only show up once in // a long block of text? double coeff = (0.5 * data.count / numTokens) + 0.5; // if term is identical to the raw form of the // field (untokenized) give it a small boost. double adjustment = 1; if (raw.size() == term.length() && raw.equalCaseInsensitive(term)) adjustment += 0.1; double& score = (*docScores)[term]; score += (weight * data.freq * coeff * adjustment); verify(score <= MAX_WEIGHT); } }
void DataFileMgr::deleteRecord(NamespaceDetails* d, const StringData& ns, Record *todelete, const DiskLoc& dl, bool cappedOK, bool noWarn, bool doLog ) { dassert( todelete == dl.rec() ); if ( d->isCapped() && !cappedOK ) { out() << "failing remove on a capped ns " << ns << endl; uassert( 10089 , "can't remove from a capped collection" , 0 ); return; } BSONObj obj = BSONObj::make( todelete ); BSONObj toDelete; if ( doLog ) { BSONElement e = obj["_id"]; if ( e.type() ) { toDelete = e.wrap(); } } Collection* collection = cc().database()->getCollection( ns ); verify( collection ); /* check if any cursors point to us. if so, advance them. */ ClientCursor::aboutToDelete(ns, d, dl); collection->getIndexCatalog()->unindexRecord( obj, dl, noWarn ); _deleteRecord(d, ns, todelete, dl); collection->infoCache()->notifyOfWriteOp(); if ( ! toDelete.isEmpty() ) { // TODO: this is crazy, need to fix logOp const char* raw = ns.rawData(); if ( strlen(raw) == ns.size() ) { logOp( "d", raw, toDelete ); } else { string temp = ns.toString(); logOp( "d", temp.c_str(), toDelete ); } } }
Status DataType::Handler<StringData>::store(const StringData& sdata, char* ptr, size_t length, size_t* advanced, std::ptrdiff_t debug_offset) { if (sdata.size() > length) { return makeStoreStatus(sdata, length, debug_offset); } if (ptr) { std::memcpy(ptr, sdata.rawData(), sdata.size()); } if (advanced) { *advanced = sdata.size(); } return Status::OK(); }
bool MozJSImplScope::exec(StringData code, const std::string& name, bool printResult, bool reportError, bool assertOnError, int timeoutMs) { MozJSEntry entry(this); JS::CompileOptions co(_context); setCompileOptions(&co); co.setFile(name.c_str()); JS::RootedScript script(_context); bool success = JS::Compile(_context, _global, co, code.rawData(), code.size(), &script); if (_checkErrorState(success, reportError, assertOnError)) return false; if (timeoutMs) _engine->getDeadlineMonitor().startDeadline(this, timeoutMs); JS::RootedValue out(_context); success = JS_ExecuteScript(_context, _global, script, &out); if (timeoutMs) _engine->getDeadlineMonitor().stopDeadline(this); if (_checkErrorState(success, reportError, assertOnError)) return false; ObjectWrapper(_context, _global).setValue(kExecResult, out); if (printResult && !out.isUndefined()) { // appears to only be used by shell std::cout << ValueWriter(_context, out).toString() << std::endl; } return true; }
// static bool ParsedProjection::_hasPositionalOperatorMatch(const MatchExpression* const query, const std::string& matchfield) { if (query->isLogical()) { for (unsigned int i = 0; i < query->numChildren(); ++i) { if (_hasPositionalOperatorMatch(query->getChild(i), matchfield)) { return true; } } } else { StringData queryPath = query->path(); const char* pathRawData = queryPath.rawData(); // We have to make a distinction between match expressions that are // initialized with an empty field/path name "" and match expressions // for which the path is not meaningful (eg. $where and the internal // expression type ALWAYS_FALSE). if (!pathRawData) { return false; } std::string pathPrefix = mongoutils::str::before(pathRawData, '.'); return pathPrefix == matchfield; } return false; }
bool IdWrapper::equalsAscii(StringData sd) const { if (isString()) { auto str = JSID_TO_STRING(_value); if (!str) { uasserted(ErrorCodes::JSInterpreterFailure, "Failed to JSID_TO_STRING"); } bool matched; if (!JS_StringEqualsAscii(_context, str, sd.rawData(), &matched)) { uasserted(ErrorCodes::JSInterpreterFailure, "Failed to JS_StringEqualsAscii"); } return matched; } if (isInt()) { JSStringWrapper jsstr(toInt32()); return jsstr.toStringData().compare(sd) == 0; } uasserted(ErrorCodes::BadValue, "Cannot equalsAscii non-string non-integer jsid"); }
// static Status WiredTigerUtil::checkTableCreationOptions(const BSONElement& configElem) { invariant(configElem.fieldNameStringData() == "configString"); if (configElem.type() != String) { return {ErrorCodes::TypeMismatch, "'configString' must be a string."}; } std::vector<std::string> errors; ErrorAccumulator eventHandler(&errors); StringData config = configElem.valueStringData(); Status status = wtRCToStatus( wiredtiger_config_validate(nullptr, &eventHandler, "WT_SESSION.create", config.rawData())); if (!status.isOK()) { StringBuilder errorMsg; errorMsg << status.reason(); for (std::string error : errors) { errorMsg << ". " << error; } errorMsg << "."; return {status.code(), errorMsg.str()}; } return Status::OK(); }
void FieldRef::reserialize() const { std::string nextDotted; // Reserve some space in the string. We know we will have, at minimum, a character for // each component we are writing, and a dot for each component, less one. We don't want // to reserve more, since we don't want to forfeit the SSO if it is applicable. nextDotted.reserve((_size * 2) - 1); // Concatenate the fields to a new string for (size_t i = 0; i != _size; ++i) { if (i > 0) nextDotted.append(1, '.'); const StringData part = getPart(i); nextDotted.append(part.rawData(), part.size()); } // Make the new string our contents _dotted.swap(nextDotted); // Fixup the parts to refer to the new string std::string::const_iterator where = _dotted.begin(); const std::string::const_iterator end = _dotted.end(); for (size_t i = 0; i != _size; ++i) { StringData& part = (i < kReserveAhead) ? _fixed[i] : _variable[getIndex(i)]; const size_t size = part.size(); part = StringData(&*where, size); where += size; // skip over '.' unless we are at the end. if (where != end) { dassert(*where == '.'); ++where; } } // Drop any replacements _replacements.clear(); }
TEST(Construction, Empty) { StringData strData; ASSERT_EQUALS(strData.size(), 0U); ASSERT_TRUE(strData.rawData() == NULL); }
int LexNumCmp::cmp( const StringData& sd1, const StringData& sd2, bool lexOnly ) { bool startWord = true; size_t s1 = 0; size_t s2 = 0; while( s1 < sd1.size() && s2 < sd2.size() ) { bool d1 = ( sd1[s1] == '.' ); bool d2 = ( sd2[s2] == '.' ); if ( d1 && !d2 ) return -1; if ( d2 && !d1 ) return 1; if ( d1 && d2 ) { ++s1; ++s2; startWord = true; continue; } bool p1 = ( sd1[s1] == (char)255 ); bool p2 = ( sd2[s2] == (char)255 ); if ( p1 && !p2 ) return 1; if ( p2 && !p1 ) return -1; if ( !lexOnly ) { bool n1 = isNumber( sd1[s1] ); bool n2 = isNumber( sd2[s2] ); if ( n1 && n2 ) { // get rid of leading 0s if ( startWord ) { while ( sd1[s1] == '0' ) s1++; while ( sd2[s2] == '0' ) s2++; } size_t e1 = s1; size_t e2 = s2; // find length // if end of string, will break immediately ('\0') while ( isNumber( sd1[e1] ) ) e1++; while ( isNumber( sd2[e2] ) ) e2++; size_t len1 = e1-s1; size_t len2 = e2-s2; int result; // if one is longer than the other, return if ( len1 > len2 ) { return 1; } else if ( len2 > len1 ) { return -1; } // if the lengths are equal, just strcmp else { result = strncmp( sd1.rawData() + s1, sd2.rawData() + s2, len1 ); if ( result ) return result; } // otherwise, the numbers are equal s1 = e1; s2 = e2; startWord = false; continue; } if ( n1 ) return 1; if ( n2 ) return -1; } if ( sd1[s1] > sd2[s2] ) return 1; if ( sd2[s2] > sd1[s1] ) return -1; s1++; s2++; startWord = false; } if ( sd1[s1] ) return 1; if ( sd2[s2] ) return -1; return 0; }