Liam Staskawicz avatar Liam Staskawicz committed c6aca58

* move ASCENDING and DESCENDING to Mongo since they're used for both sorting and index direction

Comments (0)

Files changed (5)

fan/Collection.fan

 ////////////////////////////////////////////////////////////////////////////////
 
 **
-**  Index
-**
-const class Index
-{
-  static const Int ASCENDING := 1
-  static const Int DESCENDING := -1
-}
-
-**
 **  Collection
 **
 const class Collection
   **
   ** Sort the results of this query on the given fields.
   ** The Int value for each field specifies the sort direction, 
-  ** either ascending (1) or descending (-1).  Other values are invalid.
+  ** either Mongo.ASCENDING (1) or Mongo.DESCENDING (-1).  Other values are invalid.
   **
   This sort(Str:Int fields)
   {
     itemsSeen += numberReturned
     // Sys.out.printLine("cursorID - ${cursorID}, startingFrom - ${startingFrom}, numberReturned - ${numberReturned}")
     
-    numberReturned.times { cache.add(Bson.read(ins)) }
+    while(numberReturned-- > 0)
+      cache.add(Bson.read(ins))
   }
   
 }
   static const Int OP_GET_MORE                 := 2005
   static const Int OP_DELETE                   := 2006
   static const Int OP_KILL_CURSORS             := 2007
+  
+  // for indexes and sorting
+  static const Int ASCENDING := 1
+  static const Int DESCENDING := -1
 
   new make(Str address := "127.0.0.1", Int port := 27017)
   { 

fan/gridfs/GridFS.fan

   {
     this.db = db
     this.root = root
-    db[root].createIndex([["files_id": Index.ASCENDING], ["n": Index.ASCENDING]]);
+    chunkColl.createIndex([["files_id": Mongo.ASCENDING], ["n": Mongo.ASCENDING]]);
   }
   
   Cursor fileList(Str:Obj? query := [:])
   GridFSFile? findOne(Str:Obj? query := [:])
   {
     o := filesColl.findOne(query)
-    if (o == null) return null
-    return fix(o)
+    return (o == null) ? null : fix(o)
   }
   
   GridFSFile? findOneByName(Str filename)

test/CollectionTest.fan

   Void testLotsOfInserts()
   {
     c := db["actortest"]
-    a := Actor(ActorPool()) |Int i| {
-      c.insert(["testincrement": i])
-    }
+    // a := Actor(ActorPool()) |Int i| {
+    //   c.insert(["testincrement": i])
+    // }
     
     c.insert(["warmup":true]) // make sure the collection has been created, etc.
     
       inserts := 10000
       start := Duration.now
       inserts.times |i| {
-        f := a.send(i)
-        if(i == (inserts - 1))
-          f.get
+        c.insert(["testincrement": i])
+        // f := a.send(i)
+        // if(i == (inserts - 1))
+        //   f.get
       }
       elapsed := (Duration.now - start).toMillis().toFloat
       stat := inserts.toFloat/(elapsed/1000f)
     }
     sum := runlens.reduce(0f) |Float r, Float v->Float| { return v + r }
     Sys.out.printLine("average run time - ${(Float)sum/runlens.size.toFloat}")
-    c.drop
+    // c.drop
   }
   
   Void testIndex()
     ii := c.indexInfo()
     verifyEq(0, ii.size)
     
-    singleidx := c.createIndex([["single":Index.DESCENDING]])
+    singleidx := c.createIndex([["single":Mongo.DESCENDING]])
     ii = c.indexInfo()
     verifyEq(2, ii.size) // _id is always indexed in addition to what we just added
     verify(ii.containsKey(singleidx))
-    verifyEq(singleidx, Collection.indexName([["single":Index.DESCENDING]]))
+    verifyEq(singleidx, Collection.indexName([["single":Mongo.DESCENDING]]))
     
-    doubleidx := c.createIndex([["a":Index.DESCENDING], ["b":Index.ASCENDING]])
+    doubleidx := c.createIndex([["a":Mongo.DESCENDING], ["b":Mongo.ASCENDING]])
     ii = c.indexInfo()
     verifyEq(3, ii.size)
     verify(ii.keys.containsAll([singleidx, doubleidx]))
     verify((ii[doubleidx] as Map).keys.containsAll(["a", "b"]))
     
-    uniqueidx := c.createIndex([["uni":Index.ASCENDING]], true)
-    verifyEq(uniqueidx, Collection.indexName([["uni":Index.ASCENDING]]))
+    uniqueidx := c.createIndex([["uni":Mongo.ASCENDING]], true)
+    verifyEq(uniqueidx, Collection.indexName([["uni":Mongo.ASCENDING]]))
     ii = c.indexInfo()
     verifyEq(4, ii.size)
     verify(ii.keys.containsAll([singleidx, doubleidx, uniqueidx]))
     
-    c.createIndex([["single":Index.DESCENDING]]) // duplicate of first index
+    c.createIndex([["single":Mongo.DESCENDING]]) // duplicate of first index
     ii = c.indexInfo()
     verifyEq(4, ii.size) // shouldn't create another one
     
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.