~juan457/+junk/zorba

« back to all changes in this revision

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

  • Committer: Markos Zaharioudakis
  • Date: 2012-07-11 15:38:39 UTC
  • mfrom: (10924 zorba)
  • mto: This revision was merged to the branch mainline in revision 10932.
  • Revision ID: markos_za@yahoo.com-20120711153839-0mkh15cg2ubknchd
work in progress

Show diffs side-by-side

added added

removed removed

Lines of Context:
1912
1912
 
1913
1913
  JSONObject* obj = static_cast<JSONObject*>(theTarget.getp());
1914
1914
 
 
1915
  theIsApplied = true;
 
1916
 
1915
1917
  csize numPairs = theNames.size();
1916
1918
  for (csize i = 0; i < numPairs; ++i, ++theNumApplied)
1917
1919
  {
1918
 
    JSONObjectPair_t pair =  new SimpleJSONObjectPair(theNames[i], theValues[i]);
1919
 
    if (!obj->add(pair, false))
 
1920
    if (!obj->add(theNames[i], theValues[i], false))
1920
1921
    {
1921
1922
      RAISE_ERROR(jerr::JNUP0006, theLoc, 
1922
 
      ERROR_PARAMS(pair->getName()->getStringValue()));
 
1923
      ERROR_PARAMS(theNames[i]->getStringValue()));
1923
1924
    }
1924
1925
  }
1925
 
 
1926
 
  theIsApplied = true;
1927
1926
}
1928
1927
 
1929
1928
 
1965
1964
{
1966
1965
  JSONObject* obj = static_cast<JSONObject*>(theTarget.getp());
1967
1966
 
1968
 
  JSONObjectPair_t pair = obj->remove(theName);
 
1967
  theOldValue = obj->remove(theName);
1969
1968
  
1970
 
  theOldValue = pair->getValue();
1971
 
 
1972
1969
  theIsApplied = true;
1973
1970
}
1974
1971
 
1982
1979
 
1983
1980
  JSONObject* obj = static_cast<JSONObject*>(theTarget.getp());
1984
1981
 
1985
 
  JSONObjectPair_t pair = new SimpleJSONObjectPair(theName, theOldValue);
1986
 
 
1987
 
  bool inserted = obj->add(pair, false);
 
1982
  bool inserted = obj->add(theName, theOldValue, false);
1988
1983
 
1989
1984
  ZORBA_ASSERT(inserted);
1990
1985
 
1996
1991
 
1997
1992
********************************************************************************/
1998
1993
UpdJSONObjectReplaceValue::UpdJSONObjectReplaceValue(
1999
 
      CollectionPul* pul,
2000
 
      const QueryLoc* loc,
2001
 
      store::Item_t& target,
2002
 
      store::Item_t& name,
2003
 
      store::Item_t& newValue)
 
1994
    CollectionPul* pul,
 
1995
    const QueryLoc* loc,
 
1996
    store::Item_t& target,
 
1997
    store::Item_t& name,
 
1998
    store::Item_t& newValue)
2004
1999
  :
2005
2000
  UpdatePrimitive(pul, loc, target),
2006
2001
  theName(name),
2013
2008
{
2014
2009
  JSONObject* obj = static_cast<JSONObject*>(theTarget.getp());
2015
2010
 
2016
 
  JSONObjectPair* pair = static_cast<JSONObjectPair*>(obj->getPair(theName));
 
2011
  theOldValue = obj->setValue(theName, theNewValue);
2017
2012
 
2018
 
  if (pair)
 
2013
  if (theOldValue != NULL)
2019
2014
  {
2020
 
    theOldValue = pair->getValue();
2021
 
 
2022
 
    pair->setValue(theNewValue);
2023
 
 
2024
2015
    theIsApplied = true;
2025
2016
  }
2026
2017
}
2033
2024
 
2034
2025
  JSONObject* obj = static_cast<JSONObject*>(theTarget.getp());
2035
2026
 
2036
 
  JSONObjectPair* pair = static_cast<JSONObjectPair*>(obj->getPair(theName));
2037
 
 
2038
 
  ZORBA_ASSERT(pair);
2039
 
 
2040
 
  pair->setValue(theOldValue);
 
2027
  ZORBA_ASSERT(obj);
 
2028
 
 
2029
  obj->setValue(theName, theOldValue);
2041
2030
 
2042
2031
  theIsApplied = false;
2043
2032
}
2064
2053
{
2065
2054
  JSONObject* obj = static_cast<JSONObject*>(theTarget.getp());
2066
2055
 
2067
 
  if (obj->getPair(theNewName))
 
2056
  if (obj->getObjectValue(theNewName) != NULL)
2068
2057
  {
2069
 
    RAISE_ERROR(jerr::JNUP0012, theLoc, ERROR_PARAMS(theNewName->getStringValue()));
 
2058
    RAISE_ERROR(jerr::JNUP0006, theLoc, ERROR_PARAMS(theNewName->getStringValue()));
2070
2059
  }
2071
 
 
2072
 
  JSONObjectPair* pair = static_cast<JSONObjectPair*>(obj->getPair(theName));
2073
 
 
2074
 
  if (pair)
 
2060
  
 
2061
  if (obj->rename(theName, theNewName))
2075
2062
  {
2076
 
    pair->setName(theNewName);
2077
 
 
2078
2063
    theIsApplied = true;
2079
2064
  }
2080
2065
}
2087
2072
 
