Example #1
0
/*
 * cursor,err = gridfs:list([lua_table or json_str])
 */
static int gridfs_list(lua_State *L) {
    GridFS *gridfs = userdata_to_gridfs(L, 1);

    BSONObj query;
    int type = lua_type(L, 2);
    if (type == LUA_TSTRING) {
        const char *jsonstr = luaL_checkstring(L, 2);
        query = fromjson(jsonstr);
    } else if (type == LUA_TTABLE) {
        lua_to_bson(L, 2, query);
    }
    std::auto_ptr<DBClientCursor> autocursor = gridfs->list(query);

    if (!autocursor.get()){
        lua_pushnil(L);
        lua_pushstring(L, LUAMONGO_ERR_CONNECTION_LOST);
        return 2;
    }

    DBClientCursor **cursor = (DBClientCursor **)lua_newuserdata(L, sizeof(DBClientCursor *));
    *cursor = autocursor.get();
    autocursor.release();

    luaL_getmetatable(L, LUAMONGO_CURSOR);
    lua_setmetatable(L, -2);

    return 1;
}
Example #2
0
/*
 * gridfile, err = gridfs:find_file(query)
 */
static int gridfs_find_file(lua_State *L) {
    GridFS *gridfs = userdata_to_gridfs(L, 1);
    int resultcount = 1;

    if (!lua_isnoneornil(L, 2)) {
        try {
            int type = lua_type(L, 2);
            if (type == LUA_TTABLE) {
                BSONObj obj;
                lua_to_bson(L, 2, obj);
                GridFile gridfile = gridfs->findFile(obj);
                resultcount = gridfile_create(L, gridfile);
            } else {
                GridFile gridfile = gridfs->findFile(luaL_checkstring(L, 2));
                resultcount = gridfile_create(L, gridfile);
            }

        } catch (std::exception &e) {
            lua_pushnil(L);
            lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_GRIDFS, "find_file", e.what());
            resultcount = 2;
        }
    }

    return resultcount;

}
Example #3
0
/*
 * ok, err = gridfs:remove_file(filename)
 */
static int gridfs_remove_file(lua_State *L) {
    int resultcount = 1;

    GridFS *gridfs = userdata_to_gridfs(L, 1);

    const char *filename = luaL_checkstring(L, 2);

    try {
        gridfs->removeFile(filename);
        lua_pushboolean(L, 1);
    } catch (std::exception &e) {
        lua_pushboolean(L, 0);
        lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_GRIDFS, "remove_file", e.what());
        resultcount = 2;
    }

    return resultcount;
}
Example #4
0
/*
 * gridfile, err = gridfs:find_file_by_name(filename)
 */
static int gridfs_find_file_by_name(lua_State *L) {
    GridFS *gridfs = userdata_to_gridfs(L, 1);
    int resultcount = 1;

    if (!lua_isnoneornil(L, 2)) {
	try {
	    GridFile gridfile = gridfs->findFileByName(luaL_checkstring(L, 2));
	    resultcount = gridfile_create(L, gridfile);
	    
        } catch (std::exception &e) {
            lua_pushnil(L);
            lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_GRIDFS, "find_file_by_name", e.what());
            resultcount = 2;
        }
    }

    return resultcount;

}
Example #5
0
/*
 * bson, err = gridfs:store_file(filename[, remote_file[, content_type]])
 */
static int gridfs_store_file(lua_State *L) {
    int resultcount = 1;

    GridFS *gridfs = userdata_to_gridfs(L, 1);

    const char *filename = luaL_checkstring(L, 2);
    const char *remote = luaL_optstring(L, 3, "");
    const char *content_type = luaL_optstring(L, 4, "");

    try {
        BSONObj res = gridfs->storeFile(filename, remote, content_type);
        bson_to_lua(L, res);
    } catch (std::exception &e) {
        lua_pushnil(L);
        lua_pushfstring(L, LUAMONGO_ERR_CALLING, LUAMONGO_GRIDFS, "store_file", e.what());
        resultcount = 2;
    }

    return resultcount;
}
Example #6
0
/*
 * cursor,err = gridfs:list()
 */
static int gridfs_list(lua_State *L) {
    GridFS *gridfs = userdata_to_gridfs(L, 1);

    std::auto_ptr<DBClientCursor> autocursor = gridfs->list();

    if (!autocursor.get()) {
        lua_pushnil(L);
        lua_pushstring(L, LUAMONGO_ERR_CONNECTION_LOST);
        return 2;
    }

    DBClientCursor **cursor = (DBClientCursor **)lua_newuserdata(L, sizeof(DBClientCursor *));
    *cursor = autocursor.get();
    autocursor.release();

    luaL_getmetatable(L, LUAMONGO_CURSOR);
    lua_setmetatable(L, -2);

    return 1;
}
Example #7
0
int main() {
    try {
        DBClientBase *conn = NULL;
        string err_msg;
        ConnectionString cs = ConnectionString::parse("localhost", err_msg);
        
        if (!cs.isValid()) {
            throw "bad: " + err_msg;
        }
        
        try {
            conn = cs.connect(err_msg);
        } catch (DBException &e) {
            cout << "caught " << err_msg << endl;
            return 1;
        }
        
        if (!conn){
            cout<<"Unable to connect to DB"<<endl;
            return 1;
        }
        
//        BSONObjBuilder b;
//        b.append("name", "Joe");
//        b.append("age", 33);
//        BSONObj p = b.obj();
//        
//        conn->insert("db.coll",p,0);
        
        
        GridFS * fs;
       
        fs = new GridFS(*conn,"test");
        BSONObj p = fs->storeFile("pic.JPG");
        conn->insert("db.coll",p,0);
    }
    catch( DBException &e ) {
        cout << "caught " << e.what() << endl;
    }
    return 0;
}
Example #8
0
int
main(int argc, char **argv)
{
  ArgumentParser argp(argc, argv, "ho:fd:c:");
  if (argp.has_arg("h")) {
    print_usage(argv[0]);
    exit(0);
  }

  const std::vector<const char *> &items = argp.items();

  std::string output_dir = "tmp/";
  std::string database = "fflog";
  std::string collection;
  std::string query_coll;
  bool filename_indexed = ! argp.has_arg("f");

  std::vector<std::pair<long long, long long> > times;

  if (argp.has_arg("o")) {
    output_dir = argp.arg("o");
    if (output_dir[output_dir.length() - 1] != '/') {
      output_dir += "/";
    }
  }
  if (argp.has_arg("d")) {
    database = argp.arg("d");
  }
  if (argp.has_arg("c")) {
    collection = argp.arg("c");
  } else {
    print_usage(argv[0]);
    printf("No collection given\n");
    exit(-1);
  }

  query_coll = database + "." + collection;

  if (items.empty()) {
    times.push_back(std::make_pair(0L, std::numeric_limits<long long>::max()));
  } else {
    for (unsigned int i = 0; i < items.size(); ++i) {
      std::string item = items[i];
      std::string::size_type dotpos = item.find("..");
      if (dotpos == std::string::npos) {
	// singular timestamp
	long int ts = argp.parse_item_int(i);
	times.push_back(std::make_pair(ts, ts));	
      } else {
	// range
	std::string first_ts, second_ts;
	first_ts = item.substr(0, dotpos);
	second_ts = item.substr(dotpos + 2);
	times.push_back(std::make_pair(StringConversions::to_long(first_ts),
				       StringConversions::to_long(second_ts)));
      }
    }
  }

  unsigned int image_n = 0;

  DBClientConnection *mongodb_client =
    new DBClientConnection(/* auto reconnect */ true);
  std::string errmsg;
  mongodb_client->connect("localhost", errmsg);

  GridFS *gridfs = new GridFS(*mongodb_client, "fflog");


  for (unsigned int i = 0; i < times.size(); ++i) {
    Query q;

    if (times[i].first == times[i].second) {
      printf("Querying for timestamp %lli\n", times[i].first);
      q = QUERY("timestamp" << times[i].first).sort("timestamp", 1);
    } else {
      printf("Querying for range %lli..%lli\n", times[i].first, times[i].second);
      q = QUERY("timestamp"
		<< mongo::GTE << times[i].first
		<< mongo::LTE << times[i].second)
	.sort("timestamp", 1);
    }

#if __cplusplus >= 201103L
    std::unique_ptr<mongo::DBClientCursor> cursor =
      mongodb_client->query(query_coll, q);
#else
    std::auto_ptr<mongo::DBClientCursor> cursor =
      mongodb_client->query(query_coll, q);
#endif

    while (cursor->more()) {
      BSONObj doc = cursor->next();
      
      BSONObj imgdoc = doc.getObjectField("image");
      if (imgdoc["colorspace"].String() == "RGB") {
	std::string filename = imgdoc.getFieldDotted("data.filename").String();
	long filesize = imgdoc.getFieldDotted("data.length").numberLong();
	std::string image_id = imgdoc["image_id"].String();

	std::string out_filename;
	char *fntmp;
	if (filename_indexed) {
	  if (asprintf(&fntmp, "%s%s-%08d.png", output_dir.c_str(),
		       image_id.c_str(), image_n++) != -1)
	  {
	    out_filename = fntmp;
	    free(fntmp);
	  }
	} else {
	  if (asprintf(&fntmp, "%s%s.png", output_dir.c_str(),
		       filename.c_str()) != -1)
	  {
	    out_filename = fntmp;
	    free(fntmp);
	  }
	  ++image_n;
	}

	printf("Restoring RGB image %s (%s)\n", filename.c_str(), out_filename.c_str());

	GridFile file = gridfs->findFile(filename);
	if (! file.exists()) {
	  printf("File %s does not exist\n", filename.c_str());
	  continue;
	}

	unsigned int width  = imgdoc["width"].Int();
	unsigned int height = imgdoc["height"].Int();

	if (colorspace_buffer_size(RGB, width, height) != (size_t)filesize) {
	  printf("Buffer size mismatch (DB %li vs. exp. %zu)\n",
		 filesize, colorspace_buffer_size(RGB, width, height));
	  continue;
	}

	unsigned char *buffer = malloc_buffer(RGB, width, height);

	unsigned char *tmp = buffer;
	for (int c = 0; c < file.getNumChunks(); ++c) {
	  mongo::GridFSChunk chunk = file.getChunk(c);
	  int len = 0;
	  const char *chunk_data = chunk.data(len);
	  memcpy(tmp, chunk_data, len);
	  tmp += len;
	}

	PNGWriter writer(out_filename.c_str(), width, height);
	writer.set_buffer(RGB, buffer);
	writer.write();
	
	free(buffer);
      }
    }
  }

  delete gridfs;
  delete mongodb_client;

}