int _citrus_db_get_entry(struct _citrus_db *db, int idx, struct _region *key, struct _region *data) { uint32_t num_entries; size_t offset; struct _memstream ms; struct _citrus_db_header_x *dhx; struct _citrus_db_entry_x *dex; _memstream_bind(&ms, &db->db_region); dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx)); _DIAGASSERT(dhx); num_entries = be32toh(dhx->dhx_num_entries); if (idx < 0 || (uint32_t)idx >= num_entries) return EINVAL; /* seek to the next entry */ offset = be32toh(dhx->dhx_entry_offset) + idx * _CITRUS_DB_ENTRY_SIZE; if (_citrus_memory_stream_seek(&ms, offset, SEEK_SET)) return EFTYPE; /* get the entry record */ dex = _memstream_getregion(&ms, NULL, _CITRUS_DB_ENTRY_SIZE); if (dex == NULL) return EFTYPE; /* seek to the head of the key. */ if (_memstream_seek(&ms, be32toh(dex->dex_key_offset), SEEK_SET)) return EFTYPE; /* get the region of the key. */ if (_memstream_getregion(&ms, key, be32toh(dex->dex_key_size))==NULL) return EFTYPE; /* seek to the head of the data. */ if (_memstream_seek(&ms, be32toh(dex->dex_data_offset), SEEK_SET)) return EFTYPE; /* get the region of the data. */ if (_memstream_getregion(&ms, data, be32toh(dex->dex_data_size))==NULL) return EFTYPE; return 0; }
int _citrus_db_lookup(struct _citrus_db *db, struct _citrus_region *key, struct _citrus_region *data, struct _citrus_db_locator *dl) { struct _citrus_db_entry_x *dex; struct _citrus_db_header_x *dhx; struct _citrus_region r; struct _memstream ms; uint32_t hashval, num_entries; size_t offset; _memstream_bind(&ms, &db->db_region); dhx = _memstream_getregion(&ms, NULL, sizeof(*dhx)); num_entries = be32toh(dhx->dhx_num_entries); if (num_entries == 0) return (ENOENT); if (dl != NULL && dl->dl_offset>0) { hashval = dl->dl_hashval; offset = dl->dl_offset; if (offset >= _region_size(&db->db_region)) return (ENOENT); } else { hashval = db->db_hashfunc(key)%num_entries; offset = be32toh(dhx->dhx_entry_offset) + hashval * _CITRUS_DB_ENTRY_SIZE; if (dl) dl->dl_hashval = hashval; } do { /* seek to the next entry */ if (_citrus_memory_stream_seek(&ms, offset, SEEK_SET)) return (EFTYPE); /* get the entry record */ dex = _memstream_getregion(&ms, NULL, _CITRUS_DB_ENTRY_SIZE); if (dex == NULL) return (EFTYPE); /* jump to next entry having the same hash value. */ offset = be32toh(dex->dex_next_offset); /* save the current position */ if (dl) { dl->dl_offset = offset; if (offset == 0) dl->dl_offset = _region_size(&db->db_region); } /* compare hash value. */ if (be32toh(dex->dex_hash_value) != hashval) /* not found */ break; /* compare key length */ if (be32toh(dex->dex_key_size) == _region_size(key)) { /* seek to the head of the key. */ if (_memstream_seek(&ms, be32toh(dex->dex_key_offset), SEEK_SET)) return (EFTYPE); /* get the region of the key */ if (_memstream_getregion(&ms, &r, _region_size(key)) == NULL) return (EFTYPE); /* compare key byte stream */ if (memcmp(_region_head(&r), _region_head(key), _region_size(key)) == 0) { /* match */ if (_memstream_seek( &ms, be32toh(dex->dex_data_offset), SEEK_SET)) return (EFTYPE); if (_memstream_getregion( &ms, data, be32toh(dex->dex_data_size)) == NULL) return (EFTYPE); return (0); } } } while (offset != 0); return (ENOENT); }