List of usage examples for org.apache.thrift.protocol TCompactProtocol TCompactProtocol
public TCompactProtocol(TTransport transport)
From source file:org.diqube.file.DiqubeFileReader.java
License:Open Source License
DiqubeFileReader(DataDeserializer deserializer, BigByteBuffer data) throws IOException { this.deserializer = deserializer; this.data = data; // validate file header. try (ReadCountInputStream is = new ReadCountInputStream(data.createInputStream())) { TIOStreamTransport transport = new TIOStreamTransport(is); TProtocol compactProt = new TCompactProtocol(transport); header = new SDiqubeFileHeader(); header.read(compactProt);//www.j av a2 s . c o m // first TableShard byte is followed the SDiqubeFileHeader directly. firstTableShardByteIndex = is.getNumberOfBytesRead(); if (!DiqubeFileWriter.MAGIC_STRING.equals(header.getMagic())) throw new IOException("File is invalid."); if (header.getFileVersion() != DiqubeFileWriter.FILE_VERSION) throw new IOException("Only file version " + DiqubeFileWriter.FILE_VERSION + " supported, but found version " + header.getFileVersion()); if (header.getContentVersion() != DataSerializer.DATA_VERSION) throw new IOException("Only content version " + DataSerializer.DATA_VERSION + " supported, but found version " + header.getContentVersion()); } catch (TException e) { throw new IOException("Could not load file header", e); } if (FILE_FOOTER_LENGTH_BYTES == -1) { // calculate the length of SDiqubeFileFooterInfo. This is constant and equal for all files, as it is // de-/serialized using TBinaryProtocol. SDiqubeFileFooterInfo fileFooterInfo = new SDiqubeFileFooterInfo(); fileFooterInfo.setFooterLengthBytes(1); try { byte[] fileFooterInfoBytes = new TSerializer(new TBinaryProtocol.Factory()) .serialize(fileFooterInfo); FILE_FOOTER_LENGTH_BYTES = fileFooterInfoBytes.length; } catch (TException e) { throw new IOException("Could not calculate length of SDiqubeFileFooterInfo", e); } } // read footer info int footerLengthBytes; try (InputStream is = data.createPartialInputStream(data.size() - FILE_FOOTER_LENGTH_BYTES, data.size())) { TIOStreamTransport transport = new TIOStreamTransport(is); TProtocol binaryProt = new TBinaryProtocol(transport); SDiqubeFileFooterInfo footerInfo = new SDiqubeFileFooterInfo(); footerInfo.read(binaryProt); footerLengthBytes = footerInfo.getFooterLengthBytes(); } catch (TException e) { throw new IOException("Could not read length of file footer", e); } lastTableShardByteIndex = data.size() - FILE_FOOTER_LENGTH_BYTES - footerLengthBytes - 1; // read footer. try (InputStream is = data.createPartialInputStream(lastTableShardByteIndex + 1, data.size())) { TIOStreamTransport transport = new TIOStreamTransport(is); TProtocol compactProt = new TCompactProtocol(transport); footer = new SDiqubeFileFooter(); footer.read(compactProt); } catch (TException e) { throw new IOException("Could not read footer", e); } }
From source file:org.diqube.file.DiqubeFileReader.java
License:Open Source License
/** * Deserializes all {@link TableShard}s stored in the file. *///ww w . ja v a2 s . c om public Collection<DefaultTableShard> loadAllTableShards() throws IOException, DeserializationException { List<DefaultTableShard> res = new ArrayList<>(); try (InputStream is = data.createInputStream()) { TIOStreamTransport transport = new TIOStreamTransport(is); TProtocol compactProt = new TCompactProtocol(transport); SDiqubeFileHeader header = new SDiqubeFileHeader(); header.read(compactProt); for (int i = 0; i < getNumberOfTableShards(); i++) { DefaultTableShard tableShard = deserializer.deserialize(DefaultTableShard.class, is); res.add(tableShard); } } catch (TException e) { throw new IOException("Could not load table shards", e); } return res; }
From source file:org.diqube.file.internaldb.InternalDbFileReader.java
License:Open Source License
/** * Read the newest file.//from ww w . ja v a 2s . co m * * @return <code>null</code> if no file is available. * @throws ReadException * If file cannot be read. */ public List<T> readNewest() throws ReadException { File inputFile = findFileToReadFrom(); if (inputFile == null) return null; logger.info("Loading {} entities from '{}'...", dataType, inputFile.getAbsolutePath()); try (FileInputStream fis = new FileInputStream(inputFile)) { try (TIOStreamTransport transport = new TIOStreamTransport(fis)) { TCompactProtocol protocol = new TCompactProtocol(transport); SInternalDbFileHeader header = new SInternalDbFileHeader(); header.read(protocol); if (header.getVersion() != InternalDbFileWriter.VERSION) throw new ReadException("Bad version number: " + header.getVersion()); if (!header.getDataType().equals(dataType)) throw new ReadException( "Bad data type. Expected: " + dataType + " but got: " + header.getDataType()); List<T> res = new ArrayList<>(); long size = header.getSize(); while (size-- > 0) { T newObj = factory.get(); newObj.read(protocol); res.add(newObj); } logger.info("Loaded {} entities from '{}'.", dataType, inputFile.getAbsolutePath()); return res; } catch (TException e) { throw new ReadException("Could not read identities from " + inputFile.getAbsolutePath(), e); } } catch (IOException e1) { throw new ReadException("Could not read identities from " + inputFile.getAbsolutePath(), e1); } }
From source file:org.diqube.file.internaldb.InternalDbFileWriter.java
License:Open Source License
public void write(long consensusCommitIndex, List<T> entities) throws WriteException { File[] filesToDelete = outputDir .listFiles((dir, name) -> name.startsWith(filenamePrefix) && name.endsWith(FILENAME_SUFFIX)); File newFile = new File(outputDir, filenamePrefix + String.format("%020d", consensusCommitIndex) + FILENAME_SUFFIX); logger.info("Writing updated {} entities to '{}'...", dataType, newFile.getAbsolutePath()); try (FileOutputStream fos = new FileOutputStream(newFile)) { try (TIOStreamTransport transport = new TIOStreamTransport(fos)) { TCompactProtocol protocol = new TCompactProtocol(transport); SInternalDbFileHeader header = new SInternalDbFileHeader(); header.setVersion(VERSION);/*from ww w .j av a 2 s . co m*/ header.setDataType(dataType); header.setSize(entities.size()); header.write(protocol); for (T e : entities) { e.write(protocol); } logger.info("Updated internaldb file '{}'.", newFile.getAbsolutePath()); for (File f : filesToDelete) { logger.info("Deleting old internaldb file '{}'", f.getAbsolutePath()); if (InternalDbFileUtil.parseCommitIndex(f, filenamePrefix, FILENAME_SUFFIX) > consensusCommitIndex) // This could mean that consensus replays some commits during startup. That replay might be broken, if e.g. // SerializationExceptions happen during the process and the classes of the serialized objects changed (= // new version installed?). logger.warn( "Overwriting a presumably newer version of an {} internalDb file with an older version.", dataType); f.delete(); } } catch (TException e) { throw new WriteException("Could not write internaldb file '" + newFile.getAbsolutePath() + "'", e); } } catch (IOException e1) { throw new WriteException("Could not write internaldb file '" + newFile.getAbsolutePath() + "'", e1); } }
From source file:org.diqube.itest.util.TestThriftConnectionFactory.java
License:Open Source License
/** * Open a connection to the given remote. * //from w ww .j a v a2 s . c o m * @throws TestConnectionException * If anything goes wrong. */ public static <O> TestConnection<O> open(ServerAddr addr, Class<? extends O> thriftClientClass, String serviceName) throws TestConnectionException { try { TTransport transport = DiqubeClientSocketTestFactory.createSocket(addr.getHost(), addr.getPort(), 1000, () -> { }); transport = new TFramedTransport(transport); TProtocol protocol = new TMultiplexedProtocol(new TCompactProtocol(transport), serviceName); O client = thriftClientClass.getConstructor(TProtocol.class).newInstance(protocol); transport.open(); return new TestConnection<O>(transport, client); } catch (RuntimeException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | TTransportException e) { throw new TestConnectionException("Could not open connection", e); } }
From source file:org.diqube.ticket.TicketUtil.java
License:Open Source License
/** * Serialize the given ticket into a byte array. * // w ww . ja v a 2s .c o m * @throws IllegalArgumentException * if something goes wrong. */ public static byte[] serialize(Ticket t) throws IllegalArgumentException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (TTransport transport = new TIOStreamTransport(baos)) { TCompactProtocol protocol = new TCompactProtocol(transport); t.write(protocol); transport.flush(); } catch (TException e) { throw new IllegalArgumentException("Could not serailize ticket", e); } return baos.toByteArray(); }
From source file:org.diqube.ticket.TicketUtil.java
License:Open Source License
/** * Deserialize bytes into a {@link Ticket} and capture the bytes of the serialized stream that identify the * {@link TicketClaim}./*from w ww.j a va2s . c om*/ * * @return {@link Pair} of deserialized {@link Ticket} and the bytes that were used in the serialized form to describe * the {@link TicketClaim} (that what is signed in the {@link Ticket}). */ public static Pair<Ticket, byte[]> deserialize(ByteBuffer serializedTicket) { byte[] data = new byte[serializedTicket.remaining()]; serializedTicket.get(data); ByteArrayInputStream bais = new ByteArrayInputStream(data); try (TTransport origTransport = new TIOStreamTransport(bais)) { RememberingTransport rememberingTransport = new RememberingTransport(origTransport); TCompactProtocol compactProtocol = new TCompactProtocol(rememberingTransport); PartialRememberingProtocol rememberingProtocol = new PartialRememberingProtocol(compactProtocol, rememberingTransport, // // The first "struct" that is read is the "claim" struct. // THIS DEPENDS ON THE THRIFT DEFINITION! 0); Ticket t = new Ticket(); t.read(rememberingProtocol); byte[] claimBytes = rememberingProtocol.getRememberedBytes(); return new Pair<>(t, claimBytes); } catch (TException e) { throw new IllegalArgumentException("Could not deserialize ticket", e); } }
From source file:org.diqube.ui.ticket.TicketValiditySubscriber.java
License:Open Source License
private <T> T openConnection(Class<T> thriftClientClass, String serviceName) { for (Pair<String, Short> node : config.getClusterServers()) { TTransport transport = new TFramedTransport(new TSocket(node.getLeft(), node.getRight())); TProtocol protocol = new TMultiplexedProtocol(new TCompactProtocol(transport), serviceName); T res;/*w w w .j a va 2s .co m*/ try { res = thriftClientClass.getConstructor(TProtocol.class).newInstance(protocol); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { throw new RuntimeException("Could not instantiate thrift client", e); } try { transport.open(); } catch (TTransportException e) { continue; } return res; } return null; }
From source file:org.diqube.ui.websocket.json.JsonQueryCommand.java
License:Open Source License
private UUID sendDiqlQuery(Pair<String, Short> node, QueryResultService.Iface resultHandler) { TTransport transport = new TSocket(node.getLeft(), node.getRight()); transport = new TFramedTransport(transport); TProtocol queryProtocol = new TMultiplexedProtocol(new TCompactProtocol(transport), QueryServiceConstants.SERVICE_NAME); QueryService.Client queryClient = new QueryService.Client(queryProtocol); UUID queryUuid = UUID.randomUUID(); try {/*www . j ava 2s. co m*/ transport.open(); queryResultRegistry.registerThriftResultCallback(websocketSession, queryUuid, resultHandler); queryClient.asyncExecuteQuery(RUuidUtil.toRUuid(queryUuid), // diql, // true, createOurAddress()); return queryUuid; } catch (RQueryException e) { throw new RuntimeException(e.getMessage()); } catch (TException e) { queryResultRegistry.unregisterQuery(queryUuid); return null; } }
From source file:org.diqube.ui.websocket.request.AbstractCommandClusterInteraction.java
License:Open Source License
@Override public void cancelQuery() { if (ticket == null) throw new RuntimeException("Not logged in."); Pair<UUID, Pair<String, Short>> queryUuidAndAddrPair = findQueryUuidAndServerAddr(); UUID queryUuid = queryUuidAndAddrPair.getLeft(); // server that the query was sent to. That is the query master for that query! Pair<String, Short> serverAddr = queryUuidAndAddrPair.getRight(); TTransport transport = new TSocket(serverAddr.getLeft(), serverAddr.getRight()); transport = new TFramedTransport(transport); TProtocol queryProtocol = new TMultiplexedProtocol(new TCompactProtocol(transport), QueryServiceConstants.SERVICE_NAME); QueryService.Client queryClient = new QueryService.Client(queryProtocol); try {//w w w . ja va2 s . c om transport.open(); logger.info("Sending request to cancel query {} to the diqube server at {}.", queryUuid, serverAddr); queryClient.cancelQueryExecution(ticket, RUuidUtil.toRUuid(queryUuid)); } catch (TException e) { logger.warn("Could not cancel execution of query {} although requested by user.", queryUuidAndAddrPair); } finally { transport.close(); } }