Skip to content

Instantly share code, notes, and snippets.

@kzk
Created May 28, 2010 15:45
Show Gist options
  • Select an option

  • Save kzk/417309 to your computer and use it in GitHub Desktop.

Select an option

Save kzk/417309 to your computer and use it in GitHub Desktop.
/*
sedue.arc0
sedue.system.indexes
sedue.arc1
{ _id: ObjId(4bffe5311422d029a79f7208), filename: "myfile", length: 2165584, chunkSize: 262144, uploadDate: new Date(1275061553566), md5: "801879daffc1edcb96bd3e11a038ffd7" }
file found
nChunks: 9
Success write
*/
#include<iostream>
#include <mongo/client/gridfs.h>
#include <mongo/client/dbclient.h>
using namespace std;
using namespace mongo;
// normal document-based database
static void
test_collection(DBClientConnection& conn)
{
// insert
string ns1 = "sedue.arc0";
conn.insert(ns1.c_str(), BSON("name" << "elliot" << "num" << 1));
string ns2 = "sedue.arc1";
conn.insert(ns2.c_str(), BSON("name" << "elliot" << "num" << 1));
// list collection
list<string> l = conn.getCollectionNames("sedue");
for (int i = 0; !l.empty(); i++) {
cout << l.front() << endl;
l.pop_front();
}
}
// large object-based filesystem
static void
test_gridfs(DBClientConnection& conn)
{
GridFS fs(conn, "seduedfs");
BSONObj o;
o = fs.storeFile("/boot/vmlinuz-2.6.26-4-server", "myfile");
cerr << o.toString() << endl;
GridFile file = fs.findFile("myfile");
if (!file.exists())
cerr << "file not found" << endl;
else
cerr << "file found" << endl;
cerr << "nChunks: " << file.getNumChunks() << endl;
ofstream ofs("a.obj");
gridfs_offset off = file.write(ofs);
if (off != file.getContentLength())
cerr << "Failed to write" << endl;
else
cerr << "Success write" << endl;
}
int
main()
{
DBClientConnection conn;
string errmsg;
if (!conn.connect("127.0.0.1:27017", errmsg)) {
cerr << errmsg << endl;
return -1;
}
test_collection(conn);
test_gridfs(conn);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment