Esempio n. 1
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;

}
Esempio n. 2
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;

}