1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
|
// update.cpp
/**
* Copyright (C) 2008 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
#include "query.h"
#include "pdfile.h"
#include "jsobjmanipulator.h"
#include "queryoptimizer.h"
#include "repl.h"
#include "update.h"
//#define DEBUGUPDATE(x) cout << x << endl;
#define DEBUGUPDATE(x)
namespace mongo {
const char* Mod::modNames[] = { "$inc", "$set", "$push", "$pushAll", "$pull", "$pullAll" , "$pop", "$unset" ,
"$bitand" , "$bitor" , "$bit" , "$addToSet" };
unsigned Mod::modNamesNum = sizeof(Mod::modNames)/sizeof(char*);
bool Mod::_pullElementMatch( BSONElement& toMatch ) const {
if ( elt.type() != Object ){
// if elt isn't an object, then comparison will work
return toMatch.woCompare( elt , false ) == 0;
}
if ( toMatch.type() != Object ){
// looking for an object, so this can't match
return false;
}
// now we have an object on both sides
return matcher->matches( toMatch.embeddedObject() );
}
template< class Builder >
void Mod::appendIncremented( Builder& bb , const BSONElement& in, ModState& ms ) const {
BSONType a = in.type();
BSONType b = elt.type();
if ( a == NumberDouble || b == NumberDouble ){
ms.incType = NumberDouble;
ms.incdouble = elt.numberDouble() + in.numberDouble();
}
else if ( a == NumberLong || b == NumberLong ){
ms.incType = NumberLong;
ms.inclong = elt.numberLong() + in.numberLong();
}
else {
ms.incType = NumberInt;
ms.incint = elt.numberInt() + in.numberInt();
}
ms.appendIncValue( bb , false );
}
template< class Builder >
void appendUnset( Builder &b ) {
}
template<>
void appendUnset( BSONArrayBuilder &b ) {
b.appendNull();
}
template< class Builder >
void Mod::apply( Builder& b , BSONElement in , ModState& ms ) const {
switch ( op ){
case INC: {
appendIncremented( b , in , ms );
break;
}
case SET: {
_checkForAppending( elt );
b.appendAs( elt , shortFieldName );
break;
}
case UNSET: {
appendUnset( b );
break;
}
case PUSH: {
uassert( 10131 , "$push can only be applied to an array" , in.type() == Array );
BSONObjBuilder bb( b.subarrayStart( shortFieldName ) );
BSONObjIterator i( in.embeddedObject() );
int n=0;
while ( i.more() ){
bb.append( i.next() );
n++;
}
ms.pushStartSize = n;
bb.appendAs( elt , bb.numStr( n ) );
bb.done();
break;
}
case ADDTOSET: {
uassert( 12592 , "$addToSet can only be applied to an array" , in.type() == Array );
BSONObjBuilder bb( b.subarrayStart( shortFieldName ) );
BSONObjIterator i( in.embeddedObject() );
int n=0;
if ( isEach() ){
BSONElementSet toadd;
parseEach( toadd );
while ( i.more() ){
BSONElement cur = i.next();
bb.append( cur );
n++;
toadd.erase( cur );
}
for ( BSONElementSet::iterator j=toadd.begin(); j!=toadd.end(); j++ ){
bb.appendAs( *j , BSONObjBuilder::numStr( n++ ) );
}
}
else {
bool found = false;
while ( i.more() ){
BSONElement cur = i.next();
bb.append( cur );
n++;
if ( elt.woCompare( cur , false ) == 0 )
found = true;
}
if ( ! found )
bb.appendAs( elt , bb.numStr( n ) );
}
bb.done();
break;
}
case PUSH_ALL: {
uassert( 10132 , "$pushAll can only be applied to an array" , in.type() == Array );
uassert( 10133 , "$pushAll has to be passed an array" , elt.type() );
BSONObjBuilder bb( b.subarrayStart( shortFieldName ) );
BSONObjIterator i( in.embeddedObject() );
int n=0;
while ( i.more() ){
bb.append( i.next() );
n++;
}
ms.pushStartSize = n;
i = BSONObjIterator( elt.embeddedObject() );
while ( i.more() ){
bb.appendAs( i.next() , bb.numStr( n++ ) );
}
bb.done();
break;
}
case PULL:
case PULL_ALL: {
uassert( 10134 , "$pull/$pullAll can only be applied to an array" , in.type() == Array );
BSONObjBuilder bb( b.subarrayStart( shortFieldName ) );
int n = 0;
BSONObjIterator i( in.embeddedObject() );
while ( i.more() ){
BSONElement e = i.next();
bool allowed = true;
if ( op == PULL ){
allowed = ! _pullElementMatch( e );
}
else {
BSONObjIterator j( elt.embeddedObject() );
while( j.more() ) {
BSONElement arrJ = j.next();
if ( e.woCompare( arrJ, false ) == 0 ){
allowed = false;
break;
}
}
}
if ( allowed )
bb.appendAs( e , bb.numStr( n++ ) );
}
bb.done();
break;
}
case POP: {
uassert( 10135 , "$pop can only be applied to an array" , in.type() == Array );
BSONObjBuilder bb( b.subarrayStart( shortFieldName ) );
int n = 0;
BSONObjIterator i( in.embeddedObject() );
if ( elt.isNumber() && elt.number() < 0 ){
// pop from front
if ( i.more() ){
i.next();
n++;
}
while( i.more() ) {
bb.appendAs( i.next() , bb.numStr( n - 1 ).c_str() );
n++;
}
}
else {
// pop from back
while( i.more() ) {
n++;
BSONElement arrI = i.next();
if ( i.more() ){
bb.append( arrI );
}
}
}
ms.pushStartSize = n;
assert( ms.pushStartSize == in.embeddedObject().nFields() );
bb.done();
break;
}
case BIT: {
uassert( 10136 , "$bit needs an array" , elt.type() == Object );
uassert( 10137 , "$bit can only be applied to numbers" , in.isNumber() );
uassert( 10138 , "$bit can't use a double" , in.type() != NumberDouble );
int x = in.numberInt();
long long y = in.numberLong();
BSONObjIterator it( elt.embeddedObject() );
while ( it.more() ){
BSONElement e = it.next();
uassert( 10139 , "$bit field must be number" , e.isNumber() );
if ( strcmp( e.fieldName() , "and" ) == 0 ){
switch( in.type() ){
case NumberInt: x = x&e.numberInt(); break;
case NumberLong: y = y&e.numberLong(); break;
default: assert( 0 );
}
}
else if ( strcmp( e.fieldName() , "or" ) == 0 ){
switch( in.type() ){
case NumberInt: x = x|e.numberInt(); break;
case NumberLong: y = y|e.numberLong(); break;
default: assert( 0 );
}
}
else {
throw UserException( 9016, (string)"unknown bit mod:" + e.fieldName() );
}
}
switch( in.type() ){
case NumberInt: b.append( shortFieldName , x ); break;
case NumberLong: b.append( shortFieldName , y ); break;
default: assert( 0 );
}
break;
}
default:
stringstream ss;
ss << "Mod::apply can't handle type: " << op;
throw UserException( 9017, ss.str() );
}
}
auto_ptr<ModSetState> ModSet::prepare(const BSONObj &obj) const {
ModSetState * mss = new ModSetState( obj );
// Perform this check first, so that we don't leave a partially modified object on uassert.
for ( ModHolder::const_iterator i = _mods.begin(); i != _mods.end(); ++i ) {
DEBUGUPDATE( "\t\t prepare : " << i->first );
ModState& ms = mss->_mods[i->first];
const Mod& m = i->second;
BSONElement e = obj.getFieldDotted(m.fieldName);
ms.m = &m;
ms.old = e;
if ( e.eoo() ) {
mss->amIInPlacePossible( m.op == Mod::UNSET );
continue;
}
switch( m.op ) {
case Mod::INC:
uassert( 10140 , "Cannot apply $inc modifier to non-number", e.isNumber() || e.eoo() );
if ( mss->amIInPlacePossible( e.isNumber() ) ){
// check more typing info here
if ( m.elt.type() != e.type() ){
// if i'm incrememnting with a double, then the storage has to be a double
mss->amIInPlacePossible( m.elt.type() != NumberDouble );
}
}
break;
case Mod::SET:
mss->amIInPlacePossible( m.elt.type() == e.type() &&
m.elt.valuesize() == e.valuesize() );
break;
case Mod::PUSH:
case Mod::PUSH_ALL:
uassert( 10141 , "Cannot apply $push/$pushAll modifier to non-array", e.type() == Array || e.eoo() );
mss->amIInPlacePossible( false );
break;
case Mod::PULL:
case Mod::PULL_ALL: {
uassert( 10142 , "Cannot apply $pull/$pullAll modifier to non-array", e.type() == Array || e.eoo() );
BSONObjIterator i( e.embeddedObject() );
while( mss->_inPlacePossible && i.more() ) {
BSONElement arrI = i.next();
if ( m.op == Mod::PULL ) {
mss->amIInPlacePossible( ! m._pullElementMatch( arrI ) );
}
else if ( m.op == Mod::PULL_ALL ) {
BSONObjIterator j( m.elt.embeddedObject() );
while( mss->_inPlacePossible && j.moreWithEOO() ) {
BSONElement arrJ = j.next();
if ( arrJ.eoo() )
break;
mss->amIInPlacePossible( arrI.woCompare( arrJ, false ) );
}
}
}
break;
}
case Mod::POP: {
uassert( 10143 , "Cannot apply $pop modifier to non-array", e.type() == Array || e.eoo() );
mss->amIInPlacePossible( e.embeddedObject().isEmpty() );
break;
}
case Mod::ADDTOSET: {
uassert( 12591 , "Cannot apply $addToSet modifier to non-array", e.type() == Array || e.eoo() );
BSONObjIterator i( e.embeddedObject() );
if ( m.isEach() ){
BSONElementSet toadd;
m.parseEach( toadd );
while( i.more() ) {
BSONElement arrI = i.next();
toadd.erase( arrI );
}
mss->amIInPlacePossible( toadd.size() == 0 );
}
else {
bool found = false;
while( i.more() ) {
BSONElement arrI = i.next();
if ( arrI.woCompare( m.elt , false ) == 0 ){
found = true;
break;
}
}
mss->amIInPlacePossible( found );
}
break;
}
default:
// mods we don't know about shouldn't be done in place
mss->amIInPlacePossible( false );
}
}
return auto_ptr<ModSetState>( mss );
}
void ModSetState::applyModsInPlace() {
for ( ModStateHolder::iterator i = _mods.begin(); i != _mods.end(); ++i ) {
ModState& m = i->second;
switch ( m.m->op ){
case Mod::UNSET:
case Mod::PULL:
case Mod::PULL_ALL:
case Mod::ADDTOSET:
// this should have been handled by prepare
break;
// [dm] the BSONElementManipulator statements below are for replication (correct?)
case Mod::INC:
m.m->incrementMe( m.old );
m.fixedOpName = "$set";
m.fixed = &(m.old);
break;
case Mod::SET:
BSONElementManipulator( m.old ).replaceTypeAndValue( m.m->elt );
break;
default:
uassert( 10144 , "can't apply mod in place - shouldn't have gotten here" , 0 );
}
}
}
void ModSet::extractFields( map< string, BSONElement > &fields, const BSONElement &top, const string &base ) {
if ( top.type() != Object ) {
fields[ base + top.fieldName() ] = top;
return;
}
BSONObjIterator i( top.embeddedObject() );
bool empty = true;
while( i.moreWithEOO() ) {
BSONElement e = i.next();
if ( e.eoo() )
break;
extractFields( fields, e, base + top.fieldName() + "." );
empty = false;
}
if ( empty )
fields[ base + top.fieldName() ] = top;
}
template< class Builder >
void ModSetState::_appendNewFromMods( const string& root , ModState& m , Builder& b , set<string>& onedownseen ){
const char * temp = m.fieldName();
temp += root.size();
const char * dot = strchr( temp , '.' );
if ( dot ){
string nr( m.fieldName() , 0 , 1 + ( dot - m.fieldName() ) );
string nf( temp , 0 , dot - temp );
if ( onedownseen.count( nf ) )
return;
onedownseen.insert( nf );
BSONObjBuilder bb ( b.subobjStart( nf.c_str() ) );
createNewFromMods( nr , bb , BSONObj() ); // don't infer an array from name
bb.done();
}
else {
appendNewFromMod( m , b );
}
}
template< class Builder >
void ModSetState::createNewFromMods( const string& root , Builder& b , const BSONObj &obj ){
DEBUGUPDATE( "\t\t createNewFromMods root: " << root );
BSONObjIteratorSorted es( obj );
BSONElement e = es.next();
ModStateHolder::iterator m = _mods.lower_bound( root );
ModStateHolder::iterator mend = _mods.lower_bound( root + '{' );
set<string> onedownseen;
while ( e.type() && m != mend ){
string field = root + e.fieldName();
FieldCompareResult cmp = compareDottedFieldNames( m->second.m->fieldName , field );
DEBUGUPDATE( "\t\t\t" << field << "\t" << m->second.m->fieldName << "\t" << cmp );
switch ( cmp ){
case LEFT_SUBFIELD: { // Mod is embeddeed under this element
uassert( 10145 , "LEFT_SUBFIELD only supports Object" , e.type() == Object || e.type() == Array );
if ( onedownseen.count( e.fieldName() ) == 0 ){
onedownseen.insert( e.fieldName() );
if ( e.type() == Object ) {
BSONObjBuilder bb( b.subobjStart( e.fieldName() ) );
stringstream nr; nr << root << e.fieldName() << ".";
createNewFromMods( nr.str() , bb , e.embeddedObject() );
bb.done();
} else {
BSONArrayBuilder ba( b.subarrayStart( e.fieldName() ) );
stringstream nr; nr << root << e.fieldName() << ".";
createNewFromMods( nr.str() , ba , e.embeddedObject() );
ba.done();
}
// inc both as we handled both
e = es.next();
m++;
}
continue;
}
case LEFT_BEFORE: // Mod on a field that doesn't exist
_appendNewFromMods( root , m->second , b , onedownseen );
m++;
continue;
case SAME:
m->second.apply( b , e );
e = es.next();
m++;
continue;
case RIGHT_BEFORE: // field that doesn't have a MOD
b.append( e ); // if array, ignore field name
e = es.next();
continue;
case RIGHT_SUBFIELD:
massert( 10399 , "ModSet::createNewFromMods - RIGHT_SUBFIELD should be impossible" , 0 );
break;
default:
massert( 10400 , "unhandled case" , 0 );
}
}
// finished looping the mods, just adding the rest of the elements
while ( e.type() ){
b.append( e ); // if array, ignore field name
e = es.next();
}
// do mods that don't have fields already
for ( ; m != mend; m++ ){
_appendNewFromMods( root , m->second , b , onedownseen );
}
}
BSONObj ModSetState::createNewFromMods() {
BSONObjBuilder b( (int)(_obj.objsize() * 1.1) );
createNewFromMods( "" , b , _obj );
return b.obj();
}
BSONObj ModSet::createNewFromQuery( const BSONObj& query ){
BSONObj newObj;
{
BSONObjBuilder bb;
EmbeddedBuilder eb( &bb );
BSONObjIteratorSorted i( query );
while ( i.more() ){
BSONElement e = i.next();
if ( e.type() == Object && e.embeddedObject().firstElement().fieldName()[0] == '$' ){
// this means this is a $gt type filter, so don't make part of the new object
continue;
}
eb.appendAs( e , e.fieldName() );
}
eb.done();
newObj = bb.obj();
}
auto_ptr<ModSetState> mss = prepare( newObj );
if ( mss->canApplyInPlace() )
mss->applyModsInPlace();
else
newObj = mss->createNewFromMods();
return newObj;
}
/* get special operations like $inc
{ $inc: { a:1, b:1 } }
{ $set: { a:77 } }
{ $push: { a:55 } }
{ $pushAll: { a:[77,88] } }
{ $pull: { a:66 } }
{ $pullAll : { a:[99,1010] } }
NOTE: MODIFIES source from object!
*/
ModSet::ModSet(
const BSONObj &from ,
const set<string>& idxKeys,
const set<string> *backgroundKeys)
: _isIndexed(0) , _hasDynamicArray( false ) {
BSONObjIterator it(from);
while ( it.more() ) {
BSONElement e = it.next();
const char *fn = e.fieldName();
uassert( 10147 , "Invalid modifier specified" + string( fn ), e.type() == Object );
BSONObj j = e.embeddedObject();
BSONObjIterator jt(j);
Mod::Op op = opFromStr( fn );
while ( jt.more() ) {
BSONElement f = jt.next(); // x:44
const char * fieldName = f.fieldName();
uassert( 10148 , "Mod on _id not allowed", strcmp( fieldName, "_id" ) != 0 );
uassert( 10149 , "Invalid mod field name, may not end in a period", fieldName[ strlen( fieldName ) - 1 ] != '.' );
uassert( 10150 , "Field name duplication not allowed with modifiers", ! haveModForField( fieldName ) );
uassert( 10151 , "have conflict mod" , ! haveConflictingMod( fieldName ) );
uassert( 10152 , "Modifier $inc allowed for numbers only", f.isNumber() || op != Mod::INC );
uassert( 10153 , "Modifier $pushAll/pullAll allowed for arrays only", f.type() == Array || ( op != Mod::PUSH_ALL && op != Mod::PULL_ALL ) );
_hasDynamicArray = _hasDynamicArray || strstr( fieldName , ".$" ) > 0;
Mod m;
m.init( op , f );
m.setFieldName( f.fieldName() );
if ( m.isIndexed( idxKeys ) ||
(backgroundKeys && m.isIndexed(*backgroundKeys)) ) {
_isIndexed++;
}
_mods[m.fieldName] = m;
DEBUGUPDATE( "\t\t " << fieldName << "\t" << _hasDynamicArray );
}
}
}
ModSet * ModSet::fixDynamicArray( const char * elemMatchKey ) const {
ModSet * n = new ModSet();
n->_isIndexed = _isIndexed;
n->_hasDynamicArray = _hasDynamicArray;
for ( ModHolder::const_iterator i=_mods.begin(); i!=_mods.end(); i++ ){
string s = i->first;
size_t idx = s.find( ".$" );
if ( idx == string::npos ){
n->_mods[s] = i->second;
continue;
}
StringBuilder buf(s.size()+strlen(elemMatchKey));
buf << s.substr(0,idx+1) << elemMatchKey << s.substr(idx+2);
string fixed = buf.str();
DEBUGUPDATE( "fixed dynamic: " << s << " -->> " << fixed );
n->_mods[fixed] = i->second;
ModHolder::iterator temp = n->_mods.find( fixed );
temp->second.setFieldName( temp->first.c_str() );
}
return n;
}
void checkNoMods( BSONObj o ) {
BSONObjIterator i( o );
while( i.moreWithEOO() ) {
BSONElement e = i.next();
if ( e.eoo() )
break;
uassert( 10154 , "Modifiers and non-modifiers cannot be mixed", e.fieldName()[ 0 ] != '$' );
}
}
class UpdateOp : public QueryOp {
public:
UpdateOp() : _nscanned() {}
virtual void init() {
BSONObj pattern = qp().query();
_c.reset( qp().newCursor().release() );
if ( ! _c->ok() )
setComplete();
else
_matcher.reset( new CoveredIndexMatcher( pattern, qp().indexKey() ) );
}
virtual void next() {
if ( ! _c->ok() ) {
setComplete();
return;
}
_nscanned++;
if ( _matcher->matches(_c->currKey(), _c->currLoc(), &_details ) ) {
setComplete();
return;
}
_c->advance();
}
bool curMatches(){
return _matcher->matches(_c->currKey(), _c->currLoc() , &_details );
}
virtual bool mayRecordPlan() const { return false; }
virtual QueryOp *clone() const {
return new UpdateOp();
}
shared_ptr< Cursor > c() { return _c; }
long long nscanned() const { return _nscanned; }
MatchDetails& getMatchDetails(){ return _details; }
private:
shared_ptr< Cursor > _c;
long long _nscanned;
auto_ptr< CoveredIndexMatcher > _matcher;
MatchDetails _details;
};
UpdateResult updateObjects(const char *ns, const BSONObj& updateobj, BSONObj patternOrig, bool upsert, bool multi, bool logop , OpDebug& debug ) {
DEBUGUPDATE( "update: " << ns << " update: " << updateobj << " query: " << patternOrig << " upsert: " << upsert << " multi: " << multi );
int profile = cc().database()->profile;
StringBuilder& ss = debug.str;
if ( logLevel > 2 )
ss << " update: " << updateobj;
/* idea with these here it to make them loop invariant for multi updates, and thus be a bit faster for that case */
/* NOTE: when yield() is added herein, these must be refreshed after each call to yield! */
NamespaceDetails *d = nsdetails(ns); // can be null if an upsert...
NamespaceDetailsTransient *nsdt = &NamespaceDetailsTransient::get_w(ns);
/* end note */
uassert( 10155 , "cannot update reserved $ collection", strchr(ns, '$') == 0 );
if ( strstr(ns, ".system.") ) {
/* dm: it's very important that system.indexes is never updated as IndexDetails has pointers into it */
uassert( 10156 , "cannot update system collection", legalClientSystemNS( ns , true ) );
}
auto_ptr<ModSet> mods;
bool isOperatorUpdate = updateobj.firstElement().fieldName()[0] == '$';
int modsIsIndexed = false; // really the # of indexes
if ( isOperatorUpdate ){
if( d && d->backgroundIndexBuildInProgress ) {
set<string> bgKeys;
d->backgroundIdx().keyPattern().getFieldNames(bgKeys);
mods.reset( new ModSet(updateobj, nsdt->indexKeys(), &bgKeys) );
}
else {
mods.reset( new ModSet(updateobj, nsdt->indexKeys()) );
}
modsIsIndexed = mods->isIndexed();
}
set<DiskLoc> seenObjects;
QueryPlanSet qps( ns, patternOrig, BSONObj() );
UpdateOp original;
shared_ptr< UpdateOp > u = qps.runOp( original );
massert( 10401 , u->exceptionMessage(), u->complete() );
shared_ptr< Cursor > c = u->c();
int numModded = 0;
while ( c->ok() ) {
if ( numModded > 0 && ! u->curMatches() ){
c->advance();
continue;
}
Record *r = c->_current();
DiskLoc loc = c->currLoc();
if ( c->getsetdup( loc ) ){
c->advance();
continue;
}
BSONObj js(r);
BSONObj pattern = patternOrig;
if ( logop ) {
BSONObjBuilder idPattern;
BSONElement id;
// NOTE: If the matching object lacks an id, we'll log
// with the original pattern. This isn't replay-safe.
// It might make sense to suppress the log instead
// if there's no id.
if ( js.getObjectID( id ) ) {
idPattern.append( id );
pattern = idPattern.obj();
}
else {
uassert( 10157 , "multi-update requires all modified objects to have an _id" , ! multi );
}
}
if ( profile )
ss << " nscanned:" << u->nscanned();
/* look for $inc etc. note as listed here, all fields to inc must be this type, you can't set some
regular ones at the moment. */
if ( isOperatorUpdate ) {
if ( multi ){
c->advance(); // go to next record in case this one moves
if ( seenObjects.count( loc ) )
continue;
}
const BSONObj& onDisk = loc.obj();
ModSet * useMods = mods.get();
bool forceRewrite = false;
auto_ptr<ModSet> mymodset;
if ( u->getMatchDetails().elemMatchKey && mods->hasDynamicArray() ){
useMods = mods->fixDynamicArray( u->getMatchDetails().elemMatchKey );
mymodset.reset( useMods );
forceRewrite = true;
}
auto_ptr<ModSetState> mss = useMods->prepare( onDisk );
bool indexHack = multi && ( modsIsIndexed || ! mss->canApplyInPlace() );
if ( indexHack )
c->noteLocation();
if ( modsIsIndexed <= 0 && mss->canApplyInPlace() ){
mss->applyModsInPlace();// const_cast<BSONObj&>(onDisk) );
if ( profile )
ss << " fastmod ";
if ( modsIsIndexed ){
seenObjects.insert( loc );
}
}
else {
BSONObj newObj = mss->createNewFromMods();
uassert( 12522 , "$ operator made object too large" , newObj.objsize() <= ( 4 * 1024 * 1024 ) );
DiskLoc newLoc = theDataFileMgr.updateRecord(ns, d, nsdt, r, loc , newObj.objdata(), newObj.objsize(), debug);
if ( newLoc != loc || modsIsIndexed ) {
// object moved, need to make sure we don' get again
seenObjects.insert( newLoc );
}
}
if ( logop ) {
DEV assert( mods->size() );
if ( mss->haveArrayDepMod() ) {
BSONObjBuilder patternBuilder;
patternBuilder.appendElements( pattern );
mss->appendSizeSpecForArrayDepMods( patternBuilder );
pattern = patternBuilder.obj();
}
if ( forceRewrite || mss->needOpLogRewrite() ){
DEBUGUPDATE( "\t rewrite update: " << mss->getOpLogRewrite() );
logOp("u", ns, mss->getOpLogRewrite() , &pattern );
}
else {
logOp("u", ns, updateobj, &pattern );
}
}
numModded++;
if ( ! multi )
break;
if ( indexHack )
c->checkLocation();
continue;
}
uassert( 10158 , "multi update only works with $ operators" , ! multi );
BSONElementManipulator::lookForTimestamps( updateobj );
checkNoMods( updateobj );
theDataFileMgr.updateRecord(ns, d, nsdt, r, loc , updateobj.objdata(), updateobj.objsize(), debug);
if ( logop )
logOp("u", ns, updateobj, &pattern );
return UpdateResult( 1 , 0 , 1 );
}
if ( numModded )
return UpdateResult( 1 , 1 , numModded );
if ( profile )
ss << " nscanned:" << u->nscanned();
if ( upsert ) {
if ( updateobj.firstElement().fieldName()[0] == '$' ) {
/* upsert of an $inc. build a default */
BSONObj newObj = mods->createNewFromQuery( patternOrig );
if ( profile )
ss << " fastmodinsert ";
theDataFileMgr.insert(ns, newObj);
if ( profile )
ss << " fastmodinsert ";
if ( logop )
logOp( "i", ns, newObj );
return UpdateResult( 0 , 1 , 1 );
}
uassert( 10159 , "multi update only works with $ operators" , ! multi );
checkNoMods( updateobj );
if ( profile )
ss << " upsert ";
BSONObj no = updateobj;
theDataFileMgr.insert(ns, no);
if ( logop )
logOp( "i", ns, no );
return UpdateResult( 0 , 0 , 1 );
}
return UpdateResult( 0 , 0 , 0 );
}
}
|