use of com.baidu.hugegraph.type.define.Action in project incubator-hugegraph by apache.
the class BackendMutation method optimizeUpdates.
/**
* The optimized scenes include but are not limited to:
* 1.If you want to delete an entry, the other mutations previously
* can be ignored.
* 2.As similar to the No.1 item, If you want to insert an entry,
* the other mutations previously also can be ignored.
* 3.If you append an entry and then eliminate it, the new action
* can override the old one.
*/
@Watched(prefix = "mutation")
private void optimizeUpdates(BackendEntry entry, Action action) {
final Id id = entry.id();
assert id != null;
final List<BackendAction> items = this.updates.get(entry.type(), id);
assert items != null;
boolean ignoreCurrent = false;
for (Iterator<BackendAction> iter = items.iterator(); iter.hasNext(); ) {
BackendAction originItem = iter.next();
Action originAction = originItem.action();
switch(action) {
case INSERT:
iter.remove();
break;
case DELETE:
if (originAction == Action.INSERT) {
throw incompatibleActionException(action, originAction);
} else if (originAction == Action.DELETE) {
ignoreCurrent = true;
} else {
iter.remove();
}
break;
case APPEND:
if (entry.type().isUniqueIndex() && originAction == Action.APPEND) {
throw new IllegalArgumentException(String.format("Unique constraint conflict is found in" + " transaction between %s and %s", entry, originItem.entry()));
}
if (originAction == Action.INSERT || originAction == Action.DELETE) {
throw incompatibleActionException(action, originAction);
} else {
Id subId = entry.subId();
Id originSubId = originItem.entry().subId();
assert subId != null;
if (subId == originSubId || subId.equals(originSubId)) {
iter.remove();
}
}
break;
case ELIMINATE:
if (originAction == Action.INSERT || originAction == Action.DELETE) {
throw incompatibleActionException(action, originAction);
} else {
Id subId = entry.subId();
Id originSubId = originItem.entry().subId();
assert subId != null;
if (subId == originSubId || subId.equals(originSubId)) {
iter.remove();
}
}
break;
default:
throw new AssertionError(String.format("Unknown mutate action: %s", action));
}
}
if (!ignoreCurrent) {
items.add(BackendAction.of(action, entry));
}
}
use of com.baidu.hugegraph.type.define.Action in project incubator-hugegraph by apache.
the class StoreSerializer method readMutation.
public static BackendMutation readMutation(BytesBuffer buffer) {
int size = buffer.readVInt();
BackendMutation mutation = new BackendMutation(size);
for (int i = 0; i < size; i++) {
// read action
Action action = Action.fromCode(buffer.read());
// read HugeType
HugeType type = SerialEnum.fromCode(HugeType.class, buffer.read());
// read id
byte[] idBytes = buffer.readBytes();
// read subId
Id subId = buffer.readId();
if (subId.equals(IdGenerator.ZERO)) {
subId = null;
}
// read ttl
long ttl = buffer.readVLong();
BinaryBackendEntry entry = new BinaryBackendEntry(type, idBytes);
entry.subId(subId);
entry.ttl(ttl);
// read columns
int columnsSize = buffer.readVInt();
for (int c = 0; c < columnsSize; c++) {
byte[] name = buffer.readBytes();
byte[] value = buffer.readBytes();
entry.column(BackendColumn.of(name, value));
}
mutation.put(entry, action);
}
return mutation;
}
Aggregations