All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.iotdb.db.schemaengine.template.TemplateInternalRPCUtil Maven / Gradle / Ivy

There is a newer version: 1.3.3
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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
 *
 *     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.apache.iotdb.db.schemaengine.template;

import org.apache.iotdb.tsfile.utils.Pair;
import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TemplateInternalRPCUtil {

  private TemplateInternalRPCUtil() {}

  public static byte[] generateAddTemplateSetInfoBytes(Template template, String templateSetPath) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
      ReadWriteIOUtils.write(1, outputStream);
      template.serialize(outputStream);
      ReadWriteIOUtils.write(1, outputStream);
      ReadWriteIOUtils.write(templateSetPath, outputStream);
    } catch (IOException ignored) {
      // won't reach here
    }
    return outputStream.toByteArray();
  }

  public static Map> parseAddTemplateSetInfoBytes(ByteBuffer buffer) {
    int templateNum = ReadWriteIOUtils.readInt(buffer);
    Map> result = new HashMap<>(templateNum);
    int pathNum;
    List templateSetPathList;
    for (int i = 0; i < templateNum; i++) {
      Template template = new Template();
      template.deserialize(buffer);

      pathNum = ReadWriteIOUtils.readInt(buffer);
      templateSetPathList = new ArrayList<>(pathNum);
      for (int j = 0; j < pathNum; j++) {
        templateSetPathList.add(ReadWriteIOUtils.readString(buffer));
      }
      result.put(template, templateSetPathList);
    }
    return result;
  }

  public static byte[] generateAddAllTemplateSetInfoBytes(
      Map>> templateSetInfo) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
      ReadWriteIOUtils.write(templateSetInfo.size(), outputStream);
      for (Map.Entry>> entry : templateSetInfo.entrySet()) {
        entry.getKey().serialize(outputStream);
        ReadWriteIOUtils.write(entry.getValue().size(), outputStream);
        for (Pair templateSetPath : entry.getValue()) {
          ReadWriteIOUtils.write(templateSetPath.left, outputStream);
          ReadWriteIOUtils.write(templateSetPath.right, outputStream);
        }
      }
    } catch (IOException ignored) {
      // won't reach here
    }
    return outputStream.toByteArray();
  }

  public static Map>> parseAddAllTemplateSetInfoBytes(
      ByteBuffer buffer) {
    int templateNum = ReadWriteIOUtils.readInt(buffer);
    Map>> result = new HashMap<>(templateNum);
    int pathNum;
    List> templateSetPathList;
    for (int i = 0; i < templateNum; i++) {
      Template template = new Template();
      template.deserialize(buffer);

      pathNum = ReadWriteIOUtils.readInt(buffer);
      templateSetPathList = new ArrayList<>(pathNum);
      for (int j = 0; j < pathNum; j++) {
        templateSetPathList.add(
            new Pair<>(ReadWriteIOUtils.readString(buffer), ReadWriteIOUtils.readBool(buffer)));
      }
      result.put(template, templateSetPathList);
    }
    return result;
  }

  public static byte[] generateInvalidateTemplateSetInfoBytes(int templateId, String path) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
      ReadWriteIOUtils.write(templateId, outputStream);
      ReadWriteIOUtils.write(path, outputStream);
    } catch (IOException ignored) {
      // won't reach here
    }
    return outputStream.toByteArray();
  }

  public static Pair parseInvalidateTemplateSetInfoBytes(ByteBuffer buffer) {
    return new Pair<>(ReadWriteIOUtils.readInt(buffer), ReadWriteIOUtils.readString(buffer));
  }

  public static byte[] generateUpdateTemplateInfoBytes(Template template) {
    return template.serialize().array();
  }

  public static Template parseUpdateTemplateInfoBytes(ByteBuffer buffer) {
    Template template = new Template();
    template.deserialize(buffer);
    return template;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy