int main() { Bitmap bm; bm = bitmapCreate(10); if (!bm) { fprintf(stderr, "Failed to create bitmap.\n"); return 1; } bitmapPrint(bm); bitmapSet(&bm, 3); bitmapSet(&bm, 0); bitmapSet(&bm, 8); bitmapPrint(bm); bitmapUnset(&bm, 3); bitmapPrint(bm); bitmapClear(&bm); bitmapPrint(bm); bitmapDestroy(&bm); printf("End\n"); return 0; }
static int dbPrintVar(char *varName, response *res, error *err) { if (varName == NULL) { ERROR(err, E_BADARG); goto exit; } void *payload; VAR_TYPE type; if (varMapGetVar(varName, &type, &payload, err)) { goto exit; } int resultBytes; int *results; if (type == VAR_BMP) { struct bitmap *bmp = (struct bitmap *) payload; printf("Bitmap result: \n"); bitmapPrint(bmp); int bmpSize = bitmapSize(bmp); resultBytes = bmpSize * sizeof(int); results = (int *) malloc(resultBytes); if (resultBytes == 0) { free(results); results = NULL; } else { for (int i = 0; i < bmpSize; i++) { results[i] = bitmapIsSet(bmp, i) ? 1 : 0; } } } else if (type == VAR_RESULTS) { fetchResults *fResults = (fetchResults *) payload; resultBytes = fResults->nResultEntries * sizeof(int); results = (int *) malloc(resultBytes); memcpy(results, fResults->results, resultBytes); if (resultBytes == 0) { free(results); results = NULL; } } else { ERROR(err, E_VARTYPE); goto exit; } RESPONSE(res, "Print var results:", resultBytes, results); return 0; exit: return 1; }
static int dbFetch(tableInfo *tbl, fetchArgs *args, response *res, error *err) { char *columnName = args->columnName; char *oldVarName = args->oldVarName; char *newVarName = args->newVarName; if (columnName == NULL || oldVarName == NULL) { ERROR(err, E_BADARG); goto exit; } printf("Fetching from column '%s'...\n", columnName); // Get bitmap from var name struct bitmap *bmp; VAR_TYPE type; if (varMapGetVar(oldVarName, &type, (void **) (&bmp), err)) { goto exit; } if (type != VAR_BMP) { ERROR(err, E_VARTYPE); goto exit; } printf("Got bitmap for variable '%s'\n", oldVarName); bitmapPrint(bmp); // Retrieve the column from disk column *col = (column *) malloc(sizeof(column)); if (col == NULL) { ERROR(err, E_NOMEM); goto exit; } if (columnReadFromDisk(tbl, columnName, col, err)) { free(col); goto exit; } // Fetch the results int resultBytes; int *results; int *indices; if (columnFetch(col, bmp, &resultBytes, &results, &indices, err)) { goto cleanupColumn; } // Return data to user if the haven't given a variable to store it in if (newVarName[0] == '\0') { int nResults = resultBytes / sizeof(int); printf("Got %d results from fetch\n", nResults); printf("["); for (int i = 0; i < nResults; i++) { printf("%d,", results[i]); } printf("]\n"); if (resultBytes == 0) { results = NULL; } RESPONSE(res, "Fetch results:", resultBytes, results); free(indices); } // Otherwise, store data in variable else { // Add variable-results pair to varmap fetchResults *fResults = (fetchResults *) malloc(sizeof(fetchResults)); if (fResults == NULL) { ERROR(err, E_NOMEM); goto cleanupColumn; } fResults->nColumnEntries = bitmapSize(bmp); fResults->nResultEntries = resultBytes / sizeof(int); fResults->results = results; fResults->indices = indices; if (varMapAddVar(newVarName, VAR_RESULTS, fResults, err)) { free(results); free(indices); free(fResults); goto cleanupColumn; } printf("Added variable '%s'\n", newVarName); RESPONSE_SUCCESS(res); } // Cleanup columnDestroy(col); return 0; // cleanupResults: // free(results); // free(indices); cleanupColumn: columnDestroy(col); exit: return 1; }