Skip to content

Commit 7ff71ae

Browse files
authored
Added common helper functions (#3)
1 parent 09c7729 commit 7ff71ae

File tree

4 files changed

+93
-33
lines changed

4 files changed

+93
-33
lines changed

meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ project('WiscKey', 'c', default_options : ['c_std=gnu17'])
33
### Library ###
44
include = include_directories('include')
55

6-
lib = library('wisckey', ['src/wisckey.c', 'src/memtable.c', 'src/wal.c', 'src/sstable.c', 'src/value_log.c'], include_directories : include, version : '1.0.0', soversion : '1')
6+
lib = library('wisckey', ['src/wisckey.c', 'src/common.c', 'src/memtable.c', 'src/wal.c', 'src/sstable.c', 'src/value_log.c'], include_directories : include, version : '1.0.0', soversion : '1')
77

88
### Tests ###
99
memtable_test = executable('memtable_test', 'tests/memtable_test.c', link_with : lib, include_directories : include)

src/common.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2025 Adam Bishop Comer
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <string.h>
18+
19+
#include "src/common.h"
20+
21+
int
22+
WiscKey_key_cmp(const char* lhs,
23+
size_t lhs_len,
24+
const char* rhs,
25+
size_t rhs_len)
26+
{
27+
size_t len = lhs_len < rhs_len ? lhs_len : rhs_len;
28+
29+
int cmp = memcmp(rhs, lhs, len);
30+
if (cmp != 0 || lhs_len == rhs_len) {
31+
return cmp;
32+
}
33+
34+
return rhs_len < lhs_len ? -1 : 1;
35+
}

src/common.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2025 Adam Bishop Comer
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef WISCKEY_COMMON_H
18+
#define WISCKEY_COMMON_H
19+
20+
#include <stdlib.h>
21+
22+
/**
23+
* @brief Lexigraphical comparison of two keys.
24+
*
25+
* @param lhs The left key value.
26+
* @param lhs_len The left key length.
27+
* @param rhs The right key value.
28+
* @param rhs_len The right key length.
29+
* @return The function returns a negative value if a < b, 0 if a = b, or a
30+
* positive value if a > b.
31+
*/
32+
int
33+
WiscKey_key_cmp(const char* lhs,
34+
size_t lhs_len,
35+
const char* rhs,
36+
size_t rhs_len);
37+
38+
#endif /* WISKEY_COMMON_H */

src/memtable.c

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
#include <stdlib.h>
1919
#include <string.h>
2020

21+
#include "common.h"
2122
#include "memtable.h"
2223

23-
struct MemTableRecord*
24+
static struct MemTableRecord*
2425
MemTableRecord_new(const char* key, size_t key_len, int64_t value_loc)
2526
{
2627
struct MemTableRecord* record = malloc(sizeof(struct MemTableRecord));
@@ -34,21 +35,6 @@ MemTableRecord_new(const char* key, size_t key_len, int64_t value_loc)
3435
return record;
3536
}
3637

37-
int
38-
MemTableRecord_key_cmp(const struct MemTableRecord* r,
39-
const char* key,
40-
size_t key_len)
41-
{
42-
size_t len = r->key_len < key_len ? r->key_len : key_len;
43-
44-
int cmp = memcmp(key, r->key, len);
45-
if (cmp != 0 || r->key_len == key_len) {
46-
return cmp;
47-
}
48-
49-
return key_len < r->key_len ? -1 : 1;
50-
}
51-
5238
struct MemTable*
5339
MemTable_new()
5440
{
@@ -62,10 +48,8 @@ MemTable_new()
6248
return memtable;
6349
}
6450

65-
int
66-
MemTable_binary_search(const struct MemTable* memtable,
67-
const char* key,
68-
size_t key_len)
51+
static int
52+
binary_search(const struct MemTable* memtable, const char* key, size_t key_len)
6953
{
7054
if (memtable->size == 0) {
7155
return -1;
@@ -77,7 +61,8 @@ MemTable_binary_search(const struct MemTable* memtable,
7761
while (a < b) {
7862
int m = a + (b - a) / 2;
7963

80-
int cmp = MemTableRecord_key_cmp(memtable->records[m], key, key_len);
64+
int cmp = WiscKey_key_cmp(
65+
memtable->records[m]->key, memtable->records[m]->key_len, key, key_len);
8166
if (cmp == 0) {
8267
return m;
8368
} else if (cmp < 0) {
@@ -87,25 +72,27 @@ MemTable_binary_search(const struct MemTable* memtable,
8772
}
8873
}
8974

90-
int cmp = MemTableRecord_key_cmp(memtable->records[a], key, key_len);
75+
int cmp = WiscKey_key_cmp(
76+
memtable->records[a]->key, memtable->records[a]->key_len, key, key_len);
9177
if (cmp == 0) {
9278
return a;
9379
}
9480
return -1;
9581
}
9682

97-
unsigned int
98-
MemTable_insertion_point(const struct MemTable* memtable,
99-
const char* key,
100-
size_t key_len)
83+
static unsigned int
84+
insertion_point(const struct MemTable* memtable,
85+
const char* key,
86+
size_t key_len)
10187
{
10288
int a = 0;
10389
int b = (int)memtable->size;
10490

10591
while (a < b) {
10692
int m = (a + b) / 2;
10793

108-
int cmp = MemTableRecord_key_cmp(memtable->records[m], key, key_len);
94+
int cmp = WiscKey_key_cmp(
95+
memtable->records[m]->key, memtable->records[m]->key_len, key, key_len);
10996
if (cmp < 0) {
11097
b = m - 1;
11198
} else {
@@ -119,7 +106,7 @@ MemTable_insertion_point(const struct MemTable* memtable,
119106
struct MemTableRecord*
120107
MemTable_get(const struct MemTable* memtable, const char* key, size_t key_len)
121108
{
122-
int idx = MemTable_binary_search(memtable, key, key_len);
109+
int idx = binary_search(memtable, key, key_len);
123110
if (idx == -1) {
124111
return NULL;
125112
}
@@ -133,11 +120,11 @@ MemTable_set(struct MemTable* memtable,
133120
size_t key_len,
134121
int64_t value_loc)
135122
{
136-
int idx = MemTable_binary_search(memtable, key, key_len);
123+
int idx = binary_search(memtable, key, key_len);
137124
if (idx == -1) {
138125
struct MemTableRecord* record = MemTableRecord_new(key, key_len, value_loc);
139126

140-
unsigned int insert_idx = MemTable_insertion_point(memtable, key, key_len);
127+
unsigned int insert_idx = insertion_point(memtable, key, key_len);
141128

142129
if (insert_idx < memtable->size) {
143130
// Shift the array of records by one pointer;
@@ -157,12 +144,12 @@ MemTable_set(struct MemTable* memtable,
157144
void
158145
MemTable_delete(struct MemTable* memtable, const char* key, size_t key_len)
159146
{
160-
int idx = MemTable_binary_search(memtable, key, key_len);
147+
int idx = binary_search(memtable, key, key_len);
161148

162149
if (idx == -1) {
163150
struct MemTableRecord* record = MemTableRecord_new(key, key_len, -1);
164151

165-
unsigned int insert_idx = MemTable_insertion_point(memtable, key, key_len);
152+
unsigned int insert_idx = insertion_point(memtable, key, key_len);
166153

167154
if (insert_idx < memtable->size) {
168155
// Shift the array of records by one pointer;

0 commit comments

Comments
 (0)