Package net.tomp2p.storage

Examples of net.tomp2p.storage.DataBuffer


                } else {
                    // get the checksums
                  // TODO: don't copy data, toBytes does a copy!
                    List<Checksum> checksums = RSync.checksums(data.toBytes(), blockSize);
                    AlternativeCompositeByteBuf abuf = AlternativeCompositeByteBuf.compBuffer();
                    DataBuffer dataBuffer = SyncUtils.encodeChecksum(checksums, entry.getKey().versionKey(), data.hash(), abuf);
                    retVal.put(entry.getKey(), new Data(dataBuffer));
                    LOG.debug("sync required hash = {}", data.hash());
                }
            } else {
              if(isSyncFromOldVersion) {
                //TODO: the client could send us his history to figure out what the latest version in this history is
                Entry<Number640, Data> latest = storageLayer.
                    get(entry.getKey().minVersionKey(), entry.getKey().maxVersionKey(), 1, false).lastEntry();
                // TODO: don't copy data, toBytes does a copy!
                List<Checksum> checksums = RSync.checksums(latest.getValue().toBytes(), blockSize);
                AlternativeCompositeByteBuf abuf = AlternativeCompositeByteBuf.compBuffer();
                    DataBuffer dataBuffer = SyncUtils.encodeChecksum(checksums, latest.getKey().versionKey(),
                        latest.getValue().hash(), abuf);
                    retVal.put(entry.getKey(), new Data(dataBuffer));
                    LOG.debug("sync required for version");
              } else {
                // not found
View Full Code Here


                    if (dataOld == null || !dataOld.hash().equals(hash)) {
                        continue;
                    }
                    // TODO: don't copy data, toBytes does a copy!
                    DataBuffer reconstructedValue = RSync.reconstruct(dataOld.toBytes(), instructions, blockSize);
                    //TODO: domain protection?, make the flags configurable
                    Enum<?> status = storageLayer.put(entry.getKey(), new Data(reconstructedValue), publicKey, false, false);
                    if (status == PutStatus.OK) {
                        retVal.add(entry.getKey());
                        if (replicationListener != null) {
View Full Code Here

          result.add(new Instruction(reference));
        } else {
          //otherwise the header is the length
          final int length = header;
          final int remaining = Math.min(length, buf.readableBytes());
          DataBuffer literal = new DataBuffer(buf.slice(buf.readerIndex(), remaining));
          buf.skipBytes(remaining);
          result.add(new Instruction(literal));
        }
      }
      return result;
View Full Code Here

    buf.writeBytes(hash.toByteArray());
        for(Checksum checksum:checksums) {
          buf.writeInt(checksum.weakChecksum());
          buf.writeBytes(checksum.strongChecksum());
        }
        return new DataBuffer(buf);
  }
View Full Code Here

                                         data2.toBytes(), checksums, blockSize);
                           
                            AlternativeCompositeByteBuf abuf = AlternativeCompositeByteBuf.compBuffer();
                           
                            dataCopy += SyncUtils.encodeInstructions(instructions, versionKey, hash, abuf);
                            DataBuffer dataBuffer = new DataBuffer(abuf);
                            //diff
                            Data data1 = new Data(dataBuffer).flag1();
                                retVal.put(entry.getKey(), data1);                       
                          }
                        }
View Full Code Here

    for (;;) {
      final int wcs = adler.value();
      final int reference = matches(wcs, array, offset, remaining, checksums);
      if (reference != -1) {
        if (offset > lastRefFound) {
          result.add(new Instruction(new DataBuffer(array, lastRefFound, offset - lastRefFound)));
        }
        result.add(new Instruction(reference));

        offset += remaining;
        lastRefFound = offset;
        remaining = Math.min(blockSize, length - offset);
        if (remaining == 0) {
          break;
        }
        adler.reset().update(array, offset, remaining);
      } else {
        offset++;
        if (blockSize > length - offset) {
          break;
        }
        adler.updateRolling(array);
      }
    }

    if (length > lastRefFound) {
      result.add(new Instruction(new DataBuffer(array, lastRefFound, length - lastRefFound)));
    }

    return result;
  }
View Full Code Here

   * @param blockSize
   *            The offset size
   * @return The value which is identical to the responsible peer's value
   */
  public static DataBuffer reconstruct(byte[] value, List<Instruction> instructions, int blockSize) {
    DataBuffer result = new DataBuffer();
    for (Instruction instruction : instructions) {
      int ref = instruction.reference();
      if (ref != -1) {
        int offset = blockSize * ref;
        int remaining = Math.min(blockSize, value.length - offset);
        result.add(new DataBuffer(value, offset, remaining));
      } else {
        result.add(instruction.literal());
      }
    }
    return result;
  }
View Full Code Here

    }

    public static Number160 makeSHAHash(DataBuffer buffer) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            DataBuffer copy = buffer.shallowCopy();
            for (ByteBuffer byteBuffer : copy.bufferList()) {
                md.update(byteBuffer);
            }
            byte[] digest = md.digest();
            return new Number160(digest);
        } catch (NoSuchAlgorithmException e) {
View Full Code Here

        }
        if (bufferSize == -1) {
          bufferSize = buf.readInt();
        }
        if (buffer == null) {
          buffer = new DataBuffer();
        }
       
        final int already = buffer.alreadyTransferred();
        final int remaining = bufferSize - already;
        // already finished
View Full Code Here

    int size = 6;
    byte[] oldValue = "ZurichGenevaLuganoAAA".getBytes();
    byte[] newValue = "AzurichGenevaLuganoAbbLuganoAAA".getBytes();
    List<Checksum> checksums = RSync.checksums(oldValue, size);
    List<Instruction> instructions = RSync.instructions(newValue, checksums, size);
    DataBuffer reconstructedValue = RSync.reconstruct(oldValue, instructions, size);
    Assert.assertArrayEquals(newValue, reconstructedValue.bytes());
  }
View Full Code Here

TOP

Related Classes of net.tomp2p.storage.DataBuffer

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.