int view_key_cmp(const sized_buf *key1, const sized_buf *key2, const void *user_ctx) { uint16_t json_key1_len = decode_raw16(*((raw_16 *) key1->buf)); uint16_t json_key2_len = decode_raw16(*((raw_16 *) key2->buf)); sized_buf json_key1; sized_buf json_key2; sized_buf doc_id1; sized_buf doc_id2; int res; (void)user_ctx; json_key1.buf = key1->buf + sizeof(uint16_t); json_key1.size = json_key1_len; json_key2.buf = key2->buf + sizeof(uint16_t); json_key2.size = json_key2_len; res = CollateJSON(&json_key1, &json_key2, kCollateJSON_Unicode); if (res == 0) { doc_id1.buf = key1->buf + sizeof(uint16_t) + json_key1.size; doc_id1.size = key1->size - sizeof(uint16_t) - json_key1.size; doc_id2.buf = key2->buf + sizeof(uint16_t) + json_key2.size; doc_id2.size = key2->size - sizeof(uint16_t) - json_key2.size; res = ebin_cmp(&doc_id1, &doc_id2); } return res; }
static int collateStrs(const char* str1, const char* str2, CollateJSONMode mode) { /* Be evil and put numeric garbage past the ends of str1 and str2, to make sure it doesn't confuse the numeric parsing in the collator: */ size_t len1 = strlen(str1), len2 = strlen(str2); char *padded1 = malloc(len1 + 3); char *padded2 = malloc(len2 + 3); int ret; sized_buf buf1; sized_buf buf2; assert(padded1); assert(padded2); strcpy(padded1, str1); strcat(padded1, "99"); strcpy(padded2, str2); strcat(padded2, "88"); buf1.buf = padded1; buf1.size = len1; buf2.buf = padded2; buf2.size = len2; ret = CollateJSON(&buf1, &buf2, mode); free(padded1); free(padded2); return ret; }
/** * implement a test method that can be called from java */ JNIEXPORT jint JNICALL Java_com_couchbase_lite_android_SQLiteJsonCollator_testCollateJSON (JNIEnv *env, jclass clazz, jint mode, jint len1, jstring string1, jint len2, jstring string2) { jboolean isCopy; const char* cstring1 = env->GetStringUTFChars(string1, &isCopy); const char* cstring2 = env->GetStringUTFChars(string2, &isCopy); int result = CollateJSON((void *) mode, (int)len1, cstring1, (int)len2, cstring2); env->ReleaseStringUTFChars(string1, cstring1); env->ReleaseStringUTFChars(string2, cstring2); return result; }