| 1 | /* |
| 2 | * ai_obj.h |
| 3 | * |
| 4 | * Copyright (C) 2013-2014 Aerospike, Inc. |
| 5 | * |
| 6 | * Portions may be licensed to Aerospike, Inc. under one or more contributor |
| 7 | * license agreements. |
| 8 | * |
| 9 | * This program is free software: you can redistribute it and/or modify it under |
| 10 | * the terms of the GNU Affero General Public License as published by the Free |
| 11 | * Software Foundation, either version 3 of the License, or (at your option) any |
| 12 | * later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 16 | * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more |
| 17 | * details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see http://www.gnu.org/licenses/ |
| 21 | */ |
| 22 | /* |
| 23 | * Aerospike Index Object Implementation. |
| 24 | */ |
| 25 | |
| 26 | #include <assert.h> |
| 27 | #include <ctype.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <strings.h> |
| 31 | #include <unistd.h> |
| 32 | #include <sys/param.h> // For MIN(). |
| 33 | |
| 34 | #include "ai_obj.h" |
| 35 | #include "stream.h" |
| 36 | |
| 37 | #include <citrusleaf/alloc.h> |
| 38 | |
| 39 | void init_ai_obj(ai_obj *a) |
| 40 | { |
| 41 | bzero(a, sizeof(ai_obj)); |
| 42 | a->type = COL_TYPE_INVALID; |
| 43 | } |
| 44 | |
| 45 | void init_ai_objLong(ai_obj *a, ulong l) |
| 46 | { |
| 47 | init_ai_obj(a); |
| 48 | a->l = l; |
| 49 | a->type = COL_TYPE_LONG; |
| 50 | } |
| 51 | |
| 52 | void init_ai_objU160(ai_obj *a, uint160 y) { |
| 53 | a->type = COL_TYPE_DIGEST; |
| 54 | a->y = y; |
| 55 | } |
| 56 | |
| 57 | void ai_objClone(ai_obj *dest, ai_obj *src) |
| 58 | { |
| 59 | memcpy(dest, src, sizeof(ai_obj)); |
| 60 | } |
| 61 | |
| 62 | static int ai_objCmp(ai_obj *a, ai_obj *b) |
| 63 | { |
| 64 | if (C_IS_L(a->type) || C_IS_G(a->type)) { |
| 65 | return (a->l == b->l) ? 0 : ((a->l > b->l) ? 1 : -1); |
| 66 | } else if (C_IS_DG(a->type)) { |
| 67 | return u160Cmp(&a->y, &b->y); |
| 68 | } else { |
| 69 | assert(!"ai_objCmp ERROR" ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | bool ai_objEQ(ai_obj *a, ai_obj *b) |
| 74 | { |
| 75 | return !ai_objCmp(a, b); |
| 76 | } |
| 77 | |
| 78 | static void dump_ai_obj_internal(FILE *fp, ai_obj *a, bool as_digest) |
| 79 | { |
| 80 | if (C_IS_L(a->type) || C_IS_G(a->type)) { |
| 81 | fprintf(fp, "\tLONG ai_obj: val: %lu\n" , a->l); |
| 82 | } else if (C_IS_DG(a->type)) { |
| 83 | fprintf(fp, "\tU160 ai_obj:" ); |
| 84 | if (as_digest) { |
| 85 | const int len = 20; |
| 86 | char digest_str[2 + (len * 2) + 1]; |
| 87 | digest_str[0] = '\0'; |
| 88 | generate_packed_hex_string((uint8_t *) &(a->y), len, digest_str); |
| 89 | fprintf(fp, "%s\n" , digest_str); |
| 90 | } else { |
| 91 | DEBUG_U160(fp, a->y); |
| 92 | fprintf(fp, "\n" ); |
| 93 | } |
| 94 | } else { |
| 95 | fprintf(fp, "\tUNINITIALISED ai_obj\n" ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | |
| 100 | void dump_ai_obj_as_digest(FILE *fp, ai_obj *a) |
| 101 | { |
| 102 | dump_ai_obj_internal(fp, a, true); |
| 103 | } |
| 104 | |