Skip to content

Commit

Permalink
sophia: add basic examples
Browse files Browse the repository at this point in the history
  • Loading branch information
pmwkaa committed Aug 17, 2015
1 parent 3fba784 commit 954bfc3
Show file tree
Hide file tree
Showing 8 changed files with 453 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ test/sophia-test
test/sophia-benchmark
sophia.h
sophia.c
example/crud
example/transaction
example/batch
example/cursor
example/snapshot
9 changes: 9 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

<p align="center">
<a href="http://sphia.org"><img src="http://sphia.org/logo.png" width="190px" height="95px" /></a>
</p>
<p align="center">
<a href="http://sphia.org">sophia</a> - a modern embeddable transactional key-value storage
<br>
(examples)<br>
</p>
65 changes: 65 additions & 0 deletions example/batch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

/*
* sophia database
* sphia.org
*
* Copyright (c) Dmitry Simonenko
* BSD License
*/

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>

#include <sophia.h>

int main(int argc, char *argv[])
{
(void)argc;
(void)argv;

/*
* Do fast atomic write of 100 inserts.
*/

/* open or create environment and database */
void *env = sp_env();
sp_setstring(env, "sophia.path", "_test", 0);
sp_setstring(env, "db", "test", 0);
void *db = sp_getobject(env, "db.test");
int rc = sp_open(env);
if (rc == -1)
goto error;

/* create batch object */
void *batch = sp_batch(db);

/* insert 100 keys */
uint32_t key = 0;
while (key < 100) {
void *o = sp_object(db);
sp_setstring(o, "key", &key, sizeof(key));
rc = sp_set(batch, o);
if (rc == -1)
goto error;
key++;
}

/* write batch */
rc = sp_commit(batch);
if (rc == -1)
goto error;

/* finish work */
sp_destroy(env);
return 0;

error:;
int size;
char *error = sp_getstring(env, "sophia.error", &size);
printf("error: %s\n", error);
free(error);
sp_destroy(env);
return 1;
}
80 changes: 80 additions & 0 deletions example/crud.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

/*
* sophia database
* sphia.org
*
* Copyright (c) Dmitry Simonenko
* BSD License
*/

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>

#include <sophia.h>

int main(int argc, char *argv[])
{
(void)argc;
(void)argv;

/*
* Do set, get, delete operations. (see transaction.c)
*/

/* open or create environment and database */
void *env = sp_env();
sp_setstring(env, "sophia.path", "_test", 0);
sp_setstring(env, "db", "test", 0);
void *db = sp_getobject(env, "db.test");
int rc = sp_open(env);
if (rc == -1)
goto error;

/* set */
uint32_t key = 1;
void *o = sp_object(db);
sp_setstring(o, "key", &key, sizeof(key));
sp_setstring(o, "value", &key, sizeof(key));
rc = sp_set(db, o);
if (rc == -1)
goto error;

/* get */
o = sp_object(db);
sp_setstring(o, "key", &key, sizeof(key));
o = sp_get(db, o);
if (o) {
/* ensure key and value are correct */
int size;
char *ptr = sp_getstring(o, "key", &size);
assert(size == sizeof(uint32_t));
assert(*(uint32_t*)ptr == key);

ptr = sp_getstring(o, "value", &size);
assert(size == sizeof(uint32_t));
assert(*(uint32_t*)ptr == key);

This comment has been minimized.

Copy link
@funny-falcon

funny-falcon Aug 18, 2015

? почему key ?

This comment has been minimized.

Copy link
@pmwkaa

pmwkaa Aug 18, 2015

Author Owner

т.к. записан в key и value один и тотже uint32_t key. Это чтобы больше запутать :)


sp_destroy(o);
}

/* delete */
o = sp_object(db);
sp_setstring(o, "key", &key, sizeof(key));
rc = sp_delete(db, o);
if (rc == -1)
goto error;

/* finish work */
sp_destroy(env);
return 0;

error:;
int size;
char *error = sp_getstring(env, "sophia.error", &size);
printf("error: %s\n", error);
free(error);
sp_destroy(env);
return 1;
}
77 changes: 77 additions & 0 deletions example/cursor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

/*
* sophia database
* sphia.org
*
* Copyright (c) Dmitry Simonenko
* BSD License
*/

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <assert.h>

#include <sophia.h>

