简体   繁体   中英

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? 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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