コード例 #1
0
ファイル: TestDSClient.java プロジェクト: DavidBC/bg-scalaris
  /**
   * This function is called in the load phase which is executed using the -load or -loadindex
   * argument. It is used for inserting users and resources. Any field/value pairs in the values
   * HashMap for an entity will be written into the specified entity set with the specified entity
   * key.
   *
   * @param entitySet The name of the entity set with the following two possible values: users and
   *     resources. BG passes these values in lower case. The implementation may manipulate the case
   *     to tailor it for the purposes of a data store.
   * @param entityPK The primary key of the entity to insert.
   * @param values A HashMap of field/value pairs to insert for the entity, these pairs are the
   *     other attributes for an entity and their values. The profile image is identified with the
   *     "pic" key attribute and the thumbnail image is identified with the "tpic" key attribute.
   * @param insertImage Identifies if images should be inserted for users. if set to true the code
   *     should populate each entity with an image; the size of the image is specified using the
   *     imagesize parameter.
   * @return Zero on success, a non-zero error code on error. See this class's description for a
   *     discussion of error codes. The code written for this function call should insert the entity
   *     and its attributes. The code is responsible for inserting the PK and the other attributes
   *     in the appropriate order.
   */
  @Override
  public int insertEntity(
      String entitySet,
      String entityPK,
      HashMap<String, ByteIterator> values,
      boolean insertImage) {
    /** Insert Users and Resources data using JSON-like data model. */
    JsonObject jsonObject = new JsonObject();
    for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
      if (!entry.getKey().equals("pic") && !entry.getKey().equals("tpic")) {
        jsonObject.add(entry.getKey(), new JsonPrimitive(entry.getValue().toString()));
      }
    }

    try {
      transactionHelper.writeUser(entityPK, jsonObject);
    } catch (ConnectionException | AbortException e) {
      e.printStackTrace();
      return -1;
    }

    /** Update Users data after inserting Resources. */
    if (entitySet.equals(RESOURCES)) {
      try {
        ByteIterator wallUserID = values.get(WALL_USER_ID);
        jsonObject = transactionHelper.readUser(wallUserID.toString());

        JsonArray jsonArray;
        if (jsonObject.has(RESOURCES)) {
          jsonArray = jsonObject.getAsJsonArray(RESOURCES);
        } else {
          jsonArray = new JsonArray();
        }
        jsonArray.add(new JsonPrimitive(entityPK));
        jsonObject.add(RESOURCES, jsonArray);

        transactionHelper.writeUser(wallUserID.toString(), jsonObject);
      } catch (ConnectionException | NotFoundException | AbortException e) {
        e.printStackTrace();
        return -1;
      }
    }
    return 0;
  }