UpdateJSON.updateTableWithoutQuery()

The UpdateJSON.updateTableWithoutQuery() method iterates over every row in our table looking for the proper rows to update.

This is by far the most complicated of the update methods due to the requirement to continually check for null fields. Notice that all of the following code is used to simply retrieve table rows. The actual update operation is performed by UpdateJSON.updateZipCode().

    // Utility method. Given a MapValue and a field name,
    // return the field as a MapValue. Used by 
    // updateTableWithoutQuery()
    private MapValue getMV(MapValue mv, String field) {
        FieldValue fv = null;
        if ((fv = mv.get(field)) != null)
            return fv.asMap();
        return null;
    }

    // Update the zip code found on all Boston home addresses
    // to "02102-1000"
    //
    // Because we are not using an index, we must iterate over
    // every row in the table, modifying the rows with Boston home
    // addresses.
    private void updateTableWithoutQuery(KVStore kvstore) {
        TableAPI tableH = kvstore.getTableAPI();
        Table myTable = tableH.getTable("personContacts");

        PrimaryKey pkey = myTable.createPrimaryKey();
        TableIterator<Row> iter =
            tableH.tableIterator(pkey, null, null);
        try {
            while (iter.hasNext()) {
                int account = 0;
                Row row = iter.next();
                FieldValue fv = null;
                try {
                    account = row.get("account").asInteger().get();
                    MapValue mv = row.get("person").asMap();

                    MapValue mvaddress = getMV(mv, "address");
                    if (mvaddress != null) {
                        MapValue mvhome = getMV(mvaddress, "home");
                        if (mvhome != null) {
                            fv = mvhome.get("city");
                            if (fv != null) {
                                if (fv.toString()
                                        .equalsIgnoreCase("Boston"))
                                    updateZipCode(tableH,
                                                  row,
                                                  "home",
                                                  "02102-1000");
                            }
                        }
                    }
                } catch (ClassCastException cce) {
                    System.out.println("Data error: ");
                    System.out.println("Account " + account +
                         "has a missing or incomplete person field");
                    // If this is thrown, then the "person" field
                    // doesn't exist for the row. 
                }
            }
        } finally {
            if (iter != null) {
                iter.close();
            }
        }

        System.out.println("Updated a table without using a query.");
    }