void
mongoc_bulk_operation_insert (mongoc_bulk_operation_t *bulk,
                              const bson_t            *document)
{
   mongoc_write_command_t command = { 0 };
   mongoc_write_command_t *last;

   ENTRY;

   bson_return_if_fail (bulk);
   bson_return_if_fail (document);

   if (bulk->commands.len) {
      last = &_mongoc_array_index (&bulk->commands,
                                   mongoc_write_command_t,
                                   bulk->commands.len - 1);
      if (last->type == MONGOC_WRITE_COMMAND_INSERT) {
         _mongoc_write_command_insert_append (last, &document, 1);
         EXIT;
      }
   }

   _mongoc_write_command_init_insert (&command, &document, 1, bulk->ordered,
      !_mongoc_write_concern_needs_gle (bulk->write_concern));

   _mongoc_array_append_val (&bulk->commands, command);

   EXIT;
}
Beispiel #2
0
/**
 * _mongoc_buffer_init:
 * @buffer: A mongoc_buffer_t to initialize.
 * @buf: A data buffer to attach to @buffer.
 * @buflen: The size of @buflen.
 * @realloc_func: A function to resize @buf.
 *
 * Initializes @buffer for use. If additional space is needed by @buffer, then
 * @realloc_func will be called to resize @buf.
 *
 * @buffer takes ownership of @buf and will realloc it to zero bytes when
 * cleaning up the data structure.
 */
void
_mongoc_buffer_init (mongoc_buffer_t   *buffer,
                     uint8_t           *buf,
                     size_t             buflen,
                     bson_realloc_func  realloc_func,
                     void              *realloc_data)
{
   bson_return_if_fail (buffer);
   bson_return_if_fail (buflen || !buf);

   if (!realloc_func) {
      realloc_func = bson_realloc_ctx;
   }

   if (!buflen) {
      buflen = MONGOC_BUFFER_DEFAULT_SIZE;
   }

   if (!buf) {
      buf = realloc_func (NULL, buflen, NULL);
   }

   memset (buffer, 0, sizeof *buffer);

   buffer->data = buf;
   buffer->datalen = buflen;
   buffer->len = 0;
   buffer->off = 0;
   buffer->realloc_func = realloc_func;
   buffer->realloc_data = realloc_data;
}
Beispiel #3
0
void
bson_string_truncate (bson_string_t *string, /* IN */
                      uint32_t       len)    /* IN */
{
   uint32_t alloc;

   bson_return_if_fail (string);
   bson_return_if_fail (len < INT_MAX);

   alloc = len + 1;

   if (alloc < 16) {
      alloc = 16;
   }

   if (!bson_is_power_of_two (alloc)) {
      alloc = (uint32_t)bson_next_power_of_two ((size_t)alloc);
   }

   string->str = bson_realloc (string->str, alloc);
   string->alloc = alloc;
   string->len = len;

   string->str [string->len] = '\0';
}
void
mongoc_bulk_operation_update_one (mongoc_bulk_operation_t *bulk,
                                  const bson_t            *selector,
                                  const bson_t            *document,
                                  bool                     upsert)
{
   mongoc_write_command_t command = { 0 };
   bson_iter_t iter;

   bson_return_if_fail (bulk);
   bson_return_if_fail (selector);
   bson_return_if_fail (document);

   ENTRY;

   if (bson_iter_init (&iter, document)) {
      while (bson_iter_next (&iter)) {
         if (!strchr (bson_iter_key (&iter), '$')) {
            MONGOC_WARNING ("%s(): update_one only works with $ operators.",
                            __FUNCTION__);
            EXIT;
         }
      }
   }

   _mongoc_write_command_init_update (&command, selector, document, upsert,
                                      false, bulk->ordered);
   _mongoc_array_append_val (&bulk->commands, command);
   EXIT;
}
void
_mongoc_rpc_gather (mongoc_rpc_t   *rpc,
                    mongoc_array_t *array)
{
   bson_return_if_fail(rpc);
   bson_return_if_fail(array);

   switch ((mongoc_opcode_t)rpc->header.opcode) {
   case MONGOC_OPCODE_REPLY:
      return _mongoc_rpc_gather_reply(&rpc->reply, array);
   case MONGOC_OPCODE_MSG:
      return _mongoc_rpc_gather_msg(&rpc->msg, array);
   case MONGOC_OPCODE_UPDATE:
      return _mongoc_rpc_gather_update(&rpc->update, array);
   case MONGOC_OPCODE_INSERT:
      return _mongoc_rpc_gather_insert(&rpc->insert, array);
   case MONGOC_OPCODE_QUERY:
      return _mongoc_rpc_gather_query(&rpc->query, array);
   case MONGOC_OPCODE_GET_MORE:
      return _mongoc_rpc_gather_get_more(&rpc->get_more, array);
   case MONGOC_OPCODE_DELETE:
      return _mongoc_rpc_gather_delete(&rpc->delete, array);
   case MONGOC_OPCODE_KILL_CURSORS:
      return _mongoc_rpc_gather_kill_cursors(&rpc->kill_cursors, array);
   default:
      MONGOC_WARNING("Unknown rpc type: 0x%08x", rpc->header.opcode);
      break;
   }
}
void
mongoc_bulk_operation_replace_one (mongoc_bulk_operation_t *bulk,
                                   const bson_t            *selector,
                                   const bson_t            *document,
                                   bool                     upsert)
{
   mongoc_write_command_t command = { 0 };
   size_t err_off;

   bson_return_if_fail (bulk);
   bson_return_if_fail (selector);
   bson_return_if_fail (document);

   ENTRY;

   if (!bson_validate (document,
                       (BSON_VALIDATE_DOT_KEYS | BSON_VALIDATE_DOLLAR_KEYS),
                       &err_off)) {
      MONGOC_WARNING ("%s(): replacement document may not contain "
                      "$ or . in keys. Ingoring document.",
                      __FUNCTION__);
      EXIT;
   }

   _mongoc_write_command_init_update (&command, selector, document, upsert,
                                      false, bulk->ordered);
   _mongoc_array_append_val (&bulk->commands, command);

   EXIT;
}
Beispiel #7
0
void
bson_oid_init_from_string (bson_oid_t *oid,
                           const char *str)
{
   bson_return_if_fail(oid);
   bson_return_if_fail(str);
   bson_oid_init_from_string_unsafe(oid, str);
}
Beispiel #8
0
void
bson_oid_init_from_data (bson_oid_t         *oid,
                         const bson_uint8_t *data)
{
   bson_return_if_fail(oid);
   bson_return_if_fail(data);
   memcpy(oid, data, 12);
}
void
mongoc_read_prefs_set_mode (mongoc_read_prefs_t *read_prefs,
                            mongoc_read_mode_t   mode)
{
   bson_return_if_fail(read_prefs);
   bson_return_if_fail(mode <= MONGOC_READ_NEAREST);

   read_prefs->mode = mode;
}
Beispiel #10
0
void
bson_oid_init_from_data (bson_oid_t    *oid,  /* OUT */
                         const uint8_t *data) /* IN */
{
    bson_return_if_fail (oid);
    bson_return_if_fail (data);

    memcpy (oid, data, 12);
}
Beispiel #11
0
void
bson_oid_init_from_string (bson_oid_t *oid, /* OUT */
                           const char *str) /* IN */
{
    bson_return_if_fail (oid);
    bson_return_if_fail (str);

    bson_oid_init_from_string_unsafe (oid, str);
}
Beispiel #12
0
void
bson_oid_copy (const bson_oid_t *src, /* IN */
               bson_oid_t       *dst) /* OUT */
{
    bson_return_if_fail (src);
    bson_return_if_fail (dst);

    bson_oid_copy_unsafe (src, dst);
}
void
_mongoc_array_init (mongoc_array_t *array,
                    size_t          element_size)
{
   bson_return_if_fail(array);
   bson_return_if_fail(element_size);

   array->len = 0;
   array->element_size = element_size;
   array->allocated = 128;
   array->data = bson_malloc0(array->allocated);
}
Beispiel #14
0
/**
 * bson_reader_set_read_func:
 * @reader: A bson_reader_t.
 *
 * Tell @reader to use a customized read(). By default, @reader uses read() in
 * libc.
 *
 * Note that @reader must be initialized by bson_reader_init_from_fd(), or data
 * will be destroyed.
 */
