Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

arduino - How to pass values in dblog_bin_srch_row_by_val function of github.com/siara-cc/sqlite_micro_logger_arduino to search values by 0/1?

Please help me to search a element though binary search function that getting value from sdcard using arduino library :https://github.com/siara-cc/sqlite_micro_logger_arduino

int compare_values(byte *val_at, uint32_t u32_at, int val_type, void *val, uint16_t len, byte is_rowid) { if (is_rowid) return (u32_at > *((uint32_t *) val) ? 1 : u32_at < *((uint32_t *) val) ? -1 : 0); switch (val_type) { case DBLOG_TYPE_INT: { int64_t ival_at = convert_to_i64(val_at, u32_at, 1); int64_t ival = convert_to_i64(val, len, 0); return ival_at > ival ? 1 : (ival_at < ival ? -1 : 0); } case DBLOG_TYPE_REAL: if ((len != 4 && len != 8) || u32_at != 7) return DBLOG_RES_TYPE_MISMATCH; int64_t bytes64, bytes64_at; bytes64_at = read_uint64(val_at); if (len == 4) bytes64 = float_to_double(val); else bytes64 = *((int64_t *) val); return (bytes64_at > bytes64 ? 1 : bytes64_at < bytes64 ? -1 : 0); case DBLOG_TYPE_BLOB: case DBLOG_TYPE_TEXT: { uint32_t len_at = dblog_derive_data_len(u32_at); int res = compare_bin(val_at, len_at, val, len); return (res > 0 ? 1 : (res < 0 ? -1 : 0)); } } return 1; }

// See .h file for API description
int dblog_bin_srch_row_by_val(struct dblog_read_context *rctx, int col_idx,
      int val_type, void *val, uint16_t len, byte is_rowid) {
  int32_t page_size = get_pagesize(rctx->page_size_exp);
  if (rctx->last_leaf_page == 0)
    return DBLOG_RES_NOT_FINALIZED;
  uint32_t middle, first, size;
  int res;
  first = 1;
  size = rctx->last_leaf_page + 1;
  while (first < size) {
    middle = (first + size) >> 1;
    uint16_t rec_pos;
    byte val_at[len + 1];
    uint32_t u32_at;
    res = read_last_val(rctx, middle, page_size, col_idx, 
            val_at, len + 1, &u32_at, &rec_pos, is_rowid);
    if (res)
      return res;
    int cmp = compare_values(val_at, u32_at, val_type, val, len, is_rowid);
    if (cmp == DBLOG_RES_TYPE_MISMATCH)
      return cmp;
    if (cmp < 0)
      first = middle + 1;
    else if (cmp > 0)
      size = middle;
    else {
      rctx->cur_page = middle;
      rctx->cur_rec_pos = rec_pos;
      res = read_bytes_rctx(rctx, rctx->buf, middle * page_size, page_size);
      if (res)
        return res;
      return DBLOG_RES_OK;
    }
  }
  if (size == rctx->last_leaf_page + 1)
    size--;
  uint32_t found_at_page = size;
  res = read_bytes_rctx(rctx, rctx->buf, size * page_size, page_size);
  if (res)
    return res;
  first = 0;
  int16_t rec_count = read_uint16(rctx->buf + 3) - 1;
  size = rec_count;
  while (first < size) {
    middle = (first + size) >> 1;
    uint32_t u32_at;
    byte *val_at = read_val_at(rctx, middle, col_idx, &u32_at, is_rowid);
    if (!val_at)
      return DBLOG_RES_NOT_FOUND;
    int cmp = compare_values(val_at, u32_at, val_type, val, len, is_rowid);
    if (cmp == DBLOG_RES_TYPE_MISMATCH)
      return cmp;
    if (cmp < 0)
      first = middle + 1;
    else if (cmp > 0)
      size = middle;
    else {
      rctx->cur_page = found_at_page;
      rctx->cur_rec_pos = middle;
      return DBLOG_RES_OK;
    }
  }
  rctx->cur_page = found_at_page;
  rctx->cur_rec_pos = size;
  return DBLOG_RES_OK;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...