Package org.tarantool.core

Examples of org.tarantool.core.Tuple


   * @return a {@link org.tarantool.core.Tuple} object.
   */
  public Tuple delete(int spaceNum, Tuple t) {
    Space space = spaces.get(spaceNum);

    Tuple stored = space.get(t);
    if (stored != null) {
      for (Index key : space.indexes.values()) {
        key.remove(stored);
      }
    }
View Full Code Here


    Call call = (Call) request;
    CallStub callStub = calls.get(call.getProcName());
    if (callStub == null) {
      throw new TarantoolException(50, String.format("Procedure '%s' is not defined", call.getProcName()));
    }
    Tuple args = Tuple.create(ByteBuffer.wrap(call.getBody()).order(ByteOrder.LITTLE_ENDIAN), ByteOrder.LITTLE_ENDIAN);
    return packResult(request, callStub.call(this, call.getProcName(), call.getFlags(), args), Call.OP_CODE);
  }
View Full Code Here

  private Response executeSelect(Request request) {
    Select select = ((Select) request);
    List<Tuple> result = new ArrayList<Tuple>();
    for (int i = 0; i < select.getBody().length; i++) {
      Tuple key = Tuple.create(ByteBuffer.wrap(select.getBody()[i]).order(ByteOrder.LITTLE_ENDIAN), ByteOrder.LITTLE_ENDIAN);
      result.addAll(get(select.getSpace(), select.getIndex(), key));
    }
    shiftAndLimit(select.getOffset(), select.getLimit(), result);
    return packResult(request, result, Select.OP_CODE);
  }
