core/index: Convert file to C
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
507ac4602d
commit
55a688cdcc
|
@ -3,9 +3,9 @@
|
|||
*/
|
||||
|
||||
#include <core/filter.h>
|
||||
#include <core/index.h>
|
||||
|
||||
extern "C" {
|
||||
#include <core/index.h>
|
||||
#include <core/string.h>
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/**
|
||||
/*
|
||||
* Copyright 2014 (c) Anna Schumaker.
|
||||
*/
|
||||
#include <core/index.h>
|
||||
|
@ -6,9 +6,9 @@
|
|||
|
||||
static struct index_entry *__index_alloc(gchar *key)
|
||||
{
|
||||
struct index_entry *ent = new struct index_entry;
|
||||
struct index_entry *ent = g_malloc(sizeof(struct index_entry));
|
||||
dbe_init(&ent->ie_dbe, ent);
|
||||
ent->ie_set = SET_INIT();
|
||||
set_init(&ent->ie_set);
|
||||
ent->ie_key = key;
|
||||
return ent;
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ static void index_free(struct db_entry *dbe)
|
|||
{
|
||||
struct index_entry *ent = INDEX_ENTRY(dbe);
|
||||
set_deinit(&ent->ie_set);
|
||||
delete ent;
|
||||
g_free(ent);
|
||||
}
|
||||
|
||||
static gchar *index_key(struct db_entry *dbe)
|
||||
|
@ -48,12 +48,11 @@ static struct db_entry *index_read(struct file *file)
|
|||
|
||||
|
||||
static const struct db_ops index_ops = {
|
||||
index_alloc,
|
||||
index_free,
|
||||
index_key,
|
||||
index_read,
|
||||
NULL,
|
||||
index_write,
|
||||
.dbe_alloc = index_alloc,
|
||||
.dbe_free = index_free,
|
||||
.dbe_key = index_key,
|
||||
.dbe_read = index_read,
|
||||
.dbe_write = index_write,
|
||||
};
|
||||
|
||||
|
||||
|
@ -62,8 +61,8 @@ void index_init(struct database *index, const gchar *filepath, bool autosave)
|
|||
db_init(index, filepath, autosave, &index_ops);
|
||||
}
|
||||
|
||||
index_entry *index_insert(struct database *index, const gchar *key,
|
||||
unsigned int value)
|
||||
struct index_entry *index_insert(struct database *index, const gchar *key,
|
||||
unsigned int value)
|
||||
{
|
||||
struct index_entry *it = INDEX_ENTRY(db_find(index, key));
|
||||
|
|
@ -1,6 +1,9 @@
|
|||
/*
|
||||
* Copyright 2014 (c) Anna Schumaker.
|
||||
*/
|
||||
extern "C" {
|
||||
#include <core/database.h>
|
||||
}
|
||||
#include <core/idle.h>
|
||||
#include <core/index.h>
|
||||
#include <core/library.h>
|
||||
|
|
|
@ -1,21 +1,16 @@
|
|||
/**
|
||||
/*
|
||||
* Copyright 2014 (c) Anna Schumaker.
|
||||
*
|
||||
* The struct index_entry is used to associate a database key
|
||||
* with a set of integers, creating an inverted index.
|
||||
*/
|
||||
#ifndef OCARINA_CORE_INDEX_H
|
||||
#define OCARINA_CORE_INDEX_H
|
||||
|
||||
extern "C" {
|
||||
#include <core/database.h>
|
||||
#include <core/set.h>
|
||||
}
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
/**
|
||||
* The index_entry struct is used to associate a specific key with a set of
|
||||
* integer identifiers. This lets us use a Database as an inverted index.
|
||||
*/
|
||||
struct index_entry {
|
||||
gchar *ie_key;
|
||||
struct set ie_set;
|
||||
|
@ -29,7 +24,7 @@ struct index_entry {
|
|||
void index_init(struct database *, const gchar *, bool);
|
||||
|
||||
/* Add a value to an index item with the specified key. */
|
||||
index_entry *index_insert(struct database *, const gchar *, unsigned int);
|
||||
struct index_entry *index_insert(struct database *, const gchar *, unsigned int);
|
||||
|
||||
/* Remove a value from an index item with the specified key. */
|
||||
void index_remove(struct database *, const gchar *, unsigned int);
|
||||
|
@ -40,4 +35,4 @@ bool index_has(struct database *, const gchar *, unsigned int);
|
|||
#ifdef CONFIG_TESTING
|
||||
const struct db_ops *test_index_ops();
|
||||
#endif /* CONFIG_TESTING */
|
||||
#endif /* OCARINA_CORE_DATABASE_H */
|
||||
#endif /* OCARINA_CORE_INDEX_H */
|
||||
|
|
|
@ -4,7 +4,9 @@
|
|||
#ifndef OCARINA_CORE_PLAYLIST_H
|
||||
#define OCARINA_CORE_PLAYLIST_H
|
||||
|
||||
extern "C" {
|
||||
#include <core/index.h>
|
||||
}
|
||||
#include <core/queue.h>
|
||||
|
||||
#include <string>
|
||||
|
|
|
@ -24,6 +24,10 @@ struct set_iter {
|
|||
.s_set = g_hash_table_new(g_direct_hash, g_direct_equal), \
|
||||
}
|
||||
|
||||
static inline void set_init(struct set *set)
|
||||
{
|
||||
set->s_set = g_hash_table_new(g_direct_hash, g_direct_equal);
|
||||
}
|
||||
|
||||
static inline void set_deinit(struct set *set)
|
||||
{
|
||||
|
|
|
@ -20,7 +20,7 @@ res += [ CoreTest("file", "file.c") ]
|
|||
res += [ CoreTest("date", "date.c") ]
|
||||
res += [ CoreTest("set", "set.c") ]
|
||||
res += [ CoreTest("database", "database.c") ]
|
||||
res += [ CoreTest("index", "index.cpp") ]
|
||||
res += [ CoreTest("index", "index.c") ]
|
||||
res += [ CoreTest("filter", "filter.cpp") ]
|
||||
res += [ CoreTest("idle", "idle.cpp") ]
|
||||
|
||||
|
|
|
@ -3,14 +3,14 @@
|
|||
* Test a Database
|
||||
*/
|
||||
#include <core/index.h>
|
||||
#include "test.h"
|
||||
#include <tests/test.h>
|
||||
|
||||
|
||||
static void test_entry()
|
||||
{
|
||||
const struct db_ops *index_ops = test_index_ops();
|
||||
struct index_entry *ie;
|
||||
struct set_iter it;
|
||||
index_entry *ie;
|
||||
unsigned int i;
|
||||
struct file f;
|
||||
|
||||
|
@ -57,11 +57,11 @@ static void test_entry()
|
|||
index_ops->dbe_free(&ie->ie_dbe);
|
||||
}
|
||||
|
||||
static void test_stress(unsigned int N)
|
||||
static void __test_stress(unsigned int N)
|
||||
{
|
||||
const struct db_ops *index_ops = test_index_ops();
|
||||
struct index_entry *ie, *ie2;
|
||||
struct database index;
|
||||
index_entry *ie, *ie2;
|
||||
unsigned int i;
|
||||
gchar key[2] = "";
|
||||
char c;
|
||||
|
@ -72,11 +72,11 @@ static void test_stress(unsigned int N)
|
|||
for (c = 'a'; c <= 'z'; c++) {
|
||||
g_strlcpy(key, &c, 2);
|
||||
ie = index_insert(&index, key, 0);
|
||||
test_loop_not_equal(ie, NULL, c - 'a');
|
||||
test_loop_not_equal((void *)ie, NULL, c - 'a');
|
||||
test_loop_equal(index_ops->dbe_key(&ie->ie_dbe), key, c - 'a');
|
||||
for (i = 0; i < N; i++)
|
||||
ie2 = index_insert(&index, key, i);
|
||||
test_loop_equal(ie, ie2, c - 'a');
|
||||
test_loop_equal((void *)ie, (void *)ie2, c - 'a');
|
||||
test_loop_equal(set_size(&ie->ie_set), N, c - 'a');
|
||||
test_loop_equal(index.db_size, c - 'a' + 1, c - 'a');
|
||||
} test_loop_passed();
|
||||
|
@ -86,8 +86,8 @@ static void test_stress(unsigned int N)
|
|||
for (c = 'a'; c <= 'z'; c += 4) {
|
||||
g_strlcpy(key, &c, 2);
|
||||
for (i = 0; i < N; i++)
|
||||
test_loop_equal(index_has(&index, key, i), true, i);
|
||||
test_loop_equal(index_has(&index, key, N), false, c - 'a');
|
||||
test_loop_equal(index_has(&index, key, i), (bool)true, i);
|
||||
test_loop_equal(index_has(&index, key, N), (bool)false, c - 'a');
|
||||
} test_loop_passed();
|
||||
|
||||
/* index_remove() */
|
||||
|
@ -96,7 +96,7 @@ static void test_stress(unsigned int N)
|
|||
for (i = 0; i < N; i++)
|
||||
index_remove(&index, key, i);
|
||||
ie = INDEX_ENTRY(db_find(&index, key));
|
||||
test_loop_not_equal(ie, NULL, c - 'a');
|
||||
test_loop_not_equal((void *)ie, NULL, c - 'a');
|
||||
test_loop_equal(set_size(&ie->ie_set), 0, c - 'a');
|
||||
} test_loop_passed();
|
||||
index_remove(&index, "ZZ", 42);
|
||||
|
@ -105,8 +105,8 @@ static void test_stress(unsigned int N)
|
|||
db_deinit(&index);
|
||||
}
|
||||
|
||||
static void test_basics() { test_stress(10); }
|
||||
static void test_stress() { test_stress(100000); }
|
||||
static void test_basics() { __test_stress(10); }
|
||||
static void test_stress() { __test_stress(100000); }
|
||||
|
||||
DECLARE_UNIT_TESTS(
|
||||
UNIT_TEST("Index Entry", test_entry),
|
Loading…
Reference in New Issue