Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix local documents

  • Loading branch information...
commit f3c573efa56fb48df9056ac26124a3dffd9d0b47 1 parent 47eeb38
@trondn trondn authored
View
17 src/document.c
@@ -20,6 +20,16 @@
#include <stdlib.h>
#include <string.h>
+int cbio_is_local_id(const void *data, size_t nb)
+{
+ return (nb > 6 && memcmp(data, "_local/", 7) == 0) ? 1 : 0;
+}
+
+int cbio_is_local_document(DocInfo *info)
+{
+ return cbio_is_local_id(info->id.buf, info->id.size);
+}
+
LIBCBIO_API
void cbio_document_release(libcbio_document_t doc)
{
@@ -50,7 +60,11 @@ void cbio_document_reinitialize(libcbio_document_t doc)
free(doc->doc);
} else {
if (doc->info != NULL) {
- couchstore_free_docinfo(doc->info);
+ if (cbio_is_local_document(doc->info)) {
+ couchstore_free_local_document((LocalDoc*)doc->info);
+ } else {
+ couchstore_free_docinfo(doc->info);
+ }
}
if (doc->doc) {
couchstore_free_document(doc->doc);
@@ -101,7 +115,6 @@ cbio_error_t cbio_document_set_id(libcbio_document_t doc,
return CBIO_SUCCESS;
}
-
LIBCBIO_API
cbio_error_t cbio_document_set_meta(libcbio_document_t doc,
const void *meta,
View
45 src/instance.c
@@ -68,6 +68,17 @@ off_t cbio_get_header_position(libcbio_t handle)
return (off_t)couchstore_get_header_position(handle->couchstore_handle);
}
+static cbio_error_t cbio_get_local_document(libcbio_t handle,
+ const void *id,
+ size_t nid,
+ libcbio_document_t doc)
+{
+ couchstore_error_t err;
+ err = couchstore_open_local_document(handle->couchstore_handle, id,
+ nid, (LocalDoc**)&doc->info);
+ return cbio_remap_error(err);
+}
+
LIBCBIO_API
cbio_error_t cbio_get_document(libcbio_t handle,
const void *id,
@@ -81,7 +92,18 @@ cbio_error_t cbio_get_document(libcbio_t handle,
return CBIO_ERROR_ENOMEM;
}
- err = couchstore_docinfo_by_id(handle->couchstore_handle, id, nid, &ret->info);
+ if (cbio_is_local_id(id, nid)) {
+ cbio_error_t e = cbio_get_local_document(handle, id, nid, ret);
+ if (e == CBIO_SUCCESS) {
+ *doc = ret;
+ } else {
+ cbio_document_release(ret);
+ }
+ return e;
+ }
+
+ err = couchstore_docinfo_by_id(handle->couchstore_handle, id,
+ nid, &ret->info);
if (err != COUCHSTORE_SUCCESS) {
cbio_document_release(ret);
return cbio_remap_error(err);
@@ -109,6 +131,21 @@ cbio_error_t cbio_store_document(libcbio_t handle,
return cbio_store_documents(handle, &doc, 1);
}
+static cbio_error_t cbio_store_local_documents(libcbio_t handle,
+ libcbio_document_t *doc,
+ size_t ndocs)
+{
+ for (size_t ii = 0; ii < ndocs; ++ii) {
+ couchstore_error_t err;
+ err = couchstore_save_local_document(handle->couchstore_handle,
+ (LocalDoc *)doc[ii]->info);
+ if (err != COUCHSTORE_SUCCESS) {
+ return cbio_remap_error(err);
+ }
+ }
+ return CBIO_SUCCESS;
+}
+
LIBCBIO_API
cbio_error_t cbio_store_documents(libcbio_t handle,
libcbio_document_t *doc,
@@ -119,10 +156,14 @@ cbio_error_t cbio_store_documents(libcbio_t handle,
size_t ii;
couchstore_error_t err;
- if (handle->mode == CBIO_OPEN_RDONLY) {
+ if (handle->mode == CBIO_OPEN_RDONLY || ndocs == 0) {
return CBIO_ERROR_EINVAL;
}
+ if (cbio_is_local_document(doc[0]->info)) {
+ return cbio_store_local_documents(handle, doc, ndocs);
+ }
+
docs = calloc(ndocs, sizeof(Doc *));
info = calloc(ndocs, sizeof(DocInfo *));
if (docs == NULL || info == NULL) {
View
2  src/internal.h
@@ -44,5 +44,7 @@ struct libcbio_document_st {
};
cbio_error_t cbio_remap_error(couchstore_error_t in);
+int cbio_is_local_id(const void *data, size_t nb);
+int cbio_is_local_document(DocInfo *info);
#endif
View
8 tests/testapp.c
@@ -117,6 +117,14 @@ static int get_miss(void)
return 1;
}
+ err = cbio_get_document(handle, "_local/hi-there",
+ sizeof("_local/hi-there"), &doc);
+ if (err != CBIO_ERROR_ENOENT) {
+ report("I did not expect to find \"_local/hi-there\" in the database"
+ ", but I got \"%s\"", cbio_strerror(err));
+ return 1;
+ }
+
cbio_close_handle(handle);
return 0;
}
Please sign in to comment.
Something went wrong with that request. Please try again.