예제 #1
0
파일: json.cpp 프로젝트: morsdatum/ArangoDB
void TRI_InsertArrayJson (TRI_memory_zone_t* zone,
                          TRI_json_t* object,
                          char const* name,
                          TRI_json_t const* subobject) {
  TRI_json_t copy;
  char* att;
  size_t length;

  TRI_ASSERT(object->_type == TRI_JSON_ARRAY);

  if (subobject == NULL) {
    return;
  }

  if (TRI_ReserveVector(&object->_value._objects, 2) != TRI_ERROR_NO_ERROR) {
    // TODO: signal OOM here
    return;
  }

  // attribute name
  length = strlen(name);
  att = TRI_DuplicateString2Z(zone, name, length);

  if (att == NULL) {
    // TODO: signal OOM here
    return;
  }

  InitString(&copy, att, length);
  TRI_PushBackVector(&object->_value._objects, &copy);

  // attribute value
  TRI_CopyToJson(zone, &copy, subobject);
  TRI_PushBackVector(&object->_value._objects, &copy);
}
예제 #2
0
void TRI_PushBackListJson (TRI_memory_zone_t* zone, TRI_json_t* list, TRI_json_t* object) {
  TRI_json_t copy;

  assert(list->_type == TRI_JSON_LIST);

  TRI_CopyToJson(zone, &copy, object);

  TRI_PushBackVector(&list->_value._objects, &copy);
}
예제 #3
0
파일: json.cpp 프로젝트: morsdatum/ArangoDB
int TRI_CopyToJson (TRI_memory_zone_t* zone,
                    TRI_json_t* dst,
                    TRI_json_t const* src) {
  int res;
  size_t n;
  size_t i;

  dst->_type = src->_type;

  switch (src->_type) {
    case TRI_JSON_UNUSED:
    case TRI_JSON_NULL:
      break;

    case TRI_JSON_BOOLEAN:
      dst->_value._boolean = src->_value._boolean;
      break;

    case TRI_JSON_NUMBER:
      dst->_value._number = src->_value._number;
      break;

    case TRI_JSON_STRING:
      return TRI_CopyToBlob(zone, &dst->_value._string, &src->_value._string);

    case TRI_JSON_STRING_REFERENCE:
      return TRI_AssignToBlob(zone, &dst->_value._string, &src->_value._string);

    case TRI_JSON_ARRAY:
    case TRI_JSON_LIST:
      n = src->_value._objects._length;

      TRI_InitVector(&dst->_value._objects, zone, sizeof(TRI_json_t));
      res = TRI_ResizeVector(&dst->_value._objects, n);

      if (res != TRI_ERROR_NO_ERROR) {
        return res;
      }

      for (i = 0;  i < n;  ++i) {
        TRI_json_t const* v = static_cast<TRI_json_t const*>(TRI_AtVector(&src->_value._objects, i));
        TRI_json_t* w = static_cast<TRI_json_t*>(TRI_AtVector(&dst->_value._objects, i));

        res = TRI_CopyToJson(zone, w, v);

        if (res != TRI_ERROR_NO_ERROR) {
          return res;
        }
      }

      break;
  }

  return TRI_ERROR_NO_ERROR;
}
예제 #4
0
파일: json.cpp 프로젝트: m0ppers/arangodb
void TRI_PushBackArrayJson (TRI_memory_zone_t* zone, TRI_json_t* array, TRI_json_t const* object) {
  TRI_ASSERT(array->_type == TRI_JSON_ARRAY);

  TRI_json_t* dst = static_cast<TRI_json_t*>(TRI_NextVector(&array->_value._objects));

  if (dst == nullptr) {
    // out of memory
    return;
  }

  // directly copy value into the obtained address
  TRI_CopyToJson(zone, dst, object);
}
예제 #5
0
파일: json.cpp 프로젝트: m0ppers/arangodb
TRI_json_t* TRI_CopyJson (TRI_memory_zone_t* zone, 
                          TRI_json_t const* src) {
  TRI_ASSERT(src != nullptr);
  TRI_json_t* dst = static_cast<TRI_json_t*>(TRI_Allocate(zone, sizeof(TRI_json_t), false));

  if (dst != nullptr) {
    int res = TRI_CopyToJson(zone, dst, src);

    if (res != TRI_ERROR_NO_ERROR) {
      TRI_Free(zone, dst);
      return nullptr;
    }
  }

  return dst;
}
예제 #6
0
TRI_json_t* TRI_CopyJson (TRI_memory_zone_t* zone, TRI_json_t* src) {
  TRI_json_t* dst;
  int res;

  dst = (TRI_json_t*) TRI_Allocate(zone, sizeof(TRI_json_t), false);

  if (dst == NULL) {
    return NULL;
  }

  res = TRI_CopyToJson(zone, dst, src);

  if (res != TRI_ERROR_NO_ERROR) {
    TRI_Free(zone, dst);
    return NULL;
  }

  return dst;
}
예제 #7
0
파일: json.cpp 프로젝트: morsdatum/ArangoDB
bool TRI_ReplaceArrayJson (TRI_memory_zone_t* zone, TRI_json_t* object, char const* name, TRI_json_t* replacement) {
  size_t n;
  size_t i;

  TRI_ASSERT(object->_type == TRI_JSON_ARRAY);
  TRI_ASSERT(name);

  n = object->_value._objects._length;

  for (i = 0;  i < n;  i += 2) {
    TRI_json_t* key;

    key = (TRI_json_t*) TRI_AtVector(&object->_value._objects, i);

    if (! IsString(key)) {
      continue;
    }

    if (TRI_EqualString(key->_value._string.data, name)) {
      TRI_json_t copy;

      // retrieve the old element
      TRI_json_t* old = (TRI_json_t*) TRI_AtVector(&object->_value._objects, i + 1);
      if (old != NULL) {
        TRI_DestroyJson(zone, old);
      }

      TRI_CopyToJson(zone, &copy, replacement);
      TRI_SetVector(&object->_value._objects, i + 1, &copy);
      return true;
    }
  }

  // object not found in array, now simply add it
  TRI_Insert2ArrayJson(zone, object, name, replacement);

  return false;
}
예제 #8
0
파일: json.cpp 프로젝트: m0ppers/arangodb
void TRI_InsertObjectJson (TRI_memory_zone_t* zone,
                          TRI_json_t* object,
                          char const* name,
                          TRI_json_t const* subobject) {
  TRI_ASSERT(object->_type == TRI_JSON_OBJECT);

  if (subobject == nullptr) {
    return;
  }

  if (TRI_ReserveVector(&object->_value._objects, 2) != TRI_ERROR_NO_ERROR) {
    // TODO: signal OOM here
    return;
  }

  // attribute name
  size_t length = strlen(name);
  char* att = TRI_DuplicateString2Z(zone, name, length);

  if (att == nullptr) {
    // TODO: signal OOM here
    return;
  }

  // create attribute name in place
  TRI_json_t* next = static_cast<TRI_json_t*>(TRI_NextVector(&object->_value._objects));
  // we have made sure above with the reserve that the vector has enough capacity
  TRI_ASSERT(next != nullptr);
  InitString(next, att, length);

  // attribute value
  next = static_cast<TRI_json_t*>(TRI_NextVector(&object->_value._objects));
  // we have made sure above with the reserve call that the vector has enough capacity
  TRI_ASSERT(next != nullptr);

  TRI_CopyToJson(zone, next, subobject);
}
예제 #9
0
void TRI_InsertArrayJson (TRI_memory_zone_t* zone,
                          TRI_json_t* object, 
                          char const* name,
                          TRI_json_t* subobject) {
  TRI_json_t copy;
  size_t length;

  assert(object->_type == TRI_JSON_ARRAY);

  if (subobject == NULL) {
    return;
  }

  length = strlen(name);

  copy._type = TRI_JSON_STRING;
  copy._value._string.length = length + 1;
  copy._value._string.data = TRI_DuplicateString2Z(zone, name, length); // including '\0'

  TRI_PushBackVector(&object->_value._objects, &copy);

  TRI_CopyToJson(zone, &copy, subobject);
  TRI_PushBackVector(&object->_value._objects, &copy);
}
예제 #10
0
파일: json.cpp 프로젝트: m0ppers/arangodb
bool TRI_ReplaceObjectJson (TRI_memory_zone_t* zone, 
                            TRI_json_t* object, 
                            char const* name, 
                            TRI_json_t const* replacement) {
  TRI_ASSERT(object->_type == TRI_JSON_OBJECT);
  TRI_ASSERT(name != nullptr);

  size_t const n = TRI_LengthVector(&object->_value._objects);

  for (size_t i = 0;  i < n;  i += 2) {
    TRI_json_t* key = static_cast<TRI_json_t*>(TRI_AtVector(&object->_value._objects, i));

    if (! IsString(key)) {
      continue;
    }

    if (TRI_EqualString(key->_value._string.data, name)) {
      // retrieve the old element
      TRI_json_t* old = static_cast<TRI_json_t*>(TRI_AtVector(&object->_value._objects, i + 1));

      if (old != nullptr) {
        TRI_DestroyJson(zone, old);
      }

      TRI_json_t copy;
      TRI_CopyToJson(zone, &copy, replacement);
      TRI_SetVector(&object->_value._objects, i + 1, &copy);
      return true;
    }
  }

  // object not found in array, now simply add it
  TRI_Insert2ObjectJson(zone, object, name, replacement);

  return false;
}
예제 #11
0
int BitarrayIndex_new(BitarrayIndex** baIndex,
                      TRI_memory_zone_t* memoryZone,
                      size_t cardinality,
                      TRI_vector_t* values,
                      bool supportUndef,
                      void* context) {
    int     result;
    size_t  numArrays;
    int j;


    // ...........................................................................
    // Some simple checks
    // ...........................................................................

    if (baIndex == NULL) {
        assert(false);
        return TRI_ERROR_INTERNAL;
    }



    // ...........................................................................
    // If the bit array index has arealdy been created, return internal error
    // ...........................................................................

    if (*baIndex != NULL) {
        return TRI_ERROR_INTERNAL;
    }


    // ...........................................................................
    // If the memory zone is invalid, then return an internal error
    // ...........................................................................

    if (memoryZone == NULL) {
        return TRI_ERROR_INTERNAL;
    }


    // ...........................................................................
    // Create the bit array index structure
    // ...........................................................................

    *baIndex = TRI_Allocate(TRI_UNKNOWN_MEM_ZONE, sizeof(BitarrayIndex), true);
    if (*baIndex == NULL) {
        return TRI_ERROR_OUT_OF_MEMORY;
    }


    // ...........................................................................
    // Copy the values into this index
    // ...........................................................................


    TRI_InitVector(&((*baIndex)->_values), TRI_UNKNOWN_MEM_ZONE, sizeof(TRI_json_t));

    for (j = 0;  j < values->_length;  ++j) {
        TRI_json_t value;
        TRI_CopyToJson(TRI_UNKNOWN_MEM_ZONE, &value, (TRI_json_t*)(TRI_AtVector(values,j)));
        TRI_PushBackVector(&((*baIndex)->_values), &value);
    }


    // ...........................................................................
    // Store whether or not the index supports 'undefined' documents (that is
    // documents with attributes which do not match those of the index
    // ...........................................................................

    (*baIndex)->_supportUndef = supportUndef;

    // ...........................................................................
    // Determine the number of bit columns which will comprise the bit array index.
    // ...........................................................................

    numArrays = cardinality;

    // ...........................................................................
    // Create the bit arrays
    // ...........................................................................

    result = TRI_InitBitarray(&((*baIndex)->_bitarray), memoryZone, numArrays, NULL);


    // ...........................................................................
    // return the result of creating the bit  arrays
    // ...........................................................................

    return result;
}