Search code examples
indexingarangodbaql

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.


Solution

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