Tarantool development patches archive
 help / color / mirror / Atom feed
From: Kirill Shcherbatov <kshcherbatov@tarantool.org>
To: tarantool-patches@freelists.org, vdavydov.dev@gmail.com
Cc: kostja@tarantool.org, Kirill Shcherbatov <kshcherbatov@tarantool.org>
Subject: [PATCH v5 4/9] lib: introduce json_path_cmp routine
Date: Mon, 26 Nov 2018 13:49:38 +0300	[thread overview]
Message-ID: <edf19388a098ecb3a547f403f56ca1a9c86a5f76.1543229303.git.kshcherbatov@tarantool.org> (raw)
In-Reply-To: <cover.1543229303.git.kshcherbatov@tarantool.org>
In-Reply-To: <cover.1543229303.git.kshcherbatov@tarantool.org>

Introduced a new json_path_cmp routine as a part of JSON library
to compare JSON paths that may have different representation.

Need for #1012
---
 src/lib/json/json.c        | 28 ++++++++++++++++++++++++++++
 src/lib/json/json.h        | 11 +++++++++++
 test/unit/json_path.c      | 31 ++++++++++++++++++++++++++++++-
 test/unit/json_path.result | 12 +++++++++++-
 4 files changed, 80 insertions(+), 2 deletions(-)

diff --git a/src/lib/json/json.c b/src/lib/json/json.c
index 9198dca..20fbbba 100644
--- a/src/lib/json/json.c
+++ b/src/lib/json/json.c
@@ -507,3 +507,31 @@ json_tree_postorder_next(struct json_token *root, struct json_token *pos)
 	}
 	return pos->parent != root ? pos->parent : NULL;
 }
+
+int
+json_path_cmp(const char *a, uint32_t a_len, const char *b, uint32_t b_len)
+{
+	struct json_lexer lexer_a, lexer_b;
+	json_lexer_create(&lexer_a, a, a_len);
+	json_lexer_create(&lexer_b, b, b_len);
+	struct json_token token_a, token_b;
+	int rc_a, rc_b;
+	while ((rc_a = json_lexer_next_token(&lexer_a, &token_a)) == 0 &&
+		(rc_b = json_lexer_next_token(&lexer_b, &token_b)) == 0 &&
+		token_a.key.type != JSON_TOKEN_END &&
+		token_b.key.type != JSON_TOKEN_END) {
+		int rc = json_token_key_cmp(&token_a, &token_b);
+		if (rc != 0)
+			return rc;
+	}
+	/* Path "a" should be valid. */
+	assert(rc_a == 0);
+	if (rc_b != 0)
+		return rc_b;
+	/*
+	 * The parser stopped because the end of one of the paths
+	 * was reached. As JSON_TOKEN_END > JSON_TOKEN_{NUM, STR},
+	 * the path having more tokens has lower key.type value.
+	 */
+	return token_b.key.type - token_a.key.type;
+}
diff --git a/src/lib/json/json.h b/src/lib/json/json.h
index dd09f5a..7d46601 100644
--- a/src/lib/json/json.h
+++ b/src/lib/json/json.h
@@ -137,6 +137,17 @@ json_lexer_create(struct json_lexer *lexer, const char *src, int src_len)
 int
 json_lexer_next_token(struct json_lexer *lexer, struct json_token *token);
 
+/**
+ * Compare two JSON paths using Lexer class.
+ * - @a path must be valid
+ * - at the case of paths that have same token-sequence prefix,
+ *   the path having more tokens is assumed to be greater
+ * - when @b path contains an error, the path "a" is assumed to
+ *   be greater
+ */
+int
+json_path_cmp(const char *a, uint32_t a_len, const char *b, uint32_t b_len);
+
 /** Create a JSON tree object to manage data relations. */
 int
 json_tree_create(struct json_tree *tree);
diff --git a/test/unit/json_path.c b/test/unit/json_path.c
index f6b0472..35c2164 100644
--- a/test/unit/json_path.c
+++ b/test/unit/json_path.c
@@ -352,15 +352,44 @@ test_tree()
 	footer();
 }
 
