Skip to content

Commit

Permalink
jsmap: New module for a json-to-json hash map.
Browse files Browse the repository at this point in the history
Signed-off-by: Ilya Maximets <[email protected]>
  • Loading branch information
igsilya committed Dec 17, 2024
1 parent ff9c8f4 commit 234d1b7
Show file tree
Hide file tree
Showing 5 changed files with 455 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/openvswitch/automake.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ openvswitchinclude_HEADERS = \
include/openvswitch/hmap.h \
include/openvswitch/flow.h \
include/openvswitch/geneve.h \
include/openvswitch/jsmap.h \
include/openvswitch/json.h \
include/openvswitch/list.h \
include/openvswitch/netdev.h \
Expand Down
96 changes: 96 additions & 0 deletions include/openvswitch/jsmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* Copyright (c) 2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */

#ifndef JSMAP_H
#define JSMAP_H 1

#include "openvswitch/hmap.h"

#ifdef __cplusplus
extern "C" {
#endif

struct json;

/* A map from json to json. */
struct jsmap {
struct hmap map; /* Contains "struct jsmap_node"s. */
};

struct jsmap_node {
struct hmap_node node; /* In struct jsmap's 'map' hmap. */
struct json *key;
struct json *value;
};

#define JSMAP_INITIALIZER(JSMAP) { HMAP_INITIALIZER(&(JSMAP)->map) }

#define JSMAP_FOR_EACH(JSMAP_NODE, JSMAP) \
HMAP_FOR_EACH_INIT (JSMAP_NODE, node, &(JSMAP)->map, \
BUILD_ASSERT_TYPE(JSMAP_NODE, struct jsmap_node *), \
BUILD_ASSERT_TYPE(JSMAP, struct jsmap *))

#define JSMAP_FOR_EACH_SAFE(JSMAP_NODE, JSMAP) \
HMAP_FOR_EACH_SAFE_SHORT_INIT ( \
JSMAP_NODE, node, &(JSMAP)->map, \
BUILD_ASSERT_TYPE(JSMAP_NODE, struct jsmap_node *), \
BUILD_ASSERT_TYPE(JSMAP, struct jsmap *))

#define JSMAP_NODE(KEY, VALUE, NEXT) \
&(struct jsmap_node) { \
.node = { \
.hash = json_hash(KEY, 0), \
.next = (NEXT), \
}, \
.key = CONST_CAST(struct json *, KEY), \
.value = CONST_CAST(struct json *, VALUE), \
}.node

void jsmap_init(struct jsmap *);
void jsmap_destroy(struct jsmap *, bool yield);

struct jsmap_node *jsmap_add(struct jsmap *, const struct json *key,
const struct json *value, bool deep_clone);
struct jsmap_node *jsmap_add_noclone(struct jsmap *,
struct json *key, struct json *value);
bool jsmap_add_once(struct jsmap *,
const struct json *key, const struct json *value,
bool deep_clone);

void jsmap_replace(struct jsmap *,
const struct json *key, const struct json *value,
bool deep_clone);
void jsmap_remove(struct jsmap *, const struct json *, bool yield);
void jsmap_remove_node(struct jsmap *, struct jsmap_node *, bool yield);
void jsmap_steal(struct jsmap *, struct jsmap_node *node,
struct json **keyp, struct json **valuep);
void jsmap_clear(struct jsmap *, bool yield);

const struct json *jsmap_get(const struct jsmap *, const struct json *);
struct jsmap_node *jsmap_get_node(const struct jsmap *, const struct json *);

bool jsmap_is_empty(const struct jsmap *);
size_t jsmap_count(const struct jsmap *);

void jsmap_clone(struct jsmap *dst, const struct jsmap *src, bool deep);
const struct jsmap_node **jsmap_sort(const struct jsmap *);
bool jsmap_equal(const struct jsmap *, const struct jsmap *);
struct json *jsmap_find_and_delete(struct jsmap *, const struct json *);
struct jsmap_node *jsmap_first(const struct jsmap *);

#ifdef __cplusplus
}
#endif

#endif /* jsmap.h */
1 change: 1 addition & 0 deletions lib/automake.mk
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ lib_libopenvswitch_la_SOURCES = \
lib/ipf.h \
lib/jhash.c \
lib/jhash.h \
lib/jsmap.c \
lib/json.c \
lib/json.h \
lib/jsonrpc.c \
Expand Down
Loading

0 comments on commit 234d1b7

Please sign in to comment.