static void
_upload_big_file (gridfs_test_t *gridfs_test,
                  bson_oid_t    *oid /* OUT */)
{
   mongoc_gridfs_file_t *file;
   mongoc_iovec_t iov;
   bson_error_t error;
   const bson_value_t *value;

   file = mongoc_gridfs_create_file (gridfs_test->gridfs, NULL);

   iov.iov_base = (void *) gridfs_test->data;
   iov.iov_len = gridfs_test->data_sz;
   if (iov.iov_len != mongoc_gridfs_file_writev (file, &iov, 1, 0)) {
      if (mongoc_gridfs_file_error (file, &error)) {
         MONGOC_ERROR ("file_writev: %s\n", error.message);
      } else {
         MONGOC_ERROR ("file_writev: unknown error\n");
      }

      abort ();
   }

   mongoc_gridfs_file_save (file);
   value = mongoc_gridfs_file_get_id (file);
   assert (value->value_type == BSON_TYPE_OID);
   bson_oid_copy (&value->value.v_oid, oid);

   mongoc_gridfs_file_destroy (file);
}
Пример #2
0
void
mongoc_apm_server_opening_get_topology_id (
   const mongoc_apm_server_opening_t *event,
   bson_oid_t                        *topology_id)
{
   bson_oid_copy (&event->topology_id, topology_id);
}
/*
 *--------------------------------------------------------------------------
 *
 * _mongoc_topology_description_set_max_election_id --
 *
 *       Remember that we've seen a new election id. Unconditionally sets
 *       td->max_election_id to sd->election_id.
 *
 *--------------------------------------------------------------------------
 */
static void
_mongoc_topology_description_set_max_election_id (
   mongoc_topology_description_t *td,
   mongoc_server_description_t   *sd)
{
   bson_oid_copy (&sd->election_id, &td->max_election_id);
}
Пример #4
0
void
mongoc_apm_topology_closed_get_topology_id (
   const mongoc_apm_topology_closed_t *event,
   bson_oid_t                         *topology_id)
{
   bson_oid_copy (&event->topology_id, topology_id);
}
Пример #5
0
static void
test_bson_oid_copy (void)
{
   bson_oid_t oid;
   bson_oid_t oid2;

   bson_oid_init_from_string (&oid, "000000000000000000001234");
   bson_oid_init_from_string (&oid2, "000000000000000000004321");
   bson_oid_copy (&oid, &oid2);
   BSON_ASSERT (true == bson_oid_equal (&oid, &oid2));
}
Пример #6
0
static void
test_bson_oid_copy (void)
{
   bson_oid_t oid;
   bson_oid_t oid2;

   bson_oid_init_from_string(&oid, "000000000000000000001234");
   bson_oid_init_from_string(&oid2, "000000000000000000004321");
   bson_oid_copy(&oid, &oid2);
   assert(TRUE == bson_oid_equal(&oid, &oid2));
}
/* TopologyClosedEvent */
void
_mongoc_topology_description_monitor_closed (
    const mongoc_topology_description_t *td)
{
    if (td->apm_callbacks.topology_closed) {
        mongoc_apm_topology_closed_t event;

        bson_oid_copy (&td->topology_id, &event.topology_id);
        event.context = td->apm_context;
        td->apm_callbacks.topology_closed (&event);
    }
}
/**
 * _mongoc_gridfs_file_new_from_bson:
 *
 * creates a gridfs file from a bson object
 *
 * This is only really useful for instantiating a gridfs file from a server
 * side object
 */