+void
+test_path_cmp()
+{
+	const char *a = "Data[1][\"FIO\"].fname";
+	uint32_t a_len = strlen(a);
+	const struct path_and_errpos rc[] = {
+		{a, 0},
+		{"[\"Data\"][1].FIO[\"fname\"]", 0},
+		{"Data[[1][\"FIO\"].fname", 6},
+		{"Data[1]", 1},
+		{"Data[1][\"FIO\"].fname[1]", -2},
+		{"Data[1][\"Info\"].fname[1]", -1},
+	};
+	header();
+	plan(lengthof(rc));
+
+	for (size_t i = 0; i < lengthof(rc); ++i) {
+		const char *path = rc[i].path;
+		int errpos = rc[i].errpos;
+		int rc = json_path_cmp(a, a_len, path, strlen(path));
+		is(rc, errpos, "path cmp result \"%s\" with \"%s\": "
+		   "have %d, expected %d", a, path, rc, errpos);
+	}
+
+	check_plan();
+	footer();
+}
+
 int
 main()
 {
 	header();
-	plan(3);
+	plan(4);
 
 	test_basic();
 	test_errors();
 	test_tree();
+	test_path_cmp();
 
 	int rc = check_plan();
 	footer();
diff --git a/test/unit/json_path.result b/test/unit/json_path.result
index df68210..5c1de38 100644
--- a/test/unit/json_path.result
+++ b/test/unit/json_path.result
@@ -1,5 +1,5 @@
 	*** main ***
-1..3
+1..4
 	*** test_basic ***
     1..71
     ok 1 - parse <[0]>
@@ -138,4 +138,14 @@ ok 2 - subtests
     ok 35 - records iterated count 4 of 4
 ok 3 - subtests
 	*** test_tree: done ***
+	*** test_path_cmp ***
+    1..6
+    ok 1 - path cmp result "Data[1]["FIO"].fname" with "Data[1]["FIO"].fname": have 0, expected 0
+    ok 2 - path cmp result "Data[1]["FIO"].fname" with "["Data"][1].FIO["fname"]": have 0, expected 0
+    ok 3 - path cmp result "Data[1]["FIO"].fname" with "Data[[1]["FIO"].fname": have 6, expected 6
+    ok 4 - path cmp result "Data[1]["FIO"].fname" with "Data[1]": have 1, expected 1
+    ok 5 - path cmp result "Data[1]["FIO"].fname" with "Data[1]["FIO"].fname[1]": have -2, expected -2
+    ok 6 - path cmp result "Data[1]["FIO"].fname" with "Data[1]["Info"].fname[1]": have -1, expected -1
+ok 4 - subtests
+	*** test_path_cmp: done ***
 	*** main: done ***
-- 
2.7.4

  parent reply	other threads:[~2018-11-26 10:49 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-26 10:49 [PATCH v5 0/9] box: indexes by JSON path Kirill Shcherbatov
2018-11-26 10:49 ` [PATCH v5 1/9] box: refactor json_path_parser class Kirill Shcherbatov
2018-11-26 12:53   ` [tarantool-patches] " Kirill Shcherbatov
2018-11-29 15:39     ` Vladimir Davydov
2018-11-26 10:49 ` [PATCH v5 2/9] lib: implement JSON tree class for json library Kirill Shcherbatov
2018-11-26 12:53   ` [tarantool-patches] " Kirill Shcherbatov
2018-11-29 17:38     ` Vladimir Davydov
2018-11-29 17:50       ` Vladimir Davydov
2018-12-04 15:22       ` Vladimir Davydov
2018-12-04 15:47       ` [tarantool-patches] " Kirill Shcherbatov
2018-12-04 17:54         ` Vladimir Davydov
2018-12-05  8:37           ` Kirill Shcherbatov
2018-12-05  9:07             ` Vladimir Davydov
2018-12-05  9:52               ` Vladimir Davydov
2018-12-06  7:56                 ` Kirill Shcherbatov
2018-12-06  7:56                 ` [tarantool-patches] Re: [PATCH v5 2/9] lib: make index_base support for json_lexer Kirill Shcherbatov
2018-11-26 10:49 ` [PATCH v5 3/9] box: manage format fields with JSON tree class Kirill Shcherbatov
2018-11-29 19:07   ` Vladimir Davydov
2018-12-04 15:47     ` [tarantool-patches] " Kirill Shcherbatov
2018-12-04 16:09       ` Vladimir Davydov
2018-12-04 16:32         ` Kirill Shcherbatov
2018-12-05  8:37         ` Kirill Shcherbatov
2018-12-06  7:56         ` Kirill Shcherbatov
2018-12-06  8:06           ` Vladimir Davydov
2018-11-26 10:49 ` Kirill Shcherbatov [this message]
2018-11-30 10:46   ` [PATCH v5 4/9] lib: introduce json_path_cmp routine Vladimir Davydov
2018-12-03 17:37     ` [tarantool-patches] " Konstantin Osipov
2018-12-03 18:48       ` Vladimir Davydov
2018-12-03 20:14         ` Konstantin Osipov
2018-12-06  7:56           ` [tarantool-patches] Re: [PATCH v5 4/9] lib: introduce json_path_cmp, json_path_validate Kirill Shcherbatov
2018-11-26 10:49 ` [tarantool-patches] [PATCH v5 5/9] box: introduce JSON indexes Kirill Shcherbatov
2018-11-30 21:28   ` Vladimir Davydov
2018-12-01 16:49     ` Vladimir Davydov
2018-11-26 10:49 ` [PATCH v5 6/9] box: introduce has_json_paths flag in templates Kirill Shcherbatov
2018-11-26 10:49 ` [PATCH v5 7/9] box: tune tuple_field_raw_by_path for indexed data Kirill Shcherbatov
2018-12-01 17:20   ` Vladimir Davydov
2018-11-26 10:49 ` [PATCH v5 8/9] box: introduce offset slot cache in key_part Kirill Shcherbatov
2018-12-03 21:04   ` Vladimir Davydov
2018-12-04 15:51     ` Vladimir Davydov
2018-11-26 10:49 ` [PATCH v5 9/9] box: specify indexes in user-friendly form Kirill Shcherbatov
2018-12-04 12:22   ` Vladimir Davydov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=edf19388a098ecb3a547f403f56ca1a9c86a5f76.1543229303.git.kshcherbatov@tarantool.org \
    --to=kshcherbatov@tarantool.org \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=vdavydov.dev@gmail.com \
    --subject='Re: [PATCH v5 4/9] lib: introduce json_path_cmp routine' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox