/** * Load log and rebuild KVServer by iterating over log entries. You do not need to restore the * previous cache state (i.e. ignore GETS). * * @throws KVException if an error occurs in KVServer (though we expect none) */ public void rebuildServer() throws KVException { // implement me try { loadFromDisk(); kvServer.wipeEverything(); KVMessage request = null; for (KVMessage log : entries) { if (log.getMsgType().equals(PUT_REQ) || log.getMsgType().equals(DEL_REQ)) { request = log; } else if (log.getMsgType().equals(COMMIT)) { if (request == null) { // Do nothing. If reached here, repeated COMMIT continue; } else if (request.getMsgType().equals(PUT_REQ)) { kvServer.put(request.getKey(), request.getValue()); } else if (request.getMsgType().equals(DEL_REQ)) { kvServer.del(request.getKey()); } } else if (log.getMsgType().equals(ABORT)) { request = null; } } } catch (Exception e) { // throw new KVException("Error: Rebuild failed"); } }
/** * Load log and rebuild KVServer by iterating over log entries. You do not need to restore the * previous cache state (i.e. ignore GETS). * * @throws KVException if an error occurs in KVServer (though we expect none) */ public void rebuildServer() throws KVException { KVMessage interruptedTPCOperation = null; loadFromDisk(); for (int i = 0; i < entries.size(); i++) { // System.out.println("rebuild server " + i); KVMessage entry = entries.get(i); String msgType = entry.getMsgType(); if (msgType.equals("putreq") || msgType.equals("delreq")) { interruptedTPCOperation = entry; continue; } if (msgType.equals("commit") && interruptedTPCOperation != null) { if (interruptedTPCOperation.getMsgType().equals("delreq")) { kvServer.del(interruptedTPCOperation.getKey()); } else if (interruptedTPCOperation.getMsgType().equals("putreq")) { kvServer.put(interruptedTPCOperation.getKey(), interruptedTPCOperation.getValue()); } interruptedTPCOperation = null; } else if (msgType.equals("abort")) { interruptedTPCOperation = null; } } }