Comments (6)
Thanks for the report! I will investigate.
from sparkey.
I think I know what the problem is now.
You first call next() to get "key1", then you call reset() to make it point to "key1" again.
However, you follow that with calling next() which moves it to "key2".
When I remove the last next it works as expected.
There were also some minor bugs I had to fix to verify it
- the lengths should be 1 + strlen since sparkey doesn't return zero-terminated strings, only the pure data you give it.
- the last free() call is wrong - the buffer is already freed.
from sparkey.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sparkey/sparkey.h>
#include <assert.h>
#define SPARKEY_ASSERT(rc) ({ \
if (SPARKEY_SUCCESS != rc) { \
fprintf( \
stderr \
, "error: %s (%d)\n" \
, sparkey_errstring(rc) \
, __LINE__ \
); \
exit(1); \
} \
});
int
main(void) {
sparkey_logwriter *writer = NULL;
sparkey_logreader *reader = NULL;
sparkey_logiter *iterator = NULL;
const char *key1 = "key1";
const char *value1 = "value1";
size_t key1size = 1 + strlen(key1);
size_t value1size = 1 + strlen(value1);
const char *key2 = "key2";
const char *value2 = "value2";
size_t key2size = 1 + strlen(key2);
size_t value2size = 1 + strlen(value2);
uint64_t wanted;
uint64_t actual;
uint8_t *buffer = NULL;
// create a log
SPARKEY_ASSERT(sparkey_logwriter_create(
&writer
, "test.spl"
, SPARKEY_COMPRESSION_NONE
, 0
));
// write some stuff
SPARKEY_ASSERT(sparkey_logwriter_put(
writer
, key1size
, (uint8_t *) key1
, value1size
, (uint8_t *) value1
));
SPARKEY_ASSERT(sparkey_logwriter_put(
writer
, key2size
, (uint8_t *) key2
, value2size
, (uint8_t *) value2
));
SPARKEY_ASSERT(sparkey_logwriter_close(&writer));
SPARKEY_ASSERT(sparkey_logreader_open(&reader, "test.spl"));
SPARKEY_ASSERT(sparkey_logiter_create(&iterator, reader));
// get first key
SPARKEY_ASSERT(sparkey_logiter_next(iterator, reader));
wanted = sparkey_logiter_keylen(iterator);
assert((buffer = malloc(wanted)));
SPARKEY_ASSERT(sparkey_logiter_fill_key(
iterator
, reader
, wanted
, buffer
, &actual
));
printf("buffer: %s\n", buffer);
assert(0 == strcmp("key1", (char *) buffer));
free(buffer);
// reset iterator
SPARKEY_ASSERT(sparkey_logiter_reset(iterator, reader));
wanted = sparkey_logiter_keylen(iterator);
assert((buffer = malloc(wanted)));
SPARKEY_ASSERT(sparkey_logiter_fill_key(
iterator
, reader
, wanted
, buffer
, &actual
));
printf("buffer: %s (after reset)\n", buffer);
assert(0 == strcmp("key1", (char *) buffer));
free(buffer);
// cleanup
sparkey_logiter_close(&iterator);
sparkey_logreader_close(&reader);
return 0;
}
from sparkey.
Thanks for the cleanup/fixes.
For clarification, reset
doesn't change the position of the iterator? If so, what does it actually do?
Resets the iterator to the start of the current entry.
Nevermind.
from sparkey.
The main usecase for this function is internal, but it can also be useful if you want to read the key or value twice without doing any extra allocation.
For instance, sparkey_logiter_fill_key can't be run twice on the same entry without a call to reset in between.
from sparkey.
I was thinking it'd be used for getting key/value chunks, but yes, multiple calls to fill too.
Thanks for the speedy responses :)
from sparkey.
Related Issues (20)
- What about a python binding? HOT 6
- sparkey_hash_write returns successfully when given an invalid hash size HOT 4
- REPL HOT 2
- Feature request: sparkey_logiter_hashskip HOT 4
- Is there a go client anywhere? HOT 1
- Take rpm-sparkey upstream HOT 4
- Do I have to use the C bindings of Snappy? HOT 2
- Non-exact seeks? HOT 5
- Problem creating hash indices larger than available RAM HOT 7
- does sparkey support open a store for writing subsequent times HOT 1
- Comparison to NuDB? HOT 6
- Tag a Release HOT 2
- Improve hash algorithm description in README HOT 10
- Build breaks with newer versions of GCC
- Converting multiple log-files into a hash-file in bulk HOT 4
- File descriptor leak when hash/log files are corrupt or not matching HOT 1
- libsparkey.so.0: cannot open shared object file HOT 3
- make failure HOT 2
- Completion of error handling HOT 12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sparkey.