[svn-commits] dlee: branch dlee/stasis-cache-split r393982 - /team/dlee/stasis-cache-split/...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Wed Jul 10 10:59:22 CDT 2013


Author: dlee
Date: Wed Jul 10 10:59:21 2013
New Revision: 393982

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=393982
Log:
Revert stasis_cache back to sanity

Modified:
    team/dlee/stasis-cache-split/main/stasis_cache.c

Modified: team/dlee/stasis-cache-split/main/stasis_cache.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-cache-split/main/stasis_cache.c?view=diff&rev=393982&r1=393981&r2=393982
==============================================================================
--- team/dlee/stasis-cache-split/main/stasis_cache.c (original)
+++ team/dlee/stasis-cache-split/main/stasis_cache.c Wed Jul 10 10:59:21 2013
@@ -56,6 +56,7 @@
 	struct stasis_topic *topic;
 	struct stasis_topic *original_topic;
 	struct stasis_subscription *sub;
+	snapshot_get_id id_fn;
 };
 
 static void stasis_caching_topic_dtor(void *obj) {
@@ -151,7 +152,7 @@
 	return entry;
 }
 
-static int cache_entry_hash(const void *obj, int flags)
+__attribute__((unused)) static int cache_entry_hash(const void *obj, int flags)
 {
 	const struct cache_entry *entry = obj;
 	int hash = 0;
@@ -163,7 +164,7 @@
 	return hash;
 }
 
-static int cache_entry_cmp(void *obj, void *arg, int flags)
+__attribute__((unused)) static int cache_entry_cmp(void *obj, void *arg, int flags)
 {
 	const struct cache_entry *left = obj;
 	const struct cache_entry *right = arg;
@@ -177,136 +178,23 @@
 	return 0;
 }
 
