Tarantool development patches archive
 help / color / mirror / Atom feed
From: Kirill Shcherbatov <kshcherbatov@tarantool.org>
To: tarantool-patches@freelists.org,
	Vladimir Davydov <vdavydov.dev@gmail.com>
Subject: Re: [tarantool-patches] Re: [PATCH v3 2/7] lib: introduce a new json_path_multikey_offset helper
Date: Wed, 3 Apr 2019 19:22:57 +0300	[thread overview]
Message-ID: <6becaa9e-9d2f-1fbe-7748-9c04b016b45f@tarantool.org> (raw)
In-Reply-To: <20190403125601.unoe7qjdl6clqd55@esperanza>

> IMO returning path_len if no multikey token is found would be more
> common (think of STL methods, which return end() iterator if not found).
> Doesn't really matter though.
> 
> Other than that looks good to me.

I don't mind. Updated version is on the branch.

==================================================

Introduced a new procedure json_path_multikey_offset. This helper
scans the JSON path string and returns the offset of the first character
[*] (the array index placeholder).

We need this function in the scope of the multikey index patchset to
extract the number of keys to be inserted into the index
using JSON subpath that has json_path_multikey_offset() length.

Needed for #1257
---
 src/lib/json/json.c   | 18 ++++++++++++++++++
 src/lib/json/json.h   | 10 ++++++++++
 test/unit/json.c      | 32 +++++++++++++++++++++++++++++++-
 test/unit/json.result | 12 +++++++++++-
 4 files changed, 70 insertions(+), 2 deletions(-)

diff --git a/src/lib/json/json.c b/src/lib/json/json.c
index 854158f63..7ef511d4e 100644
--- a/src/lib/json/json.c
+++ b/src/lib/json/json.c
@@ -321,6 +321,24 @@ json_path_validate(const char *path, int path_len, int index_base)
 	return rc;
 }
 
+int
+json_path_multikey_offset(const char *path, int path_len, int index_base)
+{
+	struct json_lexer lexer;
+	json_lexer_create(&lexer, path, path_len, index_base);
+	struct json_token token;
+	int rc, last_lexer_offset = 0;
+	while ((rc = json_lexer_next_token(&lexer, &token)) == 0) {
+		if (token.type == JSON_TOKEN_ANY)
+			return last_lexer_offset;
+		else if (token.type == JSON_TOKEN_END)
+			break;
+		last_lexer_offset = lexer.offset;
+	}
+	assert(rc == 0);
+	return path_len;
+}
+
 /**
  * An snprint-style helper to print an individual token key.
  */
