5555import com .marklogic .client .query .DeleteQueryDefinition ;
5656import com .marklogic .client .query .QueryManager ;
5757import com .marklogic .client .query .StructuredQueryBuilder ;
58+ import com .marklogic .client .pojo .annotation .Id ;
5859
5960/** Loads data from cities15000.txt which contains every city above 15000 people, and adds
6061 * data from countryInfo.txt.
6162 **/
6263@ FixMethodOrder (MethodSorters .NAME_ASCENDING )
6364public class BulkReadWriteTest {
64- private static int BATCH_SIZE = 100 ;
65- private static String DIRECTORY = "/cities/" ;
66- private static String COUNTRIES_FILE = "countryInfo.txt" ;
67- private static String CITIES_FILE = "cities_above_300K.txt" ;
68- private static int RECORDS_EXPECTED = 1363 ;
65+ private static final int BATCH_SIZE = 100 ;
66+ static final String DIRECTORY = "/cities/" ;
67+ private static final String COUNTRIES_FILE = "countryInfo.txt" ;
68+ private static final String CITIES_FILE = "cities_above_300K.txt" ;
69+ static final int RECORDS_EXPECTED = 1363 ;
6970 private static JAXBContext context = null ;
7071
7172 @ BeforeClass
@@ -146,6 +147,7 @@ static public class City {
146147 private long population ;
147148 private int elevation ;
148149
150+ @ Id
149151 public int getGeoNameId () {
150152 return geoNameId ;
151153 }
@@ -264,16 +266,44 @@ public City setElevation(int elevation) {
264266 }
265267 }
266268
267- @ Test
268- public void testBulkLoad () throws IOException , JAXBException {
269- // register the POJO class
270- DatabaseClientFactory .getHandleRegistry ().register (
271- JAXBHandle .newFactory (City .class )
272- );
273- XMLDocumentManager docMgr = Common .client .newXMLDocumentManager ();
269+ interface CityWriter {
270+ public void addCity (City city );
271+ public void finishBatch ();
272+ public void setNumRecords (int numWritten );
273+ }
274+
275+ private class BulkCityWriter implements CityWriter {
276+ private XMLDocumentManager docMgr = Common .client .newXMLDocumentManager ();
277+ private JAXBContext context ;
278+ private DocumentWriteSet writeSet = docMgr .newWriteSet ();
279+
280+ BulkCityWriter () throws JAXBException {
281+ // register the POJO class
282+ DatabaseClientFactory .getHandleRegistry ().register (
283+ JAXBHandle .newFactory (City .class )
284+ );
285+
286+ context = JAXBContext .newInstance (City .class );
287+ }
274288
275- JAXBContext context = JAXBContext .newInstance (City .class );
289+ public void addCity (City city ) {
290+ JAXBHandle <City > handle = new JAXBHandle <City >(context );
291+ // set the handle to the POJO instance
292+ handle .set (city );
293+ writeSet .add ( DIRECTORY + city .getGeoNameId () + ".xml" , handle );
294+ }
295+
296+ public void finishBatch () {
297+ docMgr .write (writeSet );
298+ writeSet = docMgr .newWriteSet ();
299+ }
276300
301+ public void setNumRecords (int numWritten ) {
302+ assertEquals ("Number of records not expected" , numWritten , RECORDS_EXPECTED );
303+ }
304+ }
305+
306+ static void loadCities (CityWriter cityWriter ) throws Exception {
277307 // load all the countries into a HashMap (this isn't the big data set)
278308 // we'll attach country info to each city (that's the big data set)
279309 Map <String , Country > countries = new HashMap <String , Country >();
@@ -286,35 +316,34 @@ public void testBulkLoad() throws IOException, JAXBException {
286316 countryReader .close ();
287317
288318 // write batches of cities combined with their country info
289- DocumentWriteSet writeSet = docMgr .newWriteSet ();
290- System .out .println (BulkReadWriteTest .class .getClassLoader ().getResourceAsStream (CITIES_FILE ));
319+ System .out .println (BulkReadWriteTest .class .getClassLoader ().getResource (CITIES_FILE ));
291320 BufferedReader cityReader = new BufferedReader (Common .testFileToReader (CITIES_FILE ));
292321 line = null ;
293- long numWritten = 0 ;
322+ int numWritten = 0 ;
294323 while ((line = cityReader .readLine ()) != null ) {
295324
296325 // instantiate the POJO for this city
297326 City city = newCity (line , countries );
298-
299- // set the handle to the POJO instance
300- JAXBHandle <City > handle = new JAXBHandle <City >(context );
301- handle .set (city );
302- writeSet .add ( DIRECTORY + city .getGeoNameId () + ".xml" , handle );
327+ // let the implementation handle writing the city
328+ cityWriter .addCity (city );
303329
304330 // when we have a full batch, write it out
305331 if ( ++numWritten % BATCH_SIZE == 0 ) {
306- docMgr .write (writeSet );
307- writeSet = docMgr .newWriteSet ();
332+ cityWriter .finishBatch ();
308333 }
309334 }
310335 // if there are any leftovers, let's write this last batch
311336 if ( numWritten % BATCH_SIZE > 0 ) {
312- docMgr . write ( writeSet );
337+ cityWriter . finishBatch ( );
313338 }
339+ cityWriter .setNumRecords (numWritten );
314340 cityReader .close ();
341+ }
315342
316343
317- assertEquals ("Number of records not expected" , numWritten , RECORDS_EXPECTED );
344+ @ Test
345+ public void testBulkLoad () throws IOException , Exception {
346+ loadCities (new BulkCityWriter ());
318347 }
319348
320349 @ Test
@@ -380,7 +409,7 @@ public void testJsonLoad() {
380409 assertEquals ("Failed to read document 2" , "cat" , content2 .get ().get ("animal" ).textValue ());
381410 }
382411
383- public void validateRecord (DocumentRecord record ) {
412+ private void validateRecord (DocumentRecord record ) {
384413 JAXBHandle <City > handle = new JAXBHandle <City >(context );
385414 assertNotNull ("DocumentRecord should never be null" , record );
386415 assertNotNull ("Document uri should never be null" , record .getUri ());
@@ -392,16 +421,20 @@ public void validateRecord(DocumentRecord record) {
392421 */
393422 if ( record .getUri ().equals (DIRECTORY + "1205733.xml" ) ) {
394423 City chittagong = record .getContent (handle ).get ();
395- assertEquals ("City name doesn't match" , "Chittagong" , chittagong .getName ());
396- assertEquals ("City latitude doesn't match" , 22.3384 , chittagong .getLatitude (), 0 );
397- assertEquals ("City longitude doesn't match" , 91.83168 , chittagong .getLongitude (), 0 );
398- assertEquals ("City population doesn't match" , 3920222 , chittagong .getPopulation ());
399- assertEquals ("City elevation doesn't match" , 15 , chittagong .getElevation ());
400- assertEquals ("Currency code doesn't match" , "BDT" , chittagong .getCurrencyCode ());
401- assertEquals ("Currency name doesn't match" , "Taka" , chittagong .getCurrencyName ());
424+ validateChittagong (chittagong );
402425 }
403426 }
404427
428+ public static void validateChittagong (City chittagong ) {
429+ assertEquals ("City name doesn't match" , "Chittagong" , chittagong .getName ());
430+ assertEquals ("City latitude doesn't match" , 22.3384 , chittagong .getLatitude (), 0 );
431+ assertEquals ("City longitude doesn't match" , 91.83168 , chittagong .getLongitude (), 0 );
432+ assertEquals ("City population doesn't match" , 3920222 , chittagong .getPopulation ());
433+ assertEquals ("City elevation doesn't match" , 15 , chittagong .getElevation ());
434+ assertEquals ("Currency code doesn't match" , "BDT" , chittagong .getCurrencyCode ());
435+ assertEquals ("Currency name doesn't match" , "Taka" , chittagong .getCurrencyName ());
436+ }
437+
405438 @ Test
406439 public void testTextLoad () {
407440 String docId [] = {"/foo/test/myFoo1.xml" ,"/foo/test/myFoo2.xml" ,"/foo/test/myFoo3.xml" };
@@ -423,7 +456,6 @@ public void testTextLoad() {
423456 }
424457
425458
426-
427459 private static void addCountry (String line , Map <String , Country > countries ) {
428460 // skip comment lines
429461 if ( line .startsWith ("#" ) ) return ;
@@ -440,11 +472,11 @@ private static void addCountry(String line, Map<String, Country> countries) {
440472 );
441473 }
442474
443- private static Country getCountry (String isoCode , Map <String , Country > countries ) {
475+ public static Country getCountry (String isoCode , Map <String , Country > countries ) {
444476 return countries .get (isoCode );
445477 }
446478
447- private static City newCity (String line , Map <String , Country > countries ) {
479+ public static City newCity (String line , Map <String , Country > countries ) {
448480 String [] fields = line .split (" " );
449481 try {
450482 City city = new City ()
@@ -472,7 +504,7 @@ private static City newCity(String line, Map<String, Country> countries) {
472504 }
473505 }
474506
475- private static void cleanUp () {
507+ public static void cleanUp () {
476508 QueryManager queryMgr = Common .client .newQueryManager ();
477509 DeleteQueryDefinition deleteQuery = queryMgr .newDeleteDefinition ();
478510 deleteQuery .setDirectory ("/cities/" );
0 commit comments