Skip to content

Commit 10adc54

Browse files
author
Mike Dirolf
committed
use distinct for gridfs.list
1 parent 78716ea commit 10adc54

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

gridfs/__init__.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,36 @@
2121
"""
2222

2323
from gridfs.grid_file import GridFile
24+
from pymongo import (ASCENDING,
25+
DESCENDING)
2426
from pymongo.database import Database
2527

2628
class GridFS(object):
2729
"""An instance of GridFS on top of a single Database.
2830
"""
29-
def __init__(self, database):
31+
def __init__(self, database, collection="fs"):
3032
"""Create a new instance of :class:`GridFS`.
3133
3234
Raises :class:`TypeError` if `database` is not an instance of
3335
:class:`~pymongo.database.Database`.
3436
3537
:Parameters:
3638
- `database`: database to use
39+
- `collection` (optional): root collection to use
40+
41+
.. versionadded:: 1.5.1+
42+
The `collection` parameter.
3743
3844
.. mongodoc:: gridfs
3945
"""
4046
if not isinstance(database, Database):
4147
raise TypeError("database must be an instance of Database")
4248

4349
self.__database = database
50+
self.__files = database[collection].files
51+
self.__chunks = database[collection].chunks
52+
self.__chunks.create_index([("files_id", ASCENDING), ("n", ASCENDING)],
53+
unique=True)
4454

4555
def open(self, filename, mode="r", collection="fs"):
4656
"""Open a :class:`~gridfs.grid_file.GridFile` for reading or
@@ -62,7 +72,8 @@ def open(self, filename, mode="r", collection="fs"):
6272
- `filename`: name of the :class:`~gridfs.grid_file.GridFile`
6373
to open
6474
- `mode` (optional): mode to open the file in
65-
- `collection` (optional): root collection to use for this file
75+
- `collection` (optional): root collection to use for this
76+
file
6677
"""
6778
return GridFile({"filename": filename}, self.__database, mode, collection)
6879

@@ -116,7 +127,4 @@ def list(self, collection="fs"):
116127
"""
117128
if not isinstance(collection, basestring):
118129
raise TypeError("collection must be an instance of basestring")
119-
names = []
120-
for grid_file in self.__database[collection].files.find():
121-
names.append(grid_file["filename"])
122-
return names
130+
return self.__database[collection].files.distinct("filename")

test/test_gridfs.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ def test_list(self):
8282
f = self.fs.open("hello world", "w")
8383
f.close()
8484

85-
self.assertEqual(["mike", "test", "hello world"], self.fs.list())
85+
self.assertEqual(set(["mike", "test", "hello world"]),
86+
set(self.fs.list()))
8687

8788
def test_remove(self):
8889
self.assertRaises(TypeError, self.fs.remove, 5)
@@ -98,13 +99,14 @@ def test_remove(self):
9899
f = self.fs.open("hello world", "w")
99100
f.write("fly")
100101
f.close()
101-
self.assertEqual(["mike", "test", "hello world"], self.fs.list())
102+
self.assertEqual(set(["mike", "test", "hello world"]),
103+
set(self.fs.list()))
102104
self.assertEqual(self.db.fs.files.find().count(), 3)
103105
self.assertEqual(self.db.fs.chunks.find().count(), 3)
104106

105107
self.fs.remove("test")
106108

107-
self.assertEqual(["mike", "hello world"], self.fs.list())
109+
self.assertEqual(set(["mike", "hello world"]), set(self.fs.list()))
108110
self.assertEqual(self.db.fs.files.find().count(), 2)
109111
self.assertEqual(self.db.fs.chunks.find().count(), 2)
110112
f = self.fs.open("mike")
@@ -145,8 +147,8 @@ def test_list_alt_coll(self):
145147
f.close()
146148

147149
self.assertEqual([], self.fs.list())
148-
self.assertEqual(["mike", "test", "hello world"],
149-
self.fs.list("pymongo_test"))
150+
self.assertEqual(set(["mike", "test", "hello world"]),
151+
set(self.fs.list("pymongo_test")))
150152

151153
def test_remove_alt_coll(self):
152154
f = self.fs.open("mike", "w", "pymongo_test")
@@ -160,10 +162,11 @@ def test_remove_alt_coll(self):
160162
f.close()
161163

162164
self.fs.remove("test")
163-
self.assertEqual(["mike", "test", "hello world"],
164-
self.fs.list("pymongo_test"))
165+
self.assertEqual(set(["mike", "test", "hello world"]),
166+
set(self.fs.list("pymongo_test")))
165167
self.fs.remove("test", "pymongo_test")
166-
self.assertEqual(["mike", "hello world"], self.fs.list("pymongo_test"))
168+
self.assertEqual(set(["mike", "hello world"]),
169+
set(self.fs.list("pymongo_test")))
167170

168171
f = self.fs.open("mike", collection="pymongo_test")
169172
self.assertEqual(f.read(), "hi")
@@ -204,10 +207,10 @@ def test_threaded_writes(self):
204207
self.assertEqual(f.read(), "hello")
205208
f.close()
206209

207-
# NOTE I do recognize how gross this is. There is no good way to test the
208-
# with statement because it is a syntax error in older python versions.
209-
# One option would be to use eval and skip the test if it is a syntax
210-
# error.
210+
# NOTE I do recognize how gross this is. There is no good way to
211+
# test the with statement because it is a syntax error in older
212+
# python versions. One option would be to use eval and skip the
213+
# test if it is a syntax error.
211214
if sys.version_info[:2] == (2, 5):
212215
import gridfs15
213216
test_with_statement = gridfs15.test_with_statement

0 commit comments

Comments
 (0)