index_db.c File Reference

#include "config.h"
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "log.h"
#include "index_db.h"
Include dependency graph for index_db.c:

Go to the source code of this file.

Defines

#define MAX_WAIT_DATABASE   (30)

Functions

int db_open_global (db_state_t *db_state, const gchar *directory)
void db_close (db_state_t *db_state)
int db_start_transaction (db_state_t *db_state)
int db_end_transaction (db_state_t *db_state)
void db_update_entry (db_state_t *db_state, const db_entry_t *entry)
void db_update_shortcut_entry (db_state_t *db_state, const db_entry_t *entry, const char *display_name, const char *author)
void db_add_entry (db_state_t *db_state, const db_entry_t *entry, const char *tag, const char *display_name, const char *author)
int db_delete_entry (db_state_t *db_state, const db_entry_t *entry)
static db_entry_trow2entry (const metadata_cell *row)
GSList * db_get_model (const gchar *dir)

Define Documentation

#define MAX_WAIT_DATABASE   (30)

Copyright (C) 2009 iRex Technologies B.V. All rights reserved.

Definition at line 37 of file index_db.c.

Referenced by db_start_transaction().


Function Documentation

void db_add_entry ( db_state_t db_state,
const db_entry_t entry,
const char *  tag,
const char *  display_name,
const char *  author 
)

Definition at line 133 of file index_db.c.

References ermetadb_global_add_file(), ermetadb_global_add_folder(), db_entry_t::filename, db_entry_t::filepath, db_entry_t::filesize, db_entry_t::is_dir, db_entry_t::last_modified, and db_state_t::mdb.

Referenced by handle_changes().

00134 {
00135     if (entry->is_dir) {
00136         ermetadb_global_add_folder(db_state->mdb,
00137                                    entry->filepath,
00138                                    entry->filename,
00139                                    entry->last_modified);
00140     } else {
00141         if (display_name == NULL) display_name = entry->filename;
00142         ermetadb_global_add_file(db_state->mdb,
00143                                  entry->filepath,
00144                                  entry->filename,
00145                                  entry->filesize,
00146                                  entry->last_modified,
00147                                  display_name,
00148                                  author,
00149                                  tag);
00150     }
00151 }

Here is the call graph for this function:

Here is the caller graph for this function:

void db_close ( db_state_t db_state  ) 

Definition at line 63 of file index_db.c.

References ermetadb_close(), LOGPRINTF, and db_state_t::mdb.

Referenced by db_get_model(), handle_changes(), and index_full().

00064 {
00065     LOGPRINTF("entry");
00066 
00067     if (db_state->mdb)
00068     {
00069         ermetadb_close(db_state->mdb);
00070         db_state->mdb = NULL;
00071     }
00072 }

Here is the call graph for this function:

Here is the caller graph for this function:

int db_delete_entry ( db_state_t db_state,
const db_entry_t entry 
)

Definition at line 154 of file index_db.c.

References ermetadb_global_remove_entry(), db_entry_t::file_id, db_entry_t::filename, db_entry_t::filepath, db_entry_t::is_dir, and db_state_t::mdb.

Referenced by handle_changes().

00155 {
00156     // NOTE: only need to delete entry itself, not subdirs/files
00157     return ermetadb_global_remove_entry(db_state->mdb,
00158                                         entry->file_id,
00159                                         entry->filepath,
00160                                         entry->filename,
00161                                         entry->is_dir);
00162 }

Here is the call graph for this function:

Here is the caller graph for this function:

int db_end_transaction ( db_state_t db_state  ) 

Definition at line 105 of file index_db.c.

References ER_OK, ermetadb_end_transaction(), LOGPRINTF, db_state_t::mdb, and db_state_t::transaction_started.

Referenced by handle_changes().

00106 {
00107     int  ret = ER_OK;    // return code
00108 
00109     LOGPRINTF("entry");
00110 
00111     if (db_state->transaction_started && db_state->mdb)
00112     {
00113         ret = ermetadb_end_transaction(db_state->mdb);
00114         db_state->transaction_started = FALSE;
00115     }
00116 
00117     return ret;
00118 }

Here is the call graph for this function:

Here is the caller graph for this function:

GSList* db_get_model ( const gchar *  dir  ) 

Definition at line 199 of file index_db.c.

References metadata_table::cell_data, db_close(), db_open_global(), ER_OK, ermetadb_global_select_all(), ERRORPRINTF, db_state_t::mdb, metadata_table_n_columns, metadata_table_n_rows, and row2entry().

Referenced by index_full().

00200 {
00201     db_state_t db_state;
00202     GSList* model = 0;
00203 
00204     if (db_open_global(&db_state, dir) != ER_OK) {
00205         ERRORPRINTF("error opening db");
00206         return NULL;
00207     }
00208 
00209     metadata_table *values = NULL;
00210 
00211     int ret = ermetadb_global_select_all(db_state.mdb, &values);
00212     if (ret != ER_OK) {
00213         ERRORPRINTF("error [%d]", ret);
00214         goto out;
00215     }
00216 
00217     int n_rows = metadata_table_n_rows(values);
00218     int n_cols = metadata_table_n_columns(values);
00219 
00220     const metadata_cell *row = (const metadata_cell*) (values->cell_data->data);
00221     for (int i=0; i<n_rows; i++) {
00222         db_entry_t* entry = row2entry(row);
00223         model = g_slist_prepend(model, entry);
00224         row += n_cols; 
00225     }
00226 
00227     model = g_slist_reverse(model);
00228     
00229     // DONT FREE, strings are used
00230     //metadata_table_free(values);
00231 out:
00232     db_close(&db_state);
00233     return model;
00234 }

