<commit_before>/* GStreamer
 * Copyright (C) <1999> <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <gst/gst_private.h>
#include <gst/gstversion.h>
#include <gst/gstplugin.h>
#include <gst/gstcache.h>

#define GST_TYPE_MEM_CACHE		\
  (gst_cache_get_type ())
#define GST_MEM_CACHE(obj)		\
  (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_MEM_CACHE, GstMemCache))
#define GST_MEM_CACHE_CLASS(klass)	\
  (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_MEM_CACHE, GstMemCacheClass))
#define GST_IS_MEM_CACHE(obj)		\
  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_MEM_CACHE))
#define GST_IS_MEM_CACHE_CLASS(obj)	\
  (GST_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_MEM_CACHE))

typedef struct _GstMemCache GstMemCache;
typedef struct _GstMemCacheClass GstMemCacheClass;

struct _GstMemCache {
  GstCache		 parent;
};

struct _GstMemCacheClass {
  GstCacheClass parent_class;
};

/* Cache signals and args */
enum {
  LAST_SIGNAL
};

enum {
  ARG_0,
  /* FILL ME */
};

static void		gst_mem_cache_class_init	(GstMemCacheClass *klass);
static void		gst_mem_cache_init		(GstMemCache *cache);

#define CLASS(mem_cache)  GST_MEM_CACHE_CLASS (G_OBJECT_GET_CLASS (mem_cache))

static GstCache *parent_class = NULL;
/*static guint gst_mem_cache_signals[LAST_SIGNAL] = { 0 }; */

GType
gst_mem_cache_get_type(void) {
  static GType mem_cache_type = 0;

  if (!mem_cache_type) {
    static const GTypeInfo mem_cache_info = {
      sizeof(GstMemCacheClass),
      NULL,
      NULL,
      (GClassInitFunc)gst_mem_cache_class_init,
      NULL,
      NULL,
      sizeof(GstMemCache),
      1,
      (GInstanceInitFunc)gst_mem_cache_init,
      NULL
    };
    mem_cache_type = g_type_register_static(GST_TYPE_CACHE, "GstMemCache", &mem_cache_info, 0);
  }
  return mem_cache_type;
}

static void
gst_mem_cache_class_init (GstMemCacheClass *klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass*)klass;
  gstelement_class = (GstElementClass*)klass;

  parent_class = g_type_class_ref(GST_TYPE_CACHE);
}

static void
gst_mem_cache_init (GstMemCache *cache)
{
  GST_DEBUG(0, "created new mem cache");
}

static gboolean
plugin_init (GModule *module, GstPlugin *plugin)
{
  GstCacheFactory *factory;

  gst_plugin_set_longname (plugin, "A memory cache");

  factory = gst_cache_factory_new ("memcache",
	                           "A cache that stores entries in memory",
                                   gst_mem_cache_get_type());

  if (factory != NULL) {
    gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
  }
  else {
    g_warning ("could not register memcache");
  }
  return TRUE;<commit_msg>Added current status of memcache<commit_after>/* GStreamer
 * Copyright (C) <1999> <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <gst/gst_private.h>
#include <gst/gstversion.h>
#include <gst/gstplugin.h>
#include <gst/gstcache.h>

#define GST_TYPE_MEM_CACHE		\
  (gst_cache_get_type ())
#define GST_MEM_CACHE(obj)		\
  (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_MEM_CACHE, GstMemCache))
#define GST_MEM_CACHE_CLASS(klass)	\
  (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_MEM_CACHE, GstMemCacheClass))
#define GST_IS_MEM_CACHE(obj)		\
  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_MEM_CACHE))
#define GST_IS_MEM_CACHE_CLASS(obj)	\
  (GST_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_MEM_CACHE))
	
/*
 * Object model:
 *
 * All entries are simply added to a GList first. Then we build
 * an index to each entry for each id/format
 * 
 *
 *  memcache
 *    -----------------------------...
 *    !                  !         
 *   id1                 id2        
 *    ------------
 *    !          !
 *   format1  format2
 *    !          !
 *   GTree      GTree
 *
 *
 * The memcache creates a MemCacheId object for each writer id, a
 * Hashtable is kept to map the id to the MemCacheId
 *
 * The MemCacheId keeps a MemCacheFormatIndex for each format the
 * specific writer wants indexed.
 *
 * The MemCacheFormatIndex keeps all the values of the particular 
 * format in a GTree, The values of the GTree point back to the entry. 
 *
 * Finding a value for an id/format requires locating the correct GTree,
 * then do a lookup in the Tree to get the required value.
 */

typedef struct _GstMemCacheFormatIndex {
  GstFormat 	 format;
  gint		 offset;
  GTree		*tree;
} GstMemCacheFormatIndex;

typedef struct _GstMemCacheId {
  gint 		 id;
  GHashTable	*format_index;
} GstMemCacheId;

typedef struct _GstMemCache GstMemCache;
typedef struct _GstMemCacheClass GstMemCacheClass;

struct _GstMemCache {
  GstCache		 parent;

  GList			*associations;

  GHashTable		*id_index;
};

struct _GstMemCacheClass {
  GstCacheClass parent_class;
};

/* Cache signals and args */
enum {
  LAST_SIGNAL
};

enum {
  ARG_0,
  /* FILL ME */
};

static void		gst_mem_cache_class_init	(GstMemCacheClass *klass);
static void		gst_mem_cache_init		(GstMemCache *cache);
static void 		gst_mem_cache_dispose 		(GObject *object);

static void 		gst_mem_cache_add_entry 	(GstCache *cache, GstCacheEntry *entry);
static GstCacheEntry* 	gst_mem_cache_get_assoc_entry 	(GstCache *cache, gint id,
                              				 GstCacheLookupMethod method,
                              				 GstFormat format, gint64 value,
                              				 GCompareDataFunc func,
                              				 gpointer user_data);

#define CLASS(mem_cache)  GST_MEM_CACHE_CLASS (G_OBJECT_GET_CLASS (mem_cache))

static GstCache *parent_class = NULL;
/*static guint gst_mem_cache_signals[LAST_SIGNAL] = { 0 }; */

GType
gst_mem_cache_get_type(void) {
  static GType mem_cache_type = 0;

  if (!mem_cache_type) {
    static const GTypeInfo mem_cache_info = {
      sizeof(GstMemCacheClass),
      NULL,
      NULL,
      (GClassInitFunc)gst_mem_cache_class_init,
      NULL,
      NULL,
      sizeof(GstMemCache),
      1,
      (GInstanceInitFunc)gst_mem_cache_init,
      NULL
    };
    mem_cache_type = g_type_register_static(GST_TYPE_CACHE, "GstMemCache", &mem_cache_info, 0);
  }
  return mem_cache_type;
}

static void
gst_mem_cache_class_init (GstMemCacheClass *klass)
{
  GObjectClass *gobject_class;
  GstCacheClass *gstcache_class;

  gobject_class = (GObjectClass*)klass;
  gstcache_class = (GstCacheClass*)klass;

  parent_class = g_type_class_ref(GST_TYPE_CACHE);

  gobject_class->dispose = gst_mem_cache_dispose;

  gstcache_class->add_entry 	  = gst_mem_cache_add_entry;
  gstcache_class->get_assoc_entry = gst_mem_cache_get_assoc_entry;
}

static void
gst_mem_cache_init (GstMemCache *cache)
{
  GST_DEBUG(0, "created new mem cache");

  cache->associations = NULL;
  cache->id_index = g_hash_table_new (g_int_hash, g_int_equal);
}

static void
gst_mem_cache_dispose (GObject *object)
{
  //GstMemCache *memcache = GST_MEM_CACHE (object);

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
gst_mem_cache_add_id (GstCache *cache, GstCacheEntry *entry)
{
  GstMemCache *memcache = GST_MEM_CACHE (cache);
  GstMemCacheId *id_index;

  id_index = g_hash_table_lookup (memcache->id_index, &entry->id);

  if (!id_index) {
    id_index = g_new0 (GstMemCacheId, 1);

    id_index->id = entry->id;
    id_index->format_index = g_hash_table_new (g_int_hash, g_int_equal);
    g_hash_table_insert (memcache->id_index, &entry->id, id_index);
  }
}

static gint
mem_cache_compare (gconstpointer a,
		   gconstpointer b,
		   gpointer user_data)
{
  GstMemCacheFormatIndex *index = user_data;
  gint64 val1, val2;

  val1 = GST_CACHE_ASSOC_VALUE (((GstCacheEntry *)a), index->offset);
  val2 = GST_CACHE_ASSOC_VALUE (((GstCacheEntry *)b), index->offset);
	  
  return val1 - val2;
}

static void
gst_mem_cache_index_format (GstMemCacheId *id_index, GstCacheEntry *entry, gint assoc)
{
  GstMemCacheFormatIndex *index;
  GstFormat *format;

  format = &GST_CACHE_ASSOC_FORMAT (entry, assoc);

  index = g_hash_table_lookup (id_index->format_index, format);

  if (!index) {
    index = g_new0 (GstMemCacheFormatIndex, 1);

    index->format = *format;
    index->offset = assoc;
    index->tree = g_tree_new_with_data (mem_cache_compare, index);

    g_hash_table_insert (id_index->format_index, format, index);
  }

  g_tree_insert (index->tree, entry, entry);
}

static void
gst_mem_cache_add_association (GstCache *cache, GstCacheEntry *entry)
{
  GstMemCache *memcache = GST_MEM_CACHE (cache);
  GstMemCacheId *id_index;

  memcache->associations = g_list_prepend (memcache->associations, entry);

  id_index = g_hash_table_lookup (memcache->id_index, &entry->id);
  if (id_index) {
    gint i;

    for (i = 0; i < GST_CACHE_NASSOCS (entry); i++) {
      gst_mem_cache_index_format (id_index, entry, i);
    }
  }
}

static void
gst_mem_cache_add_object (GstCache *cache, GstCacheEntry *entry)
{
}

static void
gst_mem_cache_add_format (GstCache *cache, GstCacheEntry *entry)
{
}

static void
gst_mem_cache_add_entry (GstCache *cache, GstCacheEntry *entry)
{
  GstMemCache *memcache = GST_MEM_CACHE (cache);

  GST_DEBUG (0, "adding entry %p\n", memcache);

  switch (entry->type){
     case GST_CACHE_ENTRY_ID:
       gst_mem_cache_add_id (cache, entry);
       break;
     case GST_CACHE_ENTRY_ASSOCIATION:
       gst_mem_cache_add_association (cache, entry);
       break;
     case GST_CACHE_ENTRY_OBJECT:
       gst_mem_cache_add_object (cache, entry);
       break;
     case GST_CACHE_ENTRY_FORMAT:
       gst_mem_cache_add_format (cache, entry);
       break;
     default:
       break;
  }
}

static GstCacheEntry*
gst_mem_cache_get_assoc_entry (GstCache *cache, gint id,
                               GstCacheLookupMethod method,
                               GstFormat format, gint64 value,
                               GCompareDataFunc func,
                               gpointer user_data)
{
  GstMemCache *memcache = GST_MEM_CACHE (cache);
  GList *walk = memcache->associations;
  GList *next;

  /* FIXME use GTree here */
  while (walk) {
    GstCacheEntry *entry = (GstCacheEntry *) walk->data;

    next = g_list_next (walk);

    if (entry->type == GST_CACHE_ENTRY_ASSOCIATION && entry->id == id) {
      gint64 got;

      if (gst_cache_entry_assoc_map (entry, format, &got)) {
	if (got == value && method == GST_CACHE_LOOKUP_EXACT)
          return entry;
	else {
          gint64 got_next = G_MININT64;
          GstCacheEntry *next_entry = NULL;

	  if (next != NULL) {
            next_entry = (GstCacheEntry *) next->data;

	    gst_cache_entry_assoc_map (next_entry, format, &got_next);
	  }

          if ((got >= value) && (got_next <= value)) {
	    if (method == GST_CACHE_LOOKUP_BEFORE) 
	      return next_entry;
	    else if (method == GST_CACHE_LOOKUP_AFTER) 
	      return entry;
	  }
	}
      }
    }
    
    walk = next;
  }

  return NULL;
}

static gboolean
plugin_init (GModule *module, GstPlugin *plugin)
{
  GstCacheFactory *factory;

  gst_plugin_set_longname (plugin, "A memory cache");

  factory = gst_cache_factory_new ("memcache",
	                           "A cache that stores entries in memory",
                                   gst_mem_cache_get_type());

  if (factory != NULL) {
    gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
  }
  else {
    g_warning ("could not register memcache");
  }
  return TRUE;
{'fraction_non_alphanumeric': 0.09369582348305752, 'fraction_numerical': 0.006146572104018913, 'mean_word_length': 3.4907997169143665, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5751853', 'n_tokens_mistral': 4571, 'n_tokens_neox': 4403, 'n_words': 1079}