Beispiel #1
0
static GError*
_load_meta1ref_from_base(struct sqlx_sqlite3_s *sq3, GPtrArray **result)
{
        GError *err = NULL;
        GPtrArray *array;
        sqlite3_stmt *stmt;
        int rc;
        guint count = 0;

	array = g_ptr_array_new();

        sqlite3_prepare_debug(rc, sq3->db, "SELECT addr,state,prefixes FROM meta1_ref",
			 -1, &stmt, NULL);
        if (rc != SQLITE_OK && rc != SQLITE_DONE) {
		if ( rc == SQLITE_ERROR ) {
			GRID_DEBUG("Missing table meta1ref in DB");
			*result = array;
			return NULL;
		}
                return SQLITE_GERROR(sq3->db, rc);
	}

	for (;;) {
                rc = sqlite3_step(stmt);
                if (rc == SQLITE_ROW) {
			const unsigned char *url,*prefix_nb,*ref;
			url = sqlite3_column_text(stmt,0);
			ref = sqlite3_column_text(stmt,1);
			prefix_nb = sqlite3_column_text(stmt,2);

			GRID_INFO("url %s, ref %s,prefix_nb %s ",url,ref,prefix_nb);
			g_ptr_array_add(array,meta0_utils_pack_meta1ref((gchar *)url,(gchar *)ref,(gchar *)prefix_nb));
			count++;
		}
		else if (rc == SQLITE_DONE || rc == SQLITE_OK)
                        break;
                else if (rc == SQLITE_BUSY)
                        sleep(1);
                else {
                        err = SQLITE_GERROR(sq3->db, rc);
                        break;
                }

	}
	sqlite3_finalize_debug(rc, stmt);

        if (!err) {
                *result = array;
                GRID_INFO("Reloaded %u meta1 in %p (%u)",
                                count, array, array->len);
        }

        return err;
}
static GError*
_update_container_quota(struct meta1_backend_s *m1,
		struct container_info_s *cinfo)
{
	GError *e0 = NULL;
	gint rc;
	sqlite3_stmt *stmt = NULL;
	struct sqlx_sqlite3_s *sq3 = NULL;
	struct sqlx_repctx_s *repctx = NULL;

	e0 = meta1_backend_open_base(m1, cinfo->id, M1V2_OPENBASE_MASTERONLY, &sq3);
	if (NULL != e0)
		return e0;

	e0 = sqlx_transaction_begin(sq3, &repctx);
	if (NULL != e0) {
		sqlx_repository_unlock_and_close_noerror(sq3);
		return e0;
	}

	sqlite3_prepare_debug(rc, sq3->db, "UPDATE properties SET"
			" value = ?"
			" WHERE cid = ?"
			" AND name = 'meta2.quota'", -1, &stmt, NULL);
	(void) sqlite3_bind_int64(stmt, 1, cinfo->size);
	(void) sqlite3_bind_blob(stmt, 2, cinfo->id,
			sizeof(container_id_t), NULL);

	for (rc=SQLITE_ROW; rc == SQLITE_ROW ;)
		rc = sqlite3_step(stmt);
	if (rc != SQLITE_OK && rc != SQLITE_DONE)
		e0 = SQLITE_GERROR(sq3->db, rc);
	sqlite3_finalize_debug(rc, stmt);

	if (!e0 && sqlite3_changes(sq3->db) <= 0) {
		sqlite3_prepare_debug(rc, sq3->db,
				"INSERT INTO properties (cid,name,value)"
				" VALUES (?,?,?)", -1, &stmt, NULL);
		(void) sqlite3_bind_blob(stmt, 1, cinfo->id, sizeof(container_id_t), NULL);
		(void) sqlite3_bind_text(stmt, 2, "meta2.quota", sizeof("meta2.quota")-1, NULL);
		(void) sqlite3_bind_int64(stmt, 3, cinfo->size);

		for (rc=SQLITE_ROW; rc == SQLITE_ROW ;)
			rc = sqlite3_step(stmt);
		if (rc != SQLITE_OK && rc != SQLITE_DONE)
			e0 = SQLITE_GERROR(sq3->db, rc);
		sqlite3_finalize_debug(rc, stmt);
	}

	/* Commit then close the container's base */
	e0 = sqlx_transaction_end(repctx, e0);
	sqlx_repository_unlock_and_close_noerror(sq3);
	return e0;
}
Beispiel #3
0
static GError *
_record_meta1ref(sqlite3 *db, const GPtrArray *new_assign_meta1ref)
{
	GError *err = NULL;
	gint rc;
	guint idx;
	sqlite3_stmt *stmt = NULL;

	sqlite3_prepare_debug(rc, db, "REPLACE INTO meta1_ref"
			" (addr,state,prefixes) VALUES (?,?,?)", -1, &stmt, NULL);
	if (rc != SQLITE_OK && rc != SQLITE_DONE) {
		if ( rc == SQLITE_ERROR ) {
			GRID_DEBUG("Missing table meta1ref in DB");
			return NULL;
		}
		return SQLITE_GERROR(db, rc);
	}

	for (idx=0; idx < new_assign_meta1ref->len; idx++) {
		gchar *m1ref = new_assign_meta1ref->pdata[idx];
		gchar *addr, *ref, *nb;
		if ( ! meta0_utils_unpack_meta1ref(m1ref,&addr,&ref,&nb) )
			continue;
		sqlite3_reset(stmt);
		sqlite3_clear_bindings(stmt);
		sqlite3_bind_text(stmt, 1, addr, -1, NULL);
		sqlite3_bind_text(stmt, 2, ref, -1, NULL);
		sqlite3_bind_text(stmt, 3, nb, -1, NULL);

		while (!err) {
			rc = sqlite3_step(stmt);
			if (rc == SQLITE_OK || rc == SQLITE_DONE)
				break;
			if (rc == SQLITE_BUSY)
				sleep(1);
			else
				err = SQLITE_GERROR(db, rc);
		}

		if (addr)
			g_free(addr);
		if (ref)
			g_free(ref);
		if (nb)
			g_free(nb);
	}
	sqlite3_finalize_debug(rc, stmt);
	return err;
}
Beispiel #4
0
static GError *
_delete_meta1_ref(sqlite3 *db, gchar *meta1_ref)
{
	GError *err = NULL;
	gint rc;
	sqlite3_stmt *stmt = NULL;

	sqlite3_prepare_debug(rc, db, "DELETE FROM meta1_ref where addr=?",
			-1, &stmt, NULL);
	if (rc != SQLITE_OK && rc != SQLITE_DONE)
		return SQLITE_GERROR(db, rc);

	(void) sqlite3_bind_text(stmt,1,meta1_ref, strlen(meta1_ref), NULL);
	do { rc = sqlite3_step(stmt); } while (rc == SQLITE_ROW );
	if (rc != SQLITE_OK && rc != SQLITE_DONE)
		err = SQLITE_GERROR(db, rc);

	sqlite3_finalize_debug(rc, stmt);
	return err;
}
Beispiel #5
0
static GError*
__fill(sqlite3 *db, gchar **urls, guint max, guint shift)
{
	gint rc;
	guint idx;
	sqlite3_stmt *stmt = NULL;
	GError *err = NULL;

	sqlite3_prepare_debug(rc, db, "INSERT INTO meta1"
			" (prefix,addr) VALUES (?,?)", -1, &stmt, NULL);
	if (rc != SQLITE_OK && rc != SQLITE_DONE)
		return SQLITE_GERROR(db, rc);

	/* One partition for each url */
	for (idx=0; idx<65536 ;idx++) {

		gchar **purl = urls + ((idx+shift) % max);
		guint16 index16 = idx;

		sqlite3_reset(stmt);
		sqlite3_clear_bindings(stmt);
		sqlite3_bind_blob(stmt, 1, &index16, 2, NULL);
		sqlite3_bind_text(stmt, 2, *purl, -1, NULL);
		while (!err) {
			rc = sqlite3_step(stmt);
			if (rc == SQLITE_OK || rc == SQLITE_DONE)
				break;
			if (rc == SQLITE_BUSY)
				sleep(1);
			else
				err = SQLITE_GERROR(db, rc);
		}
	}
	sqlite3_finalize_debug(rc, stmt);

	return err;
}
Beispiel #6
0
static GError *
_assign_prefixes(sqlite3 *db, const GPtrArray *new_assign_prefixes,
		gboolean init)
{
	GError *err = NULL;
	gint rc;
	guint idx;
	sqlite3_stmt *stmt = NULL;

	if ( !init ) {
		sqlite3_prepare_debug(rc, db, "DELETE FROM meta1", -1, &stmt, NULL);
		if (rc != SQLITE_OK && rc != SQLITE_DONE)
			return SQLITE_GERROR(db, rc);
		while (!err) {
			rc = sqlite3_step(stmt);
			if (rc == SQLITE_OK || rc == SQLITE_DONE)
				break;
			if (rc == SQLITE_BUSY)
				sleep(1);
			else {
				return SQLITE_GERROR(db,rc);
			}
		}
		sqlite3_finalize_debug(rc, stmt);
	}

	sqlite3_prepare_debug(rc, db, "INSERT  INTO meta1"
			" (prefix,addr) VALUES (?,?)", -1, &stmt, NULL);

	if (rc != SQLITE_OK && rc != SQLITE_DONE) {
		return SQLITE_GERROR(db, rc);
	}

	for (idx=0; idx<65536 ;idx++) {

		gchar **url = new_assign_prefixes->pdata[idx];
		guint16 index16 = idx;

		if (!url || ! *url )
			continue;
		for (; *url ;url++) {
			sqlite3_reset(stmt);
			sqlite3_clear_bindings(stmt);
			sqlite3_bind_blob(stmt, 1, &index16, 2, NULL);
			sqlite3_bind_text(stmt, 2, *url, -1, NULL);
			while (!err) {
				rc = sqlite3_step(stmt);
				if (rc == SQLITE_OK || rc == SQLITE_DONE)
					break;
				if (rc == SQLITE_BUSY)
					sleep(1);
				else {
					err = SQLITE_GERROR(db, rc);
				}
			}
		}
	}
	sqlite3_finalize_debug(rc, stmt);

	return err;

}