出于某种原因从平面缓冲区读取字符串时,我得到了 IndexOutOfBoundsExpetion。我的架构:
namespace com.busalarmclock.flatbuffers;
table Message {
routes:[Route];
stops:[Stop];
trips:[Trip];
}
table Route {
route_id:string;
route_name:string;
route_description:string;
trips:[Trip];
}
table Trip {
trip_id:string;
op_days:int;
stops:[TripStop];
}
table Stop {
stop_id:int;
stop_name:string;
stop_lon:double;
stop_lat:double;
}
table TripStop {
stop:Stop;
arrival_time:long;
departure_time:long;
dropoff_type:short;
}
root_type Message;
这就是我写缓冲区的方式:
public static byte[] createStopMessage(TopDocs hits, IndexSearcher indexSearcher) throws IOException {
FlatBufferBuilder builder = new FlatBufferBuilder(1);
int[] stopData = new int[hits.totalHits];
for (int i = 0; i < hits.totalHits; i++)
stopData[i] = createStopObject(indexSearcher.doc(hits.scoreDocs[i].doc), builder);
int stopsOffset = Message.createStopsVector(builder, stopData);
Message.startMessage(builder);
Message.addStops(builder, stopsOffset);
int root = Message.endMessage(builder);
builder.finish(root);
return builder.sizedByteArray();
}
public static byte[] createTripStopsMessage(TripModel trip, IndexSearcher indexSearcher) {
FlatBufferBuilder builder = new FlatBufferBuilder(1);
int[] tripStopData = new int[trip.tripStopModels.length];
for (int i = 0; i < trip.tripStopModels.length; i++)
tripStopData[i] = createTripStopObject(trip.tripStopModels[i], builder);
System.out.printf("tripId:%s", trip.tripId);
int tripIdOffset = builder.createString(trip.tripId);
int tripStopsOffset = Trip.createStopsVector(builder, tripStopData);
Trip.startTrip(builder);
Trip.addTripId(builder, tripIdOffset);
Trip.addStops(builder, tripStopsOffset);
int tripOffset = Trip.endTrip(builder);
Message.startMessage(builder);
Message.addTrips(builder, tripOffset);
int messageOffset = Message.endMessage(builder);
builder.finish(messageOffset);
return builder.sizedByteArray();
}
public static int createTripStopObject(TripStopModel tripStopModel, FlatBufferBuilder builder) {
int stopOffset = createStopObject(tripStopModel.stop, builder);
return TripStop.createTripStop(builder, stopOffset, tripStopModel.arrivalTime,
tripStopModel.departureTime, tripStopModel.dropoffType);
}
这些是我的模型:
public class TripModel {
public String tripId;
public int opDays;
public TripStopModel[] tripStopModels;
public TripModel() {
}
public TripModel(String tripId) {
this.tripId = tripId;
}
public TripModel(String tripId, TripStopModel[] tripStationHits) {
this.tripStopModels = tripStationHits;
this.tripId = tripId;
}
public TripModel(String tripId, int opDays, TripStopModel[] tripStationHits) {
this.tripId = tripId;
this.opDays = opDays;
this.tripStopModels = tripStationHits;
}
import org.apache.lucene.document.Document;
/**
* Created by User on 09/07/2016.
*/
public class TripStopModel {
public long arrivalTime;
public long departureTime;
public short dropoffType;
public Document stop;
public TripStopModel() {
}
public TripStopModel(long arrivalTime, long departureTime, short dropoffType, Document stop) {
this.arrivalTime = arrivalTime;
this.departureTime = departureTime;
this.dropoffType = dropoffType;
this.stop = stop;
}
}
我有一个 lucene 数据库,我正在尝试从中获取一些数据到 flatbuffer 消息中。创建缓冲区时,我没有收到任何错误,但是从第一个缓冲区读取缓冲区时,我得到了 IndexOutOfBoundsExeption。我检查了,解析时字符串不为空。