Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Enhancements to AGCatalog methods and AGServer.getCatalog

Tests added and prepush passes.  Root catalog methods also tested
with use in TBC plugins.

<release-note>
rfe9837: avoid log warning when opening an existing repo

Added method AGCatalog.hasRepository(id) which tests for existance.
Added AGCatalog.openRepository(id) which will open but not create.
Added AGCatalog.createRepository(id,strict) where the strict option
will throw an exception if the repository already exists.

rfe9980: getCatalog now works with the root catalog

Added AGCatalog.isRootId(id) to identify id's for the root catalog.
Enhanced AGServer.getCatalog(id) to accept root catalog identifiers.
Also added AGServer.getCatalog() to return the root catalog.
</release-note>

Change-Id: I9f79c18ef58e59149efa65168cde7d3c076c3a7a
Reviewed-on: https://gerrit.franz.com:9080/735
Reviewed-by: Kevin Layer <layer@franz.com>
Tested-by: Kevin Layer <layer@franz.com>
Reviewed-by: John O'Rourke <john.orourke@franz.com>
  • Loading branch information...
commit efc3986157e2859c77f3a0fd346eda6916339b93 1 parent 563a960
authored dklayer committed
100  src/com/franz/agraph/repository/AGCatalog.java
@@ -20,7 +20,6 @@
20 20
 import org.openrdf.query.TupleQueryResult;
21 21
 import org.openrdf.repository.RepositoryException;
22 22
 
23  
-import com.franz.agraph.http.AGErrorType;
24 23
 import com.franz.agraph.http.AGHTTPClient;
25 24
 import com.franz.agraph.http.AGHttpException;
26 25
 import com.franz.agraph.http.AGProtocol;
@@ -124,6 +123,15 @@ public String getRepositoriesURL() {
124 123
 		return repositoriesURL;
125 124
 	}
126 125
 	
  126
+	// TODO this should be part of AGProtocol.
  127
+	public String getRepositoryURL(String repositoryID) {
  128
+		return repositoriesURL + "/" + encode(repositoryID);
  129
+	}
  130
+	
  131
+	public AGHTTPClient getHTTPClient() {
  132
+		return getServer().getHTTPClient();
  133
+	}	
  134
