Esempio n. 1
0
static
void test_delete( void ) {
    mongo conn[1];
    gridfs gfs[1];
    gridfile gfile[1];
    char *data = (char*)bson_malloc( 1024 );
    const char *testFile = "test-delete";

    INIT_SOCKETS_FOR_WINDOWS;
    CONN_CLIENT_TEST;
    GFS_INIT;

    ASSERT( gridfs_store_buffer( gfs, data, 1024, testFile, "text/html", GRIDFILE_DEFAULT ) == MONGO_OK );
    ASSERT( gridfs_find_filename( gfs, testFile, gfile ) == MONGO_OK );
    gridfile_destroy( gfile );

    ASSERT( gridfs_remove_filename( gfs, testFile ) == MONGO_OK );
    ASSERT( gridfs_find_filename( gfs, testFile, gfile ) == MONGO_ERROR );

    ASSERT( gridfs_find_filename( gfs, "bogus-file-does-not-exist", gfile ) == MONGO_ERROR );
    ASSERT( gridfs_remove_filename( gfs, "bogus-file-does-not-exist" ) == MONGO_ERROR );

    gridfs_destroy( gfs );
    mongo_disconnect( conn );
    mongo_destroy( conn );
    bson_free( data );
}
EXPORT int  mongo_gridfs_store(struct gridfs_* gfs, mxArray* data, char* remoteName, char* contentType) {
    uint64_t size;
    void* p;
    if (mxIsComplex(data))
        mexErrMsgTxt("GridFS:store - Complex values not supported");
    p = calcSize(data, &size);
    return (gridfs_store_buffer((gridfs*)gfs, (char*)p, size, remoteName, contentType) == MONGO_OK);
}
Esempio n. 3
0
void test_streaming( void ) {
    mongo conn[1];
    gridfs gfs[1];
    gridfile gfile[1];
    char *medium = (char*)bson_malloc( 2*MEDIUM );
    char *small = (char*)bson_malloc( LOWER );
    char *buf = (char*)bson_malloc( LARGE );
    int n;

    if( buf == NULL || small == NULL ) {
        printf( "Failed to allocate" );
        exit( 1 );
    }

    srand( (unsigned int)time( NULL ) );

    INIT_SOCKETS_FOR_WINDOWS;

    if ( mongo_client( conn , TEST_SERVER, 27017 ) ) {
        printf( "failed to connect 3\n" );
        exit( 1 );
    }

    fill_buffer_randomly( medium, ( int64_t )2 * MEDIUM );
    fill_buffer_randomly( small, ( int64_t )LOWER );
    fill_buffer_randomly( buf, ( int64_t )LARGE );

    gridfs_init( conn, "test", "fs", gfs );
    gridfile_writer_init( gfile, gfs, "medium", "text/html", GRIDFILE_DEFAULT );

    gridfile_write_buffer( gfile, medium, MEDIUM );
    gridfile_write_buffer( gfile, medium + MEDIUM, MEDIUM );
    gridfile_writer_done( gfile );
    test_gridfile( gfs, medium, 2 * MEDIUM, "medium", "text/html" );
    gridfs_destroy( gfs );

    gridfs_init( conn, "test", "fs", gfs );

    gridfs_store_buffer( gfs, small, LOWER, "small", "text/html", GRIDFILE_DEFAULT );
    test_gridfile( gfs, small, LOWER, "small", "text/html" );
    gridfs_destroy( gfs );

    gridfs_init( conn, "test", "fs", gfs );
    gridfs_remove_filename( gfs, "large" );
    gridfile_writer_init( gfile, gfs, "large", "text/html", GRIDFILE_DEFAULT );
    for( n=0; n < ( LARGE / 1024 ); n++ ) {
        gridfile_write_buffer( gfile, buf + ( n * 1024 ), 1024 );
    }
    gridfile_writer_done( gfile );
    test_gridfile( gfs, buf, LARGE, "large", "text/html" );

    gridfs_destroy( gfs );
    mongo_destroy( conn );
    free( buf );
    free( small );
    free( medium );
}
Esempio n. 4
0
SEXP mongo_gridfs_store(SEXP gfs, SEXP raw, SEXP remotename, SEXP contenttype) {
    gridfs* _gfs = _checkGridfs(gfs);
    const char* _remotename = CHAR(STRING_ELT(remotename, 0));
    const char* _contenttype = CHAR(STRING_ELT(contenttype, 0));
    int len = LENGTH(raw);
    SEXP ret;
    PROTECT(ret = allocVector(LGLSXP, 1));
    LOGICAL(ret)[0] = (gridfs_store_buffer(_gfs, (char*)RAW(raw), len, _remotename, _contenttype) == MONGO_OK);
    UNPROTECT(1);
    return ret;
}
Esempio n. 5
0
void test_basic( void ) {
    mongo conn[1];
    gridfs gfs[1];
    char *data_before = (char*)bson_malloc( UPPER );
    int64_t i;
    FILE *fd;

    srand((unsigned int) time( NULL ) );

    INIT_SOCKETS_FOR_WINDOWS;

    if ( mongo_client( conn, TEST_SERVER, 27017 ) ) {
        printf( "failed to connect 2\n" );
        exit( 1 );
    }

    gridfs_init( conn, "test", "fs", gfs );

    fill_buffer_randomly( data_before, UPPER );
    for ( i = LOWER; i <= UPPER; i += DELTA ) {

        /* Input from buffer */
        gridfs_store_buffer( gfs, data_before, i, "input-buffer", "text/html", GRIDFILE_COMPRESS );
        test_gridfile( gfs, data_before, i, "input-buffer", "text/html" );

        /* Input from file */
        fd = fopen( "input-file", "w" );
        fwrite( data_before, sizeof( char ), (size_t)i, fd );
        fclose( fd );
        gridfs_store_file( gfs, "input-file", "input-file", "text/html", GRIDFILE_DEFAULT );
        test_gridfile( gfs, data_before, i, "input-file", "text/html" );

        gfs->caseInsensitive = 1;
        gridfs_store_file( gfs, "input-file", "input-file", "text/html", GRIDFILE_DEFAULT );
        test_gridfile( gfs, data_before, i, "inPut-file", "text/html" );
    }

    gridfs_destroy( gfs );
    mongo_disconnect( conn );
    mongo_destroy( conn );
    free( data_before );

    /* Clean up files. */
    _unlink( "input-file" );
    _unlink( "output" );
}
Esempio n. 6
0
int main(void) {
    mongo_connection conn[1];
    mongo_connection_options opts;
    gridfs gfs[1];
    char data_before[UPPER];
    size_t i;
    FILE *fd;
    
    srand(time(NULL));

    INIT_SOCKETS_FOR_WINDOWS;

/*    strncpy(opts.host, TEST_SERVER, 255);*/
    strncpy(opts.host, "127.0.0.1", 255);
    opts.host[254] = '\0';
    opts.port = 27017;

    if (mongo_connect( conn , &opts )){
        printf("failed to connect\n");
        exit(1);
    }
      
    gridfs_init(conn, "test", "fs", gfs);
    
    for (i = LOWER; i <= UPPER; i+=DELTA) {
        fill_buffer_randomly(data_before, i);
        
        /* Input from buffer */ 
        gridfs_store_buffer(gfs, data_before, i, "input-buffer", "text/html");
        test_gridfile(gfs, data_before, i, "input-buffer", "text/html");
        
        /* Input from file */
        fd = fopen("input-file", "w");
        fwrite(data_before, sizeof(char), i, fd);
        fclose(fd);  
        gridfs_store_file(gfs, "input-file", "input-file", "text/html");
        test_gridfile(gfs, data_before, i, "input-file", "text/html");
    }
    
    gridfs_destroy(gfs);
    mongo_cmd_drop_db(conn, "test");
    mongo_destroy(conn);
    return 0;
}
Esempio n. 7
0
void test_streaming() {
    mongo_connection conn[1];
    gridfs gfs[1];
    gridfile gfile[1];
    char *buf = malloc( LARGE );
    char *small = malloc( LOWER );
    if( buf == NULL || small == NULL ) {
        printf("Failed to allocate");
        exit(1);
    }
    int n;

    srand(time(NULL));

    INIT_SOCKETS_FOR_WINDOWS;

    if (mongo_connect( conn , TEST_SERVER, 27017 )){
        printf("failed to connect 3\n");
        exit(1);
    }

    fill_buffer_randomly(small, (uint64_t)LOWER);
    fill_buffer_randomly(buf, (uint64_t)LARGE);

    gridfs_init(conn, "test", "fs", gfs);

    gridfs_store_buffer(gfs, small, LOWER, "small", "text/html");
    test_gridfile(gfs, small, LOWER, "small", "text/html");
    gridfs_destroy(gfs);

    gridfs_init(conn, "test", "fs", gfs);
    gridfile_writer_init(gfile, gfs, "large", "text/html");
    for(n=0; n < (LARGE / 1024); n++) {
      gridfile_write_buffer(gfile, buf + (n * 1024), 1024);
    }
    gridfile_writer_done( gfile );
    test_gridfile(gfs, buf, LARGE, "large", "text/html");

    gridfs_destroy(gfs);
    mongo_destroy(conn);
    free(buf);
    free(small);
}
Esempio n. 8
0
void test_streaming() {
    mongo_connection conn[1];
    mongo_connection_options opts;
    gridfs gfs[1];
    gridfile gfile[1];
    char buf[LARGE];
    char small[LOWER];
    int n;

    srand(time(NULL));

    INIT_SOCKETS_FOR_WINDOWS;

    strncpy(opts.host, "127.0.0.1", 255);
    opts.host[254] = '\0';
    opts.port = 27017;

    if (mongo_connect( conn , &opts )) {
        printf("failed to connect\n");
        exit(1);
    }

    fill_buffer_randomly(small, (int64_t)LOWER);
    fill_buffer_randomly(buf, (int64_t)LARGE);

    gridfs_init(conn, "test", "fs", gfs);

    gridfs_store_buffer(gfs, small, LOWER, "small", "text/html");
    test_gridfile(gfs, small, LOWER, "small", "text/html");
    gridfs_destroy(gfs);

    gridfs_init(conn, "test", "fs", gfs);
    gridfile_writer_init(gfile, gfs, "large", "text/html");
    for(n=0; n < (LARGE / 1024); n++) {
        gridfile_write_buffer(gfile, buf + (n * 1024), 1024);
    }
    gridfile_writer_done( gfile );
    test_gridfile(gfs, buf, LARGE, "large", "text/html");

    gridfs_destroy(gfs);
    mongo_destroy(conn);
}
Esempio n. 9
0
void test_basic() {
    mongo_connection conn[1];
    gridfs gfs[1];
    char *data_before = malloc( UPPER );
    if( data_before == NULL ) {
        printf("Failed to allocate");
        exit(1);
    }
    uint64_t i;
    FILE *fd;

    srand(time(NULL));

    INIT_SOCKETS_FOR_WINDOWS;

    if (mongo_connect( conn, TEST_SERVER, 27017 )){
        printf("failed to connect 2\n");
        exit(1);
    }

    gridfs_init(conn, "test", "fs", gfs);

    fill_buffer_randomly( data_before, UPPER );
    for (i = LOWER; i <= UPPER; i += DELTA) {

        /* Input from buffer */
        gridfs_store_buffer(gfs, data_before, i, "input-buffer", "text/html");
        test_gridfile(gfs, data_before, i, "input-buffer", "text/html");

        /* Input from file */
        fd = fopen("input-file", "w");
        fwrite(data_before, sizeof(char), i, fd);
        fclose(fd);
        gridfs_store_file(gfs, "input-file", "input-file", "text/html");
        test_gridfile(gfs, data_before, i, "input-file", "text/html");
    }

    gridfs_destroy(gfs);
    mongo_disconnect(conn);
    mongo_destroy(conn);
    free( data_before );
}
Esempio n. 10
0
static
void test_random_write(void) {
    mongo conn[1];
    gridfs gfs[1];
    gridfile* gfile;
    char *data_before = (char*)bson_malloc( UPPER );
    char *random_data = (char*)bson_malloc( UPPER );
    char *buf = (char*) bson_malloc( UPPER );
    int64_t i;
    FILE *fd;

    srand((unsigned int) time( NULL ) );

    INIT_SOCKETS_FOR_WINDOWS;
    CONN_CLIENT_TEST;
    GFS_INIT;

    fill_buffer_randomly( data_before, UPPER );
    fill_buffer_randomly( random_data, UPPER );
    for ( i = LOWER; i <= UPPER; i += DELTA ) {
        int64_t j = i / 2 - 3;
        gridfs_offset bytes_to_write_first;
        int n;

        /* Input from buffer */
        gridfs_store_buffer( gfs, data_before, i, "input-buffer", "text/html", GRIDFILE_DEFAULT );
        if ( i > DEFAULT_CHUNK_SIZE * 4 ) {
          n = DEFAULT_CHUNK_SIZE * 3 + 6;
          memcpy(&data_before[j], random_data, n); // Let's overwrite the buffer with bytes crossing multiple chunks
          bytes_to_write_first = 10;
        } else {
          n = 6;
          memcpy(random_data, "123456", n);
          strncpy(&data_before[j], random_data, n); // Let's overwrite the buffer with a few some bytes
          bytes_to_write_first = 0;
        }
        gfile = gridfile_create();
        ASSERT(gridfs_find_filename(gfs, "input-buffer", gfile) == 0);
        gridfile_writer_init(gfile, gfs, "input-buffer", "text/html", GRIDFILE_DEFAULT );
        gridfile_seek(gfile, j); // Seek into the same buffer position within the GridFS file
        if ( bytes_to_write_first ) {
          ASSERT( gridfile_write_buffer(gfile, random_data, bytes_to_write_first) == bytes_to_write_first ); // Let's write 10 bytes first, and later the rest
        }
        ASSERT( gridfile_write_buffer(gfile, &random_data[bytes_to_write_first], n - bytes_to_write_first) == n - bytes_to_write_first ); // Try to write to the existing GridFS file on the position given by j
        gridfile_seek(gfile, j);
        gridfile_read_buffer( gfile, buf, n );
        ASSERT(memcmp( buf, &data_before[j], n) == 0);

        gridfile_writer_done(gfile);
        ASSERT(gfile->pos == (gridfs_offset)(j + n));
        gridfile_dealloc(gfile);
        test_gridfile( gfs, data_before, j + n > i ? j + n : i, "input-buffer", "text/html" );

        /* Input from file */
        fd = fopen( "input-file", "w" );
        fwrite( data_before, sizeof( char ), (size_t) (j + n > i ? j + n : i), fd );
        fclose( fd );
        gridfs_store_file( gfs, "input-file", "input-file", "text/html", GRIDFILE_DEFAULT );
        test_gridfile( gfs, data_before, j + n > i ? j + n : i, "input-file", "text/html" );
    }

    gridfs_destroy( gfs );
    mongo_disconnect( conn );
    mongo_destroy( conn );
    free( data_before );
    free( random_data );
    free( buf );

    /* Clean up files. */
    gridfs_test_unlink( "input-file" );
    gridfs_test_unlink( "output" );   
}