简体   繁体   English

ArangoDB唯一索引验证

[英]ArangoDB Unique Index Validation

Quick question: in ArangoDB, if I create a unique index (for example a unique hash index), does ArangoDB validate the uniqueness of that attribute, or just assume it because I told it it's unique? 快速问题:在ArangoDB中,如果我创建一个唯一索引(例如,一个唯一的哈希索引),ArangoDB是否会验证该属性的唯一性,还是只是因为我告诉过它唯一性而假设它是唯一的? I'm curious if I should go through a validation step to verify the uniqueness of my data before creating unique indices. 我很好奇我是否应该在创建唯一索引之前执行验证步骤来验证数据的唯一性。

As you know ArangoDB builds up the indexes before you can use them. 如您所知,ArangoDB会在使用索引之前先建立索引。 If it fails to ensure the uniqueness, it will throw an exception: 如果无法确保唯一性,它将引发异常:

127.0.0.1:8529@_system> c = db._create("c")
[ArangoCollection 169, "c" (type document, status loaded)]
127.0.0.1:8529@_system> c.insert({"abc":1})
{ 
  "_id" : "c/172", 
  "_key" : "172", 
  "_rev" : "_T1m73_m---" 
}
127.0.0.1:8529@_system> c.insert({"abc":1})
{ 
  "_id" : "c/176", 
  "_key" : "176", 
  "_rev" : "_T1m748K---" 
}
127.0.0.1:8529@_system> c.ensureIndex(
...> {"type":"hash","unique":true,"fields":["abc"]})
JavaScript exception in file '.../arangosh.js' at 97,7:
 ArangoError 1210: unique constraint violated
!      throw error;
!      ^
stacktrace: ArangoError: unique constraint violated
    at Object.exports.checkRequestResult (.../arangosh.js:95:21)
    at ArangoCollection.ensureIndex (.../arango-collection.js:733:12)
    at <shell command>:1:3

127.0.0.1:8529@_system> c.ensureIndex(
...> {"type":"skiplist","unique":true,"fields":["abc"]})
JavaScript exception in file '.../arangosh.js' at 97,7:
ArangoError 1210: unique constraint violated
!      throw error;
!      ^
stacktrace: ArangoError: unique constraint violated
    at Object.exports.checkRequestResult (.../arangosh.js:95:21)
    at ArangoCollection.ensureIndex (.../arango-collection.js:733:12)
    at <shell command>:1:3

Similar to what it does if you try to insert a document that violates the unique constraint: 与尝试插入违反唯一约束的文档时的操作类似:

127.0.0.1:8529@_system> db._drop("c")
127.0.0.1:8529@_system> c = db._create("c")
[ArangoCollection 315, "c" (type document, status loaded)]

127.0.0.1:8529@_system> c.ensureIndex({
...>"type":"skiplist","unique":true,"fields":["abc"]})
{ 
  "id" : "c/318", 
  "type" : "skiplist", 
  "fields" : [ 
    "abc" 
  ], 
  "unique" : true, 
  "sparse" : false, 
  "isNewlyCreated" : true, 
  "code" : 201 
}

127.0.0.1:8529@_system> c.insert({"abc":1})
{ 
  "_id" : "c/330", 
  "_key" : "330", 
  "_rev" : "_T1n-B2S---" 
}

127.0.0.1:8529@_system> c.insert({"abc":1})
JavaScript exception in file '.../arangosh.js' at 97,7:
 ArangoError 1210: cannot create document, unique constraint violated
!      throw error;
!      ^
stacktrace: ArangoError: cannot create document, unique constraint violated
 at Object.exports.checkRequestResult (.../arangosh.js:95:21)
 at ArangoCollection.save.ArangoCollection.insert 
   (.../arango-collection.js:978:14)
 at <shell command>:1:3

So if you insert your documents during your application setup before creating the index (for performance reasons a viable approach) you need to handle possible exceptions when creating these indices afterwards. 因此,如果在创建索引之前在应用程序安装过程中插入文档(出于性能的考虑,这是一种可行的方法),则在以后创建这些索引时需要处理可能的异常。

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

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