Directory
eXtremeDB_rt/samples/hash_lookupDescription
demonstrates operations on unique and non-unique hash indexes
Used API
Class_Index_search(), Class_Index_find()
Code snippet
class Record { uint4 iIdx; /* Index */ uint4 iSeries; /* Series of measurement */ hash <iIdx> I_Index[10000]; nonunique hash <iSeries> I_Series[10000]; }; rc = mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t); if ( MCO_S_OK == rc ) { /* Open the cursor */ rc = Record_I_Index_index_cursor(t, &csr); if ( MCO_S_OK == rc ) { /* Get found record - or not */ rc = Record_I_Index_find(t, findValue, &rec); if ( MCO_S_OK == rc ) { Record_iIdx_get(&rec, &idx); Record_iSeries_get(&rec, &series); printf("\n\tIndex %d Series %d", idx, series); } else { printf("\tnot found"); } } /* Close the transaction */ mco_trans_rollback(t); } … /* Show all records with specified value in non-unique index */ findValue = 40; printf("\n\n\tSearch for records with iSeries == %d :", findValue); /* Open a transaction */ rc = mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t); if ( MCO_S_OK == rc ) { /* Open the cursor */ rc = Record_I_Series_index_cursor(t, &csr); if ( MCO_S_OK == rc ) { /* Search for records with specified value for iSeries */ rc = Record_I_Series_search(t, &csr, findValue); if ( MCO_S_OK == rc ) { /* Move to first item */ rc = mco_cursor_first(t, &csr); /* Show all records in cursor */ for (i = 0; i < nRecs && MCO_S_OK == rc; i++) { /* Get Record from cursor */ rc = Record_from_cursor(t, &csr, &rec); if ( MCO_S_OK == rc ) { Record_iIdx_get(&rec, &idx); Record_iSeries_get(&rec, &series); printf("\n\tIndex %d Series %d", idx, series); /* Move to next item */ rc = mco_cursor_next(t, &csr); } } rc = MCO_S_OK;What to expect as an output
Insert 100 test records .......... Find record with index == 79 : Index 79 Series 40 Search for records with iSeries == 40 : Index 79 Series 40 Index 78 Series 40