void blobcache_init(void) { sqlite3 *db; extern char *showtime_cache_path; char buf[256]; blobcache_prune_old(); snprintf(buf, sizeof(buf), "%s/cachedb", showtime_cache_path); mkdir(buf, 0770); snprintf(buf, sizeof(buf), "%s/cachedb/cache.db", showtime_cache_path); // unlink(buf); cachedb_pool = db_pool_create(buf, 4); db = db_pool_get(cachedb_pool); if(db == NULL) return; int r = db_upgrade_schema(db, "bundle://resources/cachedb", "cachedb"); db_pool_put(cachedb_pool, db); if(r) cachedb_pool = NULL; else callout_arm(&blobcache_callout, blobcache_do_prune, NULL, 1); }
static void blobcache_do_prune(struct callout *c, void *opaque) { sqlite3 *db = db_pool_get(cachedb_pool); if(db != NULL) blobcache_prune(db); db_pool_put(cachedb_pool, db); }
int my_con(db_con_t* con) { struct my_con* ptr; struct my_uri* uri; /* First try to lookup the connection in the connection pool and * re-use it if a match is found */ ptr = (struct my_con*)db_pool_get(con->uri); if (ptr) { DBG("mysql: Connection to %.*s:%.*s found in connection pool\n", con->uri->scheme.len, ZSW(con->uri->scheme.s), con->uri->body.len, ZSW(con->uri->body.s)); goto found; } ptr = (struct my_con*)pkg_malloc(sizeof(struct my_con)); if (!ptr) { LOG(L_ERR, "mysql: No memory left\n"); goto error; } memset(ptr, '\0', sizeof(struct my_con)); if (db_pool_entry_init(&ptr->gen, my_con_free, con->uri) < 0) goto error; ptr->con = (MYSQL*)pkg_malloc(sizeof(MYSQL)); if (!ptr->con) { LOG(L_ERR, "mysql: No enough memory\n"); goto error; } mysql_init(ptr->con); uri = DB_GET_PAYLOAD(con->uri); DBG("mysql: Creating new connection to: %.*s:%.*s\n", con->uri->scheme.len, ZSW(con->uri->scheme.s), con->uri->body.len, ZSW(con->uri->body.s)); /* Put the newly created mysql connection into the pool */ db_pool_put((struct db_pool_entry*)ptr); DBG("mysql: Connection stored in connection pool\n"); found: /* Attach driver payload to the db_con structure and set connect and * disconnect functions */ DB_SET_PAYLOAD(con, ptr); con->connect = my_con_connect; con->disconnect = my_con_disconnect; return 0; error: if (ptr) { db_pool_entry_free(&ptr->gen); if (ptr->con) pkg_free(ptr->con); pkg_free(ptr); } return 0; }
int bdb_con(db_con_t* con) { bdb_con_t* bcon; bdb_uri_t* buri; buri = DB_GET_PAYLOAD(con->uri); /* First try to lookup the connection in the connection pool and * re-use it if a match is found */ bcon = (bdb_con_t*)db_pool_get(con->uri); if (bcon) { DBG("bdb: Connection to %s found in connection pool\n", buri->uri); goto found; } bcon = (bdb_con_t*)pkg_malloc(sizeof(bdb_con_t)); if (!bcon) { ERR("bdb: No memory left\n"); goto error; } memset(bcon, '\0', sizeof(bdb_con_t)); if (db_pool_entry_init(&bcon->gen, bdb_con_free, con->uri) < 0) goto error; DBG("bdb: Preparing new connection to %s\n", buri->uri); if(bdb_is_database(buri->path.s)!=0) { ERR("bdb: database [%.*s] does not exists!\n", buri->path.len, buri->path.s); goto error; } /* Put the newly created BDB connection into the pool */ db_pool_put((struct db_pool_entry*)bcon); DBG("bdb: Connection stored in connection pool\n"); found: /* Attach driver payload to the db_con structure and set connect and * disconnect functions */ DB_SET_PAYLOAD(con, bcon); con->connect = bdb_con_connect; con->disconnect = bdb_con_disconnect; return 0; error: if (bcon) { db_pool_entry_free(&bcon->gen); pkg_free(bcon); } return -1; }
void blobcache_put(const char *key, const char *stash, const void *data, size_t size, int maxage, const char *etag, time_t mtime) { sqlite3 *db = db_pool_get(cachedb_pool); if(db == NULL) return; blobcache_put0(db, key, stash, data, size, maxage, etag, mtime); db_pool_put(cachedb_pool, db); }
int pg_con(db_con_t *con) { struct pg_con *pcon; /* First try to lookup the connection in the connection pool and * re-use it if a match is found */ pcon = (struct pg_con *)db_pool_get(con->uri); if(pcon) { DBG("postgres: Connection to %.*s:%.*s found in connection pool\n", con->uri->scheme.len, ZSW(con->uri->scheme.s), con->uri->body.len, ZSW(con->uri->body.s)); goto found; } pcon = (struct pg_con *)pkg_malloc(sizeof(struct pg_con)); if(!pcon) { LOG(L_ERR, "postgres: No memory left\n"); goto error; } memset(pcon, '\0', sizeof(struct pg_con)); if(db_pool_entry_init(&pcon->gen, pg_con_free, con->uri) < 0) goto error; DBG("postgres: Preparing new connection to: %.*s:%.*s\n", con->uri->scheme.len, ZSW(con->uri->scheme.s), con->uri->body.len, ZSW(con->uri->body.s)); /* Put the newly created postgres connection into the pool */ db_pool_put((struct db_pool_entry *)pcon); DBG("postgres: Connection stored in connection pool\n"); found: /* Attach driver payload to the db_con structure and set connect and * disconnect functions */ DB_SET_PAYLOAD(con, pcon); con->connect = pg_con_connect; con->disconnect = pg_con_disconnect; return 0; error: if(pcon) { db_pool_entry_free(&pcon->gen); pkg_free(pcon); } return -1; }
int ld_con(db_con_t* con) { struct ld_con* lcon; struct ld_uri* luri; luri = DB_GET_PAYLOAD(con->uri); /* First try to lookup the connection in the connection pool and * re-use it if a match is found */ lcon = (struct ld_con*)db_pool_get(con->uri); if (lcon) { DBG("ldap: Connection to %s found in connection pool\n", luri->uri); goto found; } lcon = (struct ld_con*)pkg_malloc(sizeof(struct ld_con)); if (!lcon) { ERR("ldap: No memory left\n"); goto error; } memset(lcon, '\0', sizeof(struct ld_con)); if (db_pool_entry_init(&lcon->gen, ld_con_free, con->uri) < 0) goto error; DBG("ldap: Preparing new connection to %s\n", luri->uri); /* Put the newly created LDAP connection into the pool */ db_pool_put((struct db_pool_entry*)lcon); DBG("ldap: Connection stored in connection pool\n"); found: /* Attach driver payload to the db_con structure and set connect and * disconnect functions */ DB_SET_PAYLOAD(con, lcon); con->connect = ld_con_connect; con->disconnect = ld_con_disconnect; return 0; error: if (lcon) { db_pool_entry_free(&lcon->gen); pkg_free(lcon); } return -1; }
int blobcache_get_meta(const char *key, const char *stash, char **etagp, time_t *mtimep) { sqlite3 *db = db_pool_get(cachedb_pool); *etagp = NULL; *mtimep = 0; if(db == NULL) return -1; int r = blobcache_get_meta0(db, key, stash, etagp, mtimep); db_pool_put(cachedb_pool, db); return r; }
void * blobcache_get(const char *key, const char *stash, size_t *sizep, int pad, int *ignore_expiry, char **etagp, time_t *mtimep) { sqlite3 *db = db_pool_get(cachedb_pool); if(etagp != NULL) *etagp = NULL; if(mtimep != NULL) *mtimep = 0; if(db == NULL) return NULL; void *r = blobcache_get0(db, key, stash, sizep, pad, ignore_expiry, etagp, mtimep); db_pool_put(cachedb_pool, db); return r; }