/** * This tests writing a message, reading its data, and removing it. * * @throws HBaseException */ @Test public void writeMessageData() throws HBaseException { MessagePersister mp = new MessagePersister(); mp.writeMessage(messageKey1, data1); byte[] result = mp.readMessage(messageKey1); assertTrue(Arrays.equals(result, data1)); mp.removeMessage(messageKey1); result = mp.readMessage(messageKey1); assertNull(result); }
/** * This tests writing messages and removing them in a bulk delete. * * @throws HBaseException */ @Test public void writeAndRemoveRows() throws HBaseException { MessagePersister mp = new MessagePersister(); HBaseFacade hbs = new HBaseFacade(); mp.writeMessage(messageKey1, data1); mp.writeMessage(messageKey2, data2); mp.writeMessage(messageKey3, data2); mp.writeMessage(messageKey4, data2); Set<Object> keys = new HashSet<Object>(); keys.add(messageKey1); keys.add(messageKey2); keys.add(messageKey3); keys.add(messageKey4); hbs.removeRows(MessagePersister.TABLE_MESSAGE, keys); byte[] result = mp.readMessage(messageKey1); assertNull(result); result = mp.readMessage(messageKey2); assertNull(result); result = mp.readMessage(messageKey3); assertNull(result); result = mp.readMessage(messageKey4); assertNull(result); }
/** * This tests writing a message along with some corresponding metadata, which is stored in a * separate column family for this row. * * @throws HBaseException */ @Test public void writeMessageWithMetaData() throws HBaseException { MessagePersister mp = new MessagePersister(); mp.writeMessage(messageKey1, dataAndMetadata1); Map<String, Map<String, byte[]>> resultMap = mp.readMessage(messageKey1, columnFamilies1); assertNotNull(resultMap); assertTrue(resultMap.size() == 2); for (String columnFamily : columnFamilies1) { Map<String, byte[]> columnFamilyDataMap = resultMap.get(columnFamily); assertTrue( columnFamily + " does not match.", Arrays.equals( dataAndMetadata1.get(columnFamily).get(MessagePersister.QUANTIFIER_VALUE), columnFamilyDataMap.get(MessagePersister.QUANTIFIER_VALUE))); } mp.removeMessage(messageKey1); resultMap = mp.readMessage(messageKey1, columnFamilies1); assertNotNull(resultMap); assertTrue(resultMap.size() == 0); }
/** * This tests writing a message and corresponding index metadata, then reading that index * metadata, then deleting the message and index metadata. * * @throws HBaseException */ @Test public void writeMessageAndIndex() throws HBaseException { MessagePersister mp = new MessagePersister(); mp.writeMessage(messageKey1, data1); mp.writeMessageIndex(messageKey1, metadataFields1); Map<String, byte[]> indexFields = mp.readMessageIndex(messageKey1); for (Entry<String, byte[]> field : indexFields.entrySet()) { assertTrue( Arrays.equals( metadataFields1.get(field.getKey()).toString().getBytes(), field.getValue())); } mp.removeMessageIndex(messageKey1); indexFields = mp.readMessageIndex(messageKey1); assertNotNull(indexFields); assertTrue(indexFields.size() == 0); mp.removeMessage(messageKey1); byte[] result = mp.readMessage(messageKey1); assertNull(result); }
private void writePayload(String messageKey, byte[] messageBodyBytes) throws ActionProcessingException { if (configuration.getDataStore().equals(DataStore.CASSANDRA)) { ColumnFamilyUpdater<String, String> updater = cfTemplate.createUpdater(messageKey); updater.setByteArray("body", messageBodyBytes); updater.setLong("timestamp", System.currentTimeMillis()); try { cfTemplate.update(updater); } catch (HectorException ex) { throw new ActionProcessingException( "Got HectorException writing " + "message to data storage: " + ex.getMessage()); } } else if (configuration.getDataStore().equals(DataStore.HBASE)) { try { // TODO need to write timestamp too? messagePersister.writeMessage(messageKey, messageBodyBytes); } catch (HBaseException ex) { throw new ActionProcessingException( "Got HBaseException writing " + "message to data storage: " + ex.getMessage()); } } }
/** * This tests writing messages, scanning the table for the keys, and removing the messages. * * @throws HBaseException */ @Test public void writeAndScanMessageKeys() throws HBaseException { MessagePersister mp = new MessagePersister(); HBaseFacade hbs = new HBaseFacade(); mp.writeMessage(messageKey1, data1); mp.writeMessage(messageKey2, data2); mp.writeMessage(messageKey3, data2); mp.writeMessage(messageKey4, data2); mp.writeMessage(messageKey5, data2); mp.writeMessage(messageKey6, data2); mp.writeMessage(messageKey7, data2); mp.writeMessage(messageKey8, data2); mp.writeMessage(messageKey9, data2); mp.writeMessage(messageKey10, data2); Set<Object> keys = hbs.scanForKeys(MessagePersister.TABLE_MESSAGE); assertTrue("Did not return a key", keys.contains(messageKey1)); assertTrue("Did not return a key", keys.contains(messageKey2)); assertTrue("Did not return a key", keys.contains(messageKey3)); assertTrue("Did not return a key", keys.contains(messageKey4)); assertTrue("Did not return a key", keys.contains(messageKey5)); assertTrue("Did not return a key", keys.contains(messageKey6)); assertTrue("Did not return a key", keys.contains(messageKey7)); assertTrue("Did not return a key", keys.contains(messageKey8)); assertTrue("Did not return a key", keys.contains(messageKey9)); assertTrue("Did not return a key", keys.contains(messageKey10)); mp.removeMessage(messageKey1); mp.removeMessage(messageKey2); mp.removeMessage(messageKey3); mp.removeMessage(messageKey4); mp.removeMessage(messageKey5); mp.removeMessage(messageKey6); mp.removeMessage(messageKey7); mp.removeMessage(messageKey8); mp.removeMessage(messageKey9); mp.removeMessage(messageKey10); }
/** * This tests writing messages, scanning the table, and removing the messages. * * @throws HBaseException */ @Test public void writeAndScanMessageData() throws HBaseException { MessagePersister mp = new MessagePersister(); HBaseFacade hbs = new HBaseFacade(); mp.writeMessage(messageKey1, data1); mp.writeMessage(messageKey2, data2); mp.writeMessage(messageKey3, data2); mp.writeMessage(messageKey4, data2); mp.writeMessage(messageKey5, data2); mp.writeMessage(messageKey6, data2); mp.writeMessage(messageKey7, data2); mp.writeMessage(messageKey8, data2); mp.writeMessage(messageKey9, data2); mp.writeMessage(messageKey10, data2); Map<String, byte[]> dataMap = hbs.scan( MessagePersister.TABLE_MESSAGE, 10, MessagePersister.COL_FAMILY_DATA, MessagePersister.QUANTIFIER_VALUE); assertTrue(Arrays.equals(dataMap.get(messageKey1), data1)); assertTrue(Arrays.equals(dataMap.get(messageKey2), data2)); assertTrue(Arrays.equals(dataMap.get(messageKey3), data2)); assertTrue(Arrays.equals(dataMap.get(messageKey4), data2)); assertTrue(Arrays.equals(dataMap.get(messageKey5), data2)); assertTrue(Arrays.equals(dataMap.get(messageKey6), data2)); assertTrue(Arrays.equals(dataMap.get(messageKey7), data2)); assertTrue(Arrays.equals(dataMap.get(messageKey8), data2)); assertTrue(Arrays.equals(dataMap.get(messageKey9), data2)); assertTrue(Arrays.equals(dataMap.get(messageKey10), data2)); mp.removeMessage(messageKey1); mp.removeMessage(messageKey2); mp.removeMessage(messageKey3); mp.removeMessage(messageKey4); mp.removeMessage(messageKey5); mp.removeMessage(messageKey6); mp.removeMessage(messageKey7); mp.removeMessage(messageKey8); mp.removeMessage(messageKey9); mp.removeMessage(messageKey10); }