2088
2073
  JSONObject* obj = static_cast<JSONObject*>(theTarget.getp());
2089
2074
 
2090
 
  JSONObjectPair* pair = static_cast<JSONObjectPair*>(obj->getPair(theNewName));
2091
 
 
2092
 
  ZORBA_ASSERT(pair);
2093
 
 
2094
 
  pair->setName(theName);
 
2075
  ZORBA_ASSERT(obj);
 
2076
 
 
2077
  ZORBA_ASSERT(obj->rename(theNewName, theName));
2095
2078
 
2096
2079
  theIsApplied = false;
2097
2080
}
2113
2096
}
2114
2097
 
2115
2098
 
 
2099
UpdJSONArrayUpdate::UpdJSONArrayUpdate(
 
2100
    CollectionPul* pul,
 
2101
    const QueryLoc* loc,
 
2102
    store::Item_t& target)
 
2103
  :
 
2104
  UpdatePrimitive(pul, loc, target),
 
2105
  thePosition(0)
 
2106
{
 
2107
  assert(theTarget->isJSONArray());
 
2108
}
 
2109
 
 
2110
 
2116
2111
bool UpdJSONArrayUpdate::Comparator::operator() (
2117
2112
    const UpdatePrimitive* lhs,
2118
2113
    const UpdatePrimitive* rhs)
2119
2114
{
2120
2115
  const UpdJSONArrayUpdate* l = static_cast<const UpdJSONArrayUpdate*>(lhs);
2121
2116
  const UpdJSONArrayUpdate* r = static_cast<const UpdJSONArrayUpdate*>(rhs);
 
2117
  
 
2118
  if (l->thePosition == 0) // lhs is an appending UP
 
2119
    return true;
2122
2120
 
2123
2121
  if (l->thePosition > r->thePosition)
2124
2122
    return true;
2181
2179
/*******************************************************************************
2182
2180
 
2183
2181
********************************************************************************/
 
2182
UpdJSONArrayAppend::UpdJSONArrayAppend(
 
2183
    CollectionPul* pul,
 
2184
    const QueryLoc* loc,
 
2185
    store::Item_t& target,
 
2186
    std::vector<store::Item_t>& members)
 
2187
  :
 
2188
  UpdJSONArrayUpdate(pul, loc, target),
 
2189
  theNumApplied(0)
 
2190
{
 
2191
  theMembers.swap(members);
 
2192
}
 
2193
 
 
2194
 
 
2195
void UpdJSONArrayAppend::apply()
 
2196
{
 
2197
  JSONArray* array = static_cast<JSONArray*>(theTarget.getp());
 
2198
 
 
2199
  array->push_back(theMembers);
 
2200
 
 
2201
  theIsApplied = true;
 
2202
}
 
2203
 
 
2204
 
 
2205
void UpdJSONArrayAppend::undo()
 
2206
{
 
2207
  if (!theIsApplied)
 
2208
  {
 
2209
    return;
 
2210
  }
 
2211
 
 
2212
  JSONArray* array = static_cast<JSONArray*>(theTarget.getp());
 
2213
 
 
2214
  csize numNewMembers = theMembers.size();
 
2215
  xs_integer numTotalMembers = theTarget->getArraySize();
 
2216
  xs_integer lPositionToDelete = numTotalMembers - numNewMembers;
 
2217
 
 
2218
  for (csize i = 0; i < numNewMembers; ++i) 
 
2219
    array->remove(lPositionToDelete);
 
2220
 
 
2221
  theIsApplied = false;
 
2222
}
 
2223
 
 
2224
 
 
2225
/*******************************************************************************
 
2226
 
 
2227
********************************************************************************/
2184
2228
UpdJSONArrayDelete::UpdJSONArrayDelete(
2185
2229
      CollectionPul* pul,
2186
2230
      const QueryLoc* loc,
2195
2239
void UpdJSONArrayDelete::apply()
2196
2240
{
2197
2241
  JSONArray* array = static_cast<JSONArray*>(theTarget.getp());
2198
 
 
2199
 
  theOldValue = const_cast<store::Item*>(array->operator[](thePosition));
2200
 
 
2201
 
  array->remove(thePosition);
2202
 
 
2203
 
  theIsApplied = true;
 
2242
  
 
2243
  theOldValue = array->remove(thePosition);
 
2244
  
 
2245
  if (theOldValue != NULL)
 
2246
  {
 
2247
    theIsApplied = true;
 
2248
  }
2204
2249
}
2205
2250
 
2206
2251
 
2239
2284
{
2240
2285
  JSONArray* array = static_cast<JSONArray*>(theTarget.getp());
2241
2286
 
2242
 
  theOldValue = const_cast<store::Item*>(array->operator[](thePosition));
2243
 
 
2244
 
  array->replace(thePosition, theNewValue);
2245
 
 
2246
 
  theIsApplied = true;
 
2287
  theOldValue = array->replace(thePosition, theNewValue);
 
2288
  
 
2289
  if (theOldValue != NULL) {
 
2290
    theIsApplied = true;
 
2291
  }
2247
2292
}
2248
2293
 
2249
2294