opr: Add dictionary implementation

Add a simple implementation of a dictionary/hash structure based around
opr queues and the jhash hashing function.

Change-Id: I4ae5cafcef377b05c8caa7c455737a992b1d36cd
Reviewed-on: http://gerrit.openafs.org/8355
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Derrick Brashear <shadow@your-file-system.com>
This commit is contained in:
Simon Wilkinson 2012-10-30 11:25:02 +00:00 committed by Derrick Brashear
parent 07372cf7e7
commit 5be3cdc3f3
7 changed files with 232 additions and 2 deletions

View File

@ -3,11 +3,12 @@ include @TOP_OBJDIR@/src/config/Makefile.config
include @TOP_OBJDIR@/src/config/Makefile.pthread
include @TOP_OBJDIR@/src/config/Makefile.libtool
LT_objs = assert.lo casestrcpy.lo rbtree.lo uuid.lo
LT_objs = assert.lo casestrcpy.lo dict.lo rbtree.lo uuid.lo
LT_libs = $(LIB_hcrypto) $(LIB_roken)
HEADERS = $(TOP_INCDIR)/afs/opr.h \
$(TOP_INCDIR)/afs/opr_assert.h \
$(TOP_INCDIR)/opr/dict.h \
$(TOP_INCDIR)/opr/jhash.h \
$(TOP_INCDIR)/opr/lock.h \
$(TOP_INCDIR)/opr/lockstub.h \
@ -40,6 +41,9 @@ $(TOP_INCDIR)/afs/opr.h: opr.h
$(TOP_INCDIR)/afs/opr_assert.h: ${srcdir}/opr_assert.h
$(INSTALL_DATA) $? $@
$(TOP_INCDIR)/opr/dict.h: ${srcdir}/dict.h
$(INSTALL_DATA) $? $@
$(TOP_INCDIR)/opr/jhash.h: ${srcdir}/jhash.h
$(INSTALL_DATA) $? $@

63
src/opr/dict.c Normal file
View File

@ -0,0 +1,63 @@
/*
* Copyright (c) 2012 Your File System Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <afsconfig.h>
#include <afs/param.h>
#include <roken.h>
#include "dict.h"
static_inline int
isPowerOf2(int value)
{
return value && !(value & (value -1));
}
struct opr_dict *
opr_dict_Init(unsigned int size)
{
struct opr_dict *dict;
int i;
if (!isPowerOf2(size))
return NULL;
dict = calloc(1, sizeof(struct opr_dict));
dict->size = size;
dict->table = malloc(dict->size * sizeof(struct opr_queue));
for (i = 0; i < dict->size; i++) {
opr_queue_Init(&dict->table[i]);
}
return dict;
}
void
opr_dict_Free(struct opr_dict **dict)
{
free((*dict)->table);
free(*dict);
*dict = NULL;
}

59
src/opr/dict.h Normal file
View File

@ -0,0 +1,59 @@
/*
* Copyright (c) 2012 Your File System Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <opr/queue.h>
struct opr_dict {
int size;
struct opr_queue *table;
};
static_inline void
opr_dict_Prepend(struct opr_dict *dict, int index, struct opr_queue *entry)
{
opr_queue_Prepend(&dict->table[index & (dict->size-1)], entry);
}
static_inline void
opr_dict_Append(struct opr_dict *dict, int index, struct opr_queue *entry)
{
opr_queue_Append(&dict->table[index & (dict->size-1)], entry);
}
static_inline void
opr_dict_Promote(struct opr_dict *dict, int index, struct opr_queue *entry)
{
opr_queue_Remove(entry);
opr_dict_Prepend(dict, index, entry);
}
#define opr_dict_ScanBucket(dict, index, cursor) \
opr_queue_Scan(&(dict)->table[index & ((dict)->size-1)], cursor)
#define opr_dict_ScanBucketSafe(dict, index, cursor, store) \
opr_queue_ScanSafe(&(dict)->table[index & ((dict)->size-1)], \
cursor, store)
extern struct opr_dict *opr_dict_Init(unsigned int size);
extern void opr_dict_Free(struct opr_dict **dict);

View File

@ -4,6 +4,8 @@ opr_stolower
opr_stoupper
opr_strcompose
opr_AssertionFailed
opr_dict_Init
opr_dict_Free
opr_rbtree_first
opr_rbtree_next
opr_rbtree_remove

View File

@ -5,6 +5,7 @@ auth/superuser
auth/authcon
auth/realms
cmd/command
opr/dict
opr/jhash
opr/queues
opr/rbtree

View File

@ -7,10 +7,13 @@ MODULE_CFLAGS = -I$(srcdir)/../..
LIBS=../tap/libtap.a $(abs_top_builddir)/src/opr/liboafs_opr.la
tests = jhash-t queues-t rbtree-t time-t uuid-t
tests = dict-t jhash-t queues-t rbtree-t time-t uuid-t
all check test tests: $(tests)
dict-t: dict-t.o
$(LT_LDRULE_static) dict-t.o $(LIBS) $(XLIBS)
queues-t: queues-t.o
$(LT_LDRULE_static) queues-t.o ../tap/libtap.a $(XLIBS)

98
tests/opr/dict-t.c Normal file
View File

@ -0,0 +1,98 @@
/*
* Copyright (c) 2012 Your File System Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <afsconfig.h>
#include <afs/param.h>
#include <tests/tap/basic.h>
#include <opr/dict.h>
struct myentry {
int value;
struct opr_queue d;
};
int
find(struct opr_dict *dict, int value, struct myentry **entryp)
{
struct myentry *ent;
struct opr_queue *cursor;
int length = 0;
for (opr_dict_ScanBucket(dict, value, cursor)) {
ent = opr_queue_Entry(cursor, struct myentry, d);
length++;
if (ent->value == value) {
if (entryp)
*entryp = ent;
return length;
}
}
return 0;
}
int
main(void)
{
struct opr_dict *dict;
struct myentry *entry;
int members[] = {1,2,3,4,5,6,7,8,9,10,17,0};
int i;
plan(10);
ok(opr_dict_Init(3) == NULL,
"Initialising a dictionary with a bad size fails");
dict = opr_dict_Init(8);
ok(dict != NULL,
"Initialising a dictionary succeeds");
for (i = 0; members[i] !=0; i++) {
entry = malloc(sizeof(struct myentry));
entry->value = members[i];
opr_dict_Append(dict, entry->value, &entry->d);
}
ok(1, "Hash populated successfully");
is_int(1, find(dict, 1, NULL),
"Entry 1 is first in hash chain");
is_int(2, find(dict, 9, NULL),
"Entry 9 is second in hash chain");
is_int(3, find(dict, 17, NULL),
"Entry 17 is third in hash chain");
is_int(1, find(dict, 2, NULL),
"Entry 2 is first in hash chain");
is_int(1, find(dict, 8, NULL),
"Entry 8 is first in hash chain");
find(dict, 17, &entry);
ok(entry != NULL && entry->value == 17, "Retrieved entry 17");
opr_dict_Promote(dict, entry->value, &entry->d);
is_int(1, find(dict, 17, NULL),
"Entry 17 is first in hash chain following promotion");
return 0;
}