source: gs2-extensions/parallel-building/trunk/src/src/java/org/nzdl/gsdl/GSInfoDB.java@ 27910

Last change on this file since 27910 was 27910, checked in by jmt12, 11 years ago

Extended the existing HadoopGreenstoneIngest with proper Reduce phase - this also required the creation of several new classes to handle the partitioning and grouping of Map phase output, and a class to wrap access to the txt2(t)db processes.

File size: 4.1 KB
Line 
1/******************************************************************************
2 *
3 * A component of the Greenstone digital library software from the New Zealand
4 * Digital Library Project at the # University of Waikato, New Zealand.
5 * Copyright (C) 2006 New Zealand Digital Library Project
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 675 Mass Ave, Cambridge, MA 02139, USA.
20 *****************************************************************************/
21/** @author jmt12, GSDL **/
22package org.nzdl.gsdl;
23
24import java.io.*;
25import java.lang.*;
26import java.nio.file.*;
27import java.util.*;
28
29/** @class GSInfoDB
30 */
31public class GSInfoDB
32{
33 private boolean debug = true;
34
35 private PrintWriter dbwriter;
36 private Process dbprocess;
37
38 private Path file_path;
39 private Path tmp_file_path;
40
41 private String infodb_type;
42
43 // Seventy hyphens - GDBM's default record separator
44 private static String separator = "----------------------------------------------------------------------";
45
46 /** @function GSInfoDB(String)
47 */
48 public GSInfoDB(String gsdl_home, String infodb_type, String file)
49 {
50 if (debug)
51 {
52 System.err.println("[DEBUG] GSInfoDB(" + gsdl_home + ", " + infodb_type + ", " + file + ")");
53 }
54 this.infodb_type = infodb_type;
55 // most of the following code throws IOExceptions...
56 try
57 {
58 this.file_path = FileSystems.getDefault().getPath(file);
59 Path tmp_dir = FileSystems.getDefault().getPath("/tmp");
60 this.tmp_file_path = Files.createTempFile(tmp_dir, null, "." + infodb_type);
61 if (debug)
62 {
63 System.err.println("[DEBUG] Temporary file path: " + tmp_file_path);
64 }
65 ProcessBuilder pb = new ProcessBuilder(gsdl_home + "/ext/tdb-edit/linux/bin/txt2tdb", "-append", tmp_file_path.toString());
66 if (debug)
67 {
68 System.err.println("[DEBUG] Command: " + pb.command());
69 }
70 // Start the writer listening
71 dbprocess = pb.start();
72 OutputStream os = dbprocess.getOutputStream();
73 dbwriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(os)));
74 }
75 catch (Exception ex)
76 {
77 System.err.println("Error! Failed to open GSInfoBD: " + ex.toString());
78 ex.printStackTrace();
79 }
80 }
81 /** GSInfoDB(String) **/
82
83
84 /** @function close()
85 */
86 public void close()
87 {
88 if (debug)
89 {
90 System.err.println("[DEBUG] GSInfoBD::close()");
91 }
92 try
93 {
94 dbwriter.close();
95 dbprocess.waitFor();
96 // Move the database into its final location
97 Runtime rut = Runtime.getRuntime();
98 Process process = rut.exec(new String[] {"mv", this.tmp_file_path.toString(), this.file_path.toString()});
99 // The below always says the file doesn't exist over NFS?
100 /*
101 if (!this.file_path.toFile().exists())
102 {
103 System.err.println("Error! Failed to move database file: " + this.tmp_file_path.toString() + " => " + this.file_path.toString());
104 }
105 */
106 }
107 catch (Exception ex)
108 {
109 System.err.println("Error! Failed to close GSInfoBD: " + ex.toString());
110 ex.printStackTrace();
111 }
112 }
113 /** close() **/
114
115
116 /** @function write(String payload)
117 */
118 public void writeEntry(String payload)
119 {
120 if (debug)
121 {
122 System.err.println("[DEBUG] GSInfoDB::writeEntry(" + payload + ")");
123 }
124 try
125 {
126 dbwriter.println(payload);
127 dbwriter.println(separator);
128 }
129 catch (Exception ex)
130 {
131 System.err.println("Error! Failed to write to GSInfoDB: " + ex.toString());
132 ex.printStackTrace();
133 }
134 }
135 /** write(String payload) **/
136
137}
138/** class GSInfoDB **/
139
Note: See TracBrowser for help on using the repository browser.