View Full Code Here

    DMLRequest<?> dmlRequest = (DMLRequest<?>) request;
    int spaceNum = dmlRequest.space();
    int flags = dmlRequest.flags();
    byte[] body = dmlRequest.getBody();
    ByteBuffer buffer = ByteBuffer.wrap(body).order(ByteOrder.LITTLE_ENDIAN);
    Tuple tuple = Tuple.create(buffer, ByteOrder.LITTLE_ENDIAN);
    Tuple stored = null;
    Space space = spaces.get(spaceNum);
    if (op != Insert.OP_CODE && (stored = space.get(tuple)) == null) {
      Response response = new Response(op, 4, request.getId());
      response.setBody(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(0).array());
      return response;
    }

    if (op == Insert.OP_CODE) {
      stored = put(spaceNum, tuple, (flags & Flags.ADD_TUPLE) > 0, (flags & Flags.REPLACE_TUPLE) > 0);
    } else if (op == Delete.OP_CODE) {
      stored = delete(spaceNum, tuple);
    } else if (op == Update.OP_CODE) {
      int ops = buffer.getInt();
      for (int i = 0; i < ops; i++) {
        update(spaceNum, buffer, tuple);
      }
      stored = get(spaceNum, 0, new Tuple(1).setBytes(0, tuple.getBytes(0))).get(0);
    }

    if ((dmlRequest.getFlags() & Flags.RETURN_TUPLE) > 0) {
      byte[] responseBody = stored.pack();
      Response response = new Response(op, responseBody.length + 8, request.getId());
      response.setBody(ByteBuffer.allocate(responseBody.length + 8).order(ByteOrder.LITTLE_ENDIAN).putInt(1).putInt(responseBody.length)
          .put(responseBody).array());
      return response;
    } else {
View Full Code Here

  private void update(int spaceNum, ByteBuffer buffer, Tuple tuple) {
    int fieldNo = buffer.getInt();
    Updates up = Updates.valueOf((int) buffer.get());

    Tuple args = null;
    if (up.args > 0) {
      args = Tuple.createFromPackedFields(buffer, ByteOrder.LITTLE_ENDIAN, 1);
    }
    if (up.args > 1) {
      args = Tuple.createFromPackedFields(ByteBuffer.wrap(args.getBytes(0)), ByteOrder.LITTLE_ENDIAN, up.args);
    }
    Space space = spaces.get(spaceNum);
    Index primary = space.indexes.get(0);
    Tuple stored = primary.getOne(tuple);
    if (stored != null) {
      if (stored.size() < fieldNo || fieldNo < 0) {
        throw new TarantoolException(54, String.format("Field %d was not found in the tuple", fieldNo));
      }
      if (up == Updates.ADD || up == Updates.AND || up == Updates.XOR || up == Updates.OR || up == Updates.MAX || up == Updates.SUB) {
        int storedFieldLength = stored.getBytes(fieldNo).length;
        if (storedFieldLength == 4) {
          stored.setInt(fieldNo, (int) arithmeticUpdate(up, stored.getInt(fieldNo), args.getInt(0)));
        } else if (storedFieldLength == 8) {
          stored.setLong(fieldNo, arithmeticUpdate(up, stored.getLong(fieldNo), args.getBytes(0).length == 4 ? args.getInt(0) : args.getLong(0)));
        } else {
          throw new TarantoolException(40, String.format("Field type does not match one required by operation: expected a %s", "NUM or NUM 64"));
        }

      } else if (up == Updates.DELETE) {
        stored = deleteField(fieldNo, stored);
        if (stored.size() < 2) {
          throw new TarantoolException(25, "UPDATE error: the new tuple has no fields");
        }
      } else if (up == Updates.INSERT) {
        stored = insertField(fieldNo, args, stored);
      } else if (up == Updates.SPLICE) {
        splice(fieldNo, args, stored);
      } else if (up == Updates.SET) {
        stored.setBytes(fieldNo, args.getBytes(0));
      }
      delete(spaceNum, tuple);
      put(spaceNum, stored, true, false);
    }
  }
View Full Code Here

   * @param stored
   *            a {@link org.tarantool.core.Tuple} object.
   * @return a {@link org.tarantool.core.Tuple} object.
   */
  public Tuple insertField(int fieldNo, Tuple args, Tuple stored) {
    Tuple copy = new Tuple(stored.size() + 1);
    for (int i = 0, offset = 0; i < stored.size() + 1; i++) {
      if (i != fieldNo) {
        copy.setBytes(i, stored.getBytes(i - offset));
      } else {
        copy.setBytes(i, args.getBytes(0));
        offset = 1;
      }
    }
    return copy;
  }
View Full Code Here

   * @param stored
   *            a {@link org.tarantool.core.Tuple} object.
   * @return a {@link org.tarantool.core.Tuple} object.
   */
  public Tuple deleteField(int fieldNo, Tuple stored) {
    Tuple copy = new Tuple(stored.size() - 1);
    for (int i = 0, offset = 0; i < copy.size(); i++) {
      if (i == fieldNo) {
        offset = 1;
      }
      copy.setBytes(i, stored.getBytes(i + offset));
    }
    return copy;
  }
View Full Code Here

   * @param fields
   *            a int.
   * @return a {@link org.tarantool.core.Tuple} object.
   */
  public Tuple copy(Tuple tuple, int... fields) {
    Tuple t = new Tuple(fields.length);
    for (int i = 0; i < fields.length; i++) {
      t.setBytes(i, tuple.getBytes(fields[i]));
    }
    return t;
  }
View Full Code Here

    }
    return t;
  }

  public Tuple copyExcept(Tuple tuple, Integer... fields) {
    Tuple t = new Tuple(tuple.size() - fields.length);
    List<Integer> except = Arrays.asList(fields);
    for (int i = 0, j = 0; i < tuple.size(); i++) {
      if (!except.contains(i)) {
        t.setBytes(j++, tuple.getBytes(i));
      }
    }
    return t;
  }
View Full Code Here

    return this;
  }

  public List<T> call() {

    Tuple params = luaMode ? luaArgs() : mapping.getSupport().create(args);
    List<Tuple> response = factory.getSingleQueryConnection().call(flags, procName, params);
    List<T> result = new ArrayList<T>();
    for (Tuple tuple : response) {
      result.add(mapping.fromTuple(tuple));
    }
View Full Code Here

TOP

Related Classes of org.tarantool.core.Tuple

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.