Here is the call graph for this function:

Here is the caller graph for this function:

int db_open_global ( db_state_t db_state,
const gchar *  directory 
)

Definition at line 40 of file index_db.c.

References db_state_t::db, ER_FAIL, ER_OK, ermetadb_global_open(), ERRORPRINTF, LOGPRINTF, db_state_t::mdb, db_state_t::num_cols, db_state_t::num_rows, db_state_t::row, db_state_t::transaction_started, and db_state_t::values.

Referenced by db_get_model(), handle_changes(), and index_full().

00041 {
00042     LOGPRINTF("entry: dir [%s]", directory);
00043     
00044     db_state->mdb = NULL;
00045     db_state->transaction_started = FALSE;
00046     db_state->values = NULL;
00047     db_state->db = NULL;
00048     db_state->num_rows = 0;
00049     db_state->num_cols = 0;
00050     db_state->row = 0;
00051 
00052     erMetadb db = ermetadb_global_open(directory, FALSE);
00053     if (db == NULL) {
00054         ERRORPRINTF("failed to open database in %s.", directory);
00055         return ER_FAIL;
00056     }
00057     
00058     db_state->mdb = db;
00059     return ER_OK;
00060 }

Here is the call graph for this function:

Here is the caller graph for this function:

int db_start_transaction ( db_state_t db_state  ) 

Definition at line 75 of file index_db.c.

References ER_OK, ermetadb_begin_transaction(), ERMETADB_DATABASE_BUSY, LOGPRINTF, MAX_WAIT_DATABASE, db_state_t::mdb, db_state_t::transaction_started, and WARNPRINTF.

Referenced by handle_changes().

00076 {
00077     int           ret = ER_OK;    // return code
00078     const time_t  start = time(NULL);
00079 
00080     LOGPRINTF("entry");
00081 
00082     if (db_state->mdb)
00083     {
00084         if (db_state->transaction_started)
00085             WARNPRINTF("transaction already started");
00086         ret = ermetadb_begin_transaction(db_state->mdb);
00087         if (ret == ERMETADB_DATABASE_BUSY)
00088         {
00089             // database locked, wait till database becomes available
00090             while ((ret == ERMETADB_DATABASE_BUSY) &&
00091                    (time(NULL) - start) <= MAX_WAIT_DATABASE )
00092             {
00093                 WARNPRINTF("database locked");
00094                 ret = ermetadb_begin_transaction(db_state->mdb);
00095             }
00096         }
00097     }
00098 
00099     db_state->transaction_started = TRUE;
00100 
00101     return ret;
00102 }

Here is the call graph for this function:

Here is the caller graph for this function:

void db_update_entry ( db_state_t db_state,
const db_entry_t entry 
)

Definition at line 121 of file index_db.c.

References ermetadb_global_update_size(), db_entry_t::file_id, db_entry_t::filesize, db_entry_t::last_modified, and db_state_t::mdb.

Referenced by handle_changes().

00122 {
00123     ermetadb_global_update_size(db_state->mdb, entry->file_id, entry->last_modified, entry->filesize);
00124 }

Here is the call graph for this function:

Here is the caller graph for this function:

void db_update_shortcut_entry ( db_state_t db_state,
const db_entry_t entry,
const char *  display_name,
const char *  author 
)

Definition at line 127 of file index_db.c.

References ermetadb_global_update_title(), db_entry_t::file_id, and db_state_t::mdb.

Referenced by handle_changes().

00128 {
00129     ermetadb_global_update_title(db_state->mdb, entry->file_id, display_name, author);
00130 }

Here is the call graph for this function:

Here is the caller graph for this function:

static db_entry_t* row2entry ( const metadata_cell row  )  [static]

Definition at line 165 of file index_db.c.

References db_entry_t::file_id, db_entry_t::filename, filename, db_entry_t::filepath, db_entry_t::filesize, db_entry_t::is_dir, db_entry_t::last_modified, metadata_cell::v_int64, metadata_cell::v_text, and metadata_cell::value.

Referenced by db_get_model().

00166 {
00167     // SQL result: file_id, filename, dirpath, isdir, size, date
00168     db_entry_t* entry = g_malloc(sizeof(db_entry_t));
00169     memset(entry, 0, sizeof(db_entry_t));
00170 
00171     const metadata_cell *cell = row;
00172     gint64 file_id = cell->value.v_int64;
00173     entry->file_id = file_id;
00174 
00175     cell++;
00176     char* filename = cell->value.v_text->str;
00177     entry->filename = filename;
00178 
00179     cell++;
00180     char* filepath = cell->value.v_text->str;
00181     entry->filepath = filepath;
00182 
00183     cell++;
00184     gboolean is_dir = cell->value.v_int64;
00185     entry->is_dir = is_dir;
00186 
00187     cell++;
00188     gint64 size = cell->value.v_int64;
00189     entry->filesize = size;
00190 
00191     cell++;
00192     gint64 filedate = cell->value.v_int64;
00193     entry->last_modified = filedate;
00194 
00195     return entry;
00196 }

Here is the caller graph for this function:

Generated by  doxygen 1.6.2-20100208