Java Code Examples for org.apache.lucene.store.DataInput#readString()

The following examples show how to use org.apache.lucene.store.DataInput#readString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleServer.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
static Map<String,FileMetaData> readFilesMetaData(DataInput in) throws IOException {
  int fileCount = in.readVInt();
  //System.out.println("readFilesMetaData: fileCount=" + fileCount);
  Map<String,FileMetaData> files = new HashMap<>();
  for(int i=0;i<fileCount;i++) {
    String fileName = in.readString();
    //System.out.println("readFilesMetaData: fileName=" + fileName);
    long length = in.readVLong();
    long checksum = in.readVLong();
    byte[] header = new byte[in.readVInt()];
    in.readBytes(header, 0, header.length);
    byte[] footer = new byte[in.readVInt()];
    in.readBytes(footer, 0, footer.length);
    files.put(fileName, new FileMetaData(header, footer, length, checksum));
  }
  return files;
}
 
Example 2
Source File: SimpleTransLog.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void replayAddDocument(Connection c, NodeProcess primary, DataInput in) throws IOException {
  String id = in.readString();

  Document doc = new Document();
  doc.add(new StringField("docid", id, Field.Store.YES));

  String title = readNullableString(in);
  if (title != null) {
    doc.add(new StringField("title", title, Field.Store.NO));
    doc.add(new TextField("titleTokenized", title, Field.Store.NO));
  }
  String body = readNullableString(in);
  if (body != null) {
    doc.add(new TextField("body", body, Field.Store.NO));
  }
  String marker = readNullableString(in);
  if (marker != null) {
    //TestStressNRTReplication.message("xlog: replay marker=" + id);
    doc.add(new StringField("marker", marker, Field.Store.YES));
  }

  // For both add and update originally, we use updateDocument to replay,
  // because the doc could in fact already be in the index:
  // nocomit what if this fails?
  primary.addOrUpdateDocument(c, doc, false);
}
 
Example 3
Source File: SimplePrimaryNode.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void handleAddDocument(DataInput in, DataOutput out) throws IOException {
  int fieldCount = in.readVInt();
  Document doc = new Document();
  for(int i=0;i<fieldCount;i++) {
    String name = in.readString();
    String value = in.readString();
    // NOTE: clearly NOT general!
    if (name.equals("docid") || name.equals("marker")) {
      doc.add(new StringField(name, value, Field.Store.YES));
    } else if (name.equals("title")) {
      doc.add(new StringField("title", value, Field.Store.YES));
      doc.add(new Field("titleTokenized", value, tokenizedWithTermVectors));
    } else if (name.equals("body")) {
      doc.add(new Field("body", value, tokenizedWithTermVectors));
    } else {
      throw new IllegalArgumentException("unhandled field name " + name);
    }
  }
  writer.addDocument(doc);
}
 
Example 4
Source File: SimplePrimaryNode.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void handleUpdateDocument(DataInput in, DataOutput out) throws IOException {
  int fieldCount = in.readVInt();
  Document doc = new Document();
  String docid = null;
  for(int i=0;i<fieldCount;i++) {
    String name = in.readString();
    String value = in.readString();
    // NOTE: clearly NOT general!
    if (name.equals("docid")) {
      docid = value;
      doc.add(new StringField("docid", value, Field.Store.YES));
    } else if (name.equals("marker")) {
      doc.add(new StringField("marker", value, Field.Store.YES));
    } else if (name.equals("title")) {
      doc.add(new StringField("title", value, Field.Store.YES));
      doc.add(new Field("titleTokenized", value, tokenizedWithTermVectors));
    } else if (name.equals("body")) {
      doc.add(new Field("body", value, tokenizedWithTermVectors));
    } else {
      throw new IllegalArgumentException("unhandled field name " + name);
    }
  }

  writer.updateDocument(new Term("docid", docid), doc);
}
 
Example 5
Source File: FuzzySet.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static FuzzySet deserialize(DataInput in) throws IOException
{
  int version=in.readInt();
  if (version == VERSION_SPI) {
    in.readString();
  }
  final HashFunction hashFunction = hashFunctionForVersion(version);
  int bloomSize=in.readInt();
  int numLongs=in.readInt();
  long[]longs=new long[numLongs];
  for (int i = 0; i < numLongs; i++) {
    longs[i]=in.readLong();
  }
  FixedBitSet bits = new FixedBitSet(longs,bloomSize+1);
  return new FuzzySet(bits,bloomSize,hashFunction);
}
 
