Example usage for java.nio ByteBuffer getLong

List of usage examples for java.nio ByteBuffer getLong

Introduction

In this page you can find the example usage for java.nio ByteBuffer getLong.

Prototype

public abstract long getLong();

Source Link

Document

Returns the long at the current position and increases the position by 8.

Usage

From source file:org.apache.hama.monitor.fd.UDPSupervisor.java

@Override
public Object call() throws Exception {
    ByteBuffer packet = ByteBuffer.allocate(8);
    try {//from  ww  w  .  ja v a  2s.co m
        while (running.get()) {
            final InetSocketAddress source = (InetSocketAddress) channel.receive(packet);
            final String hostName = source.getHostName();
            packet.flip();
            final long seq = packet.getLong();
            packet.clear();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Seqence: " + seq + " src host: " + hostName);
            }
            final Node tmpNode = new Node(hostName, WINDOW_SIZE.get());
            receiver.submit(new Hermes(tmpNode, seq, System.currentTimeMillis()));
        }
    } catch (IOException ioe) {
        LOG.error("Problem in receiving packet from channel.", ioe);
        Thread.currentThread().interrupt();
    } finally {
        if (null != this.channel)
            try {
                this.channel.socket().close();
                this.channel.close();
            } catch (IOException ioe) {
                LOG.error("Error closing supervisor channel.", ioe);
            }
    }
    return null;
}

From source file:com.sm.connector.server.MRStore.java

@Override
public void setCurrent(int current) {
    if (current >= getTotalRecord())
        throw new RuntimeException("current " + current + " >= total " + getTotalRecord());
    else {/*from   ww w . j av  a2 s.  c  o  m*/
        curRecord = current;
        if (current == 0)
            return;
        //set 3 channel position
        long pos = OFFSET + (long) getCurRecord() * RECORD_SIZE;
        try {
            //read the first index record
            ByteBuffer buf = ByteBuffer.allocate(RECORD_SIZE);
            //re read the index buffer
            indexChannel.read(buf, pos);
            buf.rewind();
            long len = indexChannel.read(indexBuf, pos);
            indexBuf.rewind();
            indexPos = pos + len;
            // data and key pos, need to get from previous index record
            buf.get(); //get status byte
            long key = buf.getLong();
            long keyOff = getOffset(key);
            //long keyLen = getLen( key);
            len = keyChannel.read(keyBuf, keyOff);
            keyBuf.rewind();
            keyPos = keyOff + len;
            //read data channel
            long dataOffset2Len = buf.getLong();
            //long block2version = buf.getLong();
            long dataLen = getLen(dataOffset2Len);
            long offset = getOffset(dataOffset2Len);
            //long blockSize = getSize( block2version);
            len = dataChannel.read(dataBuf, offset);
            dataBuf.rewind();
            dataPos = offset + len;
        } catch (IOException ex) {
            throw new RuntimeException(ex.getMessage(), ex);
        }
    }
}

From source file:org.apache.bookkeeper.bookie.BookieJournalTest.java

/**
 * Test that if the bookie crashes in the middle of writing
 * the actual entry it can recover./*from   ww w.j  av  a 2  s  .  c o m*/
 * In this case the entry will be available, but it will corrupt.
 * This is ok, as the client will disregard the entry after looking
 * at its checksum.
 */
@Test(timeout = 60000)
public void testTruncatedInEntryJournal() throws Exception {
    File journalDir = createTempDir("bookie", "journal");
    Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(journalDir));

    File ledgerDir = createTempDir("bookie", "ledger");
    Bookie.checkDirectoryStructure(Bookie.getCurrentDirectory(ledgerDir));

    JournalChannel jc = writeV2Journal(Bookie.getCurrentDirectory(journalDir), 100);
    ByteBuffer zeros = ByteBuffer.allocate(2048);

    jc.fc.position(jc.getBufferedChannel().position() - 0x300);
    jc.fc.write(zeros);
    jc.fc.force(false);

    writeIndexFileForLedger(Bookie.getCurrentDirectory(ledgerDir), 1, "testPasswd".getBytes());

    ServerConfiguration conf = TestBKConfiguration.newServerConfiguration().setZkServers(null)
            .setJournalDirName(journalDir.getPath()).setLedgerDirNames(new String[] { ledgerDir.getPath() });

    Bookie b = new Bookie(conf);
    b.readJournal();
    b.readEntry(1, 99);

    // still able to read last entry, but it's junk
    ByteBuffer buf = b.readEntry(1, 100);
    assertEquals("Ledger Id is wrong", buf.getLong(), 1);
    assertEquals("Entry Id is wrong", buf.getLong(), 100);
    assertEquals("Last confirmed is wrong", buf.getLong(), 99);
    assertEquals("Length is wrong", buf.getLong(), 100 * 1024);
    buf.getLong(); // skip checksum
    boolean allX = true;
    for (int i = 0; i < 1024; i++) {
        byte x = buf.get();
        allX = allX && x == (byte) 'X';
    }
    assertFalse("Some of buffer should have been zeroed", allX);

    try {
        b.readEntry(1, 101);
        fail("Shouldn't have found entry 101");
    } catch (Bookie.NoEntryException e) {
        // correct behaviour
    }
}

From source file:com.datatorrent.contrib.hdht.WALTest.java

/**
 * checkpointed(1)  1 -> 10//from w  ww  .jav  a  2 s  . co  m
 * checkpointed(2)  1 -> 20
 * committed(2)
 * checkpointed(3)  1 -> 30
 * committed(3)
 * checkpointed(4)
 * committed(4)
 *
 * no null pointer exception should occure.
 */
@Test
public void testIssue4008() throws IOException {
    File file = new File("target/hds");
    FileUtils.deleteDirectory(file);

    FileAccessFSImpl bfs = new MockFileAccess();
    bfs.setBasePath(file.getAbsolutePath());
    bfs.init();
    ((MockFileAccess) bfs).disableChecksum();

    HDHTWriter hds = new HDHTWriter();
    hds.setFileStore(bfs);
    hds.setFlushSize(2);
    hds.setFlushIntervalCount(1);
    hds.setup(new OperatorContextTestHelper.TestIdOperatorContext(1, new DefaultAttributeMap()));
    hds.writeExecutor = MoreExecutors.sameThreadExecutor();

    hds.beginWindow(1);
    hds.put(1, getLongByteArray(1), getLongByteArray(10).toByteArray());
    hds.endWindow();
    hds.checkpointed(1);

    hds.beginWindow(2);
    hds.put(1, getLongByteArray(1), getLongByteArray(20).toByteArray());
    hds.endWindow();
    hds.checkpointed(2);
    hds.committed(2);

    hds.beginWindow(3);
    hds.put(1, getLongByteArray(1), getLongByteArray(30).toByteArray());
    hds.endWindow();
    hds.checkpointed(3);
    hds.committed(3);

    hds.beginWindow(4);
    hds.endWindow();
    hds.checkpointed(4);
    hds.committed(4);

    /* The latest value is recovered from WAL */
    ByteBuffer bb = ByteBuffer.wrap(hds.get(1, getLongByteArray(1)));
    long l = bb.getLong();
    Assert.assertEquals("Value of 1 is recovered from WAL", 30, l);
}

From source file:org.opendaylight.lispflowmapping.lisp.serializer.MapNotifySerializer.java

