2010-02-08 15:30:06 +02:00
|
|
|
/*
|
2014-01-06 22:48:02 +01:00
|
|
|
* Licensed to Elasticsearch under one or more contributor
|
|
|
|
* license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright
|
|
|
|
* ownership. Elasticsearch licenses this file to you under
|
|
|
|
* the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2010-02-08 15:30:06 +02:00
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.elasticsearch.index.translog;
|
|
|
|
|
|
|
|
import org.apache.lucene.index.Term;
|
2014-01-06 21:58:46 +01:00
|
|
|
import org.elasticsearch.ElasticsearchIllegalStateException;
|
2011-01-17 19:45:37 +02:00
|
|
|
import org.elasticsearch.common.Nullable;
|
2010-06-15 17:59:04 +03:00
|
|
|
import org.elasticsearch.common.Strings;
|
2012-07-07 01:26:40 +02:00
|
|
|
import org.elasticsearch.common.bytes.BytesArray;
|
|
|
|
import org.elasticsearch.common.bytes.BytesReference;
|
2010-06-15 17:28:05 +03:00
|
|
|
import org.elasticsearch.common.io.stream.StreamInput;
|
|
|
|
import org.elasticsearch.common.io.stream.StreamOutput;
|
|
|
|
import org.elasticsearch.common.io.stream.Streamable;
|
|
|
|
import org.elasticsearch.common.lease.Releasable;
|
2014-01-31 14:38:21 +01:00
|
|
|
import org.elasticsearch.common.lucene.uid.Versions;
|
2013-07-06 10:56:36 -07:00
|
|
|
import org.elasticsearch.common.unit.ByteSizeValue;
|
2013-03-04 20:41:19 -08:00
|
|
|
import org.elasticsearch.index.CloseableIndexComponent;
|
2014-01-31 14:38:21 +01:00
|
|
|
import org.elasticsearch.index.VersionType;
|
2010-02-08 15:30:06 +02:00
|
|
|
import org.elasticsearch.index.engine.Engine;
|
|
|
|
import org.elasticsearch.index.shard.IndexShardComponent;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2010-08-23 01:45:03 +03:00
|
|
|
import java.io.InputStream;
|
2010-02-08 15:30:06 +02:00
|
|
|
|
|
|
|
/**
|
2011-12-06 02:42:25 +02:00
|
|
|
*
|
2010-02-08 15:30:06 +02:00
|
|
|
*/
|
2013-03-04 20:41:19 -08:00
|
|
|
public interface Translog extends IndexShardComponent, CloseableIndexComponent {
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2013-07-06 10:56:36 -07:00
|
|
|
static ByteSizeValue INACTIVE_SHARD_TRANSLOG_BUFFER = ByteSizeValue.parseBytesSizeValue("1kb");
|
|
|
|
|
2011-05-19 18:04:22 +03:00
|
|
|
public static final String TRANSLOG_ID_KEY = "translog_id";
|
|
|
|
|
2013-07-06 10:56:36 -07:00
|
|
|
void updateBuffer(ByteSizeValue bufferSize);
|
|
|
|
|
2013-03-04 20:41:19 -08:00
|
|
|
void closeWithDelete();
|
|
|
|
|
2010-02-08 15:30:06 +02:00
|
|
|
/**
|
|
|
|
* Returns the id of the current transaction log.
|
|
|
|
*/
|
|
|
|
long currentId();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of operations in the transaction log.
|
|
|
|
*/
|
2011-05-19 11:39:18 +03:00
|
|
|
int estimatedNumberOfOperations();
|
2010-02-08 15:30:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The estimated memory size this translog is taking.
|
|
|
|
*/
|
2011-01-29 04:04:58 +02:00
|
|
|
long memorySizeInBytes();
|
|
|
|
|
2011-01-30 17:41:22 +02:00
|
|
|
/**
|
|
|
|
* Returns the size in bytes of the translog.
|
|
|
|
*/
|
2011-01-29 04:04:58 +02:00
|
|
|
long translogSizeInBytes();
|
2010-02-08 15:30:06 +02:00
|
|
|
|
|
|
|
/**
|
2011-05-19 11:39:18 +03:00
|
|
|
* Creates a new transaction log internally.
|
2011-12-06 02:42:25 +02:00
|
|
|
* <p/>
|
2011-05-19 19:29:02 +03:00
|
|
|
* <p>Can only be called by one thread.
|
2010-08-24 23:12:18 +03:00
|
|
|
*/
|
|
|
|
void newTranslog(long id) throws TranslogException;
|
|
|
|
|
2011-05-19 19:29:02 +03:00
|
|
|
/**
|
|
|
|
* Creates a new transient translog, where added ops will be added to the current one, and to
|
|
|
|
* it.
|
2011-12-06 02:42:25 +02:00
|
|
|
* <p/>
|
2011-05-19 19:29:02 +03:00
|
|
|
* <p>Can only be called by one thread.
|
|
|
|
*/
|
|
|
|
void newTransientTranslog(long id) throws TranslogException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Swaps the transient translog to be the current one.
|
2011-12-06 02:42:25 +02:00
|
|
|
* <p/>
|
2011-05-19 19:29:02 +03:00
|
|
|
* <p>Can only be called by one thread.
|
|
|
|
*/
|
|
|
|
void makeTransientCurrent();
|
|
|
|
|
2011-07-29 19:51:01 +03:00
|
|
|
/**
|
|
|
|
* Reverts back to not have a transient translog.
|
|
|
|
*/
|
|
|
|
void revertTransient();
|
|
|
|
|
2010-02-08 15:30:06 +02:00
|
|
|
/**
|
|
|
|
* Adds a create operation to the transaction log.
|
|
|
|
*/
|
2011-06-24 09:39:37 +03:00
|
|
|
Location add(Operation operation) throws TranslogException;
|
|
|
|
|
|
|
|
byte[] read(Location location);
|
2010-02-08 15:30:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Snapshots the current transaction log allowing to safely iterate over the snapshot.
|
|
|
|
*/
|
|
|
|
Snapshot snapshot() throws TranslogException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Snapshots the delta between the current state of the translog, and the state defined
|
|
|
|
* by the provided snapshot. If a new translog has been created after the provided snapshot
|
|
|
|
* has been take, will return a snapshot on the current trasnlog.
|
|
|
|
*/
|
|
|
|
Snapshot snapshot(Snapshot snapshot);
|
|
|
|
|
2010-10-31 14:36:10 +02:00
|
|
|
/**
|
|
|
|
* Clears unreferenced transaclogs.
|
|
|
|
*/
|
|
|
|
void clearUnreferenced();
|
|
|
|
|
2010-09-24 12:29:52 +02:00
|
|
|
/**
|
2010-09-24 21:25:10 +02:00
|
|
|
* Sync's the translog.
|
2010-09-24 12:29:52 +02:00
|
|
|
*/
|
2014-03-30 19:51:53 +02:00
|
|
|
void sync() throws IOException;
|
2010-09-24 21:25:10 +02:00
|
|
|
|
2012-02-01 13:05:21 +02:00
|
|
|
boolean syncNeeded();
|
|
|
|
|
2010-09-24 21:25:10 +02:00
|
|
|
void syncOnEachOperation(boolean syncOnEachOperation);
|
2010-09-24 12:29:52 +02:00
|
|
|
|
2013-12-11 12:09:59 +01:00
|
|
|
/**
|
|
|
|
* return stats
|
|
|
|
*/
|
|
|
|
TranslogStats stats();
|
|
|
|
|
2011-06-24 09:39:37 +03:00
|
|
|
static class Location {
|
|
|
|
public final long translogId;
|
|
|
|
public final long translogLocation;
|
|
|
|
public final int size;
|
|
|
|
|
|
|
|
public Location(long translogId, long translogLocation, int size) {
|
|
|
|
this.translogId = translogId;
|
|
|
|
this.translogLocation = translogLocation;
|
|
|
|
this.size = size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-08 15:30:06 +02:00
|
|
|
/**
|
|
|
|
* A snapshot of the transaction log, allows to iterate over all the transaction log operations.
|
|
|
|
*/
|
2010-07-06 00:12:40 +03:00
|
|
|
static interface Snapshot extends Releasable {
|
2010-02-08 15:30:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The id of the translog the snapshot was taken with.
|
|
|
|
*/
|
|
|
|
long translogId();
|
|
|
|
|
2010-07-06 00:12:40 +03:00
|
|
|
long position();
|
|
|
|
|
2010-02-08 15:30:06 +02:00
|
|
|
/**
|
2010-07-06 00:12:40 +03:00
|
|
|
* Returns the internal length (*not* number of operations) of this snapshot.
|
2010-02-08 15:30:06 +02:00
|
|
|
*/
|
2010-07-06 00:12:40 +03:00
|
|
|
long length();
|
|
|
|
|
2010-08-23 14:48:49 +03:00
|
|
|
/**
|
|
|
|
* The total number of operations in the translog.
|
|
|
|
*/
|
2011-05-19 11:39:18 +03:00
|
|
|
int estimatedTotalOperations();
|
2010-08-23 14:48:49 +03:00
|
|
|
|
2010-07-06 00:12:40 +03:00
|
|
|
boolean hasNext();
|
|
|
|
|
|
|
|
Operation next();
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2010-07-07 09:30:07 +03:00
|
|
|
void seekForward(long length);
|
2010-08-23 01:45:03 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a stream of this snapshot.
|
|
|
|
*/
|
|
|
|
InputStream stream() throws IOException;
|
|
|
|
|
|
|
|
/**
|
2010-08-23 14:48:49 +03:00
|
|
|
* The length in bytes of this stream.
|
2010-08-23 01:45:03 +03:00
|
|
|
*/
|
|
|
|
long lengthInBytes();
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-24 18:27:48 +03:00
|
|
|
* A generic interface representing an operation performed on the transaction log.
|
2010-02-08 15:30:06 +02:00
|
|
|
* Each is associated with a type.
|
|
|
|
*/
|
|
|
|
static interface Operation extends Streamable {
|
|
|
|
static enum Type {
|
|
|
|
CREATE((byte) 1),
|
|
|
|
SAVE((byte) 2),
|
|
|
|
DELETE((byte) 3),
|
|
|
|
DELETE_BY_QUERY((byte) 4);
|
|
|
|
|
|
|
|
private final byte id;
|
|
|
|
|
|
|
|
private Type(byte id) {
|
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte id() {
|
|
|
|
return this.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Type fromId(byte id) {
|
|
|
|
switch (id) {
|
|
|
|
case 1:
|
|
|
|
return CREATE;
|
|
|
|
case 2:
|
|
|
|
return SAVE;
|
|
|
|
case 3:
|
|
|
|
return DELETE;
|
|
|
|
case 4:
|
|
|
|
return DELETE_BY_QUERY;
|
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("No type mapped for [" + id + "]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Type opType();
|
|
|
|
|
|
|
|
long estimateSize();
|
2011-06-24 09:39:37 +03:00
|
|
|
|
2012-01-08 17:23:37 +02:00
|
|
|
Source readSource(StreamInput in) throws IOException;
|
2011-08-30 22:35:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static class Source {
|
2012-07-07 01:26:40 +02:00
|
|
|
public final BytesReference source;
|
2011-08-30 22:35:16 +03:00
|
|
|
public final String routing;
|
|
|
|
public final String parent;
|
|
|
|
public final long timestamp;
|
2011-08-30 17:48:06 +02:00
|
|
|
public final long ttl;
|
2011-08-30 22:35:16 +03:00
|
|
|
|
2012-07-07 01:26:40 +02:00
|
|
|
public Source(BytesReference source, String routing, String parent, long timestamp, long ttl) {
|
2011-08-30 22:35:16 +03:00
|
|
|
this.source = source;
|
|
|
|
this.routing = routing;
|
|
|
|
this.parent = parent;
|
|
|
|
this.timestamp = timestamp;
|
2011-08-30 17:48:06 +02:00
|
|
|
this.ttl = ttl;
|
2011-08-30 22:35:16 +03:00
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static class Create implements Operation {
|
2014-01-31 14:38:21 +01:00
|
|
|
public static final int SERIALIZATION_FORMAT = 6;
|
|
|
|
|
2010-02-08 15:30:06 +02:00
|
|
|
private String id;
|
|
|
|
private String type;
|
2012-07-07 01:26:40 +02:00
|
|
|
private BytesReference source;
|
2010-11-15 11:34:56 +02:00
|
|
|
private String routing;
|
2010-12-08 00:16:05 +02:00
|
|
|
private String parent;
|
2011-08-26 11:06:10 +02:00
|
|
|
private long timestamp;
|
2011-08-30 17:48:06 +02:00
|
|
|
private long ttl;
|
2014-01-31 14:38:21 +01:00
|
|
|
private long version = Versions.MATCH_ANY;
|
|
|
|
private VersionType versionType = VersionType.INTERNAL;
|
2010-02-08 15:30:06 +02:00
|
|
|
|
|
|
|
public Create() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public Create(Engine.Create create) {
|
2011-08-31 22:29:04 +03:00
|
|
|
this.id = create.id();
|
|
|
|
this.type = create.type();
|
2012-07-07 01:26:40 +02:00
|
|
|
this.source = create.source();
|
2010-11-15 11:34:56 +02:00
|
|
|
this.routing = create.routing();
|
2010-12-08 00:16:05 +02:00
|
|
|
this.parent = create.parent();
|
2011-08-26 11:06:10 +02:00
|
|
|
this.timestamp = create.timestamp();
|
2011-08-30 17:48:06 +02:00
|
|
|
this.ttl = create.ttl();
|
2011-01-04 04:04:30 +02:00
|
|
|
this.version = create.version();
|
2014-01-31 14:38:21 +01:00
|
|
|
this.versionType = create.versionType();
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2010-02-23 18:40:22 +02:00
|
|
|
public Create(String type, String id, byte[] source) {
|
2010-02-08 15:30:06 +02:00
|
|
|
this.id = id;
|
|
|
|
this.type = type;
|
2012-07-07 01:26:40 +02:00
|
|
|
this.source = new BytesArray(source);
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public Type opType() {
|
2010-02-08 15:30:06 +02:00
|
|
|
return Type.CREATE;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public long estimateSize() {
|
2012-01-08 17:23:37 +02:00
|
|
|
return ((id.length() + type.length()) * 2) + source.length() + 12;
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String id() {
|
|
|
|
return this.id;
|
|
|
|
}
|
|
|
|
|
2012-07-07 01:26:40 +02:00
|
|
|
public BytesReference source() {
|
2010-02-08 15:30:06 +02:00
|
|
|
return this.source;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String type() {
|
|
|
|
return this.type;
|
|
|
|
}
|
|
|
|
|
2010-11-15 11:34:56 +02:00
|
|
|
public String routing() {
|
|
|
|
return this.routing;
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2010-12-08 00:16:05 +02:00
|
|
|
public String parent() {
|
|
|
|
return this.parent;
|
|
|
|
}
|
|
|
|
|
2011-08-26 11:06:10 +02:00
|
|
|
public long timestamp() {
|
|
|
|
return this.timestamp;
|
|
|
|
}
|
|
|
|
|
2011-08-30 17:48:06 +02:00
|
|
|
public long ttl() {
|
|
|
|
return this.ttl;
|
|
|
|
}
|
|
|
|
|
2011-01-04 04:04:30 +02:00
|
|
|
public long version() {
|
|
|
|
return this.version;
|
|
|
|
}
|
|
|
|
|
2014-01-31 14:38:21 +01:00
|
|
|
public VersionType versionType() {
|
|
|
|
return versionType;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
2012-01-08 17:23:37 +02:00
|
|
|
public Source readSource(StreamInput in) throws IOException {
|
|
|
|
readFrom(in);
|
2011-08-30 17:48:06 +02:00
|
|
|
return new Source(source, routing, parent, timestamp, ttl);
|
2011-06-24 09:39:37 +03:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public void readFrom(StreamInput in) throws IOException {
|
2010-11-15 11:34:56 +02:00
|
|
|
int version = in.readVInt(); // version
|
2013-01-17 22:38:42 +01:00
|
|
|
id = in.readString();
|
|
|
|
type = in.readString();
|
2012-01-08 17:23:37 +02:00
|
|
|
source = in.readBytesReference();
|
2010-12-08 00:16:05 +02:00
|
|
|
if (version >= 1) {
|
2010-11-15 11:34:56 +02:00
|
|
|
if (in.readBoolean()) {
|
2013-01-17 22:38:42 +01:00
|
|
|
routing = in.readString();
|
2010-11-15 11:34:56 +02:00
|
|
|
}
|
|
|
|
}
|
2010-12-08 00:16:05 +02:00
|
|
|
if (version >= 2) {
|
|
|
|
if (in.readBoolean()) {
|
2013-01-17 22:38:42 +01:00
|
|
|
parent = in.readString();
|
2010-12-08 00:16:05 +02:00
|
|
|
}
|
|
|
|
}
|
2011-01-04 04:04:30 +02:00
|
|
|
if (version >= 3) {
|
|
|
|
this.version = in.readLong();
|
|
|
|
}
|
2011-08-26 11:06:10 +02:00
|
|
|
if (version >= 4) {
|
|
|
|
this.timestamp = in.readLong();
|
|
|
|
}
|
2011-08-30 17:48:06 +02:00
|
|
|
if (version >= 5) {
|
|
|
|
this.ttl = in.readLong();
|
|
|
|
}
|
2014-01-31 14:38:21 +01:00
|
|
|
if (version >= 6) {
|
|
|
|
this.versionType = VersionType.fromValue(in.readByte());
|
|
|
|
}
|
|
|
|
|
2014-04-23 11:13:25 +02:00
|
|
|
assert versionType.validateVersionForWrites(version);
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public void writeTo(StreamOutput out) throws IOException {
|
2014-01-31 14:38:21 +01:00
|
|
|
out.writeVInt(SERIALIZATION_FORMAT);
|
2013-01-17 22:38:42 +01:00
|
|
|
out.writeString(id);
|
|
|
|
out.writeString(type);
|
2012-07-07 22:49:31 +02:00
|
|
|
out.writeBytesReference(source);
|
2010-11-15 11:34:56 +02:00
|
|
|
if (routing == null) {
|
|
|
|
out.writeBoolean(false);
|
|
|
|
} else {
|
|
|
|
out.writeBoolean(true);
|
2013-01-17 22:38:42 +01:00
|
|
|
out.writeString(routing);
|
2010-11-15 11:34:56 +02:00
|
|
|
}
|
2010-12-08 00:16:05 +02:00
|
|
|
if (parent == null) {
|
|
|
|
out.writeBoolean(false);
|
|
|
|
} else {
|
|
|
|
out.writeBoolean(true);
|
2013-01-17 22:38:42 +01:00
|
|
|
out.writeString(parent);
|
2010-12-08 00:16:05 +02:00
|
|
|
}
|
2011-01-04 04:04:30 +02:00
|
|
|
out.writeLong(version);
|
2011-08-26 11:06:10 +02:00
|
|
|
out.writeLong(timestamp);
|
2011-08-30 17:48:06 +02:00
|
|
|
out.writeLong(ttl);
|
2014-01-31 14:38:21 +01:00
|
|
|
out.writeByte(versionType.getValue());
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static class Index implements Operation {
|
2014-01-31 14:38:21 +01:00
|
|
|
public static final int SERIALIZATION_FORMAT = 6;
|
|
|
|
|
2010-02-08 15:30:06 +02:00
|
|
|
private String id;
|
|
|
|
private String type;
|
2014-01-31 14:38:21 +01:00
|
|
|
private long version = Versions.MATCH_ANY;
|
|
|
|
private VersionType versionType = VersionType.INTERNAL;
|
2012-07-07 01:26:40 +02:00
|
|
|
private BytesReference source;
|
2010-11-15 11:34:56 +02:00
|
|
|
private String routing;
|
2010-12-08 00:16:05 +02:00
|
|
|
private String parent;
|
2011-08-26 11:06:10 +02:00
|
|
|
private long timestamp;
|
2011-08-30 17:48:06 +02:00
|
|
|
private long ttl;
|
2010-02-08 15:30:06 +02:00
|
|
|
|
|
|
|
public Index() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public Index(Engine.Index index) {
|
2011-08-31 22:29:04 +03:00
|
|
|
this.id = index.id();
|
|
|
|
this.type = index.type();
|
2012-07-07 01:26:40 +02:00
|
|
|
this.source = index.source();
|
2010-11-15 11:34:56 +02:00
|
|
|
this.routing = index.routing();
|
2010-12-08 00:16:05 +02:00
|
|
|
this.parent = index.parent();
|
2011-01-04 04:04:30 +02:00
|
|
|
this.version = index.version();
|
2011-08-26 11:06:10 +02:00
|
|
|
this.timestamp = index.timestamp();
|
2011-08-30 17:48:06 +02:00
|
|
|
this.ttl = index.ttl();
|
2014-01-31 14:38:21 +01:00
|
|
|
this.versionType = index.versionType();
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2010-02-23 18:40:22 +02:00
|
|
|
public Index(String type, String id, byte[] source) {
|
2010-02-08 15:30:06 +02:00
|
|
|
this.type = type;
|
|
|
|
this.id = id;
|
2012-07-07 01:26:40 +02:00
|
|
|
this.source = new BytesArray(source);
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public Type opType() {
|
2010-02-08 15:30:06 +02:00
|
|
|
return Type.SAVE;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public long estimateSize() {
|
2012-01-08 17:23:37 +02:00
|
|
|
return ((id.length() + type.length()) * 2) + source.length() + 12;
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String type() {
|
|
|
|
return this.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String id() {
|
|
|
|
return this.id;
|
|
|
|
}
|
|
|
|
|
2010-11-15 11:34:56 +02:00
|
|
|
public String routing() {
|
|
|
|
return this.routing;
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2010-12-08 00:16:05 +02:00
|
|
|
public String parent() {
|
|
|
|
return this.parent;
|
|
|
|
}
|
|
|
|
|
2011-08-26 11:06:10 +02:00
|
|
|
public long timestamp() {
|
|
|
|
return this.timestamp;
|
|
|
|
}
|
|
|
|
|
2011-08-30 17:48:06 +02:00
|
|
|
public long ttl() {
|
|
|
|
return this.ttl;
|
|
|
|
}
|
|
|
|
|
2012-07-07 01:26:40 +02:00
|
|
|
public BytesReference source() {
|
2010-11-15 11:34:56 +02:00
|
|
|
return this.source;
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2011-01-04 04:04:30 +02:00
|
|
|
public long version() {
|
|
|
|
return this.version;
|
|
|
|
}
|
|
|
|
|
2014-01-31 14:38:21 +01:00
|
|
|
public VersionType versionType() {
|
|
|
|
return versionType;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
2012-01-08 17:23:37 +02:00
|
|
|
public Source readSource(StreamInput in) throws IOException {
|
|
|
|
readFrom(in);
|
2011-08-30 17:48:06 +02:00
|
|
|
return new Source(source, routing, parent, timestamp, ttl);
|
2011-06-24 09:39:37 +03:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public void readFrom(StreamInput in) throws IOException {
|
2010-11-15 11:34:56 +02:00
|
|
|
int version = in.readVInt(); // version
|
2013-01-17 22:38:42 +01:00
|
|
|
id = in.readString();
|
|
|
|
type = in.readString();
|
2012-01-08 17:23:37 +02:00
|
|
|
source = in.readBytesReference();
|
2010-12-08 00:16:05 +02:00
|
|
|
if (version >= 1) {
|
2010-11-15 11:34:56 +02:00
|
|
|
if (in.readBoolean()) {
|
2013-01-17 22:38:42 +01:00
|
|
|
routing = in.readString();
|
2010-11-15 11:34:56 +02:00
|
|
|
}
|
|
|
|
}
|
2010-12-08 00:16:05 +02:00
|
|
|
if (version >= 2) {
|
|
|
|
if (in.readBoolean()) {
|
2013-01-17 22:38:42 +01:00
|
|
|
parent = in.readString();
|
2010-12-08 00:16:05 +02:00
|
|
|
}
|
|
|
|
}
|
2011-01-04 04:04:30 +02:00
|
|
|
if (version >= 3) {
|
|
|
|
this.version = in.readLong();
|
|
|
|
}
|
2011-08-26 11:06:10 +02:00
|
|
|
if (version >= 4) {
|
|
|
|
this.timestamp = in.readLong();
|
|
|
|
}
|
2011-08-30 17:48:06 +02:00
|
|
|
if (version >= 5) {
|
|
|
|
this.ttl = in.readLong();
|
|
|
|
}
|
2014-01-31 14:38:21 +01:00
|
|
|
if (version >= 6) {
|
|
|
|
this.versionType = VersionType.fromValue(in.readByte());
|
|
|
|
}
|
|
|
|
|
2014-04-23 11:13:25 +02:00
|
|
|
assert versionType.validateVersionForWrites(version);
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public void writeTo(StreamOutput out) throws IOException {
|
2014-01-31 14:38:21 +01:00
|
|
|
out.writeVInt(SERIALIZATION_FORMAT);
|
2013-01-17 22:38:42 +01:00
|
|
|
out.writeString(id);
|
|
|
|
out.writeString(type);
|
2012-07-07 22:49:31 +02:00
|
|
|
out.writeBytesReference(source);
|
2010-11-15 11:34:56 +02:00
|
|
|
if (routing == null) {
|
|
|
|
out.writeBoolean(false);
|
|
|
|
} else {
|
|
|
|
out.writeBoolean(true);
|
2013-01-17 22:38:42 +01:00
|
|
|
out.writeString(routing);
|
2010-11-15 11:34:56 +02:00
|
|
|
}
|
2010-12-08 00:16:05 +02:00
|
|
|
if (parent == null) {
|
|
|
|
out.writeBoolean(false);
|
|
|
|
} else {
|
|
|
|
out.writeBoolean(true);
|
2013-01-17 22:38:42 +01:00
|
|
|
out.writeString(parent);
|
2010-12-08 00:16:05 +02:00
|
|
|
}
|
2011-01-04 04:04:30 +02:00
|
|
|
out.writeLong(version);
|
2011-08-26 11:06:10 +02:00
|
|
|
out.writeLong(timestamp);
|
2011-08-30 17:48:06 +02:00
|
|
|
out.writeLong(ttl);
|
2014-01-31 14:38:21 +01:00
|
|
|
out.writeByte(versionType.getValue());
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static class Delete implements Operation {
|
2014-01-31 14:38:21 +01:00
|
|
|
public static final int SERIALIZATION_FORMAT = 2;
|
|
|
|
|
2010-02-08 15:30:06 +02:00
|
|
|
private Term uid;
|
2014-01-31 14:38:21 +01:00
|
|
|
private long version = Versions.MATCH_ANY;
|
|
|
|
private VersionType versionType = VersionType.INTERNAL;
|
2010-02-08 15:30:06 +02:00
|
|
|
|
|
|
|
public Delete() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public Delete(Engine.Delete delete) {
|
|
|
|
this(delete.uid());
|
2011-01-04 04:04:30 +02:00
|
|
|
this.version = delete.version();
|
2014-01-31 14:38:21 +01:00
|
|
|
this.versionType = delete.versionType();
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Delete(Term uid) {
|
|
|
|
this.uid = uid;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public Type opType() {
|
2010-02-08 15:30:06 +02:00
|
|
|
return Type.DELETE;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public long estimateSize() {
|
2010-02-08 15:30:06 +02:00
|
|
|
return ((uid.field().length() + uid.text().length()) * 2) + 20;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Term uid() {
|
|
|
|
return this.uid;
|
|
|
|
}
|
|
|
|
|
2011-01-04 04:04:30 +02:00
|
|
|
public long version() {
|
|
|
|
return this.version;
|
|
|
|
}
|
|
|
|
|
2014-01-31 14:38:21 +01:00
|
|
|
public VersionType versionType() {
|
|
|
|
return this.versionType;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
2012-01-08 17:23:37 +02:00
|
|
|
public Source readSource(StreamInput in) throws IOException {
|
2014-01-06 21:58:46 +01:00
|
|
|
throw new ElasticsearchIllegalStateException("trying to read doc source from delete operation");
|
2011-06-24 09:39:37 +03:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public void readFrom(StreamInput in) throws IOException {
|
2011-01-04 04:04:30 +02:00
|
|
|
int version = in.readVInt(); // version
|
2013-01-17 22:38:42 +01:00
|
|
|
uid = new Term(in.readString(), in.readString());
|
2011-01-04 04:04:30 +02:00
|
|
|
if (version >= 1) {
|
|
|
|
this.version = in.readLong();
|
|
|
|
}
|
2014-01-31 14:38:21 +01:00
|
|
|
if (version >= 2) {
|
|
|
|
this.versionType = VersionType.fromValue(in.readByte());
|
|
|
|
}
|
2014-04-23 11:13:25 +02:00
|
|
|
assert versionType.validateVersionForWrites(version);
|
2014-01-31 14:38:21 +01:00
|
|
|
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public void writeTo(StreamOutput out) throws IOException {
|
2014-01-31 14:38:21 +01:00
|
|
|
out.writeVInt(SERIALIZATION_FORMAT);
|
2013-01-17 22:38:42 +01:00
|
|
|
out.writeString(uid.field());
|
|
|
|
out.writeString(uid.text());
|
2011-01-04 04:04:30 +02:00
|
|
|
out.writeLong(version);
|
2014-01-31 14:38:21 +01:00
|
|
|
out.writeByte(versionType.getValue());
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static class DeleteByQuery implements Operation {
|
2014-01-31 14:38:21 +01:00
|
|
|
|
|
|
|
public static final int SERIALIZATION_FORMAT = 2;
|
2012-07-07 01:26:40 +02:00
|
|
|
private BytesReference source;
|
2011-12-06 02:42:25 +02:00
|
|
|
@Nullable
|
|
|
|
private String[] filteringAliases;
|
2010-02-08 15:30:06 +02:00
|
|
|
private String[] types = Strings.EMPTY_ARRAY;
|
|
|
|
|
|
|
|
public DeleteByQuery() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public DeleteByQuery(Engine.DeleteByQuery deleteByQuery) {
|
2011-08-03 11:30:01 +03:00
|
|
|
this(deleteByQuery.source(), deleteByQuery.filteringAliases(), deleteByQuery.types());
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2012-07-07 01:26:40 +02:00
|
|
|
public DeleteByQuery(BytesReference source, String[] filteringAliases, String... types) {
|
2010-02-08 15:30:06 +02:00
|
|
|
this.source = source;
|
2011-06-03 04:32:27 +03:00
|
|
|
this.types = types == null ? Strings.EMPTY_ARRAY : types;
|
2011-05-25 11:20:09 -04:00
|
|
|
this.filteringAliases = filteringAliases;
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public Type opType() {
|
2010-02-08 15:30:06 +02:00
|
|
|
return Type.DELETE_BY_QUERY;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public long estimateSize() {
|
2012-01-08 20:52:48 +02:00
|
|
|
return source.length() + 8;
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2012-07-07 01:26:40 +02:00
|
|
|
public BytesReference source() {
|
2010-02-08 15:30:06 +02:00
|
|
|
return this.source;
|
|
|
|
}
|
|
|
|
|
2011-05-25 11:20:09 -04:00
|
|
|
public String[] filteringAliases() {
|
|
|
|
return filteringAliases;
|
|
|
|
}
|
|
|
|
|
2010-02-08 15:30:06 +02:00
|
|
|
public String[] types() {
|
|
|
|
return this.types;
|
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
2012-01-08 17:23:37 +02:00
|
|
|
public Source readSource(StreamInput in) throws IOException {
|
2014-01-06 21:58:46 +01:00
|
|
|
throw new ElasticsearchIllegalStateException("trying to read doc source from delete_by_query operation");
|
2011-06-24 09:39:37 +03:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public void readFrom(StreamInput in) throws IOException {
|
2011-05-25 11:20:09 -04:00
|
|
|
int version = in.readVInt(); // version
|
2012-01-08 20:52:48 +02:00
|
|
|
source = in.readBytesReference();
|
2011-06-03 04:32:27 +03:00
|
|
|
if (version < 2) {
|
|
|
|
// for query_parser_name, which was removed
|
|
|
|
if (in.readBoolean()) {
|
2013-01-17 22:38:42 +01:00
|
|
|
in.readString();
|
2011-06-03 04:32:27 +03:00
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
2010-03-19 03:04:35 +02:00
|
|
|
int typesSize = in.readVInt();
|
2010-02-08 15:30:06 +02:00
|
|
|
if (typesSize > 0) {
|
|
|
|
types = new String[typesSize];
|
|
|
|
for (int i = 0; i < typesSize; i++) {
|
2013-01-17 22:38:42 +01:00
|
|
|
types[i] = in.readString();
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
}
|
2011-05-25 11:20:09 -04:00
|
|
|
if (version >= 1) {
|
|
|
|
int aliasesSize = in.readVInt();
|
|
|
|
if (aliasesSize > 0) {
|
|
|
|
filteringAliases = new String[aliasesSize];
|
|
|
|
for (int i = 0; i < aliasesSize; i++) {
|
2013-01-17 22:38:42 +01:00
|
|
|
filteringAliases[i] = in.readString();
|
2011-05-25 11:20:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public void writeTo(StreamOutput out) throws IOException {
|
2014-01-31 14:38:21 +01:00
|
|
|
out.writeVInt(SERIALIZATION_FORMAT);
|
2012-07-07 22:49:31 +02:00
|
|
|
out.writeBytesReference(source);
|
2010-03-19 03:04:35 +02:00
|
|
|
out.writeVInt(types.length);
|
2010-02-08 15:30:06 +02:00
|
|
|
for (String type : types) {
|
2013-01-17 22:38:42 +01:00
|
|
|
out.writeString(type);
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
2011-05-25 11:20:09 -04:00
|
|
|
if (filteringAliases != null) {
|
|
|
|
out.writeVInt(filteringAliases.length);
|
|
|
|
for (String alias : filteringAliases) {
|
2013-01-17 22:38:42 +01:00
|
|
|
out.writeString(alias);
|
2011-05-25 11:20:09 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
out.writeVInt(0);
|
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|