void
bson_reader_set_read_func (bson_reader_t   *reader,
                           bson_read_func_t func)
{
   bson_reader_fd_t *real = (bson_reader_fd_t *)reader;

   bson_return_if_fail (reader);
   bson_return_if_fail (reader->type == BSON_READER_FD);
   bson_return_if_fail (func);

   real->read_func = func;
}
Beispiel #15
0
static void
_bson_reader_handle_fill_buffer (bson_reader_handle_t *reader) /* IN */
{
   ssize_t ret;

   BSON_ASSERT (reader);

   /*
    * Handle first read specially.
    */
   if ((!reader->done) && (!reader->offset) && (!reader->end)) {
      ret = reader->read_func (reader->handle, &reader->data[0], reader->len);

      if (ret <= 0) {
         reader->done = true;
         return;
      }
      reader->bytes_read += ret;

      reader->end = ret;
      return;
   }

   /*
    * Move valid data to head.
    */
   memmove (&reader->data[0],
            &reader->data[reader->offset],
            reader->end - reader->offset);
   reader->end = reader->end - reader->offset;
   reader->offset = 0;

   /*
    * Read in data to fill the buffer.
    */
   ret = reader->read_func (reader->handle,
                            &reader->data[reader->end],
                            reader->len - reader->end);

   if (ret <= 0) {
      reader->done = true;
      reader->failed = (ret < 0);
   } else {
      reader->bytes_read += ret;
      reader->end += ret;
   }

   bson_return_if_fail (reader->offset == 0);
   bson_return_if_fail (reader->end <= reader->len);
}
Beispiel #16
0
static void
_bson_reader_fd_fill_buffer (bson_reader_fd_t *reader)
{
   bson_ssize_t ret;

   bson_return_if_fail (reader);
   bson_return_if_fail (reader->fd >= 0);

   /*
    * Handle first read specially.
    */
   if ((!reader->done) && (!reader->offset) && (!reader->end)) {
      ret = reader->read_func (reader->fd, &reader->data[0], reader->len);

      if (ret <= 0) {
         reader->done = TRUE;
         return;
      }

      reader->end = ret;
      return;
   }

   /*
    * Move valid data to head.
    */
   memmove (&reader->data[0],
            &reader->data[reader->offset],
            reader->end - reader->offset);
   reader->end = reader->end - reader->offset;
   reader->offset = 0;

   /*
    * Read in data to fill the buffer.
    */
   ret = reader->read_func (reader->fd,
                            &reader->data[reader->end],
                            reader->len - reader->end);

   if (ret <= 0) {
      reader->done = TRUE;
      reader->failed = (ret < 0);
   } else {
      reader->end += ret;
   }

   bson_return_if_fail (reader->offset == 0);
   bson_return_if_fail (reader->end <= reader->len);
}
Beispiel #17
0
void
bson_string_append (bson_string_t *string,
                    const char    *str)
{
   bson_uint32_t len;

   bson_return_if_fail(string);
   bson_return_if_fail(str);

   len = strlen(str);
   string->str = bson_realloc(string->str, string->len + len);
   memcpy(&string->str[string->len - 1], str, len);
   string->len += len;
   string->str[string->len-1] = '\0';
}
Beispiel #18
0
void
bson_reader_destroy (bson_reader_t *reader) /* IN */
{
   bson_return_if_fail (reader);

   switch (reader->type) {
   case 0:
      break;
   case BSON_READER_HANDLE:
      {
         bson_reader_handle_t *handle = (bson_reader_handle_t *)reader;

         if (handle->destroy_func) {

            handle->destroy_func(handle->handle);
         }

         bson_free (handle->data);
      }
      break;
   case BSON_READER_DATA:
      break;
   default:
      fprintf (stderr, "No such reader type: %02x\n", reader->type);
      break;
   }

   reader->type = 0;

   bson_free (reader);
}
Beispiel #19
0
void
ha_sharded_cluster_add_replica_set (ha_sharded_cluster_t *cluster,
                                    ha_replica_set_t     *replica_set)
{
   int i;

   bson_return_if_fail(cluster);
   bson_return_if_fail(replica_set);

   for (i = 0; i < 12; i++) {
      if (!cluster->replicas[i]) {
         cluster->replicas[i] = replica_set;
         break;
      }
   }
}
Beispiel #20
0
void
mongoc_cursor_set_batch_size (mongoc_cursor_t *cursor,
                              uint32_t         batch_size)
{
   bson_return_if_fail (cursor);
   cursor->batch_size = batch_size;
}
void
mongoc_bulk_operation_remove_one (mongoc_bulk_operation_t *bulk,     /* IN */
                                  const bson_t            *selector) /* IN */
{
   mongoc_write_command_t command = { 0 };

   bson_return_if_fail (bulk);
   bson_return_if_fail (selector);

   ENTRY;

   _mongoc_write_command_init_delete (&command, selector, false, bulk->ordered);
   _mongoc_array_append_val (&bulk->commands, command);

   EXIT;
}
void
_mongoc_rpc_printf (mongoc_rpc_t *rpc)
{
   bson_return_if_fail(rpc);

   switch ((mongoc_opcode_t)rpc->header.opcode) {
   case MONGOC_OPCODE_REPLY:
      _mongoc_rpc_printf_reply(&rpc->reply);
      break;
   case MONGOC_OPCODE_MSG:
      _mongoc_rpc_printf_msg(&rpc->msg);
      break;
   case MONGOC_OPCODE_UPDATE:
      _mongoc_rpc_printf_update(&rpc->update);
      break;
   case MONGOC_OPCODE_INSERT:
      _mongoc_rpc_printf_insert(&rpc->insert);
      break;
   case MONGOC_OPCODE_QUERY:
      _mongoc_rpc_printf_query(&rpc->query);
      break;
   case MONGOC_OPCODE_GET_MORE:
      _mongoc_rpc_printf_get_more(&rpc->get_more);
      break;
   case MONGOC_OPCODE_DELETE:
      _mongoc_rpc_printf_delete(&rpc->delete);
      break;
   case MONGOC_OPCODE_KILL_CURSORS:
      _mongoc_rpc_printf_kill_cursors(&rpc->kill_cursors);
      break;
   default:
      MONGOC_WARNING("Unknown rpc type: 0x%08x", rpc->header.opcode);
      break;
   }
}
Beispiel #23
0
/**
 * bson_reader_destroy:
 * @reader: An initialized bson_reader_t.
 *
 * Releases resources that were allocated during the use of a bson_reader_t.
 * This should be called after you have finished using the structure.
 */