int main(int argc, char *argv[])
{
(void)argc;
(void)argv;

/*
* Do cursor iteration.
*/

/* open or create environment and database */
void *env = sp_env();
sp_setstring(env, "sophia.path", "_test", 0);
sp_setstring(env, "db", "test", 0);
void *db = sp_getobject(env, "db.test");
int rc = sp_open(env);
if (rc == -1)
goto error;

/* insert 10 keys */
uint32_t key = 0;
while (key < 10) {
void *o = sp_object(db);
sp_setstring(o, "key", &key, sizeof(key));
rc = sp_set(db, o);
if (rc == -1)
goto error;
key++;
}

/* create cursor and do forward iteration */
void *cursor = sp_cursor(env);
void *o = sp_object(db);
while ((o = sp_get(cursor, o))) {
printf("%"PRIu32"\n", *(uint32_t*)sp_getstring(o, "key", NULL));
}
sp_destroy(cursor);

printf("\n");

/* create cursor and do backward iteration */
cursor = sp_cursor(env);
o = sp_object(db);
sp_setstring(o, "order", "<", 0);
while ((o = sp_get(cursor, o))) {
printf("%"PRIu32"\n", *(uint32_t*)sp_getstring(o, "key", NULL));
}
sp_destroy(cursor);

/* finish work */
sp_destroy(env);
return 0;

error:;
int size;
char *error = sp_getstring(env, "sophia.error", &size);
printf("error: %s\n", error);
free(error);
sp_destroy(env);
return 1;
}
32 changes: 32 additions & 0 deletions example/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

# sophia examples
#

all: validate clean set-get-delete transaction batch cursor snapshot
validate:
@if [ ! -f ../sophia.o ]; then \
echo "Please build sophia first."; \
echo ""; \
exit 1; \
fi
set-get-delete:
gcc crud.c ../libsophia.a -I../ -pedantic -std=c99 -Wall -Wextra -pthread -g -o crud
transaction:
gcc transaction.c ../libsophia.a -I../ -pedantic -std=c99 -Wall -Wextra -pthread -g -o transaction
batch:
gcc batch.c ../libsophia.a -I../ -pedantic -std=c99 -Wall -Wextra -pthread -g -o batch
cursor:
gcc cursor.c ../libsophia.a -I../ -pedantic -std=c99 -Wall -Wextra -pthread -g -o cursor
snapshot:
gcc snapshot.c ../libsophia.a -I../ -pedantic -std=c99 -Wall -Wextra -pthread -g -o snapshot
clean:
@rm -rf _test
@rm -f crud
@rm -f transaction
@rm -f batch
@rm -f cursor
@rm -f snapshot

.PHONY: all validate clean

# vim: syntax=make
93 changes: 93 additions & 0 deletions example/snapshot.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

/*
* sophia database
* sphia.org
*
* Copyright (c) Dmitry Simonenko
* BSD License
*/

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <assert.h>

#include <sophia.h>

int main(int argc, char *argv[])
{
(void)argc;
(void)argv;

/* open or create environment and database */
void *env = sp_env();
sp_setstring(env, "sophia.path", "_test", 0);
sp_setstring(env, "db", "test", 0);
void *db = sp_getobject(env, "db.test");
int rc = sp_open(env);
if (rc == -1)
goto error;

/* insert 10 keys */
uint32_t key = 0;
while (key < 10) {
void *o = sp_object(db);
sp_setstring(o, "key", &key, sizeof(key));
sp_setstring(o, "value", &key, sizeof(key));
rc = sp_set(db, o);
if (rc == -1)
goto error;
key++;
}

/* create snapshot */
sp_setstring(env, "snapshot", "test", 0);
void *snapshot = sp_getobject(env, "snapshot.test");

/* update previous 10 keys */
key = 0;
while (key < 10) {
uint32_t value = 123;
void *o = sp_object(db);
sp_setstring(o, "key", &key, sizeof(key));
sp_setstring(o, "value", &value, sizeof(value));
rc = sp_set(db, o);
if (rc == -1)
goto error;
key++;
}

/* read keys from snapshot (see old versions) */
key = 0;
while (key < 10) {
void *o = sp_object(db);
sp_setstring(o, "key", &key, sizeof(key));
o = sp_get(snapshot, o);
if (o) {
int size;
char *ptr = sp_getstring(o, "key", &size);
assert(size == sizeof(uint32_t));
assert(*(uint32_t*)ptr == key);

ptr = sp_getstring(o, "value", &size);
assert(size == sizeof(uint32_t));
assert(*(uint32_t*)ptr == key);

sp_destroy(o);
}
key++;
}

/* finish work */
sp_destroy(env);
return 0;

error:;
int size;
char *error = sp_getstring(env, "sophia.error", &size);
printf("error: %s\n", error);
free(error);
sp_destroy(env);
return 1;
}
Loading

0 comments on commit 954bfc3

Please sign in to comment.