~juan457/+junk/zorba

« back to all changes in this revision

Viewing changes to src/store/naive/simple_pul.cpp

  • Committer: Tarmac
  • Author(s): Markos Zaharioudakis
  • Date: 2012-07-12 17:29:55 UTC
  • mfrom: (10870.4.9 index-maintenance)
  • Revision ID: tarmac-20120712172955-zju6u2p3v102mg43
Incremental maintenance for general indexes. Approved: Markos Zaharioudakis

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include "simple_store.h"
25
25
#include "collection.h"
26
26
#include "simple_index_value.h"
 
27
#include "simple_index_general.h"
27
28
#include "simple_pul.h"
28
29
#include "pul_primitives.h"
29
30
#include "node_items.h"
2756
2757
  modified by this pul, compute the index entries for I and D, and insert them
2757
2758
  into the given deltas vector
2758
2759
********************************************************************************/
2759
 
void CollectionPul::computeIndexDeltas(std::vector<store::IndexDelta>& deltas)
 
2760
void CollectionPul::computeIndexDeltas(std::vector<IndexDeltaImpl>& deltas)
2760
2761
{
2761
2762
  csize numIncrementalIndices = theIncrementalIndices.size();
2762
2763
 
2785
2786
 
2786
2787
  for (csize idx = 0; idx < numIncrementalIndices; ++idx)
2787
2788
  {
2788
 
    store::IndexDelta::iterator ite;
2789
 
    store::IndexDelta::iterator end;
2790
 
    store::IndexDelta* delta;
2791
 
    csize numApplied;
 
2789
    if (theIncrementalIndices[idx]->isGeneral())
 
2790
    {
 
2791
      ;
 
2792
    }
 
2793
    else
 
2794
    {
 
2795
      IndexDeltaImpl::ValueIterator ite;
 
2796
      IndexDeltaImpl::ValueIterator end;
 
2797
      store::IndexDelta::ValueDelta* delta;
 
2798
      csize numApplied;
2792
2799
 
2793
 
    delta = &theInsertedDocsIndexDeltas[idx];
2794
 
    if (delta)
2795
 
    {
 
2800
      delta = &theInsertedDocsIndexDeltas[idx].getValueDelta();
2796
2801
      numApplied = theNumInsertedDocsIndexDeltasApplied[idx];
2797
2802
      ite = delta->begin() + numApplied;
2798
2803
      end = delta->end();
2800
2805
      {
2801
2806
        delete (*ite).second;
2802
2807
      }
2803
 
    }
2804
2808
 
2805
 
    delta = &theAfterIndexDeltas[idx];
2806
 
    if (delta)
2807
 
    {
 
2809
      delta = &theAfterIndexDeltas[idx].getValueDelta();
2808
2810
      numApplied = theNumAfterIndexDeltasApplied[idx];
2809
2811
      ite = delta->begin() + numApplied;
2810
2812
      end = delta->end();
2812
2814
      {
2813
2815
        delete (*ite).second;
2814
2816
      }
2815
 
    }
2816
 
 
2817
 
    delta = &theDeletedDocsIndexDeltas[idx];
2818
 
    if (delta)
2819
 
    {
2820
 
      ite = delta->begin();
2821
 
      end = delta->end();
2822
 
      for (; ite != end; ++ite)
2823
 
      {
2824
 
        delete (*ite).second;
2825
 
      }
2826
 
    }
2827
 
 
2828
 
    delta = &theBeforeIndexDeltas[idx];
2829
 
    if (delta)
2830
 
    {
2831
 
      ite = delta->begin();
2832
 
      end = delta->end();
2833
 
      for (; ite != end; ++ite)
2834
 
      {
2835
 
        delete (*ite).second;
2836
 
      }
 
2817
      
 
2818
      theDeletedDocsIndexDeltas[idx].clear();
 
2819
      
 
2820
      theBeforeIndexDeltas[idx].clear();
2837
2821
    }
2838
2822
  }
2839
2823
}
2840
 
 
2841
 
 
 
2824
  
 
2825
  
2842
2826
/*******************************************************************************
2843
2827
  Refresh the incrementally maintained indexes.
2844
2828
********************************************************************************/
2846
2830
{
2847
2831
  csize numIncrementalIndices = theIncrementalIndices.size();
2848
2832
 
2849
 
  STORE_TRACE1("Refreshing indexes for collection "
2850
 
               << (theCollection ?
2851
 
                   theCollection->getName()->getStringValue().c_str() :
2852
 
                   "NULL")); 
2853
 
 
2854
 
  for (csize idx = 0; idx < numIncrementalIndices; ++idx)
2855
 
  {
2856
 
    ValueIndex* index = static_cast<ValueIndex*>(theIncrementalIndices[idx]);
2857
 
 
2858
 
    STORE_TRACE2("Index size before do = " 
2859
 
                 << (!index->isTreeIndex() ? index->size() : 0));
2860
 
 
2861
 
    store::IndexDelta& beforeDelta = theBeforeIndexDeltas[idx];
2862
 
    store::IndexDelta& afterDelta = theAfterIndexDeltas[idx];
2863
 
    store::IndexDelta& deletedDelta = theDeletedDocsIndexDeltas[idx];
2864
 
    store::IndexDelta& insertedDelta = theInsertedDocsIndexDeltas[idx];
2865
 
 
2866
 
    csize& numBeforeApplied = theNumBeforeIndexDeltasApplied[idx];
2867
 
    csize& numAfterApplied = theNumAfterIndexDeltasApplied[idx];
2868
 
    csize& numDeletedApplied = theNumDeletedDocsIndexDeltasApplied[idx];
2869
 
    csize& numInsertedApplied = theNumInsertedDocsIndexDeltasApplied[idx];
2870
 
 
2871
 
    store::IndexKey* key;
2872
 
    store::Item_t node;
2873
 
 
2874
 
    store::IndexDelta::iterator ite;
2875
 
    store::IndexDelta::iterator end;
2876
 
 
2877
 
    ite = beforeDelta.begin();
2878
 
    end = beforeDelta.end();
2879
 
    for (; ite != end; ++ite, ++numBeforeApplied)
2880
 
    {
2881
 
      index->remove((*ite).second, (*ite).first);
2882
 
    }
2883
 
 
2884
 
    ite = afterDelta.begin();
2885
 
    end = afterDelta.end();
2886
 
    for (; ite != end; ++ite, ++numAfterApplied)
2887
 
    {
2888
 
      node = (*ite).first;
2889
 
      key = (*ite).second;
2890
 
 
2891
 
      // If the index had its own key obj already, delete the key obj that was
2892
 
      // allocated during the delta creation.
2893
 
      if (index->insert((*ite).second, node))
2894
 
      {
2895
 
        assert(key != (*ite).second);
2896
 
        delete key;
2897
 
      }
2898
 
    }
2899
 
 
2900
 
    STORE_TRACE2("deleted-delta size = " << deletedDelta.size());
2901
 
 
2902
 
    ite = deletedDelta.begin();
2903
 
    end = deletedDelta.end();
2904
 
    for (; ite != end; ++ite, ++numDeletedApplied)
2905
 
    {
2906
 
      index->remove((*ite).second, (*ite).first);
2907
 
    }
2908
 
 
2909
 
    STORE_TRACE2("inserted-delta size = " << insertedDelta.size());
2910
 
 
2911
 
    ite = insertedDelta.begin();
2912
 
    end = insertedDelta.end();
2913
 
    for (; ite != end; ++ite, ++numInsertedApplied)
2914
 
    {
2915
 
      node = (*ite).first;
2916
 
      key = (*ite).second;
2917
 
 
2918
 
      if (index->insert((*ite).second, node))
2919
 
      {
2920
 
        assert(key != (*ite).second);
2921
 
        delete key;
2922
 
      }
2923
 
    }
2924
 
 
2925
 
    STORE_TRACE2("Index size after do = " 
2926
 
                 << (!index->isTreeIndex() ? index->size() : 0));
2927
 
  }
2928
 
 
2929
 
  STORE_TRACE1("Refreshed indexes for collection " 
2930
 
               << (theCollection ?
2931
 
                   theCollection->getName()->getStringValue().c_str() :
2932
 
                   "NULL")
2933
 
               << std::endl);
 
2833
  if (numIncrementalIndices > 0)
 
2834
  {
 
2835
    assert(theCollection);
 
2836
 
 
2837
    STORE_TRACE1("Refreshing indexes for collection "
 
2838
                 << theCollection->getName()->getStringValue().c_str()); 
 
2839
 
 
2840
    for (csize idx = 0; idx < numIncrementalIndices; ++idx)
 
2841
    {
 
2842
      if (theIncrementalIndices[idx]->isGeneral())
 
2843
        refreshGeneralIndex(idx);
 
2844
      else
 
2845
        refreshValueIndex(idx);
 
2846
    }
 
2847
    
 
2848
    STORE_TRACE1("Refreshed indexes for collection " 
 
2849
                 << theCollection->getName()->getStringValue().c_str()
 
2850
                 << std::endl);
 
2851
  }
 
2852
}
 
2853
 
 
2854
 
 
2855
 
 
2856
/*******************************************************************************
 
2857
 
 
2858
********************************************************************************/
 
2859
void CollectionPul::refreshValueIndex(csize idx)
 
2860
{
 
2861
  ValueIndex* index = static_cast<ValueIndex*>(theIncrementalIndices[idx]);
 
2862
 
 
2863
  STORE_TRACE2("Index size before do = " << index->size());
 
2864
 
 
2865
  store::IndexDelta::ValueDelta& 
 
2866
  beforeDelta = theBeforeIndexDeltas[idx].getValueDelta();
 
2867
  store::IndexDelta::ValueDelta& 
 
2868
  afterDelta = theAfterIndexDeltas[idx].getValueDelta();
 
2869
  store::IndexDelta::ValueDelta& 
 
2870
  deletedDelta = theDeletedDocsIndexDeltas[idx].getValueDelta();
 
2871
  store::IndexDelta::ValueDelta&
 
2872
  insertedDelta = theInsertedDocsIndexDeltas[idx].getValueDelta();
 
2873
  
 
2874
  csize& numBeforeApplied = theNumBeforeIndexDeltasApplied[idx];
 
2875
  csize& numAfterApplied = theNumAfterIndexDeltasApplied[idx];
 
2876
  csize& numDeletedApplied = theNumDeletedDocsIndexDeltasApplied[idx];
 
2877
  csize& numInsertedApplied = theNumInsertedDocsIndexDeltasApplied[idx];
 
2878
 
 
2879
  store::IndexKey* key;
 
2880
  store::Item_t node;
 
2881
 
 
2882
  IndexDeltaImpl::ValueIterator ite;
 
2883
  IndexDeltaImpl::ValueIterator end;
 
2884
 
 
2885
  ite = beforeDelta.begin();
 
2886
  end = beforeDelta.end();
 
2887
  for (; ite != end; ++ite, ++numBeforeApplied)
 
2888
  {
 
2889
    index->remove((*ite).second, (*ite).first, false);
 
2890
  }
 
2891
 
 
2892
  ite = afterDelta.begin();
 
2893
  end = afterDelta.end();
 
2894
  for (; ite != end; ++ite, ++numAfterApplied)
 
2895
  {
 
2896
    node = (*ite).first;
 
2897
    key = (*ite).second;
 
2898
    
 
2899
    // If the index had its own key obj already, delete the key obj that was
 
2900
    // allocated during the delta creation.
 
2901
    if (index->insert((*ite).second, node))
 
2902
    {
 
2903
      assert(key != (*ite).second);
 
2904
      delete key;
 
2905
    }
 
2906
  }
 
2907
  
 
2908
  STORE_TRACE2("deleted-delta size = " << deletedDelta.size());
 
2909
  
 
2910
  ite = deletedDelta.begin();
 
2911
  end = deletedDelta.end();
 
2912
  for (; ite != end; ++ite, ++numDeletedApplied)
 
2913
  {
 
2914
    index->remove((*ite).second, (*ite).first, false);
 
2915
  }
 
2916
  
 
2917
  STORE_TRACE2("inserted-delta size = " << insertedDelta.size());
 
2918
  
 
2919
  ite = insertedDelta.begin();
 
2920
  end = insertedDelta.end();
 
2921
  for (; ite != end; ++ite, ++numInsertedApplied)
 
2922
  {
 
2923
    node = (*ite).first;
 
2924
    key = (*ite).second;
 
2925
    
 
2926
    if (index->insert((*ite).second, node))
 
2927
    {
 
2928
      assert(key != (*ite).second);
 
2929
      delete key;
 
2930
    }
 
2931
  }
 
2932
  
 
2933
  STORE_TRACE2("Index size after do = " << index->size());
 
2934
}
 
2935
 
 
2936
 
 
2937
/*******************************************************************************
 
2938
 
 
2939
********************************************************************************/
 
2940
void CollectionPul::refreshGeneralIndex(csize idx)
 
2941
{
 
2942
  GeneralIndex* index = static_cast<GeneralIndex*>(theIncrementalIndices[idx]);
 
2943
 
 
2944
  store::IndexDelta::GeneralDelta& 
 
2945
  beforeDelta = theBeforeIndexDeltas[idx].getGeneralDelta();
 
2946
  store::IndexDelta::GeneralDelta& 
 
2947
  afterDelta = theAfterIndexDeltas[idx].getGeneralDelta();
 
2948
  store::IndexDelta::GeneralDelta& 
 
2949
  deletedDelta = theDeletedDocsIndexDeltas[idx].getGeneralDelta();
 
2950
  store::IndexDelta::GeneralDelta&
 
2951
  insertedDelta = theInsertedDocsIndexDeltas[idx].getGeneralDelta();
 
2952
  
 
2953
  csize& numBeforeApplied = theNumBeforeIndexDeltasApplied[idx];
 
2954
  csize& numAfterApplied = theNumAfterIndexDeltasApplied[idx];
 
2955
  csize& numDeletedApplied = theNumDeletedDocsIndexDeltasApplied[idx];
 
2956
  csize& numInsertedApplied = theNumInsertedDocsIndexDeltasApplied[idx];
 
2957
 
 
2958
  store::Item_t key;
 
2959
  store::Item_t node;
 
2960
 
 
2961
  IndexDeltaImpl::GeneralIterator ite;
 
2962
  IndexDeltaImpl::GeneralIterator end;
 
2963
  std::vector<store::Item_t>::iterator keyIte;
 
2964
  std::vector<store::Item_t>::iterator keyEnd;
 
2965
 
 
2966
  STORE_TRACE2("before-delta size = " << deletedDelta.size());
 
2967
 
 
2968
  ite = beforeDelta.begin();
 
2969
  end = beforeDelta.end();
 
2970
 
 
2971
  while (ite != end)
 
2972
  {
 
2973
    store::Item* nodep = (*ite).first.getp(); 
 
2974
 
 
2975
    index->remove((*ite).second, (*ite).first);
 
2976
    ++numBeforeApplied;
 
2977
    ++ite;
 
2978
 
 
2979
    if (ite != end && (*ite).first.getp() == nodep)
 
2980
    {
 
2981
      index->remove((*ite).second, (*ite).first);
 
2982
      ++numDeletedApplied;
 
2983
      ++ite;
 
2984
 
 
2985
      // Call removeMultiKey() after removing the 2nd key for the same node.
 
2986
      // We do this to make undo easier.
 
2987
      index->removeMultiKey();
 
2988
 
 
2989
      while (ite != end && (*ite).first.getp() == nodep)
 
2990
      {
 
2991
        index->remove((*ite).second, (*ite).first);
 
2992
        ++numBeforeApplied;
 
2993
        ++ite;
 
2994
      }
 
2995
    }
 
2996
  }
 
2997
 
 
2998
  STORE_TRACE2("after-delta size = " << deletedDelta.size());
 
2999
 
 
3000
  ite = afterDelta.begin();
 
3001
  end = afterDelta.end();
 
3002
 
 
3003
  while (ite != end)
 
3004
  {
 
3005
    node = (*ite).first;
 
3006
    key = (*ite).second;
 
3007
    store::Item* nodep = node.getp();
 
3008
 
 
3009
    index->insert(key, node);
 
3010
    ++numAfterApplied;
 
3011
    ++ite;
 
3012
 
 
3013
    if (ite != end && (*ite).first.getp() == nodep)
 
3014
    {
 
3015
      node = (*ite).first;
 
3016
      key = (*ite).second;
 
3017
 
 
3018
      index->insert(key, node);
 
3019
      ++numAfterApplied;
 
3020
      ++ite;
 
3021
 
 
3022
      index->addMultiKey();
 
3023
 
 
3024
      while (ite != end && (*ite).first.getp() == nodep)
 
3025
      {
 
3026
        node = (*ite).first;
 
3027
        key = (*ite).second;
 
3028
 
 
3029
        index->insert(key, node);
 
3030
        ++numAfterApplied;
 
3031
        ++ite;
 
3032
      }
 
3033
    }
 
3034
  }
 
3035
 
 
3036
  STORE_TRACE2("deleted-delta size = " << deletedDelta.size());
 
3037
  
 
3038
  ite = deletedDelta.begin();
 
3039
  end = deletedDelta.end();
 
3040
 
 
3041
  while (ite != end)
 
3042
  {
 
3043
    store::Item* nodep = (*ite).first.getp(); 
 
3044
 
 
3045
    index->remove((*ite).second, (*ite).first);
 
3046
    ++numDeletedApplied;
 
3047
    ++ite;
 
3048
 
 
3049
    if (ite != end && (*ite).first.getp() == nodep)
 
3050
    {
 
3051
      index->remove((*ite).second, (*ite).first);
 
3052
      ++numDeletedApplied;
 
3053
      ++ite;
 
3054
 
 
3055
      // Call removeMultiKey() after removing the 2nd key for the same node.
 
3056
      // We do this to make undo easier.
 
3057
      index->removeMultiKey();
 
3058
 
 
3059
      while (ite != end && (*ite).first.getp() == nodep)
 
3060
      {
 
3061
        index->remove((*ite).second, (*ite).first);
 
3062
        ++numDeletedApplied;
 
3063
        ++ite;
 
3064
      }
 
3065
    }
 
3066
  }
 
3067
 
 
3068
  STORE_TRACE2("inserted-delta size = " << insertedDelta.size());
 
3069
  
 
3070
  ite = insertedDelta.begin();
 
3071
  end = insertedDelta.end();
 
3072
 
 
3073
  while (ite != end)
 
3074
  {
 
3075
    node = (*ite).first;
 
3076
    key = (*ite).second;
 
3077
    store::Item* nodep = node.getp(); 
 
3078
      
 
3079
    index->insert(key, node);
 
3080
    ++numInsertedApplied;
 
3081
    ++ite;
 
3082
 
 
3083
    if (ite != end && (*ite).first.getp() == nodep)
 
3084
    {
 
3085
      node = (*ite).first;
 
3086
      key = (*ite).second;
 
3087
 
 
3088
      index->insert(key, node);
 
3089
      ++numInsertedApplied;
 
3090
      ++ite;
 
3091
 
 
3092
      index->addMultiKey();
 
3093
 
 
3094
      while (ite != end && (*ite).first.getp() == nodep)
 
3095
      {
 
3096
        node = (*ite).first;
 
3097
        key = (*ite).second;
 
3098
 
 
3099
        index->insert(key, node);
 
3100
        ++numInsertedApplied;
 
3101
        ++ite;
 
3102
      }
 
3103
    }
 
3104
  }
2934
3105
}
2935
3106
 
2936
3107
 
2949
3120
 
2950
3121
  for (csize idx = 0; idx < numIncrementalIndices; ++idx)
2951
3122
  {
2952
 
    ValueIndex* index = static_cast<ValueIndex*>(theIncrementalIndices[idx]);
2953
 
 
2954
 
    STORE_TRACE2("Index size before undo = " 
2955
 
                 << (!index->isTreeIndex() ? index->size() : 0));
2956
 
    
2957
 
    store::IndexDelta& beforeDelta = theBeforeIndexDeltas[idx];
2958
 
    store::IndexDelta& afterDelta = theAfterIndexDeltas[idx];
2959
 
    store::IndexDelta& insertedDelta = theInsertedDocsIndexDeltas[idx];
2960
 
    store::IndexDelta& deletedDelta = theDeletedDocsIndexDeltas[idx];
2961
 
 
2962
 
    csize numBeforeApplied = theNumBeforeIndexDeltasApplied[idx];
2963
 
    csize numAfterApplied = theNumAfterIndexDeltasApplied[idx];
2964
 
    csize numDeletedApplied = theNumDeletedDocsIndexDeltasApplied[idx];
2965
 
    csize numInsertedApplied = theNumInsertedDocsIndexDeltasApplied[idx];
2966
 
 
2967
 
    store::IndexDelta::reverse_iterator ite;
2968
 
    store::IndexDelta::reverse_iterator end;
2969
 
 
2970
 
    ite = insertedDelta.rbegin() + (insertedDelta.size() - numInsertedApplied);
2971
 
    end = insertedDelta.rend();
2972
 
    for (; ite != end; ++ite)
2973
 
    {
2974
 
      index->remove((*ite).second, (*ite).first);
2975
 
    }
2976
 
 
2977
 
    ite = deletedDelta.rbegin() + (deletedDelta.size() - numDeletedApplied);
2978
 
    end = deletedDelta.rend();
2979
 
    for (; ite != end; ++ite)
2980
 
    {
2981
 
      store::IndexKey* key = (*ite).second;
2982
 
 
2983
 
      // If the index takes ownership of the key obj, set the key ptr to null
2984
 
      // so that the key obj will not be deleted during cleanIndexDeltas().
2985
 
      if (!index->insert(key, (*ite).first))
2986
 
      {
2987
 
        assert(key == (*ite).second);
2988
 
        (*ite).second = NULL;
2989
 
      }
2990
 
    }
2991
 
 
2992
 
    ite = afterDelta.rbegin() + (afterDelta.size() - numAfterApplied);
2993
 
    end = afterDelta.rend();
2994
 
    for (; ite != end; ++ite)
2995
 
    {
2996
 
      index->remove((*ite).second, (*ite).first);
2997
 
    }
2998
 
 
2999
 
    ite = beforeDelta.rbegin() + (beforeDelta.size() - numBeforeApplied);
3000
 
    end = beforeDelta.rend();
3001
 
    for (; ite != end; ++ite)
3002
 
    {
3003
 
      store::IndexKey* key = (*ite).second;
3004
 
 
3005
 
      // If the index takes ownership of the key obj, set the key ptr to null
3006
 
      // so that the key obj will not be deleted during cleanIndexDeltas().
3007
 
      if (!index->insert(key, (*ite).first))
3008
 
      {
3009
 
        assert(key == (*ite).second);
3010
 
        (*ite).second = NULL;
3011
 
      }
3012
 
    }
3013
 
 
3014
 
    STORE_TRACE2("Index size after undo = " 
3015
 
                 << (!index->isTreeIndex() ? index->size() : 0));
 
3123
    if (theIncrementalIndices[idx]->isGeneral())
 
3124
      undoGeneralIndexRefresh(idx);
 
3125
    else
 
3126
      undoValueIndexRefresh(idx);
3016
3127
  }
3017
3128
 
3018
3129
  STORE_TRACE1("Reverted indexes for collection " 
3024
3135
 
3025
3136
 
3026
3137
/*******************************************************************************
 
3138
 
 
3139
********************************************************************************/
 
3140
void CollectionPul::undoValueIndexRefresh(csize idx)
 
3141
{
 
3142
  ValueIndex* index = static_cast<ValueIndex*>(theIncrementalIndices[idx]);
 
3143
 
 
3144
  STORE_TRACE2("Index size before undo = " << index->size());
 
3145
    
 
3146
  store::IndexDelta::ValueDelta& 
 
3147
  beforeDelta = theBeforeIndexDeltas[idx].getValueDelta();
 
3148
  store::IndexDelta::ValueDelta&
 
3149
  afterDelta = theAfterIndexDeltas[idx].getValueDelta();
 
3150
  store::IndexDelta::ValueDelta&
 
3151
  insertedDelta = theInsertedDocsIndexDeltas[idx].getValueDelta();
 
3152
  store::IndexDelta::ValueDelta&
 
3153
  deletedDelta = theDeletedDocsIndexDeltas[idx].getValueDelta();
 
3154
 
 
3155
  csize numBeforeApplied = theNumBeforeIndexDeltasApplied[idx];
 
3156
  csize numAfterApplied = theNumAfterIndexDeltasApplied[idx];
 
3157
  csize numDeletedApplied = theNumDeletedDocsIndexDeltasApplied[idx];
 
3158
  csize numInsertedApplied = theNumInsertedDocsIndexDeltasApplied[idx];
 
3159
 
 
3160
  IndexDeltaImpl::ReverseValueIterator ite;
 
3161
  IndexDeltaImpl::ReverseValueIterator end;
 
3162
 
 
3163
  ite = insertedDelta.rbegin() + (insertedDelta.size() - numInsertedApplied);
 
3164
  end = insertedDelta.rend();
 
3165
  for (; ite != end; ++ite)
 
3166
  {
 
3167
    index->remove((*ite).second, (*ite).first, false);
 
3168
  }
 
3169
  
 
3170
  ite = deletedDelta.rbegin() + (deletedDelta.size() - numDeletedApplied);
 
3171
  end = deletedDelta.rend();
 
3172
  for (; ite != end; ++ite)
 
3173
  {
 
3174
    store::IndexKey* key = (*ite).second;
 
3175
 
 
3176
    // If the index takes ownership of the key obj, set the key ptr to null
 
3177
    // so that the key obj will not be deleted during cleanIndexDeltas().
 
3178
    if (!index->insert(key, (*ite).first))
 
3179
    {
 
3180
      assert(key == (*ite).second);
 
3181
      (*ite).second = NULL;
 
3182
    }
 
3183
  }
 
3184
 
 
3185
  ite = afterDelta.rbegin() + (afterDelta.size() - numAfterApplied);
 
3186
  end = afterDelta.rend();
 
3187
  for (; ite != end; ++ite)
 
3188
  {
 
3189
    index->remove((*ite).second, (*ite).first, false);
 
3190
  }
 
3191
 
 
3192
  ite = beforeDelta.rbegin() + (beforeDelta.size() - numBeforeApplied);
 
3193
  end = beforeDelta.rend();
 
3194
  for (; ite != end; ++ite)
 
3195
  {
 
3196
    store::IndexKey* key = (*ite).second;
 
3197
    
 
3198
    // If the index takes ownership of the key obj, set the key ptr to null
 
3199
    // so that the key obj will not be deleted during cleanIndexDeltas().
 
3200
    if (!index->insert(key, (*ite).first))
 
3201
    {
 
3202
      assert(key == (*ite).second);
 
3203
      (*ite).second = NULL;
 
3204
    }
 
3205
  }
 
3206
  
 
3207
  STORE_TRACE2("Index size after undo = " << index->size());
 
3208
}
 
3209
 
 
3210
 
 
3211
/*******************************************************************************
 
3212
 
 
3213
********************************************************************************/
 
3214
void CollectionPul::undoGeneralIndexRefresh(csize idx)
 
3215
{
 
3216
  GeneralIndex* index = static_cast<GeneralIndex*>(theIncrementalIndices[idx]);
 
3217
 
 
3218
  store::IndexDelta::GeneralDelta& 
 
3219
  beforeDelta = theBeforeIndexDeltas[idx].getGeneralDelta();
 
3220
  store::IndexDelta::GeneralDelta&
 
3221
  afterDelta = theAfterIndexDeltas[idx].getGeneralDelta();
 
3222
  store::IndexDelta::GeneralDelta&
 
3223
  insertedDelta = theInsertedDocsIndexDeltas[idx].getGeneralDelta();
 
3224
  store::IndexDelta::GeneralDelta&
 
3225
  deletedDelta = theDeletedDocsIndexDeltas[idx].getGeneralDelta();
 
3226
 
 
3227
  csize numBeforeApplied = theNumBeforeIndexDeltasApplied[idx];
 
3228
  csize numAfterApplied = theNumAfterIndexDeltasApplied[idx];
 
3229
  csize numDeletedApplied = theNumDeletedDocsIndexDeltasApplied[idx];
 
3230
  csize numInsertedApplied = theNumInsertedDocsIndexDeltasApplied[idx];
 
3231
 
 
3232
  IndexDeltaImpl::ReverseGeneralIterator ite;
 
3233
  IndexDeltaImpl::ReverseGeneralIterator end;
 
3234
  std::vector<store::Item_t>::reverse_iterator keyIte;
 
3235
  std::vector<store::Item_t>::reverse_iterator keyEnd;
 
3236
 
 
3237
  // Inserted delta
 
3238
 
 
3239
  ite = insertedDelta.rbegin() + (insertedDelta.size() - numInsertedApplied);
 
3240
  end = insertedDelta.rend();
 
3241
 
 
3242
  while (ite != end)
 
3243
  {
 
3244
    store::Item* nodep = (*ite).first.getp(); 
 
3245
 
 
3246
    index->remove((*ite).second, (*ite).first);
 
3247
    ++ite;
 
3248
 
 
3249
    if (ite != end && (*ite).first.getp() == nodep)
 
3250
    {
 
3251
      index->removeMultiKey();
 
3252
 
 
3253
      do
 
3254
      {
 
3255
        index->remove((*ite).second, (*ite).first);
 
3256
        ++ite;
 
3257
      }
 
3258
      while (ite != end && (*ite).first.getp() == nodep);
 
3259
    }
 
3260
  }
 
3261
 
 
3262
  // Deleted delta
 
3263
 
 
3264
  ite = deletedDelta.rbegin() + (deletedDelta.size() - numDeletedApplied);
 
3265
  end = deletedDelta.rend();
 
3266
 
 
3267
  while (ite != end)
 
3268
  {
 
3269
    store::Item* nodep = (*ite).first.getp(); 
 
3270
 
 
3271
    index->insert((*ite).second, (*ite).first);
 
3272
    ++ite;
 
3273
 
 
3274
    if (ite != end && (*ite).first.getp() == nodep)
 
3275
    {
 
3276
      index->addMultiKey();
 
3277
 
 
3278
      do
 
3279
      {
 
3280
        index->insert((*ite).second, (*ite).first);
 
3281
        ++ite;
 
3282
      }
 
3283
      while (ite != end && (*ite).first.getp() == nodep);
 
3284
    }
 
3285
  }
 
3286
 
 
3287
  // After delta
 
3288
 
 
3289
  ite = afterDelta.rbegin() + (afterDelta.size() - numAfterApplied);
 
3290
  end = afterDelta.rend();
 
3291
 
 
3292
  while (ite != end)
 
3293
  {
 
3294
    store::Item* nodep = (*ite).first.getp(); 
 
3295
 
 
3296
    index->remove((*ite).second, (*ite).first);
 
3297
    ++ite;
 
3298
 
 
3299
    if (ite != end && (*ite).first.getp() == nodep)
 
3300
    {
 
3301
      index->removeMultiKey();
 
3302
 
 
3303
      do
 
3304
      {
 
3305
        index->remove((*ite).second, (*ite).first);
 
3306
        ++ite;
 
3307
      }
 
3308
      while (ite != end && (*ite).first.getp() == nodep);
 
3309
    }
 
3310
  }
 
3311
 
 
3312
  // Before delta
 
3313
 
 
3314
  ite = beforeDelta.rbegin() + (beforeDelta.size() - numBeforeApplied);
 
3315
  end = beforeDelta.rend();
 
3316
 
 
3317
  while (ite != end)
 
3318
  {
 
3319
    store::Item* nodep = (*ite).first.getp(); 
 
3320
 
 
3321
    index->insert((*ite).second, (*ite).first);
 
3322
    ++ite;
 
3323
 
 
3324
    if (ite != end && (*ite).first.getp() == nodep)
 
3325
    {
 
3326
      index->addMultiKey();
 
3327
 
 
3328
      do
 
3329
      {
 
3330
        index->insert((*ite).second, (*ite).first);
 
3331
        ++ite;
 
3332
      }
 
3333
      while (ite != end && (*ite).first.getp() == nodep);
 
3334
    }
 
3335
  }
 
3336
}
 
3337
 
 
3338
 
 
3339
/*******************************************************************************
3027
3340
  The method is called from CollectionPul::finalizeUpdates()
3028
3341
********************************************************************************/
3029
3342
void CollectionPul::truncateIndexes()
3173
3486
          theMergeList.push_back(mergeInfo);
3174
3487
 
3175
3488
          (void)GET_NODE_FACTORY().createTextNode(node->getTree(),
3176
 
              node,
3177
 
              false,
3178
 
              i,
3179
 
              newContent);
 
3489
                                                  node,
 
3490
                                                  false,
 
3491
                                                  i,
 
3492
                                                  newContent);
3180
3493
        }
3181
3494
      }
3182
3495
    }
3215
3528
  }
3216
3529
  catch (const std::exception& e) 
3217
3530
  {
3218
 
#ifndef NDEBUG
3219
 
    std::cerr << "Exception thrown during pul::applyUpdates: " << e.what() << std::endl;
3220
 
#endif
 
3531
    //std::cerr << "Exception thrown during pul::applyUpdates: " << e.what() << std::endl;
3221
3532
    throw;
3222
3533
  }
3223
3534
  catch (...)
3224
3535
  {
3225
 
#ifndef NDEBUG
3226
 
    std::cerr << "Exception thrown during pul::applyUpdates " << std::endl;
3227
 
#endif
 
3536
    //std::cerr << "Exception thrown during pul::applyUpdates " << std::endl;
3228
3537
    throw;
3229
3538
  }
3230
3539