void
bson_reader_destroy (bson_reader_t *reader)
{
   bson_return_if_fail (reader);

   switch (reader->type) {
   case 0:
      break;
   case BSON_READER_FD:
      {
         bson_reader_fd_t *fd = (bson_reader_fd_t *)reader;

         if (fd->close_fd) {
            bson_close (fd->fd);
         }

         bson_free (fd->data);
      }
      break;
   case BSON_READER_DATA:
      break;
   default:
      fprintf (stderr, "No such reader type: %02x\n", reader->type);
      break;
   }

   reader->type = 0;

   bson_free (reader);
}
void
_mongoc_queue_init (mongoc_queue_t *queue)
{
   bson_return_if_fail(queue);

   memset (queue, 0, sizeof *queue);
}
Beispiel #25
0
void
bson_string_append_unichar (bson_string_t  *string,
                            bson_unichar_t  unichar)
{
   bson_uint32_t len;
   char str[7];

   bson_return_if_fail(string);
   bson_return_if_fail(unichar);

   bson_utf8_from_unichar(unichar, str, &len);

   if (len <= 6) {
      str[len] = '\0';
      bson_string_append(string, str);
   }
}
void
mongoc_bulk_operation_set_client (mongoc_bulk_operation_t *bulk,
                                  void                    *client)
{
   bson_return_if_fail (bulk);

   bulk->client = client;
}
void
mongoc_bulk_operation_set_hint (mongoc_bulk_operation_t *bulk,
                                uint32_t                 hint)
{
   bson_return_if_fail (bulk);

   bulk->hint = hint;
}
Beispiel #28
0
void
bson_oid_to_string
(const bson_oid_t *oid,                                   /* IN */
 char              str[BSON_ENSURE_ARRAY_PARAM_SIZE(25)]) /* OUT */
{
#if !defined(__i386__) && !defined(__x86_64__)
    bson_return_if_fail (oid);
    bson_return_if_fail (str);

    bson_snprintf (str, 25,
                   "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                   oid->bytes[0],
                   oid->bytes[1],
                   oid->bytes[2],
                   oid->bytes[3],
                   oid->bytes[4],
                   oid->bytes[5],
                   oid->bytes[6],
                   oid->bytes[7],
                   oid->bytes[8],
                   oid->bytes[9],
                   oid->bytes[10],
                   oid->bytes[11]);
#else
    uint16_t *dst;
    uint8_t *id = (uint8_t *)oid;

    bson_return_if_fail (oid);
    bson_return_if_fail (str);

    dst = (uint16_t *)(void *)str;
    dst[0] = gHexCharPairs[id[0]];
    dst[1] = gHexCharPairs[id[1]];
    dst[2] = gHexCharPairs[id[2]];
    dst[3] = gHexCharPairs[id[3]];
    dst[4] = gHexCharPairs[id[4]];
    dst[5] = gHexCharPairs[id[5]];
    dst[6] = gHexCharPairs[id[6]];
    dst[7] = gHexCharPairs[id[7]];
    dst[8] = gHexCharPairs[id[8]];
    dst[9] = gHexCharPairs[id[9]];
    dst[10] = gHexCharPairs[id[10]];
    dst[11] = gHexCharPairs[id[11]];
    str[24] = '\0';
#endif
}
void
_mongoc_cursor_get_host (mongoc_cursor_t    *cursor,
                         mongoc_host_list_t *host)
{
   bson_return_if_fail(cursor);
   bson_return_if_fail(host);

   memset(host, 0, sizeof *host);

   if (!cursor->hint) {
      MONGOC_WARNING("%s(): Must send query before fetching peer.",
                     __FUNCTION__);
      return;
   }

   *host = cursor->client->cluster.nodes[cursor->hint - 1].host;
   host->next = NULL;
}
void
mock_server_quit (mock_server_t *server,
                  int            code)
{
   bson_return_if_fail(server);

   /*
    * TODO: Exit server loop.
    */
}