mongoc_gridfs_file_t *
_mongoc_gridfs_file_new_from_bson (mongoc_gridfs_t *gridfs,
                                   const bson_t    *data)
{
   mongoc_gridfs_file_t *file;
   const char *key;
   bson_iter_t iter;
   const uint8_t *buf;
   uint32_t buf_len;

   ENTRY;

   BSON_ASSERT (gridfs);
   BSON_ASSERT (data);

   file = bson_malloc0 (sizeof *file);

   file->gridfs = gridfs;
   bson_copy_to (data, &file->bson);

   bson_iter_init (&iter, &file->bson);

   while (bson_iter_next (&iter)) {
      key = bson_iter_key (&iter);

      if (0 == strcmp (key, "_id")) {
         bson_oid_copy (bson_iter_oid (&iter), &file->files_id);
      } else if (0 == strcmp (key, "length")) {
         file->length = bson_iter_int64 (&iter);
      } else if (0 == strcmp (key, "chunkSize")) {
         file->chunk_size = bson_iter_int32 (&iter);
      } else if (0 == strcmp (key, "uploadDate")) {
         file->upload_date = bson_iter_date_time (&iter);
      } else if (0 == strcmp (key, "md5")) {
         file->bson_md5 = bson_iter_utf8 (&iter, NULL);
      } else if (0 == strcmp (key, "filename")) {
         file->bson_filename = bson_iter_utf8 (&iter, NULL);
      } else if (0 == strcmp (key, "contentType")) {
         file->bson_content_type = bson_iter_utf8 (&iter, NULL);
      } else if (0 == strcmp (key, "aliases")) {
         bson_iter_array (&iter, &buf_len, &buf);
         bson_init_static (&file->bson_aliases, buf, buf_len);
      } else if (0 == strcmp (key, "metadata")) {
         bson_iter_document (&iter, &buf_len, &buf);
         bson_init_static (&file->bson_metadata, buf, buf_len);
      }
   }

   /* TODO: is there are a minimal object we should be verifying that we
    * actually have here? */

   RETURN (file);
}
/* ServerClosedEvent */
void
_mongoc_topology_description_monitor_server_closed (
    const mongoc_topology_description_t *td,
    const mongoc_server_description_t *sd)
{
    if (td->apm_callbacks.server_closed) {
        mongoc_apm_server_closed_t event;

        bson_oid_copy (&td->topology_id, &event.topology_id);
        event.host = &sd->host;
        event.context = td->apm_context;
        td->apm_callbacks.server_closed (&event);
    }
}
Пример #10
0
Object createMongoBsonObjectIDObject(const bson_oid_t *v_oid)
{
	static Class* c_objectId;

	c_objectId = Unit::lookupClass(s_MongoBsonObjectID_className.get());
	assert(c_objectId);
	Object obj = Object{c_objectId};

	MongoDBBsonObjectIDData* obj_data = Native::data<MongoDBBsonObjectIDData>(obj.get());
	bson_oid_copy(v_oid, &obj_data->m_oid);
	obj->o_set(s_MongoBsonObjectID_oid, Variant(oidAsString(obj_data)), s_MongoBsonObjectID_className);

	return obj;
}
Пример #11
0
static void
test_bson_oid_init_sequence_thread_safe (void)
{
   bson_context_t context;
   bson_oid_t oid;
   bson_oid_t oid2;
   int i;

   bson_context_init(&context, BSON_CONTEXT_THREAD_SAFE);
   bson_oid_init_sequence(&oid, &context);
   for (i = 0; i < 10000; i++) {
      bson_oid_init_sequence(&oid2, &context);
      assert(FALSE == bson_oid_equal(&oid, &oid2));
      assert(0 > bson_oid_compare(&oid, &oid2));
      bson_oid_copy(&oid2, &oid);
   }
}
Пример #12
0
static void
test_bson_oid_init_sequence_with_tid (void)
{
   bson_context_t context;
   bson_oid_t oid;
   bson_oid_t oid2;
   int i;

   bson_context_init(&context, BSON_CONTEXT_USE_TASK_ID);
   bson_oid_init_sequence(&oid, &context);
   for (i = 0; i < 10000; i++) {
      bson_oid_init_sequence(&oid2, &context);
      assert(FALSE == bson_oid_equal(&oid, &oid2));
      assert(0 > bson_oid_compare(&oid, &oid2));
      bson_oid_copy(&oid2, &oid);
   }
}
/* TopologyDescriptionChangedEvent */
void
_mongoc_topology_description_monitor_changed (
    const mongoc_topology_description_t *prev_td,
    const mongoc_topology_description_t *new_td)
{
    if (new_td->apm_callbacks.topology_changed) {
        mongoc_apm_topology_changed_t event;

        /* callbacks, context, and id are the same in previous and new td */
        bson_oid_copy (&new_td->topology_id, &event.topology_id);
        event.context = new_td->apm_context;
        event.previous_description = prev_td;
        event.new_description = new_td;

        new_td->apm_callbacks.topology_changed (&event);
    }
}
Пример #14
0
static void
test_bson_oid_init (void)
{
   bson_context_t context;
   bson_oid_t oid;
   bson_oid_t oid2;
   int i;

   bson_context_init(&context, BSON_CONTEXT_NONE);
   bson_oid_init(&oid, &context);
   for (i = 0; i < 10000; i++) {
      bson_oid_init(&oid2, &context);
      assert(FALSE == bson_oid_equal(&oid, &oid2));
      assert(0 > bson_oid_compare(&oid, &oid2));
      bson_oid_copy(&oid2, &oid);
   }
}
/* Send TopologyOpeningEvent when first called on this topology description.
 * td is not const: we set its "opened" field here */
