简体   繁体   English

在Casbah问题中创建索引

[英]Creating Index in Casbah Issue

My program will do the following (using Casbah): 我的程序将执行以下操作(使用Casbah):

load2000DocsIntoMongo() 
def myIndexExists= collection.getIndexInfo().exists( x => x.getAs[String] 
         ("name").getOrElse("") == MY_INDEX_NAME)
if (myIndexExists) println("log exists")
else { 
  val start = System.nanoTime()
  collection.ensureIndex(MY_INDEX) 
  println( (System.nanoTime - start) / 1000000000 + "seconds to index")
}

When starting mongod from scratch, and then running my test, the index works. 从头开始mongod ,然后运行我的测试,索引工作。 After running the test, I check db.collection.getIndexes() to see if it was created. 运行测试后,我检查db.collection.getIndexes()以查看它是否已创建。

However, after running my test once, and then running db.collection.drop() , I re-ran the test. 但是,在运行我的测试一次,然后运行db.collection.drop() ,我重新运行了测试。 The test inserts the documents correctly, but it incorrectly reports that that index was created. 测试正确插入文档,但它错误地报告该索引已创建。 I say this, because even though the X seconds to index was printed out, Mongo shell's db.collection.getIndexes() shows that it was not created. 我这样说,因为即使打印出索引X秒 ,Mongo shell的db.collection.getIndexes()表明它没有被创建。

Why isn't collection.ensureIndex(MY_INDEX) always creating the index if it doesn't exist? 如果不存在, collection.ensureIndex(MY_INDEX)为什么不总是创建索引?

EDIT 编辑

When adding an index via collection.ensureIndex(MY_INDEX) , Casbah called the Java library's method to create an index. 通过collection.ensureIndex(MY_INDEX)添加索引时,Casbah调用Java库的方法来创建索引。 In this method, a private map variable, _createdIndexes , was updated with this index. 在此方法中,使用此索引更新了私有映射变量_createdIndexes

When I had modified Mongo's indexes outside of the Java library, it did not know to update the _createdIndexes variable. 当我在Java库之外修改了Mongo的索引时,它不知道更新_createdIndexes变量。 As a result, when trying to create the same index, _createdIndexes already had that value, so it simply called return; 因此,当尝试创建相同的索引时, _createdIndexes已经具有该值,因此它只是调用return; since the library's cache, ie the variable, already put this index in its map. 因为库的缓存,即变量,已经将此索引放在其映射中。

To work around this issue, I call collection.dropIndexes() , which will clear the _createdIndexes variable. 要解决此问题,我调用collection.dropIndexes() ,它将清除_createdIndexes变量。

Casbah source - https://github.com/mongodb/casbah/blob/master/casbah-core/src/main/scala/MongoCollection.scala Casbah source - https://github.com/mongodb/casbah/blob/master/casbah-core/src/main/scala/MongoCollection.scala

Java source - https://github.com/mongodb/mongo-java-driver/blob/master/src/main/com/mongodb/DBCollection.java Java source - https://github.com/mongodb/mongo-java-driver/blob/master/src/main/com/mongodb/DBCollection.java

Please see Ross 's detailed answer for the full story. 请看罗斯关于完整故事的详细答案。

Its not a bug per sae however, I agree this highlights an issue if you use the Casvah driver and the shell or another driver at the same time. 它不是每个sae的错误,但我同意这突出了一个问题,如果你同时使用Casvah驱动程序和shell或其他驱动程序。

The underlying java code cache doesn't know what you are doing in the shell and it expects to be the only source of true (other drivers also follow this pattern). 底层的Java代码缓存不知道你在shell中做了什么,它希望是唯一的true源(其他驱动程序也遵循这种模式)。 The reason there is a cache is to aid performance, so that ensureIndex can be repeatedly called and have little performance impact. 存在缓存的原因是为了提高性能,因此可以重复调用ensureIndex并且对性能影响很小。

So the question is what is the best course of action in this scenario? 那么问题是这种情况下最好的行动方案是什么?

  1. Only use the Casbah driver to create and manage indexes - what ensureIndex relies on 仅使用Casbah驱动程序来创建和管理索引 - ensureIndex依赖的内容
  2. Only use the shell to create and manage indexes - the shell doesnt cache 只使用shell来创建和管理索引 - shell不会缓存
  3. Don't trust the cache in Casbah code 不要相信Casbah代码中的缓存

You could call createIndex and bypass the cache altogether. 您可以调用createIndex并完全绕过缓存。 There is a jira ticket on this: JAVA-667 and it looks for the next major release (3.0) the cache is being removed. 有一个jira票: JAVA-667 ,它寻找下一个主要版本(3.0)缓存被删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM