/** * Insert a record in the database. Any field/value pairs in the specified values HashMap will be * written into the record with the specified record key. * * @param table The name of the table * @param key The record key of the record to insert. * @param values A HashMap of field/value pairs to insert in the record * @return Zero on success, a non-zero error code on error. See this class's description for a * discussion of error codes. */ public int insert(String table, String key, HashMap<String, String> values) { // Iterate over collection of values, serialize, insert. StringBuffer data = new StringBuffer(); for (Map.Entry<String, String> entry : values.entrySet()) { data.append(entry.getKey()); data.append(":"); data.append(entry.getValue()); data.append(";"); } client.put(key, data.toString()); return 0; }
/** * Read a record from the database. Each field/value pair from the result will be stored in a * HashMap. * * @param table The name of the table * @param key The record key of the record to read. * @param fields The list of fields to read, or null for all of them * @param result A HashMap of field/value pairs for the result * @return Zero on success, a non-zero error code on error or "not found". */ public int read(String table, String key, Set<String> fields, HashMap<String, String> result) { // System.out.println("Reading (client port " + myPort + ")"); String data = client.get(key); String[] values = data.split(";"); for (int i = 0; i < values.length; i++) { String[] pair = values[i].split(":"); if (fields != null && (fields.isEmpty() || fields.contains(pair[0]))) { result.put(pair[0], pair[1]); } } return 0; }
/** * Delete a record from the database. * * @param table The name of the table * @param key The record key of the record to delete. * @return Zero on success, a non-zero error code on error. See this class's description for a * discussion of error codes. */ public int delete(String table, String key) { client.remove(key); return 0; }