db_insert.c revision 10e23eebca4175a8dfe3a788b2bebacb1fcfce54
1/**
2 * @file db_insert.c
3 * Inserting a key-value pair into a DB
4 *
5 * @remark Copyright 2002 OProfile authors
6 * @remark Read the file COPYING
7 *
8 * @author Philippe Elie
9 */
10
11#define _GNU_SOURCE
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <errno.h>
17
18#include "odb.h"
19
20
21static inline int add_node(odb_data_t * data, odb_key_t key, odb_value_t value)
22{
23	odb_index_t new_node;
24	odb_node_t * node;
25	odb_index_t index;
26
27	/* no locking is necessary: iteration interface retrieve data through
28	 * the node_base array, we doesn't increase current_size now but it's
29	 * done by odb_commit_reservation() so the new slot is visible only
30	 * after the increment
31	 */
32	if (data->descr->current_size >= data->descr->size) {
33		if (odb_grow_hashtable(data))
34			return EINVAL;
35	}
36	new_node = data->descr->current_size;
37
38	node = &data->node_base[new_node];
39	node->value = value;
40	node->key = key;
41
42	index = odb_do_hash(data, key);
43	node->next = data->hash_base[index];
44	data->hash_base[index] = new_node;
45
46	/* FIXME: we need wrmb() here */
47	odb_commit_reservation(data);
48
49	return 0;
50}
51
52int odb_update_node(odb_t * odb, odb_key_t key)
53{
54	odb_index_t index;
55	odb_node_t * node;
56	odb_data_t * data;
57
58	data = odb->data;
59	index = data->hash_base[odb_do_hash(data, key)];
60	while (index) {
61		node = &data->node_base[index];
62		if (node->key == key) {
63			if (node->value + 1 != 0) {
64				node->value += 1;
65			} else {
66				/* post profile tools must handle overflow */
67				/* FIXME: the tricky way will be just to add
68				 * a goto to jump right before the return
69				 * add_node(), in this way we no longer can
70				 * overflow. It'll work because new node are
71				 * linked at the start of the node list for
72				 * this bucket so this loop will see first a
73				 * non overflowed node if one exist. When we
74				 * grow the hashtable the most recently
75				 * allocated node for this key will be setup
76				 * last, so again it'll be linked at start of
77				 * the list. pp tools looke like ok with this
78				 * change.
79				 *
80				 * This change doesn't involve any file format
81				 * change but perhaps it's a bit hacky to do
82				 * this w/o bumping the sample file format
83				 * version. The drawback of this is the added
84				 * node are additive not multiplicative.
85				 * (multiplicative as if we add more bits to
86				 * store a value)
87				 */
88			}
89			return 0;
90		}
91
92		index = node->next;
93	}
94
95	return add_node(data, key, 1);
96}
97
98
99int odb_add_node(odb_t * odb, odb_key_t key, odb_value_t value)
100{
101	return add_node(odb->data, key, value);
102}
103