diff --git a/src/lib/json/json.h b/src/lib/json/json.h
index c1de3e579..d66a9c7a4 100644
--- a/src/lib/json/json.h
+++ b/src/lib/json/json.h
@@ -259,6 +259,16 @@ json_path_cmp(const char *a, int a_len, const char *b, int b_len,
 int
 json_path_validate(const char *path, int path_len, int index_base);
 
+/**
+ * Scan the JSON path string and return the offset of the first
+ * character [*] (the array index placeholder).
+ * - if [*] is not found, path_len is returned.
+ * - specified JSON path must be valid
+ *   (may be tested with json_path_validate).
+ */
+int
+json_path_multikey_offset(const char *path, int path_len, int index_base);
+
 /**
  * Test if a given JSON token is a JSON tree leaf, i.e.
  * has no child nodes.
diff --git a/test/unit/json.c b/test/unit/json.c
index fd320c9eb..2b7236ea8 100644
--- a/test/unit/json.c
+++ b/test/unit/json.c
@@ -555,17 +555,47 @@ test_path_snprint()
 	footer();
 }
 
+void
+test_path_multikey()
+{
+	static struct {
+		const char *str;
+		int rc;
+	} test_cases[] = {
+		{"", 0},
+		{"[1]Data[1]extra[1]", 18},
+		{"[*]Data[1]extra[1]", 0},
+		{"[*]Data[*]extra[1]", 0},
+		{"[1]Data[*]extra[1]", 7},
+		{"[1]Data[1]extra[*]", 15},
+	};
+
+	header();
+	plan(lengthof(test_cases));
+	for (unsigned i = 0; i < lengthof(test_cases); i++) {
+		int rc = json_path_multikey_offset(test_cases[i].str,
+						   strlen(test_cases[i].str),
+						   INDEX_BASE);
+		is(rc, test_cases[i].rc, "Test json_path_multikey_offset with "
+		   "%s: have %d expected %d", test_cases[i].str, rc,
+		   test_cases[i].rc);
+	}
+	check_plan();
+	footer();
+}
+
 int
 main()
 {
 	header();
-	plan(5);
+	plan(6);
 
 	test_basic();
 	test_errors();
 	test_tree();
 	test_path_cmp();
 	test_path_snprint();
+	test_path_multikey();
 
 	int rc = check_plan();
 	footer();
diff --git a/test/unit/json.result b/test/unit/json.result
index 3a15e84bb..1c3e7f7d2 100644
--- a/test/unit/json.result
+++ b/test/unit/json.result
@@ -1,5 +1,5 @@
 	*** main ***
-1..5
+1..6
 	*** test_basic ***
     1..71
     ok 1 - parse <[1]>
@@ -194,4 +194,14 @@ ok 4 - subtests
     ok 9 - 0-byte buffer - retval
 ok 5 - subtests
 	*** test_path_snprint: done ***
+	*** test_path_multikey ***
+    1..6
+    ok 1 - Test json_path_multikey_offset with : have 0 expected 0
+    ok 2 - Test json_path_multikey_offset with [1]Data[1]extra[1]: have 18 expected 18
+    ok 3 - Test json_path_multikey_offset with [*]Data[1]extra[1]: have 0 expected 0
+    ok 4 - Test json_path_multikey_offset with [*]Data[*]extra[1]: have 0 expected 0
+    ok 5 - Test json_path_multikey_offset with [1]Data[*]extra[1]: have 7 expected 7
+    ok 6 - Test json_path_multikey_offset with [1]Data[1]extra[*]: have 15 expected 15
+ok 6 - subtests
+	*** test_path_multikey: done ***
 	*** main: done ***
-- 
2.21.0

 

  reply	other threads:[~2019-04-03 16:22 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-02 15:49 [PATCH v3 0/7] box: introduce multikey indexes in memtx Kirill Shcherbatov
2019-04-02 15:49 ` [PATCH v3 1/7] box: cleanup key_def virtual extract_key setter Kirill Shcherbatov
2019-04-03 12:42   ` Vladimir Davydov
2019-04-03 16:22     ` [tarantool-patches] " Kirill Shcherbatov
2019-04-03 18:01       ` Vladimir Davydov
2019-04-02 15:49 ` [PATCH v3 2/7] lib: introduce a new json_path_multikey_offset helper Kirill Shcherbatov
2019-04-03 12:56   ` Vladimir Davydov
2019-04-03 16:22     ` Kirill Shcherbatov [this message]
2019-04-03 18:02       ` [tarantool-patches] " Vladimir Davydov
2019-04-04  6:17       ` Konstantin Osipov
2019-04-02 15:49 ` [PATCH v3 3/7] box: move offset_slot init to tuple_format_add_field Kirill Shcherbatov
2019-04-03 12:57   ` Vladimir Davydov
2019-04-03 18:02   ` Vladimir Davydov
2019-04-04  6:19   ` [tarantool-patches] " Konstantin Osipov
2019-04-05 17:17     ` [tarantool-patches] " Kirill Shcherbatov
2019-04-02 15:49 ` [PATCH v3 4/7] lib: update msgpuck library Kirill Shcherbatov
2019-04-03 17:49   ` [tarantool-patches] " Kirill Shcherbatov
2019-04-04 15:54     ` Vladimir Davydov
2019-04-05 17:17       ` [tarantool-patches] " Kirill Shcherbatov
2019-04-07 12:22         ` Vladimir Davydov
2019-04-02 15:49 ` [PATCH v3 5/7] box: introduce tuple_parse_iterator class Kirill Shcherbatov
2019-04-03 14:04   ` Vladimir Davydov
2019-04-05 17:17     ` [tarantool-patches] " Kirill Shcherbatov
2019-04-02 15:49 ` [PATCH v3 6/7] box: introduce field_map_builder class Kirill Shcherbatov
2019-04-03 14:38   ` Vladimir Davydov
2019-04-05 17:17     ` [tarantool-patches] " Kirill Shcherbatov
2019-04-03 16:30   ` Vladimir Davydov
2019-04-02 15:49 ` [PATCH v3 7/7] box: introduce multikey indexes in memtx Kirill Shcherbatov
2019-04-04 14:20   ` 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=6becaa9e-9d2f-1fbe-7748-9c04b016b45f@tarantool.org \
    --to=kshcherbatov@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=vdavydov.dev@gmail.com \
    --subject='Re: [tarantool-patches] Re: [PATCH v3 2/7] lib: introduce a new json_path_multikey_offset helper' \
    /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