summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSuren A. Chilingaryan <csa@dside.dyndns.org>2012-11-11 05:57:30 +0100
committerSuren A. Chilingaryan <csa@dside.dyndns.org>2012-11-11 05:57:30 +0100
commit277ced9daa6b30c50fe7c90b4c2379faf5d18c86 (patch)
tree0db96d305bb6967f7995a6231ff9caf35e277649
parentbd736049994e107e45d30c3a3dcc299c24acc28d (diff)
downloadlibrcc-277ced9daa6b30c50fe7c90b4c2379faf5d18c86.tar.gz
librcc-277ced9daa6b30c50fe7c90b4c2379faf5d18c86.tar.bz2
librcc-277ced9daa6b30c50fe7c90b4c2379faf5d18c86.tar.xz
librcc-277ced9daa6b30c50fe7c90b4c2379faf5d18c86.zip
Do not init berkeleydb until caches are enabled
-rw-r--r--src/rccconfig.c4
-rw-r--r--src/rccdb4.c72
-rw-r--r--src/rccdb4.h4
-rw-r--r--src/rccexternal.c2
4 files changed, 57 insertions, 25 deletions
diff --git a/src/rccconfig.c b/src/rccconfig.c
index 1f52ac3..b7737f7 100644
--- a/src/rccconfig.c
+++ b/src/rccconfig.c
@@ -165,9 +165,9 @@ rcc_option_value_name rcc_sn_translate[] = { "OFF", "TRANSLITERATE", "TO_ENGLISH
rcc_option_description rcc_option_descriptions[RCC_MAX_OPTIONS+1];
rcc_option_description rcc_option_descriptions_embeded[RCC_MAX_OPTIONS+1] = {
#ifdef HAVE_DB_H
- {RCC_OPTION_LEARNING_MODE, 1, { RCC_OPTION_RANGE_TYPE_MENU, 0, 3, 1 }, RCC_OPTION_TYPE_STANDARD, "LEARNING_MODE", rcc_sn_learning },
+ {RCC_OPTION_LEARNING_MODE, 0, { RCC_OPTION_RANGE_TYPE_MENU, 0, 3, 1 }, RCC_OPTION_TYPE_STANDARD, "LEARNING_MODE", rcc_sn_learning },
#else
- {RCC_OPTION_LEARNING_MODE, 1, { RCC_OPTION_RANGE_TYPE_MENU, 0, 3, 1 }, RCC_OPTION_TYPE_INVISIBLE, "LEARNING_MODE", rcc_sn_learning },
+ {RCC_OPTION_LEARNING_MODE, 0, { RCC_OPTION_RANGE_TYPE_MENU, 0, 3, 1 }, RCC_OPTION_TYPE_INVISIBLE, "LEARNING_MODE", rcc_sn_learning },
#endif /* HAVE_DB_H */
{RCC_OPTION_AUTODETECT_FS_NAMES, 1, { RCC_OPTION_RANGE_TYPE_BOOLEAN, 0, 0, 0}, RCC_OPTION_TYPE_STANDARD, "AUTODETECT_FS_NAMES", rcc_sn_boolean},
{RCC_OPTION_AUTODETECT_FS_TITLES, 1, { RCC_OPTION_RANGE_TYPE_BOOLEAN, 0, 0, 0}, RCC_OPTION_TYPE_INVISIBLE, "AUTODETECT_FS_TITLES", rcc_sn_boolean},
diff --git a/src/rccdb4.c b/src/rccdb4.c
index acbed9b..3a6165a 100644
--- a/src/rccdb4.c
+++ b/src/rccdb4.c
@@ -30,8 +30,44 @@
#define DATABASE "autolearn.db"
db4_context rccDb4CreateContext(const char *dbpath, rcc_db4_flags flags) {
- int err;
db4_context ctx;
+
+ if (!dbpath) return NULL;
+
+ ctx = (db4_context)malloc(sizeof(db4_context_s));
+ if (!ctx) return NULL;
+
+ memset(ctx, 0, sizeof(db4_context_s));
+ ctx->dbpath = strdup(dbpath);
+ ctx->flags = flags;
+
+ if (!ctx->dbpath) {
+ free(ctx);
+ return NULL;
+ }
+
+ return ctx;
+}
+
+static int rccDb4InitContext(db4_context ctx, const char *dbpath, rcc_db4_flags flags) {
+ int err;
+
+ if (ctx->initialized) {
+ if ((ctx->dbe)&&(ctx->db)) return 0;
+ return -1;
+ }
+
+ err = rccLock();
+ if (err) return -1;
+
+ if (ctx->initialized) {
+ if ((ctx->dbe)&&(ctx->db)) return 0;
+ return -1;
+ }
+
+ ctx->initialized = 1;
+ rccUnLock();
+
#ifdef HAVE_DB_H
DB_ENV *dbe;
DB *db;
@@ -41,9 +77,9 @@ db4_context rccDb4CreateContext(const char *dbpath, rcc_db4_flags flags) {
char stmp[160];
# endif /* DB_VERSION_MISMATCH */
# endif /* DB_LOG_AUTOREMOVE */
-
+
err = db_env_create(&dbe, 0);
- if (err) return NULL;
+ if (err) return -1;
# if defined(DB_LOG_AUTOREMOVE)
dbe->set_flags(dbe, DB_LOG_AUTOREMOVE, 1);
@@ -81,7 +117,7 @@ db4_context rccDb4CreateContext(const char *dbpath, rcc_db4_flags flags) {
dbe->close(dbe, 0);
}
- if (err) return NULL;
+ if (err) return -1;
if (strlen(dbpath)<128) {
sprintf(stmp, "%s/log.0000000001", dbpath);
@@ -89,7 +125,7 @@ db4_context rccDb4CreateContext(const char *dbpath, rcc_db4_flags flags) {
}
err = db_env_create(&dbe, 0);
- if (err) return NULL;
+ if (err) return -1;
err = dbe->open(dbe, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 00644);
@@ -100,13 +136,13 @@ db4_context rccDb4CreateContext(const char *dbpath, rcc_db4_flags flags) {
if (err) {
// fprintf(stderr, "BerkelyDB initialization failed: %i (%s)\n", err, db_strerror(err));
dbe->close(dbe, 0);
- return NULL;
+ return -1;
}
err = db_create(&db, dbe, 0);
if (err) {
dbe->close(dbe, 0);
- return NULL;
+ return -1;
}
@@ -114,32 +150,24 @@ db4_context rccDb4CreateContext(const char *dbpath, rcc_db4_flags flags) {
if (err) {
db->close(db, 0);
dbe->close(dbe, 0);
- return NULL;
+ return -1;
}
#endif /* HAVE_DB_H */
- ctx = (db4_context)malloc(sizeof(db4_context_s));
- if (!ctx) {
-#ifdef HAVE_DB_H
- db->close(db, 0);
- dbe->close(dbe, 0);
-#endif /* HAVE_DB_H */
- return NULL;
- }
-
#ifdef HAVE_DB_H
ctx->db = db;
ctx->dbe = dbe;
#endif /* HAVE_DB_H */
- ctx->flags = flags;
- return ctx;
+
+ return 0;
}
void rccDb4FreeContext(db4_context ctx) {
if (ctx) {
#ifdef HAVE_DB_H
- ctx->db->close(ctx->db, 0);
- ctx->dbe->close(ctx->dbe, 0);
+ if (ctx->db) ctx->db->close(ctx->db, 0);
+ if (ctx->dbe) ctx->dbe->close(ctx->dbe, 0);
+ if (ctx->dbpath) free(ctx->dbpath);
#endif /* HAVE_DB_H */
free(ctx);
}
@@ -172,6 +200,7 @@ int rccDb4SetKey(db4_context ctx, const char *orig, size_t olen, const char *str
#endif /* HAVE_DB_H */
if ((!ctx)||(!orig)||(!string)) return -1;
+ if (rccDb4InitContext(ctx, ctx->dbpath, ctx->flags)) return -1;
#ifdef HAVE_DB_H
memset(&key, 0, sizeof(key));
@@ -197,6 +226,7 @@ char *rccDb4GetKey(db4_context ctx, const char *orig, size_t olen) {
#endif /* HAVE_DB_H */
if ((!ctx)||(!orig)) return NULL;
+ if (rccDb4InitContext(ctx, ctx->dbpath, ctx->flags)) return NULL;
#ifdef HAVE_DB_H
memset(&key, 0, sizeof(key));
diff --git a/src/rccdb4.h b/src/rccdb4.h
index 54c2106..fa4d28d 100644
--- a/src/rccdb4.h
+++ b/src/rccdb4.h
@@ -33,7 +33,9 @@ struct db4_context_t {
DB_ENV *dbe;
DB *db;
#endif /* HAVE_DB_H */
-
+
+ int initialized;
+ char *dbpath;
rcc_db4_flags flags;
};
diff --git a/src/rccexternal.c b/src/rccexternal.c
index aee6797..3d1f12f 100644
--- a/src/rccexternal.c
+++ b/src/rccexternal.c
@@ -111,7 +111,7 @@ int rccExternalInit() {
}
# endif /* HAVE_SYS_STAT_H */
#endif /* HAVE_UNISTD_H */
- exit(1);
+ _exit(1);
}
void rccExternalFree() {