Пример #1
0
TRI_query_javascript_converter_t* TRI_InitQueryJavascript (void) {
  TRI_string_buffer_t* buffer;
  TRI_query_javascript_converter_t* converter;

  converter = (TRI_query_javascript_converter_t*) 
    TRI_Allocate(sizeof(TRI_query_javascript_converter_t));

  if (!converter) { 
    return NULL;
  }

  // init
  converter->_buffer = NULL;
  converter->_prefix = NULL;

  buffer = (TRI_string_buffer_t*) TRI_Allocate(sizeof(TRI_string_buffer_t));
  if (!buffer) {
    TRI_Free(converter);
    return NULL;
  }

  TRI_InitStringBuffer(buffer);
  converter->_buffer = buffer;

  return converter;
}
Пример #2
0
bool TRI_PrintJson (int fd, TRI_json_t const* object) {
  TRI_string_buffer_t buffer;
  char const* p;
  size_t n;

  TRI_InitStringBuffer(&buffer, TRI_UNKNOWN_MEM_ZONE);
  StringifyJson(buffer._memoryZone, &buffer, object, true);

  p = TRI_BeginStringBuffer(&buffer);
  n = TRI_LengthStringBuffer(&buffer);

  while (0 < n) {
    ssize_t m = TRI_WRITE(fd, p, n);

    if (m <= 0) {
      TRI_AnnihilateStringBuffer(&buffer);
      return false;
    }

    n -= m;
    p += m;
  }

  TRI_AnnihilateStringBuffer(&buffer);
  return true;
}
Пример #3
0
char* TRI_StringDouble (double value) {
  TRI_string_buffer_t buffer;

  TRI_InitStringBuffer(&buffer, TRI_CORE_MEM_ZONE);
  TRI_AppendDoubleStringBuffer(&buffer, value);

  return buffer._buffer;
}
Пример #4
0
TRI_string_buffer_t* TRI_CreateStringBuffer (TRI_memory_zone_t* zone) {
  TRI_string_buffer_t* self = (TRI_string_buffer_t*) TRI_Allocate(zone, sizeof(TRI_string_buffer_t), false);

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

  TRI_InitStringBuffer(self, zone);

  return self;
}
Пример #5
0
static char* GetIndexIdString (TRI_aql_collection_hint_t* const hint) {
  TRI_string_buffer_t buffer;
  char* result;

  TRI_InitStringBuffer(&buffer, TRI_UNKNOWN_MEM_ZONE);
  TRI_AppendUInt64StringBuffer(&buffer, hint->_collection->_collection->_cid);
  TRI_AppendCharStringBuffer(&buffer, '/');
  TRI_AppendUInt64StringBuffer(&buffer, hint->_index->_idx->_iid);

  result = TRI_DuplicateStringZ(TRI_UNKNOWN_MEM_ZONE, buffer._buffer);
  TRI_DestroyStringBuffer(&buffer);

  return result;
}
Пример #6
0
static TRI_json_t* NodeDescription (const TRI_aql_node_t* const node) {
  TRI_string_buffer_t buffer;
  TRI_json_t* result;

  assert(node);

  TRI_InitStringBuffer(&buffer, TRI_UNKNOWN_MEM_ZONE);
  
  TRI_NodeStringAql(&buffer, node);

  result = TRI_CreateStringCopyJson(TRI_UNKNOWN_MEM_ZONE, buffer._buffer);
  TRI_DestroyStringBuffer(&buffer);

  return result;
}
Пример #7
0
static bool PrintVelocyPack(int fd, VPackSlice const& slice,
                            bool appendNewline) {
  if (slice.isNone()) {
    // sanity check
    return false;
  }

  TRI_string_buffer_t buffer;
  TRI_InitStringBuffer(&buffer, TRI_UNKNOWN_MEM_ZONE);
  arangodb::basics::VPackStringBufferAdapter bufferAdapter(&buffer);
  try {
    VPackDumper dumper(&bufferAdapter);
    dumper.dump(slice);
  } catch (...) {
    // Writing failed
    TRI_AnnihilateStringBuffer(&buffer);
    return false;
  }

  if (TRI_LengthStringBuffer(&buffer) == 0) {
    // should not happen
    return false;
  }

  if (appendNewline) {
    // add the newline here so we only need one write operation in the ideal
    // case
    TRI_AppendCharStringBuffer(&buffer, '\n');
  }

  char const* p = TRI_BeginStringBuffer(&buffer);
  size_t n = TRI_LengthStringBuffer(&buffer);

  while (0 < n) {
    ssize_t m = TRI_WRITE(fd, p, (TRI_write_t)n);

    if (m <= 0) {
      TRI_AnnihilateStringBuffer(&buffer);
      return false;
    }

    n -= m;
    p += m;
  }

  TRI_AnnihilateStringBuffer(&buffer);
  return true;
}
Пример #8
0
static TRI_json_t* NodeType (const TRI_aql_node_t* const node) {
  TRI_json_t* result;
  TRI_string_buffer_t buffer;

  assert(node);
  
  TRI_InitStringBuffer(&buffer, TRI_UNKNOWN_MEM_ZONE);

  TRI_AppendStringStringBuffer(&buffer, TRI_NodeGroupAql(node, true));

  result = TRI_CreateStringCopyJson(TRI_UNKNOWN_MEM_ZONE, buffer._buffer);
  
  TRI_DestroyStringBuffer(&buffer);

  return result;
}
Пример #9
0
bool TRI_PrintJson (int fd, 
                    TRI_json_t const* object,
                    bool appendNewline) {
  TRI_string_buffer_t buffer;
  char const* p;
  size_t n;

  if (object == NULL) {
    // sanity check
    return false;
  }

  TRI_InitStringBuffer(&buffer, TRI_UNKNOWN_MEM_ZONE);
  if (StringifyJson(buffer._memoryZone, &buffer, object, true) != TRI_ERROR_NO_ERROR) {
    TRI_AnnihilateStringBuffer(&buffer);
    return false;
  }

  if (TRI_LengthStringBuffer(&buffer) == 0) {
    // should not happen
    return false;
  }

  if (appendNewline) {
    // add the newline here so we only need one write operation in the ideal case
    TRI_AppendCharStringBuffer(&buffer, '\n');
  }

  p = TRI_BeginStringBuffer(&buffer);
  n = TRI_LengthStringBuffer(&buffer);

  while (0 < n) {
    ssize_t m = TRI_WRITE(fd, p, (TRI_write_t) n);

    if (m <= 0) {
      TRI_AnnihilateStringBuffer(&buffer);
      return false;
    }

    n -= m;
    p += m;
  }

  TRI_AnnihilateStringBuffer(&buffer);
  return true;
}
Пример #10
0
int TRI_DeflateStringBuffer (TRI_string_buffer_t* self, 
                             size_t bufferSize) {
  TRI_string_buffer_t deflated;
  const char* ptr;
  const char* end;
  char* buffer;
  int res;

  z_stream strm;
  strm.zalloc = Z_NULL;
  strm.zfree  = Z_NULL;
  strm.opaque = Z_NULL;

  // initialise deflate procedure
  res = deflateInit(&strm, Z_DEFAULT_COMPRESSION);

  if (res != Z_OK) {
    return TRI_ERROR_OUT_OF_MEMORY;
  }
   
  buffer = (char*) TRI_Allocate(TRI_UNKNOWN_MEM_ZONE, bufferSize, false);

  if (buffer == NULL) {
    (void) deflateEnd(&strm);

    return TRI_ERROR_OUT_OF_MEMORY;
  }

  // we'll use this buffer for the output
  TRI_InitStringBuffer(&deflated, TRI_UNKNOWN_MEM_ZONE);

  ptr = TRI_BeginStringBuffer(self);
  end = ptr + TRI_LengthStringBuffer(self);

  while (ptr < end) {
    int flush;

    strm.next_in = (unsigned char*) ptr;

    if (end - ptr > (int) bufferSize) {
      strm.avail_in = (int) bufferSize;
      flush = Z_NO_FLUSH;
    }
    else {
      strm.avail_in = (uInt) (end - ptr);
      flush = Z_FINISH;
    }
    ptr += strm.avail_in;

    do {
      strm.avail_out = (int) bufferSize;
      strm.next_out = (unsigned char*) buffer;
      res = deflate(&strm, flush);

      if (res == Z_STREAM_ERROR) {
        (void) deflateEnd(&strm);
        TRI_Free(TRI_UNKNOWN_MEM_ZONE, buffer);
        TRI_DestroyStringBuffer(&deflated);

        return TRI_ERROR_INTERNAL; 
      }
              
      if (TRI_AppendString2StringBuffer(&deflated, (char*) buffer, bufferSize - strm.avail_out) != TRI_ERROR_NO_ERROR) {
        (void) deflateEnd(&strm);
        TRI_Free(TRI_UNKNOWN_MEM_ZONE, buffer);
        TRI_DestroyStringBuffer(&deflated);

        return TRI_ERROR_OUT_OF_MEMORY;
      }
    } 
    while (strm.avail_out == 0);
  } 

  // deflate successful
  (void) deflateEnd(&strm);

  TRI_SwapStringBuffer(self, &deflated);
  TRI_DestroyStringBuffer(&deflated);

  TRI_Free(TRI_UNKNOWN_MEM_ZONE, buffer);

  return TRI_ERROR_NO_ERROR;
}