Package org.xtreemfs.foundation.buffer

Examples of org.xtreemfs.foundation.buffer.ReusableBuffer


        int minChunkSize = 512;
       
        int start = random.nextInt((int) (maxTestFileSize - minChunkSize));
        int end = random.nextInt((int) (maxTestFileSize - minChunkSize - start))
                        + minChunkSize + start;
        ReusableBuffer result = client.chunk(testFileName, start, end).get();
        assertEquals(end - start, result.remaining());
       
        byte[] testData = new byte[end - start];
        FileInputStream sIn = new FileInputStream(testFileName);
        sIn.skip(start);
        sIn.read(testData, 0, end - start);
        sIn.close();
       
        assertEquals(new String(testData), new String(result.array()));
       
        // clean up
        BufferPool.free(result);
    }
View Full Code Here


    @Test
    public void testReplicateRequest() throws Exception {
       
        // serialize the request
        Checksum csumAlgo = new CRC32();
        ReusableBuffer data = testEntry.serialize(csumAlgo);
        csumAlgo.reset();
       
        client.replicate(testLSN, data).get();
    }
View Full Code Here

                                  request.getStart().getSequenceNo());
        final LSN end = new LSN(request.getEnd().getViewId(),
                                request.getEnd().getSequenceNo());
       
        LogEntries.Builder result = LogEntries.newBuilder();
        ReusableBuffer resultPayLoad = BufferPool.allocate(0);
       
        Logging.logMessage(Logging.LEVEL_INFO, this, "REQUEST received " +
                "(start: %s, end: %s) from %s", start.toString(),
                end.toString(), rq.getSenderAddress().toString());
       
        // enhancement to prevent slaves from loading the DB from the master unnecessarily
        if (start.equals(lastOnView.get())) {
           
            Logging.logMessage(Logging.LEVEL_INFO, this,
                   "REQUEST answer is empty (there has been a failover only).");
           
            rq.sendSuccess(result.build());
            return
        }
       
        final LSN firstEntry = new LSN(start.getViewId(),
                                       start.getSequenceNo() + 1L);
       
        assert (firstEntry.compareTo(end) < 0) :
            "At least one LogEntry has to be requested!";
       
        DiskLogIterator it = null;
        LogEntry le = null;
        synchronized (babuInterface.getCheckpointerLock()) {
            try {  
                // wait, if there is a checkpoint in proceeding
                babuInterface.waitForCheckpoint();
               
                it = fileIO.getLogEntryIterator(start);
               
                while (it.hasNext() &&
                       result.getLogEntriesCount() <
                       MAX_LOGENTRIES_PER_REQUEST && (le = it.next())
                           .getLSN().compareTo(end) < 0) {
                   
                    try {
                        // we are not at the right position yet -> skip
                        if (le.getLSN().compareTo(firstEntry) < 0) continue;
                   
                        // the first entry was not available ... bad
                        if (le.getLSN().compareTo(firstEntry) > 0 &&
                            result.getLogEntriesCount() == 0) {
                            break;
                        }
                         
                        // add the logEntry to result list
                        assert (le.getPayload().array().length > 0) :
                            "Empty log-entries are not allowed!";
                        ReusableBuffer buf = le.serialize(checksum);
                        result.addLogEntries(
                                org.xtreemfs.babudb.pbrpc.GlobalTypes.LogEntry
                                .newBuilder().setLength(buf.remaining()));
                        int newSize = resultPayLoad.remaining() +
                                      buf.remaining();
                        if (!resultPayLoad.enlarge(newSize)) {
                            ReusableBuffer tmp = BufferPool.allocate(newSize);
                           
                            tmp.put(resultPayLoad);
                            BufferPool.free(resultPayLoad);
                            resultPayLoad = tmp;
                        }
                        resultPayLoad.put(buf);
                        BufferPool.free(buf);
View Full Code Here

        Logging.logMessage(Logging.LEVEL_INFO, this,
                "%s request received from %s", chunk.toString(),
                rq.getSenderAddress().toString());
       
        FileChannel channel = null;
        ReusableBuffer payload = null;
        try {
            // get the requested chunk
            channel = new FileInputStream(chunk.getFileName()).getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(length);
            if (channel.read(buffer, chunk.getStart()) != length)
                throw new Exception();
           
            buffer.flip();
            payload = new ReusableBuffer(buffer);
            rq.sendSuccess(ErrorCodeResponse.getDefaultInstance(), payload);
           
        } catch (Exception e) {
           
            if (e.getMessage() == null) {
View Full Code Here

        }
       
        //dezerialize payload (serialized logEntry)
        if (resp == null) {
           
            ReusableBuffer data = rq.getData().createViewBuffer();
            try {
                rq.setAttachment(LogEntry.deserialize(data, checksum));
            } catch (LogEntryException e){
                Logging.logError(Logging.LEVEL_WARN, this, e);
                resp = ErrorResponse.newBuilder()
View Full Code Here

            // if the block size limit has been reached, or there are no more
            // key-value pairs, serialize the block and write it to disk
            if (entryCount % maxBlockEntries == 0 || !iterator.hasNext()) {
               
                // serialize the offset of the block into a new buffer
                ReusableBuffer buf = ReusableBuffer.wrap(new byte[(Integer.SIZE / 8) + (Short.SIZE / 8)]);
                buf.putInt(blockOffset);
                buf.putShort(blockFileId);
               
                // add the key-offset mapping to the block index
                blockIndex.add(InternalBufferUtil.toBuffer(block.getBlockKey()), buf.array());
               
                // serialize the block and calculate the next block offset
                SerializedBlock serializedBlock = block.serialize();
                blockOffset += serializedBlock.size();
               
View Full Code Here

   
    public String toString() {
       
        buf.position(offset);
        buf.limit(limit);
        ReusableBuffer newBuf = BufferPool.allocate(limit - buf.position());
        newBuf.put(buf);
        String result = OutputUtils.byteArrayToFormattedHexString(newBuf.array());
        BufferPool.free(newBuf);
        buf.clear();
       
        return result;
    }
View Full Code Here

   
    public String toString() {
       
        buf.position(offset);
        buf.limit(offsetListStart + numEntries * Integer.SIZE / 8);
        ReusableBuffer newBuf = BufferPool.allocate(buf.limit() - buf.position());
        newBuf.put(buf);
        String result = OutputUtils.byteArrayToFormattedHexString(newBuf.array());
        BufferPool.free(newBuf);
        buf.clear();
       
        return result;
    }
View Full Code Here

    public ReusableBuffer serialize(Checksum csumAlgo) {
        assert (viewId > 0);
        assert (logSequenceNo > 0);
       
        final int bufSize = headerLength + payload.remaining();
        ReusableBuffer buf = BufferPool.allocate(bufSize);
        buf.putInt(bufSize);
        buf.putInt(checksum);
        buf.putInt(viewId);
        buf.putLong(logSequenceNo);
        buf.put(payloadType);
        buf.put(payload);
        payload.flip(); // otherwise payload is not reusable
        buf.putInt(bufSize);
        buf.flip();
       
        if (USE_CHECKSUMS) {
            // reset the old checksum to 0, before calculating a new one
            buf.position(Integer.SIZE / 8);
            buf.putInt(0);
            buf.position(0);
           
            csumAlgo.update(buf.array(), 0, buf.limit());
            int cPos = buf.position();
           
            // write the checksum to the buffer
            buf.position(Integer.SIZE / 8);
            buf.putInt((int) csumAlgo.getValue());
            buf.position(cPos);
        }
       
        return buf;
    }
View Full Code Here

        e.viewId = data.getInt();
        e.logSequenceNo = data.getLong();
        e.payloadType = data.get();
        final int payloadSize = bufSize - headerLength;
        int payloadPosition = data.position();
        ReusableBuffer payload = data.createViewBuffer();
        payload.range(payloadPosition, payloadSize);
        e.payload = payload;
       
        if (USE_CHECKSUMS) {
            // reset the old checksum to 0, before calculating a new one
            data.position(Integer.SIZE / 8);
View Full Code Here

TOP

Related Classes of org.xtreemfs.foundation.buffer.ReusableBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.