Skiplists
Introduction to Skiplist Indexes
This is an introduction to ArangoDB's skiplists.
It is possible to define a skiplist index on one or more attributes (or paths) of documents. This skiplist is then used in queries to locate documents within a given range. If the skiplist is declared unique, then no two documents are allowed to have the same set of attribute values.
Creating a new document or updating a document will fail if the uniqueness is violated.
If the skiplist index is declared sparse, a document will be excluded from the index and no
uniqueness checks will be performed if any index attribute value is not set or has a value
of null
.
Accessing Skiplist Indexes from the Shell
Unique Skiplist Index
Ensures that a unique skiplist index exists:
collection.ensureIndex({ type: "skiplist", fields: [ "field1", ..., "fieldn" ], unique: true })
Creates a unique skiplist index on all documents using field1, ... fieldn as attribute paths. At least one attribute path has to be given. The index will be non-sparse by default.
All documents in the collection must differ in terms of the indexed attributes. Creating a new document or updating an existing document will fail if the attribute uniqueness is violated.
To create a sparse unique index, set the sparse attribute to true
:
collection.ensureIndex({ type: "skiplist", fields: [ "field1", ..., "fieldn" ], unique: true, sparse: true })
In a sparse index all documents will be excluded from the index that do not
contain at least one of the specified index attributes or that have a value
of null
in any of the specified index attributes. Such documents will
not be indexed, and not be taken into account for uniqueness checks.
In a non-sparse index, these documents will be indexed (for non-present
indexed attributes, a value of null
will be used) and will be taken into
account for uniqueness checks.
In case that the index was successfully created, an object with the index details, including the index-identifier, is returned.
arangosh> db.ids.ensureIndex({ type: "skiplist", fields: [ "myId" ], unique: true });
{
"deduplicate" : true,
"fields" : [
"myId"
],
"id" : "ids/15418",
"isNewlyCreated" : true,
"sparse" : false,
"type" : "skiplist",
"unique" : true,
"code" : 201
}
arangosh> db.ids.save({ "myId": 123 });
{
"_id" : "ids/15421",
"_key" : "15421",
"_rev" : "_VTxTP0K---"
}
arangosh> db.ids.save({ "myId": 456 });
{
"_id" : "ids/15425",
"_key" : "15425",
"_rev" : "_VTxTP0K--_"
}
arangosh> db.ids.save({ "myId": 789 });
{
"_id" : "ids/15428",
"_key" : "15428",
"_rev" : "_VTxTP0K--A"
}
arangosh> db.ids.save({ "myId": 123 });
[ArangoError 1210: unique constraint violated - in index 15418 of type skiplist over ["myId"]]
arangosh> db.ids.ensureIndex({ type: "skiplist", fields: [ "myId" ], unique: true });
arangosh> db.ids.save({ "myId": 123 });
arangosh> db.ids.save({ "myId": 456 });
arangosh> db.ids.save({ "myId": 789 });
arangosh> db.ids.save({ "myId": 123 });
arangosh> db.ids.ensureIndex({ type: "skiplist", fields: [ "name.first", "name.last" ], unique: true });
{
"deduplicate" : true,
"fields" : [
"name.first",
"name.last"
],
"id" : "ids/15400",
"isNewlyCreated" : true,
"sparse" : false,
"type" : "skiplist",
"unique" : true,
"code" : 201
}
arangosh> db.ids.save({ "name" : { "first" : "hans", "last": "hansen" }});
{
"_id" : "ids/15403",
"_key" : "15403",
"_rev" : "_VTxTPxO---"
}
arangosh> db.ids.save({ "name" : { "first" : "jens", "last": "jensen" }});
{
"_id" : "ids/15407",
"_key" : "15407",
"_rev" : "_VTxTPxS---"
}
arangosh> db.ids.save({ "name" : { "first" : "hans", "last": "jensen" }});
{
"_id" : "ids/15410",
"_key" : "15410",
"_rev" : "_VTxTPxS--_"
}
arangosh> db.ids.save({ "name" : { "first" : "hans", "last": "hansen" }});
[ArangoError 1210: unique constraint violated - in index 15400 of type skiplist over ["name.first","name.last"]]
arangosh> db.ids.ensureIndex({ type: "skiplist", fields: [ "name.first", "name.last" ], unique: true });
arangosh> db.ids.save({ "name" : { "first" : "hans", "last": "hansen" }});
arangosh> db.ids.save({ "name" : { "first" : "jens", "last": "jensen" }});
arangosh> db.ids.save({ "name" : { "first" : "hans", "last": "jensen" }});
arangosh> db.ids.save({ "name" : { "first" : "hans", "last": "hansen" }});
Non-unique Skiplist Index
Ensures that a non-unique skiplist index exists:
collection.ensureIndex({ type: "skiplist", fields: [ "field1", ..., "fieldn" ] })
Creates a non-unique skiplist index on all documents using field1, ... fieldn as attribute paths. At least one attribute path has to be given. The index will be non-sparse by default.
To create a sparse non-unique index, set the sparse attribute to true
.
collection.ensureIndex({ type: "skiplist", fields: [ "field1", ..., "fieldn" ], sparse: true })
In case that the index was successfully created, an object with the index details, including the index-identifier, is returned.
arangosh> db.names.ensureIndex({ type: "skiplist", fields: [ "first" ] });
{
"deduplicate" : true,
"fields" : [
"first"
],
"id" : "names/15273",
"isNewlyCreated" : true,
"sparse" : false,
"type" : "skiplist",
"unique" : false,
"code" : 201
}
arangosh> db.names.save({ "first" : "Tim" });
{
"_id" : "names/15276",
"_key" : "15276",
"_rev" : "_VTxTPaW---"
}
arangosh> db.names.save({ "first" : "Tom" });
{
"_id" : "names/15280",
"_key" : "15280",
"_rev" : "_VTxTPaa---"
}
arangosh> db.names.save({ "first" : "John" });
{
"_id" : "names/15283",
"_key" : "15283",
"_rev" : "_VTxTPaa--_"
}
arangosh> db.names.save({ "first" : "Tim" });
{
"_id" : "names/15286",
"_key" : "15286",
"_rev" : "_VTxTPae---"
}
arangosh> db.names.save({ "first" : "Tom" });
{
"_id" : "names/15289",
"_key" : "15289",
"_rev" : "_VTxTPae--_"
}
arangosh> db.names.ensureIndex({ type: "skiplist", fields: [ "first" ] });
arangosh> db.names.save({ "first" : "Tim" });
arangosh> db.names.save({ "first" : "Tom" });
arangosh> db.names.save({ "first" : "John" });
arangosh> db.names.save({ "first" : "Tim" });
arangosh> db.names.save({ "first" : "Tom" });
Skiplist Array Index
Ensures that a skiplist array index exists (non-unique):
collection.ensureIndex({ type: "skiplist", fields: [ "field1[*]", ..., "fieldn[*]" ] })
Creates a non-unique skiplist array index for the individual elements of the array attributes field1[*], ... fieldn[*] found in the documents. At least one attribute path has to be given. The index always treats the indexed arrays as sparse.
It is possible to combine array indexing with standard indexing:
collection.ensureIndex({ type: "skiplist", fields: [ "field1[*]", "field2" ] })
In case that the index was successfully created, an object with the index details, including the index-identifier, is returned.
arangosh> db.test.ensureIndex({ type: "skiplist", fields: [ "a[*]" ] });
{
"deduplicate" : true,
"fields" : [
"a[*]"
],
"id" : "test/15295",
"isNewlyCreated" : true,
"sparse" : false,
"type" : "skiplist",
"unique" : false,
"code" : 201
}
arangosh> db.test.save({ a : [ 1, 2 ] });
{
"_id" : "test/15298",
"_key" : "15298",
"_rev" : "_VTxTPdy---"
}
arangosh> db.test.save({ a : [ 1, 3 ] });
{
"_id" : "test/15302",
"_key" : "15302",
"_rev" : "_VTxTPd2---"
}
arangosh> db.test.save({ a : null });
{
"_id" : "test/15305",
"_key" : "15305",
"_rev" : "_VTxTPd2--_"
}
arangosh> db.test.ensureIndex({ type: "skiplist", fields: [ "a[*]" ] });
arangosh> db.test.save({ a : [ 1, 2 ] });
arangosh> db.test.save({ a : [ 1, 3 ] });
arangosh> db.test.save({ a : null });
Query by example using a skiplist index
Constructs a query-by-example using a skiplist index:
collection.byExample(example)
Selects all documents from the collection that match the specified example and returns a cursor. A skiplist index will be used if present.
You can use toArray, next, or hasNext to access the result. The result can be limited using the skip and limit operator.
An attribute name of the form a.b is interpreted as attribute path, not as attribute. If you use
{ "a" : { "c" : 1 } }
as example, then you will find all documents, such that the attribute a contains a document of the form {c : 1 }. For example the document
{ "a" : { "c" : 1 }, "b" : 1 }
will match, but the document
{ "a" : { "c" : 1, "b" : 1 } }
will not.
However, if you use
{ "a.c" : 1 },
then you will find all documents, which contain a sub-document in a that has an attribute c of value 1. Both the following documents
{ "a" : { "c" : 1 }, "b" : 1 }
and
{ "a" : { "c" : 1, "b" : 1 } }
will match.