-static void cache_dtor(void *obj)
-{
-	struct stasis_cache *cache = obj;
-
-	ao2_cleanup(cache->entries);
-	cache->entries = NULL;
-	ao2_cleanup(cache->secondary_cache);
-	cache->secondary_cache = NULL;
-}
-
-struct stasis_cache *stasis_cache_create(snapshot_get_id id_fn)
-{
-	RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
-
-	cache = ao2_alloc(sizeof(*cache), cache_dtor);
-	if (!cache) {
-		return NULL;
-	}
-
-	cache->entries = ao2_container_alloc(NUM_CACHE_BUCKETS, cache_entry_hash,
-		cache_entry_cmp);
-	if (!cache->entries) {
-		return NULL;
-	}
-
-	cache->id_fn = id_fn;
-
-	ao2_ref(cache, +1);
-	return cache;
-}
-
-struct stasis_cache *stasis_cache_create_secondary(
-	struct stasis_cache *primary_cache, snapshot_get_id id_fn)
-{
-	RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
-
-	if (!primary_cache) {
-		return NULL;
-	}
-
-	cache = stasis_cache_create(id_fn);
-	if (!cache) {
-		return NULL;
-	}
-
-	{
-		SCOPED_AO2LOCK(lock, parent);
-		cache->secondary_cache = parent->secondary_cache;
-		ao2_ref(cache, +1);
-		parent->secondary_cache = cache;
-	}
-
-	ao2_ref(cache, +1);
-	return cache;
-}
-
-struct stasis_message *stasis_cache_get(struct stasis_cache *cache,
-	struct stasis_message_type *type, const char *id)
-{
-	RAII_VAR(struct cache_entry *, search_entry, NULL, ao2_cleanup);
+static struct stasis_message *cache_put(struct stasis_cache *cache, struct stasis_message_type *type, const char *id, struct stasis_message *new_snapshot)
+{
+	RAII_VAR(struct cache_entry *, new_entry, NULL, ao2_cleanup);
 	RAII_VAR(struct cache_entry *, cached_entry, NULL, ao2_cleanup);
-
-	search_entry = cache_entry_create(type, id, NULL);
-	if (search_entry == NULL) {
-		return NULL;
-	}
-
-	cached_entry = ao2_find(cache->entries, search_entry, OBJ_POINTER);
-	if (cached_entry == NULL) {
-		return NULL;
-	}
-
-	ast_assert(cached_entry->snapshot != NULL);
-	ao2_ref(cached_entry->snapshot, +1);
-	return cached_entry->snapshot;
-}
-
-static struct stasis_message *cache_put(struct stasis_cache *cache,
-	struct stasis_message *message);
-
-static void cache_put_secondary(struct stasis_cache *cache,
-	struct stasis_message *message)
-{
-	RAII_VAR(struct stasis_cache *, secondary_cache, NULL, ao2_cleanup);
-
-	{
-		SCOPED_AO2LOCK(lock, cache);
-		secondary_cache = cache->secondary_cache;
-		if (!secondary_cache) {
-			return;
-		}
-		ao2_ref(secondary_cache, +1);
-	}
-
-	cache_put(secondary_cache, message);
-}
-
-static struct stasis_message *cache_put(struct stasis_cache *cache,
-	struct stasis_message *message)
-{
-	RAII_VAR(struct cache_entry *, cached_entry, NULL, ao2_cleanup);
-	const char *id;
-
-	/* update secondary cache */
-	cache_put_secondary(cache, message);
-
-	/* Handle cache clear event */
-	if (stasis_cache_clear_type() == stasis_message_type(message)) {
-		RAII_VAR(struct cache_entry *, clear_entry, NULL, ao2_cleanup);
-		struct stasis_message *clear_snapshot =
-			stasis_message_data(message);
-		id = cache->id_fn(clear_snapshot);
-
-		clear_entry = cache_entry_create(type, id, clear_snapshot);
-
-		cached_entry = ao2_find(cache->entries, clear_entry, OBJ_POINTER | OBJ_UNLINK);
+	struct stasis_message *old_snapshot = NULL;
+
+	ast_assert(cache->entries != NULL);
+
+	new_entry = cache_entry_create(type, id, new_snapshot);
+
+	if (new_snapshot == NULL) {
+		/* Remove entry from cache */
+		cached_entry = ao2_find(cache->entries, new_entry, OBJ_POINTER | OBJ_UNLINK);
 		if (cached_entry) {
 			old_snapshot = cached_entry->snapshot;
 			cached_entry->snapshot = NULL;
 		}
-	}
-
-	if (!id) {
-		return NULL;
-	}
-
-	ast_assert(cache->entries != NULL);
-
-
-	if (is_clear) {
 	} else {
 		/* Insert/update cache */
 		SCOPED_AO2LOCK(lock, cache->entries);
@@ -321,10 +209,32 @@
 			/* Insert into the cache */
 			ao2_link_flags(cache->entries, new_entry, OBJ_NOLOCK);
 		}
-	}
-
+
+	}
 
 	return old_snapshot;
+}
+
+struct stasis_message *stasis_cache_get(struct stasis_cache *cache, struct stasis_message_type *type, const char *id)
+{
+	RAII_VAR(struct cache_entry *, search_entry, NULL, ao2_cleanup);
+	RAII_VAR(struct cache_entry *, cached_entry, NULL, ao2_cleanup);
+
+	ast_assert(cache->entries != NULL);
+
+	search_entry = cache_entry_create(type, id, NULL);
+	if (search_entry == NULL) {
+		return NULL;
+	}
+
+	cached_entry = ao2_find(cache->entries, search_entry, OBJ_POINTER);
+	if (cached_entry == NULL) {
+		return NULL;
+	}
+
+	ast_assert(cached_entry->snapshot != NULL);
+	ao2_ref(cached_entry->snapshot, +1);
+	return cached_entry->snapshot;
 }
 
 struct cache_dump_data {
@@ -431,45 +341,42 @@
 static void caching_topic_exec(void *data, struct stasis_subscription *sub, struct stasis_topic *topic, struct stasis_message *message)
 {
 	RAII_VAR(struct stasis_caching_topic *, caching_topic_needs_unref, NULL, ao2_cleanup);
-	RAII_VAR(struct stasis_message *, new_message, NULL, ao2_cleanup);
 	struct stasis_caching_topic *caching_topic = data;
 	const char *id = NULL;
 
 	ast_assert(caching_topic->topic != NULL);
-	ast_assert(caching_topic->cache != NULL);
+	ast_assert(caching_topic->id_fn != NULL);
 
 	if (stasis_subscription_final_message(sub, message)) {
 		caching_topic_needs_unref = caching_topic;
 	}
 
-	new_message = cache_put(caching_topic->cache, message);
-	if (new_message) {
-		stasis_forward_message(caching_topic->topic, topic, message);
-
-	}
-
+	/* Handle cache clear event */
 	if (stasis_cache_clear_type() == stasis_message_type(message)) {
 		RAII_VAR(struct stasis_message *, old_snapshot, NULL, ao2_cleanup);
 		RAII_VAR(struct stasis_message *, update, NULL, ao2_cleanup);
 		struct stasis_message *clear_msg = stasis_message_data(message);
+		const char *clear_id = caching_topic->id_fn(clear_msg);
 		struct stasis_message_type *clear_type = stasis_message_type(clear_msg);
 
 		ast_assert(clear_type != NULL);
 
-		old_snapshot = cache_put(caching_topic->cache, clear_type, clear_msg, 1);
-		if (old_snapshot) {
-			update = update_create(topic, old_snapshot, NULL);
-			stasis_publish(caching_topic->topic, update);
+		if (clear_id) {
+			old_snapshot = cache_put(caching_topic->cache, clear_type, clear_id, NULL);
+			if (old_snapshot) {
+				update = update_create(topic, old_snapshot, NULL);
+				stasis_publish(caching_topic->topic, update);
+				return;
+			}
+
+			ast_log(LOG_ERROR,
+				"Attempting to remove an item from the %s cache that isn't there: %s %s\n",
+				stasis_topic_name(caching_topic->topic), stasis_message_type_name(clear_type), clear_id);
 			return;
 		}
-
-		ast_log(LOG_ERROR,
-			"Attempting to remove an item from the %s cache that isn't there: %s %s\n",
-			stasis_topic_name(caching_topic->topic), stasis_message_type_name(clear_type), clear_id);
-		return;
-	}
-
-	id = caching_topic->cache->id_fn(message);
+	}
+
+	id = caching_topic->id_fn(message);
 	if (id == NULL) {
 		/* Object isn't cached; forward */
 		stasis_forward_message(caching_topic->topic, topic, message);
@@ -478,7 +385,7 @@
 		RAII_VAR(struct stasis_message *, old_snapshot, NULL, ao2_cleanup);
 		RAII_VAR(struct stasis_message *, update, NULL, ao2_cleanup);
 
-		old_snapshot = cache_put(caching_topic->cache, stasis_message_type(message), message, );
+		old_snapshot = cache_put(caching_topic->cache, stasis_message_type(message), id, message);
 
 		update = update_create(topic, old_snapshot, message);
 		if (update == NULL) {
@@ -493,8 +400,7 @@
 	}
 }
 
-struct stasis_caching_topic *stasis_caching_topic_create(
-	struct stasis_topic *original_topic, struct stasis_cache *cache)
+struct stasis_caching_topic *stasis_caching_topic_create(struct stasis_topic *original_topic, struct stasis_cache *cache)
 {
 	RAII_VAR(struct stasis_caching_topic *, caching_topic, NULL, ao2_cleanup);
 	struct stasis_subscription *sub;
@@ -511,13 +417,13 @@
 		return NULL;
 	}
 
+	caching_topic->topic = stasis_topic_create(new_name);
+	if (caching_topic->topic == NULL) {
+		return NULL;
+	}
+
 	ao2_ref(cache, +1);
 	caching_topic->cache = cache;
-
-	caching_topic->topic = stasis_topic_create(new_name);
-	if (caching_topic->topic == NULL) {
-		return NULL;
-	}
 
 	sub = internal_stasis_subscribe(original_topic, caching_topic_exec, caching_topic, 0);
 	if (sub == NULL) {




More information about the svn-commits mailing list