Skip to content

Commit

Permalink
Make save_docs take a arrays of pointers
Browse files Browse the repository at this point in the history
So that library users aren't forced to have docs and infos in contiguous arrays.
  • Loading branch information
apage43 committed Feb 9, 2012
1 parent 80af5fb commit a04c657
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions include/libcouchstore/couch_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ int close_db(Db* db);

/* Save document pointed to by doc and docinfo to db. */
int save_doc(Db* db, Doc* doc, DocInfo* info, uint64_t options);
/* Save array of docs to db */
int save_docs(Db* db, Doc* docs, DocInfo* infos, long numDocs, uint64_t options);
/* Save array of docs to db, expects arrays of Doc and DocInfo pointers */
int save_docs(Db* db, Doc** docs, DocInfo** infos, long numDocs, uint64_t options);

/* To delete docuemnts, call save_doc or save_docs with doc or docs set to NULL,
* the docs referenced by the docinfos will be deleted.
Expand Down
10 changes: 5 additions & 5 deletions src/couch_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ int add_doc_to_update_list(Db* db, Doc* doc, DocInfo* info, fatbuf* fb,
return errcode;
}

int save_docs(Db* db, Doc* docs, DocInfo* infos, long numdocs, uint64_t options)
int save_docs(Db* db, Doc** docs, DocInfo** infos, long numdocs, uint64_t options)
{
int errcode = 0, i;
sized_buf *seqklist, *idklist, *seqvlist, *idvlist;
Expand All @@ -835,7 +835,7 @@ int save_docs(Db* db, Doc* docs, DocInfo* infos, long numdocs, uint64_t options)
for(i = 0; i < numdocs; i++)
{
//Get additional size for terms to be inserted into indexes
term_meta_size += 109 + (2 * (infos[i].id.size + infos[i].meta.size));
term_meta_size += 109 + (2 * (infos[i]->id.size + infos[i]->meta.size));
}

fb = fatbuf_alloc(term_meta_size +
Expand All @@ -853,10 +853,10 @@ int save_docs(Db* db, Doc* docs, DocInfo* infos, long numdocs, uint64_t options)
{
seq++;
if(docs)
curdoc = &docs[i];
curdoc = docs[i];
else
curdoc = NULL;
error_pass(add_doc_to_update_list(db, curdoc, &infos[i], fb,
error_pass(add_doc_to_update_list(db, curdoc, infos[i], fb,
&seqklist[i], &idklist[i], &seqvlist[i], &idvlist[i], seq));
}

Expand All @@ -872,7 +872,7 @@ int save_docs(Db* db, Doc* docs, DocInfo* infos, long numdocs, uint64_t options)

int save_doc(Db* db, Doc* doc, DocInfo* info, uint64_t options)
{
return save_docs(db, doc, info, 1, options);
return save_docs(db, &doc, &info, 1, options);
}

int local_doc_fetch(couchfile_lookup_request *rq, void *k, sized_buf *v)
Expand Down
14 changes: 11 additions & 3 deletions tests/testapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,21 @@ void test_save_docs()
int errcode = 0;
docset_init(4);
SETDOC(0, "doc1", "{\"test_doc_index\":1}", "test binary 1", zerometa);
SETDOC(1, "doc2", "{\"test_doc_index\":2}", "test binary 2", zerometa);
SETDOC(1, "doc4", "{\"test_doc_index\":2}", "test binary 4", zerometa);
SETDOC(2, "doc3", "{\"test_doc_index\":3}", "test binary 3", zerometa);
SETDOC(3, "doc4", "{\"test_doc_index\":4}", "test binary 4", zerometa);
SETDOC(3, "doc2", "{\"test_doc_index\":2}", "test binary 2", zerometa);
Doc* docptrs [4] = { &testdocset.docs[0],
&testdocset.docs[1],
&testdocset.docs[2],
&testdocset.docs[3]};
DocInfo* nfoptrs [4] = { &testdocset.infos[0],
&testdocset.infos[1],
&testdocset.infos[2],
&testdocset.infos[3]};
unlink("test.couch");
Db* db;
try(open_db("test.couch", COUCH_CREATE_FILES, &db));
try(save_docs(db, testdocset.docs, testdocset.infos, 4, 0));
try(save_docs(db, docptrs, nfoptrs, 4, 0));
try(commit_all(db, 0));
close_db(db);
//Read back
Expand Down

0 comments on commit a04c657

Please sign in to comment.