use of org.neo4j.kernel.impl.transaction.xaframework.XaDataSource in project graphdb by neo4j-attic.
the class MasterUtil method applyReceivedTransactions.
public static <T> void applyReceivedTransactions(Response<T> response, GraphDatabaseService graphDb, TxHandler txHandler) throws IOException {
XaDataSourceManager dataSourceManager = ((AbstractGraphDatabase) graphDb).getConfig().getTxModule().getXaDataSourceManager();
for (Triplet<String, Long, TxExtractor> tx : IteratorUtil.asIterable(response.transactions())) {
String resourceName = tx.first();
XaDataSource dataSource = dataSourceManager.getXaDataSource(resourceName);
txHandler.accept(tx, dataSource);
ReadableByteChannel txStream = tx.third().extract();
try {
dataSource.applyCommittedTransaction(tx.second(), txStream);
} finally {
txStream.close();
}
}
}
use of org.neo4j.kernel.impl.transaction.xaframework.XaDataSource in project graphdb by neo4j-attic.
the class MasterUtil method rotateLogsAndStreamStoreFiles.
public static SlaveContext rotateLogsAndStreamStoreFiles(GraphDatabaseService graphDb, StoreWriter writer) {
File baseDir = getBaseDir(graphDb);
XaDataSourceManager dsManager = ((AbstractGraphDatabase) graphDb).getConfig().getTxModule().getXaDataSourceManager();
Collection<XaDataSource> sources = dsManager.getAllRegisteredDataSources();
@SuppressWarnings("unchecked") Pair<String, Long>[] appliedTransactions = new Pair[sources.size()];
int i = 0;
for (XaDataSource ds : sources) {
appliedTransactions[i++] = Pair.of(ds.getName(), ds.getLastCommittedTxId());
try {
ds.getXaContainer().getResourceManager().rotateLogicalLog();
} catch (IOException e) {
// TODO: what about error message?
throw new MasterFailureException(e);
}
}
SlaveContext context = new SlaveContext(-1, -1, appliedTransactions);
ByteBuffer temporaryBuffer = ByteBuffer.allocateDirect(1024 * 1024);
for (XaDataSource ds : sources) {
try {
ClosableIterable<File> files = ds.listStoreFiles();
try {
for (File storefile : files) {
FileInputStream stream = new FileInputStream(storefile);
try {
writer.write(relativePath(baseDir, storefile), stream.getChannel(), temporaryBuffer, storefile.length() > 0);
} finally {
stream.close();
}
}
} finally {
files.close();
}
} catch (IOException e) {
// TODO: what about error message?
throw new MasterFailureException(e);
}
}
return context;
}
use of org.neo4j.kernel.impl.transaction.xaframework.XaDataSource in project graphdb by neo4j-attic.
the class MasterUtil method packResponse.
public static <T> Response<T> packResponse(GraphDatabaseService graphDb, SlaveContext context, T response, Predicate<Long> filter) {
List<Triplet<String, Long, TxExtractor>> stream = new ArrayList<Triplet<String, Long, TxExtractor>>();
Set<String> resourceNames = new HashSet<String>();
XaDataSourceManager dsManager = ((AbstractGraphDatabase) graphDb).getConfig().getTxModule().getXaDataSourceManager();
for (Pair<String, Long> txEntry : context.lastAppliedTransactions()) {
String resourceName = txEntry.first();
final XaDataSource dataSource = dsManager.getXaDataSource(resourceName);
if (dataSource == null) {
throw new RuntimeException("No data source '" + resourceName + "' found");
}
resourceNames.add(resourceName);
long masterLastTx = dataSource.getLastCommittedTxId();
for (long txId = txEntry.other() + 1; txId <= masterLastTx; txId++) {
if (filter.accept(txId)) {
final long tx = txId;
TxExtractor extractor = new TxExtractor() {
@Override
public ReadableByteChannel extract() {
try {
return dataSource.getCommittedTransaction(tx);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void extract(LogBuffer buffer) {
try {
dataSource.getCommittedTransaction(tx, buffer);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
stream.add(Triplet.of(resourceName, txId, extractor));
}
}
}
StoreId storeId = ((NeoStoreXaDataSource) dsManager.getXaDataSource(Config.DEFAULT_DATA_SOURCE_NAME)).getStoreId();
return new Response<T>(response, storeId, TransactionStream.create(resourceNames, stream));
}
use of org.neo4j.kernel.impl.transaction.xaframework.XaDataSource in project neo4j-mobile-android by neo4j-contrib.
the class XaDataSourceManager method unregisterDataSource.
/**
* Public for testing purpose. Do not use.
*/
public synchronized void unregisterDataSource(String name) {
XaDataSource dataSource = dataSources.get(name);
byte[] branchId = getBranchId(dataSource.getXaConnection().getXaResource());
dataSources.remove(name);
branchIdMapping.remove(UTF8.decode(branchId));
sourceIdMapping.remove(name);
dataSource.close();
}
use of org.neo4j.kernel.impl.transaction.xaframework.XaDataSource in project neo4j-mobile-android by neo4j-contrib.
the class TxModule method registerDataSource.
public XaDataSource registerDataSource(String dsName, String className, byte[] resourceId, Map<?, ?> params, boolean useExisting) {
XaDataSourceManager xaDsMgr = xaDsManager;
String name = dsName.toLowerCase();
if (xaDsMgr.hasDataSource(name)) {
if (useExisting) {
return xaDsMgr.getXaDataSource(name);
}
throw new TransactionFailureException("Data source[" + name + "] has already been registered");
}
try {
XaDataSource dataSource = xaDsMgr.create(className, params);
xaDsMgr.registerDataSource(name, dataSource, resourceId);
return dataSource;
} catch (Exception e) {
throw new TransactionFailureException("Could not create data source " + name + "[" + name + "]", e);
}
}
Aggregations