void
_mongoc_topology_description_monitor_opening (mongoc_topology_description_t *td)
{
    mongoc_topology_description_t *prev_td = NULL;
    size_t i;
    mongoc_server_description_t *sd;

    if (td->opened) {
        return;
    }

    if (td->apm_callbacks.topology_changed) {
        /* prepare to call monitor_changed */
        prev_td = bson_malloc0 (sizeof (mongoc_topology_description_t));
        mongoc_topology_description_init (
            prev_td, MONGOC_TOPOLOGY_UNKNOWN, td->heartbeat_msec);
    }

    td->opened = true;

    if (td->apm_callbacks.topology_opening) {
        mongoc_apm_topology_opening_t event;

        bson_oid_copy (&td->topology_id, &event.topology_id);
        event.context = td->apm_context;
        td->apm_callbacks.topology_opening (&event);
    }

    if (td->apm_callbacks.topology_changed) {
        /* send initial description-changed event */
        _mongoc_topology_description_monitor_changed (prev_td, td);
    }

    for (i = 0; i < td->servers->items_len; i++) {
        sd = (mongoc_server_description_t *) mongoc_set_get_item (td->servers,
                (int) i);

        _mongoc_topology_description_monitor_server_opening (td, sd);
    }

    if (prev_td) {
        mongoc_topology_description_destroy (prev_td);
        bson_free (prev_td);
    }
}
Пример #16
0
static void
test_bson_oid_init_sequence_thread_safe (void)
{
   bson_context_t *context;
   bson_oid_t oid;
   bson_oid_t oid2;
   int i;

   context = bson_context_new (BSON_CONTEXT_THREAD_SAFE);
   bson_oid_init_sequence (&oid, context);
   for (i = 0; i < 10000; i++) {
      bson_oid_init_sequence (&oid2, context);
      BSON_ASSERT (false == bson_oid_equal (&oid, &oid2));
      BSON_ASSERT (0 > bson_oid_compare (&oid, &oid2));
      bson_oid_copy (&oid2, &oid);
   }
   bson_context_destroy (context);
}
Пример #17
0
static void
test_bson_oid_init_sequence (void)
{
   bson_context_t *context;
   bson_oid_t oid;
   bson_oid_t oid2;
   int i;

   context = bson_context_new(BSON_CONTEXT_NONE);
   bson_oid_init_sequence(&oid, context);
   for (i = 0; i < 10000; i++) {
      bson_oid_init_sequence(&oid2, context);
      assert(FALSE == bson_oid_equal(&oid, &oid2));
      assert(0 > bson_oid_compare(&oid, &oid2));
      bson_oid_copy(&oid2, &oid);
   }
   bson_context_destroy(context);
}
/* ServerDescriptionChangedEvent */
void
_mongoc_topology_description_monitor_server_changed (
    const mongoc_topology_description_t *td,
    const mongoc_server_description_t *prev_sd,
    const mongoc_server_description_t *new_sd)
{
    if (td->apm_callbacks.server_changed) {
        mongoc_apm_server_changed_t event;

        /* address is same in previous and new sd */
        bson_oid_copy (&td->topology_id, &event.topology_id);
        event.host = &new_sd->host;
        event.previous_description = prev_sd;
        event.new_description = new_sd;
        event.context = td->apm_context;
        td->apm_callbacks.server_changed (&event);
    }
}
Пример #19
0
static void
test_bson_oid_init_sequence_with_tid (void)
{
   bson_context_t *context;
   bson_oid_t oid;
   bson_oid_t oid2;
   int i;

   context = bson_context_new (BSON_CONTEXT_USE_TASK_ID);
   bson_oid_init_sequence (&oid, context);
   for (i = 0; i < 10000; i++) {
      bson_oid_init_sequence (&oid2, context);
      BSON_ASSERT (false == bson_oid_equal (&oid, &oid2));
      BSON_ASSERT (0 > bson_oid_compare (&oid, &oid2));
      bson_oid_copy (&oid2, &oid);
   }
   bson_context_destroy (context);
}
Пример #20
0
static void *
oid_worker (void *data)
{
   bson_context_t *context = data;
   bson_oid_t oid;
   bson_oid_t oid2;
   int i;

   bson_oid_init(&oid2, context);
   for (i = 0; i < 500000; i++) {
      bson_oid_init(&oid, context);
      assert(FALSE == bson_oid_equal(&oid, &oid2));
      assert(0 < bson_oid_compare(&oid, &oid2));
      bson_oid_copy(&oid, &oid2);
   }

   return NULL;
}
Пример #21
0
static void
test_bson_oid_init (void)
{
   bson_context_t *context;
   bson_oid_t oid;
   bson_oid_t oid2;
   int i;

   context = bson_context_new(BSON_CONTEXT_NONE);
   bson_oid_init(&oid, context);
   for (i = 0; i < 10000; i++) {
      bson_oid_init(&oid2, context);
      assert(FALSE == bson_oid_equal(&oid, &oid2));
      assert(0 > bson_oid_compare(&oid, &oid2));
      bson_oid_copy(&oid2, &oid);
   }
   bson_context_destroy(context);

   /*
    * Test that the shared context works.
    */
   bson_oid_init(&oid, NULL);
   BSON_ASSERT(bson_context_get_default());
}
Пример #22
0
void
bson_value_copy (const bson_value_t *src, /* IN */
                 bson_value_t       *dst) /* OUT */
{
   BSON_ASSERT (src);
   BSON_ASSERT (dst);

   dst->value_type = src->value_type;

   switch (src->value_type) {
   case BSON_TYPE_DOUBLE:
      dst->value.v_double = src->value.v_double;
      break;
   case BSON_TYPE_UTF8:
      dst->value.v_utf8.len = src->value.v_utf8.len;
      dst->value.v_utf8.str = bson_malloc (src->value.v_utf8.len + 1);
      memcpy (dst->value.v_utf8.str,
              src->value.v_utf8.str,
              dst->value.v_utf8.len);
      dst->value.v_utf8.str [dst->value.v_utf8.len] = '\0';
      break;
   case BSON_TYPE_DOCUMENT:
   case BSON_TYPE_ARRAY:
      dst->value.v_doc.data_len = src->value.v_doc.data_len;
      dst->value.v_doc.data = bson_malloc (src->value.v_doc.data_len);
      memcpy (dst->value.v_doc.data,
              src->value.v_doc.data,
              dst->value.v_doc.data_len);
      break;
   case BSON_TYPE_BINARY:
      dst->value.v_binary.subtype = src->value.v_binary.subtype;
      dst->value.v_binary.data_len = src->value.v_binary.data_len;
      dst->value.v_binary.data = bson_malloc (src->value.v_binary.data_len);
      memcpy (dst->value.v_binary.data,
              src->value.v_binary.data,
              dst->value.v_binary.data_len);
      break;
   case BSON_TYPE_OID:
      bson_oid_copy (&src->value.v_oid, &dst->value.v_oid);
      break;
   case BSON_TYPE_BOOL:
      dst->value.v_bool = src->value.v_bool;
      break;
   case BSON_TYPE_DATE_TIME:
      dst->value.v_datetime = src->value.v_datetime;
      break;
   case BSON_TYPE_REGEX:
      dst->value.v_regex.regex = bson_strdup (src->value.v_regex.regex);
      dst->value.v_regex.options = bson_strdup (src->value.v_regex.options);
      break;
   case BSON_TYPE_DBPOINTER:
      dst->value.v_dbpointer.collection_len = src->value.v_dbpointer.collection_len;
      dst->value.v_dbpointer.collection = bson_malloc (src->value.v_dbpointer.collection_len + 1);
      memcpy (dst->value.v_dbpointer.collection,
              src->value.v_dbpointer.collection,
              dst->value.v_dbpointer.collection_len);
      dst->value.v_dbpointer.collection [dst->value.v_dbpointer.collection_len] = '\0';
      bson_oid_copy (&src->value.v_dbpointer.oid, &dst->value.v_dbpointer.oid);
      break;
   case BSON_TYPE_CODE:
      dst->value.v_code.code_len = src->value.v_code.code_len;
      dst->value.v_code.code = bson_malloc (src->value.v_code.code_len + 1);
      memcpy (dst->value.v_code.code,
              src->value.v_code.code,
              dst->value.v_code.code_len);
      dst->value.v_code.code [dst->value.v_code.code_len] = '\0';
      break;
   case BSON_TYPE_SYMBOL:
      dst->value.v_symbol.len = src->value.v_symbol.len;
      dst->value.v_symbol.symbol = bson_malloc (src->value.v_symbol.len + 1);
      memcpy (dst->value.v_symbol.symbol,
              src->value.v_symbol.symbol,
              dst->value.v_symbol.len);
      dst->value.v_symbol.symbol [dst->value.v_symbol.len] = '\0';
      break;
   case BSON_TYPE_CODEWSCOPE:
      dst->value.v_codewscope.code_len = src->value.v_codewscope.code_len;
      dst->value.v_codewscope.code = bson_malloc (src->value.v_codewscope.code_len + 1);
      memcpy (dst->value.v_codewscope.code,
              src->value.v_codewscope.code,
              dst->value.v_codewscope.code_len);
      dst->value.v_codewscope.code [dst->value.v_codewscope.code_len] = '\0';
      dst->value.v_codewscope.scope_len = src->value.v_codewscope.scope_len;
      dst->value.v_codewscope.scope_data = bson_malloc (src->value.v_codewscope.scope_len);
      memcpy (dst->value.v_codewscope.scope_data,
              src->value.v_codewscope.scope_data,
              dst->value.v_codewscope.scope_len);
      break;
   case BSON_TYPE_INT32:
      dst->value.v_int32 = src->value.v_int32;
      break;
   case BSON_TYPE_TIMESTAMP:
      dst->value.v_timestamp.timestamp = src->value.v_timestamp.timestamp;
      dst->value.v_timestamp.increment = src->value.v_timestamp.increment;
      break;
   case BSON_TYPE_INT64:
      dst->value.v_int64 = src->value.v_int64;
      break;
   case BSON_TYPE_UNDEFINED:
   case BSON_TYPE_NULL:
   case BSON_TYPE_MAXKEY:
   case BSON_TYPE_MINKEY:
      break;
   case BSON_TYPE_EOD:
   default:
      BSON_ASSERT (false);
      return;
   }
}
Пример #23
0
void VariantToBsonConverter::convertDocument(bson_t *bson, const char *property_name, Variant v)
{
	bson_t child;
	int unmangle = 0;
	Array document;

	/* if we are not at a top-level, we need to check (and convert) special
	 * BSON types too */
	if (v.isObject()) {
		if (convertSpecialObject(bson, property_name, v.toObject())) {
			return;
		}
		/* The "convertSpecialObject" method didn't understand this type, so we
		 * will continue treating this as a normal document */
	}

	document = v.toObject()->o_toIterArray(null_string, ObjectData::PreserveRefs);

	if (_isPackedArray(document) && !v.isObject()) {
		if (property_name != NULL) {
			bson_append_array_begin(bson, property_name, -1, &child);
		}
	} else {
		unmangle = 1;
		if (property_name != NULL) {
			bson_append_document_begin(bson, property_name, -1, &child);
		}
	}

	for (ArrayIter iter(document); iter; ++iter) {
		Variant key(iter.first());
		const Variant& data(iter.secondRef());
		String s_key = key.toString();

		if (m_level == 0 && (m_flags & HIPPO_BSON_ADD_ID)) {
			/* If we have an ID, we don't need to add it. But we also need to
			 * set m_out to the value! */
			if (strncmp(s_key.c_str(), "_id", s_key.length()) == 0) {
				m_flags &= ~HIPPO_BSON_ADD_ID;
				if (m_flags & HIPPO_BSON_RETURN_ID) {
					/* FIXME: Should we add a ref here? */
					m_out = data;
				}
			}
		}

		m_level++;
		if (unmangle) {
			const char *unmangledName;

			unmangledName = _getUnmangledPropertyName(s_key);
			convertElement(property_name != NULL ? &child : bson, unmangledName, data);
			free((void*) unmangledName);
		} else {
			convertElement(property_name != NULL ? &child : bson, s_key.c_str(), data);
		}
		m_level--;
	}

	if (m_level == 0 && (m_flags & HIPPO_BSON_ADD_ID)) {
		bson_oid_t oid;

		bson_oid_init(&oid, NULL);
		bson_append_oid(bson, "_id", strlen("_id"), &oid);

		if (m_flags & HIPPO_BSON_RETURN_ID) {
			static Class* c_objectId;
			c_objectId = Unit::lookupClass(s_MongoBsonObjectID_className.get());
			assert(c_objectId);
			Object obj = Object{c_objectId};

			MongoDBBsonObjectIDData* obj_data = Native::data<MongoDBBsonObjectIDData>(obj.get());
			bson_oid_copy(&oid, &obj_data->m_oid);

			m_out = obj;
		}
	}

	if (property_name != NULL) {
		if (_isPackedArray(document)) {
			bson_append_array_end(bson, &child);
		} else {
			bson_append_document_end(bson, &child);
		}
	}
}