Logo Search packages:      
Sourcecode: easyh10 version File versions  Download package

int h10db_update ( h10db_t *  h10db,
int  flags 
)

Update the H10 database.

Parameters:
h10db The pointer to the h10db_t instance.
flags The operation flags.
Returns:
0 if succeeded, otherwise -1.

Definition at line 718 of file h10db.c.

References h10db_get_size(), and h10db_resize().

{
      int ret, i, num_deleted = 0, num_valid_idx = 0, idx_count = 0;

      h10db_progress(h10db, H10DB_PROGRESS_UPDATE_START, 0, 0);

      /* Clean unused entries if indicated. */
      if (flags & H10DB_UPDATEF_CLEAN) {
            h10db_progress(h10db, H10DB_PROGRESS_UPDATE_CLEAN, 0, 1);

            for (i = 0;;) {
                  h10db_dat_t* dat = NULL;
                  if (i >= h10db_get_size(h10db)) {
                        break;
                  }

                  /* Delete inactive entries. */
                  if (h10db->dat[i].status != 0) {
                        /* Destroy this item. */
                        h10db_dat_finish(&h10db->dat[i]);

                        /* Chink the deleted space. */
                        memmove(
                              &h10db->dat[i],
                              &h10db->dat[i+1],
                              sizeof(h10db_dat_t) * (h10db_get_size(h10db) - (i+1))
                              );
                        h10db_resize(h10db, h10db_get_size(h10db)-1);
                        h10db->hdr->num_dat_inactive_entries--;
                        num_deleted++;

                        /* Stay the current position. */
                  } else {
                        /* Move to next position. */
                        i++;
                  }
            }

            h10db_progress(h10db, H10DB_PROGRESS_UPDATE_CLEAN, 1, num_deleted);
      }

      /* Count the number of inactive entries. */
      h10db->hdr->num_dat_inactive_entries = count_invalid_entries(h10db->dat, h10db->hdr->num_dat_entries);

      /* Free allocated indexes. */
      free(h10db->idx);

      /* Allocate new indexes */
      h10db->idx = (h10db_idx_t**)calloc(h10db->hdr->num_dat_fields, sizeof(h10db_idx_t*));
      if (!h10db->idx) {
            return H10DBE_OUTOFMEMORY;          
      }           

      /* Count the number of valid indexes (only for progress report). */
      for (i = 0;i < H10DB_NUM_DAT_FIELDS;i++) {
            if (h10db->hdr->fd[i].has_index && h10db->hdr->fd[i].index_pathname) {
                  num_valid_idx++;
            }
      }

      /* Generate *.idx files. */
      for (i = 0;i < H10DB_NUM_DAT_FIELDS;i++) {
            h10db_progress(h10db, H10DB_PROGRESS_UPDATE_IDX, idx_count++, num_valid_idx);
            if (h10db->hdr->fd[i].has_index && h10db->hdr->fd[i].index_pathname) {
                  free(h10db->idx[i]);
                  ret = h10db_generate_idx(
                        &h10db->idx[i],
                        i,
                        h10db->dat,
                        h10db->hdr->num_dat_entries,
                        &h10db->hdr->fd[i]
                        );
                  if (ret != 0) {
                        return ret;
                  }
            }
      }
      h10db_progress(h10db, H10DB_PROGRESS_UPDATE_IDX, idx_count, num_valid_idx);

      h10db_progress(h10db, H10DB_PROGRESS_UPDATE_END, 0, 0);

      return 0;
}


Generated by  Doxygen 1.6.0   Back to index