Esempio n. 1
0
  /* This method is thread safe */
  public Object lookup(long pos) {
    // A negative position can result from a log replay (which does not re-log, but does
    // update the version map.  This is OK since the node won't be ACTIVE when this happens.
    if (pos < 0) return null;

    try {
      // make sure any unflushed buffer has been flushed
      synchronized (this) {
        // TODO: optimize this by keeping track of what we have flushed up to
        fos.flushBuffer();
        /**
         * * System.out.println("###flushBuffer to " + fos.size() + " raf.length()=" + raf.length()
         * + " pos="+pos); if (fos.size() != raf.length() || pos >= fos.size() ) { throw new
         * RuntimeException("ERROR" + "###flushBuffer to " + fos.size() + " raf.length()=" +
         * raf.length() + " pos="+pos); } *
         */
      }

      ChannelFastInputStream fis = new ChannelFastInputStream(channel, pos);
      LogCodec codec = new LogCodec(resolver);
      return codec.readVal(fis);
    } catch (IOException e) {
      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
    }
  }
Esempio n. 2
0
 public void marshal(Object nl, OutputStream os) throws IOException {
   assert !alreadyMarshalled;
   init(FastOutputStream.wrap(os));
   try {
     daos.writeByte(VERSION);
     writeVal(nl);
   } finally {
     daos.flushBuffer();
     alreadyMarshalled = true;
   }
 }
Esempio n. 3
0
  public void finish(UpdateLog.SyncLevel syncLevel) {
    if (syncLevel == UpdateLog.SyncLevel.NONE) return;
    try {
      synchronized (this) {
        fos.flushBuffer();
      }

      if (syncLevel == UpdateLog.SyncLevel.FSYNC) {
        // Since fsync is outside of synchronized block, we can end up with a partial
        // last record on power failure (which is OK, and does not represent an error...
        // we just need to be aware of it when reading).
        raf.getFD().sync();
      }

    } catch (IOException e) {
      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
    }
  }
  @Test
  public void testgzip() throws Exception {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    FastOutputStream fos = new FastOutputStream(b);
    GZIPOutputStream gzos = new GZIPOutputStream(fos);
    String ss = "Helloooooooooooooooooooo";
    writeChars(gzos, ss, 0, ss.length());
    gzos.close();
    JavaBinCodec.writeVInt(10, fos);
    fos.flushBuffer();
    GZIPInputStream gzis =
        new GZIPInputStream(new ByteArrayInputStream(b.toByteArray(), 0, b.size()));
    char[] cbuf = new char[ss.length()];
    readChars(gzis, cbuf, 0, ss.length());
    assertEquals(new String(cbuf), ss);
    // System.out.println("passes w/o FastInputStream");

    ByteArrayInputStream bis = new ByteArrayInputStream(b.toByteArray(), 0, b.size());
    gzis = new GZIPInputStream(new FastInputStream(bis));
    cbuf = new char[ss.length()];
    readChars(gzis, cbuf, 0, ss.length());
    assertEquals(new String(cbuf), ss);
    // System.out.println("passes w FastInputStream");
  }
Esempio n. 5
0
 @Override
 public void close() throws IOException {
   flushBuffer();
   if (out != null) out.close();
 }
Esempio n. 6
0
 /** reserve at least len bytes at the end of the buffer. Invalid if len &gt; buffer.length */
 public void reserve(int len) throws IOException {
   if (len > (buf.length - pos)) flushBuffer();
 }