Skip to content

Commit 26ce78b

Browse files
author
a-brandt
committed
added exists()
1 parent f6a2e0a commit 26ce78b

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

src/main/java/com/arangodb/ArangoDriver.java

+91
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,97 @@ public List<String> getDocuments(String collectionName, boolean handleConvert) t
17341734
return documentDriver.getDocuments(getDefaultDatabase(), collectionName, handleConvert);
17351735
}
17361736

1737+
/**
1738+
* The exists method determines whether a document exists given its
1739+
* identifier. Instead of returning the found document or an error, this
1740+
* method will return either true or false. It can thus be used for easy
1741+
* existence checks.
1742+
*
1743+
* @param collectionId
1744+
* The collection id.
1745+
* @param documentId
1746+
* The document id
1747+
* @return true, if the document exists
1748+
* @throws ArangoException
1749+
*/
1750+
public boolean exists(long collectionId, long documentId) throws ArangoException {
1751+
return exists(createDocumentHandle(collectionId, String.valueOf(documentId)));
1752+
}
1753+
1754+
/**
1755+
* The exists method determines whether a document exists given its
1756+
* identifier. Instead of returning the found document or an error, this
1757+
* method will return either true or false. It can thus be used for easy
1758+
* existence checks.
1759+
*
1760+
* @param collectionName
1761+
* The collection name.
1762+
* @param documentId
1763+
* The document id
1764+
* @return true, if the document exists
1765+
* @throws ArangoException
1766+
*/
1767+
public boolean exists(String collectionName, long documentId) throws ArangoException {
1768+
return exists(createDocumentHandle(collectionName, String.valueOf(documentId)));
1769+
}
1770+
1771+
/**
1772+
* The exists method determines whether a document exists given its
1773+
* identifier. Instead of returning the found document or an error, this
1774+
* method will return either true or false. It can thus be used for easy
1775+
* existence checks.
1776+
*
1777+
* @param collectionId
1778+
* The collection id.
1779+
* @param documentKey
1780+
* The document key
1781+
* @return true, if the document exists
1782+
* @throws ArangoException
1783+
*/
1784+
public boolean exists(long collectionId, String documentKey) throws ArangoException {
1785+
return exists(createDocumentHandle(collectionId, documentKey));
1786+
}
1787+
1788+
/**
1789+
* The exists method determines whether a document exists given its
1790+
* identifier. Instead of returning the found document or an error, this
1791+
* method will return either true or false. It can thus be used for easy
1792+
* existence checks.
1793+
*
1794+
* @param collectionName
1795+
* The collection name.
1796+
* @param documentKey
1797+
* The document key
1798+
* @return true, if the document exists
1799+
* @throws ArangoException
1800+
*/
1801+
public boolean exists(String collectionName, String documentKey) throws ArangoException {
1802+
return exists(createDocumentHandle(collectionName, documentKey));
1803+
}
1804+
1805+
/**
1806+
* The exists method determines whether a document exists given its
1807+
* identifier. Instead of returning the found document or an error, this
1808+
* method will return either true or false. It can thus be used for easy
1809+
* existence checks.
1810+
*
1811+
* @param documentHandle
1812+
* The document handle
1813+
* @return true, if the document exists
1814+
* @throws ArangoException
1815+
*/
1816+
public boolean exists(String documentHandle) throws ArangoException {
1817+
try {
1818+
documentDriver.checkDocument(getDefaultDatabase(), documentHandle);
1819+
} catch (ArangoException e) {
1820+
if (e.getCode() == ErrorNums.ERROR_HTTP_NOT_FOUND) {
1821+
return false;
1822+
}
1823+
throw e;
1824+
}
1825+
return true;
1826+
}
1827+
17371828
/**
17381829
* This method returns the current revision of a document.
17391830
*

src/test/java/com/arangodb/ArangoDriverDocumentTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,16 @@ public void test_check_document_doc_not_found() throws ArangoException {
595595
}
596596
}
597597

598+
@Test
599+
public void test_check_document_doc_not_exists() throws ArangoException {
600+
601+
driver.createDocument(collectionName, new TestComplexEntity02(1, 2, 3), null, null);
602+
603+
boolean b = driver.exists(collectionName, 1);
604+
605+
assertThat(b, is(false));
606+
}
607+
598608
@Test
599609
public void test_delete() throws ArangoException {
600610
DocumentEntity<TestComplexEntity02> doc = driver.createDocument(collectionName,

0 commit comments

Comments
 (0)