GridStore.list to list gridfs files

This commit is contained in:
Mike Dirolf 2009-07-15 10:06:45 -04:00
parent 242d0020a5
commit 73d7b5ea5d
2 changed files with 22 additions and 0 deletions

View File

@ -93,6 +93,18 @@ module XGen
}
end
# List the contains of all GridFS files stored in the given db and
# root collection.
#
# :db :: the database to use
#
# :root_collection :: the root collection to use
def list(db, root_collection=DEFAULT_ROOT_COLLECTION)
db.collection("#{root_collection}.files").find().map { |f|
f['filename']
}
end
def readlines(db, name, separator=$/)
GridStore.open(db, name, 'r') { |gs|
gs.readlines(separator)

View File

@ -31,6 +31,16 @@ class GridStoreTest < Test::Unit::TestCase
assert !GridStore.exist?(@@db, 'foobar', 'another_root')
end
def test_list
assert_equal ['foobar'], GridStore.list(@@db)
assert_equal ['foobar'], GridStore.list(@@db, 'fs')
assert_equal [], GridStore.list(@@db, 'my_fs')
GridStore.open(@@db, 'test', 'w') { |f| f.write("my file") }
assert_equal ['foobar', 'test'], GridStore.list(@@db)
end
def test_small_write
rows = @@files.find({'filename' => 'foobar'}).to_a
assert_not_nil rows