Example 6
Source File: TSTLookup.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void readRecursively(DataInput in, TernaryTreeNode node) throws IOException {
  node.splitchar = in.readString().charAt(0);
  byte mask = in.readByte();
  if ((mask & HAS_TOKEN) != 0) {
    node.token = in.readString();
  }
  if ((mask & HAS_VALUE) != 0) {
    node.val = Long.valueOf(in.readLong());
  }
  if ((mask & LO_KID) != 0) {
    node.loKid = new TernaryTreeNode();
    readRecursively(in, node.loKid);
  }
  if ((mask & EQ_KID) != 0) {
    node.eqKid = new TernaryTreeNode();
    readRecursively(in, node.eqKid);
  }
  if ((mask & HI_KID) != 0) {
    node.hiKid = new TernaryTreeNode();
    readRecursively(in, node.hiKid);
  }
}
 
Example 7
Source File: CodecUtil.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** Like {@link
 *  #checkHeader(DataInput,String,int,int)} except this
 *  version assumes the first int has already been read
 *  and validated from the input. */
public static int checkHeaderNoMagic(DataInput in, String codec, int minVersion, int maxVersion) throws IOException {
  final String actualCodec = in.readString();
  if (!actualCodec.equals(codec)) {
    throw new CorruptIndexException("codec mismatch: actual codec=" + actualCodec + " vs expected codec=" + codec, in);
  }

  final int actualVersion = in.readInt();
  if (actualVersion < minVersion) {
    throw new IndexFormatTooOldException(in, actualVersion, minVersion, maxVersion);
  }
  if (actualVersion > maxVersion) {
    throw new IndexFormatTooNewException(in, actualVersion, minVersion, maxVersion);
  }

  return actualVersion;
}
 
Example 8
Source File: SortedNumericSortField.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public SortField readSortField(DataInput in) throws IOException {
  SortedNumericSortField sf = new SortedNumericSortField(in.readString(), readType(in), in.readInt() == 1, readSelectorType(in));
  if (in.readInt() == 1) {
    switch (sf.type) {
      case INT:
        sf.setMissingValue(in.readInt());
        break;
      case LONG:
        sf.setMissingValue(in.readLong());
        break;
      case FLOAT:
        sf.setMissingValue(NumericUtils.sortableIntToFloat(in.readInt()));
        break;
      case DOUBLE:
        sf.setMissingValue(NumericUtils.sortableLongToDouble(in.readLong()));
        break;
      default:
        throw new AssertionError();
    }
  }
  return sf;
}
 
Example 9
Source File: SimpleTransLog.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private String readNullableString(DataInput in) throws IOException {
  byte b = in.readByte();
  if (b == 0) {
    return null;
  } else if (b == 1) {
    return in.readString();
  } else {
    throw new CorruptIndexException("invalid string lead byte " + b, in);
  }
}
 
Example 10
Source File: SortedSetSortField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public SortField readSortField(DataInput in) throws IOException {
  SortField sf = new SortedSetSortField(in.readString(), in.readInt() == 1, readSelectorType(in));
  int missingValue = in.readInt();
  if (missingValue == 1) {
    sf.setMissingValue(SortField.STRING_FIRST);
  }
  else if (missingValue == 2) {
    sf.setMissingValue(SortField.STRING_LAST);
  }
  return sf;
}
 
Example 11
Source File: SortField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public SortField readSortField(DataInput in) throws IOException {
  SortField sf = new SortField(in.readString(), readType(in), in.readInt() == 1);
  if (in.readInt() == 1) {
    // missing object
    switch (sf.type) {
      case STRING:
        int missingString = in.readInt();
        if (missingString == 1) {
          sf.setMissingValue(STRING_FIRST);
        }
        else {
          sf.setMissingValue(STRING_LAST);
        }
        break;
      case INT:
        sf.setMissingValue(in.readInt());
        break;
      case LONG:
        sf.setMissingValue(in.readLong());
        break;
      case FLOAT:
        sf.setMissingValue(NumericUtils.sortableIntToFloat(in.readInt()));
        break;
      case DOUBLE:
        sf.setMissingValue(NumericUtils.sortableLongToDouble(in.readLong()));
        break;
      default:
        throw new IllegalArgumentException("Cannot deserialize sort of type " + sf.type);
    }
  }
  return sf;
}
 
Example 12
Source File: SortField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected static Type readType(DataInput in) throws IOException {
  String type = in.readString();
  try {
    return Type.valueOf(type);
  }
  catch (IllegalArgumentException e) {
    throw new IllegalArgumentException("Can't deserialize SortField - unknown type " + type);
  }
}
 
Example 13
Source File: SegmentInfos.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static Codec readCodec(DataInput input) throws IOException {
  final String name = input.readString();
  try {
    return Codec.forName(name);
  } catch (IllegalArgumentException e) {
    // maybe it's an old default codec that moved
    if (name.startsWith("Lucene")) {
      throw new IllegalArgumentException("Could not load codec '" + name + "'.  Did you forget to add lucene-backward-codecs.jar?", e);
    }
    throw e;
  }
}
 
Example 14
Source File: SimpleTransLog.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void replayDeleteDocuments(Connection c, NodeProcess primary, DataInput in) throws IOException {
  String id = in.readString();
  // nocomit what if this fails?
  primary.deleteDocument(c, id);
}
 
Example 15
Source File: SimplePrimaryNode.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** Called when another node (replica) wants to copy files from us */
private boolean handleFetchFiles(Random random, Socket socket, DataInput destIn, DataOutput destOut, BufferedOutputStream bos) throws IOException {
  Thread.currentThread().setName("send");

  int replicaID = destIn.readVInt();
  message("top: start fetch for R" + replicaID + " socket=" + socket);
  byte b = destIn.readByte();
  CopyState copyState;
  if (b == 0) {
    // Caller already has CopyState
    copyState = null;
  } else if (b == 1) {
    // Caller does not have CopyState; we pull the latest one:
    copyState = getCopyState();
    Thread.currentThread().setName("send-R" + replicaID + "-" + copyState.version);
  } else {
    // Protocol error:
    throw new IllegalArgumentException("invalid CopyState byte=" + b);
  }

  try {
    if (copyState != null) {
      // Serialize CopyState on the wire to the client:
      writeCopyState(copyState, destOut);
      bos.flush();
    }

    byte[] buffer = new byte[16384];
    int fileCount = 0;
    long totBytesSent = 0;
    while (true) {
      byte done = destIn.readByte();
      if (done == 1) {
        break;
      } else if (done != 0) {
        throw new IllegalArgumentException("expected 0 or 1 byte but got " + done);
      }

      // Name of the file the replica wants us to send:
      String fileName = destIn.readString();

      // Starting offset in the file we should start sending bytes from:
      long fpStart = destIn.readVLong();

      try (IndexInput in = dir.openInput(fileName, IOContext.DEFAULT)) {
        long len = in.length();
        //message("fetch " + fileName + ": send len=" + len);
        destOut.writeVLong(len);
        in.seek(fpStart);
        long upto = fpStart;
        while (upto < len) {
          int chunk = (int) Math.min(buffer.length, (len-upto));
          in.readBytes(buffer, 0, chunk);
          if (doFlipBitsDuringCopy) {
            if (random.nextInt(3000) == 17 && bitFlipped.contains(fileName) == false) {
              bitFlipped.add(fileName);
              message("file " + fileName + " to R" + replicaID + ": now randomly flipping a bit at byte=" + upto);
              int x = random.nextInt(chunk);
              int bit = random.nextInt(8);
              buffer[x] ^= 1 << bit;
            }
          }
          destOut.writeBytes(buffer, 0, chunk);
          upto += chunk;
          totBytesSent += chunk;
        }
      }

      fileCount++;
    }

    message("top: done fetch files for R" + replicaID + ": sent " + fileCount + " files; sent " + totBytesSent + " bytes");
  } catch (Throwable t) {
    message("top: exception during fetch: " + t.getMessage() + "; now close socket");
    socket.close();
    return false;
  } finally {
    if (copyState != null) {
      message("top: fetch: now release CopyState");
      releaseCopyState(copyState);
    }
  }

  return true;
}
 
Example 16
Source File: SimplePrimaryNode.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void handleDeleteDocument(DataInput in, DataOutput out) throws IOException {
  String docid = in.readString();
  writer.deleteDocuments(new Term("docid", docid));
}