(concord_opened_feature_list): New variable.
[chise/concord.git] / concord.c
index 2a646d6..4e220e9 100644 (file)
--- a/concord.c
+++ b/concord.c
 #include "cos-hash.h"
 #include "concord-bdb.h"
 
+COS_Feature concord_opened_feature_list = NULL;
+COS_Feature concord_opened_feature_list_last = NULL;
+size_t concord_opened_feature_list_length = 0;
+size_t concord_opened_feature_list_limit = 96;
+
+COS_Feature_INDEX concord_opened_index_list = NULL;
+COS_Feature_INDEX concord_opened_index_list_last = NULL;
+size_t concord_opened_index_list_length = 0;
+size_t concord_opened_index_list_limit = 32;
+
 
 int
 CONCORD_String_size (const CONCORD_String s)
@@ -546,6 +556,7 @@ concord_genre_open_feature (CONCORD_Genre genre, const char* feature)
     }
   strcpy (table->name, feature);
   table->value_table = NULL;
+  table->next = NULL;
   return table;
 }
 
@@ -613,6 +624,17 @@ concord_feature_setup_db (CONCORD_Feature feature, int writable)
     {
       CONCORD_Genre genre = feature->genre;
 
+      if ( concord_opened_feature_list_length
+          >= concord_opened_feature_list_limit )
+       {
+         CONCORD_Feature top_feature = concord_opened_feature_list;
+
+         CONCORD_BDB_close (top_feature->db);
+         top_feature->db = NULL;
+         concord_opened_feature_list = top_feature->next;
+         top_feature->next = NULL;
+         concord_opened_feature_list_length--;
+       }
       feature->db
        = CONCORD_BDB_open (genre->ds->location, genre->name,
                            "feature", feature->name,
@@ -621,6 +643,17 @@ concord_feature_setup_db (CONCORD_Feature feature, int writable)
       if (feature->db == NULL)
        return -1;
       feature->access = access;
+      if ( concord_opened_feature_list == NULL )
+       {
+         concord_opened_feature_list = feature;
+         concord_opened_feature_list_last = feature;
+       }
+      else
+       {
+         concord_opened_feature_list_last->next = feature;
+         concord_opened_feature_list_last = feature;
+       }
+      concord_opened_feature_list_length++;
     }
   return 0;
 }
@@ -753,6 +786,7 @@ concord_genre_open_index (CONCORD_Genre genre, const char* index)
     }
   strcpy (table->name, index);
   table->decoding_table = NULL;
+  table->next = NULL;
   return table;
 }
 
@@ -820,6 +854,17 @@ concord_index_setup_db (CONCORD_INDEX index, int writable)
     {
       CONCORD_Genre genre = index->genre;
 
+      if ( concord_opened_index_list_length
+          >= concord_opened_index_list_limit )
+       {
+         CONCORD_INDEX top_index = concord_opened_index_list;
+
+         CONCORD_BDB_close (top_index->db);
+         top_index->db = NULL;
+         concord_opened_index_list = top_index->next;
+         top_index->next = NULL;
+         concord_opened_index_list_length--;
+       }
       index->db
        = CONCORD_BDB_open (genre->ds->location, genre->name,
                            "index", index->name,
@@ -828,6 +873,17 @@ concord_index_setup_db (CONCORD_INDEX index, int writable)
       if (index->db == NULL)
        return -1;
       index->access = access;
+      if ( concord_opened_index_list == NULL )
+       {
+         concord_opened_index_list = index;
+         concord_opened_index_list_last = index;
+       }
+      else
+       {
+         concord_opened_index_list_last->next = index;
+         concord_opened_index_list_last = index;
+       }
+      concord_opened_index_list_length++;
     }
   return 0;
 }