Rapid Replace implementation. Replaces parts of the database with the specified data instance. @param pre PRE value of the node to be replaced @param source clip with source data @return success flag
(final int pre, final DataClip source)
| 579 | * @return success flag |
| 580 | */ |
| 581 | public final boolean replace(final int pre, final DataClip source) { |
| 582 | final int sCount = source.size(); |
| 583 | if(sCount == 0 || !bufferSize(sCount)) return false; |
| 584 | |
| 585 | meta.update(); |
| 586 | |
| 587 | // update index structures |
| 588 | final int tKind = kind(pre), tSize = size(pre, tKind), tPar = parent(pre, tKind); |
| 589 | indexDelete(pre, id(pre), tSize); |
| 590 | |
| 591 | final Data sData = source.data; |
| 592 | int sTopPre = source.start; |
| 593 | for(int sPre = source.start; sPre < source.end; ++sPre) { |
| 594 | // properties of the source node |
| 595 | final int sKind = sData.kind(sPre); |
| 596 | final int sSize = sData.size(sPre, sKind); |
| 597 | final int sPar = sData.parent(sPre, sKind); |
| 598 | final int cPre = pre + sPre - source.start; |
| 599 | |
| 600 | // calculate new distance value |
| 601 | final int cDist; |
| 602 | if(sPre == sTopPre) { |
| 603 | // handle top level entry: calculate distance based on target database |
| 604 | cDist = cPre - tPar; |
| 605 | // calculate PRE value of next top level entry |
| 606 | sTopPre += sSize; |
| 607 | } else { |
| 608 | cDist = sPre - sPar; |
| 609 | } |
| 610 | |
| 611 | switch(sKind) { |
| 612 | case DOC: |
| 613 | // add document |
| 614 | doc(sSize, sData.text(sPre, true)); |
| 615 | ++meta.ndocs; |
| 616 | break; |
| 617 | case ELEM: |
| 618 | // add element |
| 619 | final byte[] en = sData.name(sPre, sKind); |
| 620 | elem(cDist, elemNames.put(en), sData.attSize(sPre, sKind), sSize, |
| 621 | nspaces.uriIdForPrefix(prefix(en), true), false); |
| 622 | break; |
| 623 | case TEXT: |
| 624 | case COMM: |
| 625 | case PI: |
| 626 | // add text |
| 627 | text(cDist, sData.text(sPre, true), sKind); |
| 628 | break; |
| 629 | case ATTR: |
| 630 | // add attribute |
| 631 | final byte[] an = sData.name(sPre, sKind); |
| 632 | attr(cDist, attrNames.put(an), sData.text(sPre, false), |
| 633 | nspaces.uriIdForPrefix(prefix(an), false)); |
| 634 | break; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | // replace table entries, reset buffer size |
no test coverage detected