+
127 135
 	protected String getCatalogPrefixedRepositoryID(String repositoryID) {
128 136
 		String catalogPrefixedRepositoryID;
129 137
 		switch (getCatalogType()) {
@@ -139,11 +147,6 @@ protected String getCatalogPrefixedRepositoryID(String repositoryID) {
139 147
 		return catalogPrefixedRepositoryID;
140 148
 	}
141 149
 
142  
-	// TODO this should be part of AGProtocol.
143  
-	public String getRepositoryURL(String repositoryID) {
144  
-		return repositoriesURL + "/" + encode(repositoryID);
145  
-	}
146  
-	
147 150
 	/**
148 151
 	 * Returns a List of repository ids contained in this catalog.
149 152
 	 * 
@@ -166,10 +169,17 @@ public String getRepositoryURL(String repositoryID) {
166 169
         return result;
167 170
 	}
168 171
 
169  
-	public AGHTTPClient getHTTPClient() {
170  
-		return getServer().getHTTPClient();
  172
+	/**
  173
+	 * Returns true if the repository id is contained in this catalog.
  174
+	 * 
  175
+	 * @return true if the repository id is contained in this catalog.
  176
+	 * @throws OpenRDFException
  177
+	 */
  178
+	public boolean hasRepository(String repoId) throws OpenRDFException {
  179
+		List<String> repos = listRepositories();
  180
+		return repos.contains(repoId);
171 181
 	}
172  
-
  182
+	       
173 183
 	/**
174 184
 	 * Returns an uninitialized AGRepository instance for the given 
175 185
 	 * repository id.
@@ -183,25 +193,64 @@ public AGHTTPClient getHTTPClient() {
183 193
 	 */
184 194
 	public AGRepository createRepository(String repositoryID)
185 195
 			throws RepositoryException {
  196
+		return createRepository(repositoryID, false);
  197
+	}
  198
+
  199
+	/**
  200
+	 * Returns an uninitialized AGRepository instance for the given
  201
+	 * repository id.
  202
+	 * 
  203
+	 * The repository is created if it does not exist.  If the
  204
+	 * repository already exists, it is simply opened, or an exception
  205
+	 * is thrown if strict=true.
  206
+	 * 
  207
+	 * @param repositoryID the id (the name) of the repository
  208
+	 * @param strict if true and the repository already exists, throw an exception
  209
+	 * @return an uninitialized AGRepository instance.
  210
+	 * @throws RepositoryException
  211
+	 */
  212
+	public AGRepository createRepository(String repositoryID, boolean strict)
  213
+			throws RepositoryException {
186 214
 		String repoURL = AGProtocol.getRepositoryLocation(getCatalogURL(),
187 215
 				repositoryID);
188 216
 		try {
189  
-			getHTTPClient().putRepository(repoURL);
  217
+			if (strict || !hasRepository(repositoryID)) {
  218
+				getHTTPClient().putRepository(repoURL);
  219
+			}
190 220
 		} catch (IOException e) {
191 221
 			throw new RepositoryException(e);
  222
+		} catch (OpenRDFException e) {
  223
+			// TODO: consider having methods in this class all throw OpenRDFExceptions
  224
+			throw new RepositoryException(e);
192 225
 		} catch (AGHttpException e) {
193  
-			if (AGErrorType.PRECONDITION_FAILED == e.getErrorInfo()
194  
-					.getErrorType()) {
195  
-				// don't error if repo already exists
196  
-				// TODO: check if repo exists first
197  
-			} else {
198  
-				throw new RepositoryException(e);
199  
-			}
  226
+			throw new RepositoryException(e);
200 227
 		}
201 228
 		return new AGRepository(this, repositoryID);
202 229
 	}
203 230
 
204 231
 	/**
  232
+	 * Returns an uninitialized AGRepository instance for the given 
  233
+	 * repository id.
  234
+	 * 
  235
+	 * If the repository already exists, it is simply opened.
  236
+	 * 
  237
+	 * @param repositoryID the id (the name) of the repository. 
  238
+	 * @return an uninitialized AGRepository instance.
  239
+	 * @throws RepositoryException if the repositoryID does not exist
  240
+	 */
  241
+	public AGRepository openRepository(String repositoryID)
  242
+			throws RepositoryException {
  243
+		try {
  244
+			if (!hasRepository(repositoryID)) {
  245
+				throw new RepositoryException("Repository not found with ID: " + repositoryID);
  246
+			}
  247
+		} catch (OpenRDFException e) {
  248
+			// TODO: consider having methods in this class all throw OpenRDFExceptions
  249
+			throw new RepositoryException(e);
  250
+		}
  251
+		return new AGRepository(this, repositoryID);
  252
+	}
  253
+	/**
205 254
 	 * Deletes the repository with the given repository id.
206 255
 	 * 
207 256
 	 * @param repositoryID the name of the repository to delete.
@@ -218,4 +267,21 @@ public void deleteRepository(String repositoryID)
218 267
 		}
219 268
 	}
220 269
 
  270
+	/**
  271
+	 * Returns true iff the id identifies the root catalog.
  272
+	 * 
  273
+	 * Currently null, the empty string, and "/" are all considered to identify
  274
+	 * the root catalog.
  275
+	 * 
  276
+	 * @param catalogID
  277
+	 * @return true iff the id identifies the root catalog.
  278
+	 */
  279
+	public static boolean isRootID(String catalogID) {
  280
+		boolean result = false;
  281
+		if (catalogID == null || catalogID.equals("") || catalogID.equals("/")) {
  282
+			result = true;
  283
+		}
  284
+		return result;
  285
+	}
  286
+
221 287
 }
25  src/com/franz/agraph/repository/AGServer.java
@@ -59,9 +59,11 @@ AGHTTPClient getHTTPClient() {
59 59
 	}
60 60
 	
61 61
 	/**
62  
-	 * Returns the root catalog for this AllegroGraph server.
  62
+	 * Returns the unnamed root catalog for this AllegroGraph server.
  63
+	 * Note: this method may be deprecated in an upcoming release.
63 64
 	 * 
64  
-	 * @return the root catalog. 
  65
+	 * @return the root catalog.
  66
+	 * @see #getCatalog() 
65 67
 	 */
66 68
 	public AGCatalog getRootCatalog() {
67 69
 		return rootCatalog;
@@ -92,11 +94,28 @@ public AGCatalog getRootCatalog() {
92 94
 	/**
93 95
 	 * Gets the catalog instance for a given catalog id.
94 96
 	 * 
  97
+	 * Returns the root catalog if the id is a root id.
  98
+	 *    
95 99
 	 * @param catalogID a catalog id.
96 100
 	 * @return the corresponding catalog instance.
97 101
 	 */
98 102
 	public AGCatalog getCatalog(String catalogID) {
99  
-		return new AGCatalog(this, catalogID);
  103
+		AGCatalog catalog;
  104
+		if (AGCatalog.isRootID(catalogID)) {
  105
+			catalog = getRootCatalog();
  106
+		} else {
  107
+			catalog = new AGCatalog(this, catalogID);
  108
+		}
  109
+		return catalog;
  110
+	}
  111
+
  112
+	/**
  113
+	 * Returns the unnamed root catalog for this AllegroGraph server.
  114
+	 * 
  115
+	 * @return the root catalog. 
  116
+	 */
  117
+	public AGCatalog getCatalog() {
  118
+		return rootCatalog;
100 119
 	}
101 120
 
102 121
 	public AGVirtualRepository virtualRepository(String spec) {
36  src/test/QuickTests.java
@@ -31,8 +31,12 @@
31 31
 import org.openrdf.model.Statement;
32 32
 import org.openrdf.model.URI;
33 33
 import org.openrdf.query.QueryLanguage;
  34
+import org.openrdf.repository.RepositoryException;
34 35
 import org.openrdf.repository.RepositoryResult;
35 36
 
  37
+import com.franz.agraph.repository.AGRepository;
  38
+import com.franz.agraph.repository.AGRepositoryConnection;
  39
+
36 40
 import test.TestSuites.NonPrepushTest;
37 41
 
38 42
 public class QuickTests extends AGAbstractTest {
@@ -136,7 +140,7 @@ public void bulkDelete() throws Exception {
136 140
         URI firstname = vf.createURI("http://example.org/ontology/firstname");
137 141
         URI lastname = vf.createURI("http://example.org/ontology/lastname");
138 142
         Literal alicesName = vf.createLiteral("Alice");
139  
-        List input = new ArrayList<Statement>();
  143
+        List<Statement> input = new ArrayList<Statement>();
140 144
         input.add(vf.createStatement(alice, firstname, alicesName));
141 145
         input.add(vf.createStatement(alice, lastname, alicesName));
142 146
         conn.add(input);
@@ -145,4 +149,32 @@ public void bulkDelete() throws Exception {
145 149
         assertEquals("size", 0, conn.size());
146 150
     }
147 151
 
148  
-}
  152
+    @Test
  153
+    @Category(TestSuites.Prepush.class)
  154
+    public void openRepo_rfe9837() throws Exception {
  155
+    	AGRepository repo2 = closeLater( cat.openRepository(repo.getRepositoryID()));
  156
+    	AGRepositoryConnection conn2 = closeLater( repo2.getConnection());
  157
+    	assertEquals("size", conn.size(), conn2.size());
  158
+    	assertEquals("id", repo.getRepositoryID(), repo2.getRepositoryID());
  159
+    	try {
  160
+    		AGRepository repo3 = closeLater( cat.createRepository(repo.getRepositoryID(), true));
  161
+    		fail("strict should cause an exception: " + repo3.getRepositoryURL());
  162
+    	} catch (RepositoryException e) {
  163
+    		if (e.getMessage().contains("There is already a repository")) {
  164
+    			// good
  165
+    		} else {
  166
+    			throw e;
  167
+    		}
  168
+    	}
  169
+    	try {
  170
+    		AGRepository repo3 = closeLater( cat.openRepository("no-such-repo"));
  171
+    		fail("should not exist: " + repo3.getRepositoryURL());
  172
+    	} catch (RepositoryException e) {
  173
+    		if (e.getMessage().contains("Repository not found with ID:")) {
  174
+    			// good
  175
+    		} else {
  176
+    			throw e;
  177
+    		}
  178
+    	}
  179
+    }
  180
+}

0 notes on commit efc3986

Please sign in to comment.
Something went wrong with that request. Please try again.