public MapNotify deserialize(ByteBuffer notifyBuffer) {
    try {/*from   www .  ja  va 2s.  c  o m*/
        MapNotifyBuilder builder = new MapNotifyBuilder();
        builder.setMappingRecordItem(new ArrayList<MappingRecordItem>());

        byte typeAndFlags = notifyBuffer.get();
        boolean xtrSiteIdPresent = ByteUtil.extractBit(typeAndFlags, Flags.XTRSITEID);
        builder.setXtrSiteIdPresent(xtrSiteIdPresent);

        notifyBuffer.position(notifyBuffer.position() + Length.RES);
        builder.setMergeEnabled(ByteUtil.extractBit(notifyBuffer.get(), Flags.MERGE_ENABLED));

        byte recordCount = (byte) ByteUtil.getUnsignedByte(notifyBuffer);
        builder.setNonce(notifyBuffer.getLong());
        builder.setKeyId(notifyBuffer.getShort());
        short authenticationLength = notifyBuffer.getShort();
        byte[] authenticationData = new byte[authenticationLength];
        notifyBuffer.get(authenticationData);
        builder.setAuthenticationData(authenticationData);

        if (xtrSiteIdPresent) {
            List<MappingRecordBuilder> mrbs = new ArrayList<MappingRecordBuilder>();
            for (int i = 0; i < recordCount; i++) {
                mrbs.add(MappingRecordSerializer.getInstance().deserializeToBuilder(notifyBuffer));
            }
            byte[] xtrIdBuf = new byte[MapRegisterSerializer.Length.XTRID_SIZE];
            notifyBuffer.get(xtrIdBuf);
            XtrId xtrId = new XtrId(xtrIdBuf);
            byte[] siteIdBuf = new byte[MapRegisterSerializer.Length.SITEID_SIZE];
            notifyBuffer.get(siteIdBuf);
            SiteId siteId = new SiteId(siteIdBuf);
            builder.setXtrId(xtrId);
            builder.setSiteId(siteId);
            for (MappingRecordBuilder mrb : mrbs) {
                mrb.setXtrId(xtrId);
                mrb.setSiteId(siteId);
                builder.getMappingRecordItem()
                        .add(new MappingRecordItemBuilder().setMappingRecord(mrb.build()).build());
            }
        } else {
            for (int i = 0; i < recordCount; i++) {
                builder.getMappingRecordItem()
                        .add(new MappingRecordItemBuilder()
                                .setMappingRecord(
                                        MappingRecordSerializer.getInstance().deserialize(notifyBuffer))
                                .build());
            }
        }

        notifyBuffer.limit(notifyBuffer.position());
        return builder.build();
    } catch (RuntimeException re) {
        throw new LispSerializationException(
                "Couldn't deserialize Map-Notify (len=" + notifyBuffer.capacity() + ")", re);
    }
}

From source file:voldemort.store.cachestore.impl.LogChannel.java

