use of org.fusesource.hawtbuf.Buffer in project camel by apache.
the class LevelDBAggregationRepository method remove.
public void remove(final CamelContext camelContext, final String key, final Exchange exchange) {
LOG.debug("Removing key [{}]", key);
try {
byte[] lDbKey = keyBuilder(repositoryName, key);
final String exchangeId = exchange.getExchangeId();
final Buffer exchangeBuffer = codec.marshallExchange(camelContext, exchange, allowSerializedHeaders);
// remove the exchange
byte[] rc = levelDBFile.getDb().get(lDbKey);
if (rc != null) {
WriteBatch batch = levelDBFile.getDb().createWriteBatch();
try {
batch.delete(lDbKey);
LOG.trace("Removed key index {} -> {}", key, new Buffer(rc));
// add exchange to confirmed index
byte[] confirmedLDBKey = keyBuilder(getRepositoryNameCompleted(), exchangeId);
batch.put(confirmedLDBKey, exchangeBuffer.toByteArray());
LOG.trace("Added confirm index {} for repository {}", exchangeId, getRepositoryNameCompleted());
levelDBFile.getDb().write(batch, levelDBFile.getWriteOptions());
} finally {
batch.close();
}
}
} catch (IOException e) {
throw new RuntimeException("Error removing key " + key + " from repository " + repositoryName, e);
}
}
use of org.fusesource.hawtbuf.Buffer in project camel by apache.
the class HawtDBAggregationRepository method remove.
public void remove(final CamelContext camelContext, final String key, final Exchange exchange) {
LOG.debug("Removing key [{}]", key);
try {
final Buffer keyBuffer = codec.marshallKey(key);
final Buffer confirmKeyBuffer = codec.marshallKey(exchange.getExchangeId());
final Buffer exchangeBuffer = codec.marshallExchange(camelContext, exchange, allowSerializedHeaders);
hawtDBFile.execute(new Work<Buffer>() {
public Buffer execute(Transaction tx) {
SortedIndex<Buffer, Buffer> index = hawtDBFile.getRepositoryIndex(tx, repositoryName, true);
// remove from the in progress index
Buffer buffer = index.remove(keyBuffer);
LOG.trace("Removed key index {} -> {}", keyBuffer, buffer);
// and add it to the confirmed index
SortedIndex<Buffer, Buffer> indexCompleted = hawtDBFile.getRepositoryIndex(tx, getRepositoryNameCompleted(), true);
indexCompleted.put(confirmKeyBuffer, exchangeBuffer);
LOG.trace("Added confirm index {}", confirmKeyBuffer);
return null;
}
@Override
public String toString() {
return "Removing key [" + key + "]";
}
});
} catch (IOException e) {
throw new RuntimeException("Error removing key " + key + " from repository " + repositoryName, e);
}
}
use of org.fusesource.hawtbuf.Buffer in project camel by apache.
the class HawtDBAggregationRepository method get.
public Exchange get(final CamelContext camelContext, final String key) {
Exchange answer = null;
try {
final Buffer keyBuffer = codec.marshallKey(key);
Buffer rc = hawtDBFile.execute(new Work<Buffer>() {
public Buffer execute(Transaction tx) {
SortedIndex<Buffer, Buffer> index = hawtDBFile.getRepositoryIndex(tx, repositoryName, false);
if (index == null) {
return null;
}
Buffer buffer = index.get(keyBuffer);
LOG.trace("Getting key index {}", keyBuffer);
return buffer;
}
@Override
public String toString() {
return "Getting key [" + key + "]";
}
});
if (rc != null) {
answer = codec.unmarshallExchange(camelContext, rc);
}
} catch (IOException e) {
throw new RuntimeException("Error getting key " + key + " from repository " + repositoryName, e);
}
LOG.debug("Getting key [{}] -> {}", key, answer);
return answer;
}
use of org.fusesource.hawtbuf.Buffer in project camel by apache.
the class HawtDBGrowIssueTest method testGrowIssue.
@Test
public void testGrowIssue() throws Exception {
// a 1kb string for testing
StringBuilder sb = new StringBuilder(size);
for (int i = 0; i < 1024; i++) {
sb.append("X");
}
// the key
final Buffer key = codec.marshallKey("foo");
// we update using the same key, which means we should be able to do this within the file size limit
for (int i = 0; i < size; i++) {
final Buffer data = codec.marshallKey(i + "-" + sb.toString());
log.debug("Updating " + i);
hawtDBFile.execute(new Work<Object>() {
public Object execute(Transaction tx) {
SortedIndex<Buffer, Buffer> index = hawtDBFile.getRepositoryIndex(tx, "repo", true);
return index.put(key, data);
}
});
}
// get the last
Buffer out = hawtDBFile.execute(new Work<Buffer>() {
public Buffer execute(Transaction tx) {
SortedIndex<Buffer, Buffer> index = hawtDBFile.getRepositoryIndex(tx, "repo", true);
return index.get(key);
}
});
String data = codec.unmarshallKey(out);
log.info(data);
assertTrue("Should be 1023", data.startsWith("1023"));
assertEquals(1029, data.length());
}
use of org.fusesource.hawtbuf.Buffer in project fabric8 by jboss-fuse.
the class ActiveMQTextMessage method beforeMarshall.
public void beforeMarshall(OpenWireFormat wireFormat) throws IOException {
super.beforeMarshall(wireFormat);
Buffer content = getContent();
if (content == null && text != null) {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
OutputStream os = bytesOut;
if (Settings.enable_compression()) {
compressed = true;
os = new DeflaterOutputStream(os);
}
DataOutputStream dataOut = new DataOutputStream(os);
MarshallingSupport.writeUTF8(dataOut, this.text);
dataOut.close();
setContent(bytesOut.toBuffer());
}
}
Aggregations