public KeyValue readRecord(int recordNo) {
    if (isEOF(recordNo))
        throw new RuntimeException("record out of range " + getTotalRecord() + " expected " + recordNo);
    ByteBuffer buf = ByteBuffer.allocate(RECORD_SIZE);
    long pos = ChannelStore.OFFSET + (long) recordNo * RECORD_SIZE;
    try {// w w  w . j  av a 2  s .  co m
        if (isLastRecord(recordNo)) {
            logger.info("skip due to " + totalRecord + " read " + recordNo);
            return null;
        }
        getIndexChannel().read(buf, pos);
        assert (buf.capacity() == RECORD_SIZE);
        buf.rewind();
        byte status = buf.get();
        long keyOffset2Len = buf.getLong();
        byte[] keys = readChannel(keyOffset2Len, getKeyChannel());
        Key k = toKey(keys);
        long dataOffset2Len = buf.getLong();
        byte[] datas = readChannel(dataOffset2Len, getDataChannel());
        long block2version = buf.getLong();
        Value<byte[]> value = null;
        //if delete return value=null, not delete read value
        if (!isDeleted(status))
            value = new BlockValue<byte[]>(datas, BlockUtil.getVersionNo(block2version), (short) 0);
        return new KeyValue(k, value);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:org.apache.carbondata.sdk.file.CSVCarbonWriterTest.java

@Test
public void testBlocklet() throws IOException {
    String path = "./testWriteFiles";
    FileUtils.deleteDirectory(new File(path));

    Field[] fields = new Field[2];
    fields[0] = new Field("name", DataTypes.STRING);
    fields[1] = new Field("age", DataTypes.INT);

    TestUtil.writeFilesAndVerify(1000000, new Schema(fields), path, new String[] { "name" }, 3, 8);

    // read footer and verify number of blocklets
    CarbonFile folder = FileFactory.getCarbonFile(path);
    List<CarbonFile> files = folder.listFiles(true);
    List<CarbonFile> dataFiles = new LinkedList<>();
    for (CarbonFile file : files) {
        if (file.getName().endsWith(CarbonTablePath.CARBON_DATA_EXT)) {
            dataFiles.add(file);/*w  ww  . ja  v  a  2 s  . c om*/
        }
    }
    for (CarbonFile dataFile : dataFiles) {
        FileReader fileReader = FileFactory.getFileHolder(FileFactory.getFileType(dataFile.getPath()));
        ByteBuffer buffer = fileReader.readByteBuffer(FileFactory.getUpdatedFilePath(dataFile.getPath()),
                dataFile.getSize() - 8, 8);
        fileReader.finish();
        CarbonFooterReaderV3 footerReader = new CarbonFooterReaderV3(dataFile.getAbsolutePath(),
                buffer.getLong());
        FileFooter3 footer = footerReader.readFooterVersion3();
        Assert.assertEquals(2, footer.blocklet_index_list.size());
        Assert.assertEquals(2, footer.blocklet_info_list3.size());
    }
    FileUtils.deleteDirectory(new File(path));
}

From source file:com.mellanox.jxio.EventQueueHandler.java

private boolean handleEvent(ByteBuffer eventQueue) {

    Eventable eventable;/*w  w w. j a v  a  2  s .c  o  m*/
    int eventType = eventQueue.getInt();
    long id = eventQueue.getLong();
    boolean userNotified = false;
    switch (eventType) {

    case 0: // session error event
    {
        int errorType = eventQueue.getInt();
        int reason = eventQueue.getInt();
        EventSession evSes = new EventSession(eventType, id, errorType, reason);
        synchronized (eventables) {
            eventable = eventables.get(id);
        }
        if (eventable == null) {
            LOG.warn(
                    this.toLogString() + "eventable with id " + Long.toHexString(id) + " was not found in map");
            break;
        }
        userNotified = eventable.onEvent(evSes);
    }
        break;

    case 1: // msg error server
    {

        // msg was added to msgsPendingNewRequest after sendResponce. the real lookup of the Msg is done on C
        // side. msgsPendingNewRequest is used for look up of the java object based on the id
        Msg msg = this.msgsPendingNewRequest.remove(id);
        final long session_id = eventQueue.getLong();
        final int reason = eventQueue.getInt();
        eventable = eventables.get(session_id);
        if (eventable == null) {
            LOG.warn(this.toLogString() + "eventable with id " + Long.toHexString(session_id)
                    + " was not found in map");
            break;
        }
        EventMsgError evMsgErr = new EventMsgError(eventType, id, msg, reason);
        userNotified = eventable.onEvent(evMsgErr);
    }
        break;

    case 2: // msg error client
    {
        Msg msg = msgsPendingReply.remove(id);
        final int reason = eventQueue.getInt();
        EventMsgError evMsgErr = new EventMsgError(eventType, id, msg, reason);
        eventable = msg.getClientSession();
        userNotified = eventable.onEvent(evMsgErr);

    }
        break;

    case 3: // session established
    {
        EventSessionEstablished evSesEstab = new EventSessionEstablished(eventType, id);
        eventable = eventables.get(id);
        if (eventable == null) {
            LOG.warn(
                    this.toLogString() + "eventable with id " + Long.toHexString(id) + " was not found in map");
            break;
        }
        userNotified = eventable.onEvent(evSesEstab);
    }
        break;

    case 4: // on request (server side)
    {
        Msg msg = this.msgsPendingNewRequest.remove(id);
        msg.resetPositions();
        final int msg_in_size = eventQueue.getInt();
        msg.getIn().limit(msg_in_size);
        int msg_out_size = eventQueue.getInt();
        if (msg_out_size > msg.getOut().capacity())
            msg_out_size = msg.getOut().capacity();
        msg.getOut().limit(msg_out_size);
        final long session_id = eventQueue.getLong();
        if (LOG.isTraceEnabled()) {
            LOG.trace(this.toLogString() + "session refToCObject " + Long.toHexString(session_id));
        }
        eventable = eventables.get(session_id);
        if (eventable == null) {
            LOG.warn(this.toLogString() + "eventable with id " + Long.toHexString(session_id)
                    + " was not found in map");
            break;
        }
        EventNewMsg evMsg = new EventNewMsg(eventType, id, msg);
        userNotified = eventable.onEvent(evMsg);
    }
        break;

    case 5: // on response (client side)
    {
        Msg msg = msgsPendingReply.remove(id);
        final int msg_size = eventQueue.getInt();
        msg.getIn().limit(msg_size);
        if (LOG.isTraceEnabled()) {
            LOG.trace(this.toLogString() + "got msg " + msg);
        }
        EventNewMsg evMsg = new EventNewMsg(eventType, id, msg);
        eventable = msg.getClientSession();
        if (LOG.isTraceEnabled()) {
            LOG.trace(this.toLogString() + "eventable is " + eventable);
        }
        userNotified = eventable.onEvent(evMsg);
    }
        break;

    case 6: // on new session
    {
        long ptrSes = eventQueue.getLong();
        String uri = readString(eventQueue);
        String srcIP = readString(eventQueue);

        synchronized (eventables) {
            eventable = eventables.get(id);
        }
        if (eventable == null) {
            LOG.warn(
                    this.toLogString() + "eventable with id " + Long.toHexString(id) + " was not found in map");
            break;
        }
        EventNewSession evNewSes = new EventNewSession(eventType, id, ptrSes, uri, srcIP);
        userNotified = eventable.onEvent(evNewSes);
    }
        break;

    case 8: // on fd ready
    {
        /*
         * int fd = eventQueue.getInt(); int events = eventQueue.getInt();
         */
        LOG.error(this.toLogString() + "received FD Ready event - not handled");
    }
        break;

    default:
        LOG.error(this.toLogString() + "received an unknown event " + eventType);
        // TODO: throw exception
    }
    return userNotified;
}

From source file:com.datatorrent.contrib.hdht.WALTest.java

/**
* checkpointed(1)  1 -> 10//  w  ww  .  ja  v a2s  . com
* checkpointed(2)  1 -> 20
* checkpointed(3)  1 -> 30
* checkpointed(4)  1 -> 40
* committed(2)
* checkpointed(5)
*
* restore from 3rd checkpoint.
* do a get and value should be 30.
*/
@Test
public void testWalRecoveryValues() throws IOException {
    File file = new File("target/hds");
    FileUtils.deleteDirectory(file);

    FileAccessFSImpl bfs = new MockFileAccess();
    bfs.setBasePath(file.getAbsolutePath());
    bfs.init();
    ((MockFileAccess) bfs).disableChecksum();

    FileAccessFSImpl walfs = new MockFileAccess();
    walfs.setBasePath(file.getAbsolutePath());
    walfs.init();
    ((MockFileAccess) walfs).disableChecksum();

    HDHTWriter hds = new HDHTWriter();
    hds.setFileStore(bfs);
    hds.setWalStore(walfs);
    hds.setFlushSize(1);
    hds.setFlushIntervalCount(1);
    hds.setup(new OperatorContextTestHelper.TestIdOperatorContext(1, new DefaultAttributeMap()));
    hds.writeExecutor = MoreExecutors.sameThreadExecutor();

    hds.beginWindow(1);
    hds.put(1, getLongByteArray(1), getLongByteArray(10).toByteArray());
    hds.endWindow();
    hds.checkpointed(1);

    hds.beginWindow(2);
    hds.put(1, getLongByteArray(1), getLongByteArray(20).toByteArray());
    hds.endWindow();
    hds.checkpointed(2);

    hds.beginWindow(3);
    hds.put(1, getLongByteArray(1), getLongByteArray(30).toByteArray());
    hds.endWindow();
    hds.checkpointed(3);

    // Commit window id 2
    hds.committed(2);
    // use checkpoint after window 3 for recovery.
    HDHTWriter newOperator = KryoCloneUtils.cloneObject(new Kryo(), hds);

    hds.beginWindow(4);
    hds.put(1, getLongByteArray(1), getLongByteArray(40).toByteArray());
    hds.put(1, getLongByteArray(2), getLongByteArray(200).toByteArray());
    hds.endWindow();
    hds.checkpointed(4);

    hds.beginWindow(5);
    hds.put(1, getLongByteArray(1), getLongByteArray(50).toByteArray());
    hds.put(1, getLongByteArray(2), getLongByteArray(210).toByteArray());
    hds.endWindow();
    hds.checkpointed(5);
    hds.forceWal();

    /* Simulate recovery after failure, checkpoint is restored to after
       processing of window 3.
     */
    newOperator.setFlushIntervalCount(1);
    newOperator.setFileStore(bfs);
    newOperator.setWalStore(bfs);
    newOperator.setFlushSize(1);
    newOperator.setup(new OperatorContextTestHelper.TestIdOperatorContext(1, new DefaultAttributeMap()));
    newOperator.writeExecutor = MoreExecutors.sameThreadExecutor();

    // This should run recovery, as first tuple is added in bucket
    newOperator.beginWindow(4);
    newOperator.put(1, getLongByteArray(1), getLongByteArray(40).toByteArray());
    newOperator.put(1, getLongByteArray(2), getLongByteArray(200).toByteArray());
    // current tuple, being added is put into write cache.
    Assert.assertEquals("Number of tuples in write cache ", 2, newOperator.unflushedDataSize(1));
    // one tuples are put in to committed write cache.
    Assert.assertEquals("Number of tuples in committed cache ", 1, newOperator.committedDataSize(1));
    newOperator.endWindow();
    newOperator.checkpointed(4);
    newOperator.forceWal();
    /* The latest value is recovered from WAL */
    ByteBuffer bb = ByteBuffer.wrap(newOperator.getUncommitted(1, getLongByteArray(1)));
    long l = bb.getLong();
    Assert.assertEquals("Value of 1 is recovered from WAL", 40, l);

    newOperator.committed(3);

    bb = ByteBuffer.wrap(newOperator.get(1, getLongByteArray(1)));
    l = bb.getLong();
    Assert.assertEquals("Value is persisted ", 30, l);

    newOperator.committed(4);
    bb = ByteBuffer.wrap(newOperator.get(1, getLongByteArray(1)));
    l = bb.getLong();
    Assert.assertEquals("Value is persisted ", 40, l);
}

From source file:org.opendaylight.controller.topology.web.Topology.java

/**
 * Add regular hosts to main topology//from w w w .j a  v a2  s .co m
 *
 * @param hostEdges - node-nodeconnectors host-specific mapping from topology
 * @param topology - topology instance
 */
private void addHostNodes(Map<Node, Set<NodeConnector>> hostEdges, ITopologyManager topology,
        String containerName) {
    for (Map.Entry<Node, Set<NodeConnector>> e : hostEdges.entrySet()) {
        for (NodeConnector connector : e.getValue()) {
            List<Host> hosts = topology.getHostsAttachedToNodeConnector(connector);
            for (Host host : hosts) {
                EthernetAddress dmac = (EthernetAddress) host.getDataLayerAddress();

                ByteBuffer addressByteBuffer = ByteBuffer.allocate(8);
                addressByteBuffer.putShort((short) 0);
                addressByteBuffer.put(dmac.getValue());
                addressByteBuffer.rewind();

                long hid = addressByteBuffer.getLong();
                String hostId = String.valueOf(hid);

                NodeBean hostBean = new NodeBean(hostId, host.getNetworkAddressAsString(), NodeType.HOST);
                List<Map<String, Object>> adjacencies = new LinkedList<Map<String, Object>>();
                EdgeBean edge = new EdgeBean(connector, hid);
                adjacencies.add(edge.out());
                hostBean.setLinks(adjacencies);

                if (metaCache.get(containerName).containsKey(hostId)) {
                    Map<String, Object> hostEntry = metaCache.get(containerName).get(hostId);
                    hostEntry.put("adjacencies", adjacencies);
                    stagedNodes.put(hostId, hostEntry);
                } else {
                    newNodes.put(String.valueOf(hid), hostBean.out());
                }
            }
        }
    }
}