<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Apache IoTDB on Medium]]></title>
        <description><![CDATA[Stories by Apache IoTDB on Medium]]></description>
        <link>https://medium.com/@Apache_IoTDB?source=rss-4091fa3c965a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*D8tEnzYsxX9EN4eP-uuWvA.png</url>
            <title>Stories by Apache IoTDB on Medium</title>
            <link>https://medium.com/@Apache_IoTDB?source=rss-4091fa3c965a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 17:52:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Apache_IoTDB/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Command Pattern and Its Application in Apache IoTDB]]></title>
            <link>https://medium.com/@Apache_IoTDB/command-pattern-and-its-application-in-apache-iotdb-d32a4164ac45?source=rss-4091fa3c965a------2</link>
            <guid isPermaLink="false">https://medium.com/p/d32a4164ac45</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[command-pattern]]></category>
            <category><![CDATA[apache-iotdb]]></category>
            <category><![CDATA[apache]]></category>
            <dc:creator><![CDATA[Apache IoTDB]]></dc:creator>
            <pubDate>Mon, 11 Jul 2022 07:01:25 GMT</pubDate>
            <atom:updated>2022-07-11T07:01:25.122Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This article is composed by one of Apache IoTDB’s community committers, Yukun Zhou.</em></p><h3>1. The motivation of command pattern</h3><p>Before introducing the principle of command pattern, let’s first consider two scenarios that often occur in programming.</p><h4><strong>1.1 Implement a feature</strong></h4><p>Remember when first started programming, to implement a function, programmers would try to design its input and output according to this function. When coding, programmers tend to type the inputs one by one into the functional interface to generate a bunch of parameters. They then put the outputs in the return values of the function.</p><p>For some functions with few inputs, they can be computed by a dozen lines of code using the above method. Function declarations may seem a bit redundant at this point but are still acceptable.</p><p>However, the function carries more features as the demand increases, so these dozens of lines of code may grow to a hundred or more lines. To maintain the code’s readability, programmers will start the process partition. Some code with strong logical cohesion will be encapsulated into a sub-function. The original main function is then only responsible for invoking the sub-function to represent the overall process. As a result, all or most of the parameters of the main function may be sent to one or several sub-functions, so those parameters would have been copied and pasted several times, making the code very redundant. In addition to the complexity of the process, it is very likely that the parameters of the function will be increased. For example, several new control parameters and Boolean values will be added.</p><p>At this point, some programmers will start to consider global variables in order to reduce typing and copying times of variables. In this way, the interface of sub-functions will be substantially simplified, and all the parameters to be used can be taken directly from the global space, making the code a lot more concise. Other programmers may choose to package this batch of parameters, encapsulate them into a class, and change the interface of sub-functions to receive a single object to make the interface more organized. Sometimes, each sub-function may have to constantly use the Get function to copy this bunch of parameters out, but at least they can be copied as required. Programmers can directly get parameters that are rarely invoked, while new quotes can be created to reduce the number of times using the Get function for the parameters that are frequently invoked.</p><p>The two methods mentioned above both simplify the code to a certain extent, but they also have their limitations. For example, in the method of global variables, if it is a single-threaded task, it is no problem to simply run Online Judge. However, in practical scenarios, if this function is invoked in multiple nodes at the same time, obviously one set of global parameters is not enough to maintain concurrent use of this function in multiple-threaded tasks. For the method of encapsulating class, as analyzed above, it will create the problem of redundant acquisition.</p><p>Is it possible to integrate these two methods? The answer is yes. The above approach is like using an executor to provide functional interfaces, and the code for implementing these functions is encapsulated in the executor. For external requests, encapsulating a bunch of parameters into one object is considered. Can this object represent this task? The parameters to execute this task was input through this object to the executor,<strong> </strong>so how about encapsulating the function directly into the task object rather than into the executor? The executor only needs to invoke the executing method provided by this object to complete the task. Moreover, such encapsulation also allows programmers to use parameters related to the task in different classes in the form of global variables during coding. As a result, declaration of various Get methods and local variables are omitted, and parameters of one task will not affect the execution of other tasks consisting of other parameters.</p><p>Through such thinking and attempting, we find ourselves somehow approaching the command pattern.</p><h4><strong>1.2 Implement a set of features</strong></h4><p>We recalled the approach programmers usually adopt when implementing a single feature or task from the descriptions above, but in practice, programmers often have to process a set of features. Sometimes, programmers are working in a team, which may be divided by different features, with each member being responsible for the implementation of specific feature (vertical division); it may also be divided by the steps, for example, with someone responsible for the implementation of all features in one step (horizontal division).</p><p>In the second type of division of labor, it is inevitable for colleagues in adjacent links to communicate with each other and discuss interfaces that need their mutual collaboration and interaction. When this discussion is reflected in the code, the components of the previous step should initiate the request to the components of the latter step. Different tasks may be executed by different components.</p><p>Let’s assume that programmers A and B are working together to implement a set of features. They adopt a horizontal division of labor, with programmer A responsible for feature implementation in the previous step and programmer B responsible for the feature implementation in the next step. In this case, programmer B will implement a bunch of components to fulfill features, then programmer A has to know the interface of each component, such as which method of which class, and then invoke functions.</p><p>In this case, we can find that programmers A and B are strongly coupled and dependent, but the work of programmer B is largely intransparent to programmer A. Is there a way where programmer A can just send the task to programmer B without paying attention to the specific way of implementation done by B? That’s when a command pattern may be useful.</p><p>In software design, programmers often need to send requests to certain objects, but they do not know who the receiver of the request is or which operation is being requested. Programmers only need to specify the specific request receiver when the program is running. At this point, a command pattern can be used to design the program so that the request sender and request receiver would not be strongly coupled, making the invoking more flexible between objects. There will be no direct reference between them. The request sender only needs to know how to send the request, rather than how to complete the request. This is also one of the motivations for command patterns.</p><h3>2. Principles of Command Pattern</h3><h4>2.1 Definition and Structure of Command Pattern</h4><p>Command pattern encapsulates a request as an object, enabling programmers to parameterize clients with different requests, which consists of queueing requests, logging requests, and executing revocable operations. A command pattern is a pattern based on object behavior, also known as an Action Pattern or Transaction Pattern.</p><p>Command pattern contains the following roles:</p><ul><li>Command</li><li>ConcreteCommand</li><li>Invoker</li><li>Receiver</li><li>Client</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/0*Xj3LMV2vZsQskpEI" /></figure><h4>2.2 Analysis of Command Pattern</h4><ul><li>The essence of the command pattern is to encapsulate commands and separate the roles and responsibilities of invoking and executing them.</li><li>Each command is an operation: the invoker sends a request to perform an operation; the receiver receives the request and performs the operation.</li><li>Command pattern allows the invoker and receiver to be independent. This makes it unnecessary for the invoker to know the interface of the receiver, how the request is received, and whether, when, and how the operation is executed.</li><li>Command pattern makes the request itself an object, which can be saved and sent like any other object.</li><li>The key to the command pattern is introducing an abstract command interface, and the sender is programmed based on the abstract command interface. Only the concrete commands that implement the abstract command interface can be sent to the receiver.</li></ul><h4>2.3 Advantages of Command Pattern</h4><ul><li>System coupling can be weakened.</li><li>New commands can be easily added to the system.</li><li>A command queue and macro command (combined command) can be easily added to the system.</li><li>The system can easily perform Undo() and Redo() on request.</li></ul><h4>2.4 Disadvantages of Command Pattern</h4><p>Using the command pattern may result in too many concrete commands on some systems. Since a concrete command needs to be designed for each command, some systems may require a large number of concrete commands, which will affect the use of the command pattern.</p><h4>2.5 Application Scenarios of Command Pattern</h4><ul><li>The system needs to decouple the invoker and receiver of the request so that they do not interact directly.</li><li>The system needs to specify, queue, and execute requests at different times.</li><li>The system needs to be able to combine a set of operations, which is to enable macro command.</li><li>The system needs to enable Undo() and Redo() of commands.</li></ul><h3>3. Command Pattern in Apache IoTDB</h3><h4>3.1 Logical Plans and Physical Plans</h4><p>As a database, the structure of Apache IoTDB follows classical database structure, and the design in SQL processing is based on logical plans and physical plans.</p><p>If you are familiar with databases, you would know that we use the database through SQL statements. As a declarative programming language, SQL statement only defines the tasks themselves, rather than the execution process of tasks. When programmers submit a SQL statement to the database, we will get the logical plan described by this statement through parsing SQL syntax. This logical plan contains important parameters and operators defined in the SQL statement. However, the logical plan cannot clarify the execution process of the task. Therefore, the database converts the logical concepts and parameters in the logical plan into a physical plan related to physical storage in practice. Then the database will complete the execution of the SQL statement according to the established process based on the physical plan.</p><p>In Apache IoTDB, the parsing of SQL is implemented by Antlr4, and the parameters and operators obtained by Antlr4 parsing will form the logical plan of IoTDB, which corresponds to a series of Operator classes under the /server/src/main/java/org/apache/iotdb/db/qp/logical package. Each Operator class corresponds to a specific logical task. For example, InsertOperator corresponds to the inserting task, and QueryOperator corresponds to the query task.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/0*YU8igseAj-nAkOgq" /></figure><p>Each logical plan Operators in IoTDB encapsulate a specific method to generate physical plans, which can be generated by generatePhysicalPlan and will be called by PhysicalGenerator to convert the logical plan into the physical plan. The physical plan corresponds to a series of Plan classes under the /server/src/main/java/org/apache/iotdb/db/qp/physical package. For example, InsertPlan is a physical plan of inserting tasks, and RawDataQueryPlan is a physical plan of the original query tasks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/0*2SRXQ2y35RrTapR9" /></figure><p>In the process of generating physical plans in IoTDB, a typical step is to eliminate * for the path of inputs in SQL. IoTDB adopts a tree-like data model, with each leaf node of the tree representing a time series, and IoTDB provides a wildcard feature to help users select multiple series at the same time. Assuming that there are two series, root.sg.d1.s and root.sg.d2.s, in the metadata tree, users can use root.sg.*.s to select two series at the same time. For example, we can write an SQL statement like this:</p><pre>select s from root.sg.* where time  &gt; 1</pre><p>Taking this SQL statement as an example, the path of the time series recorded in the logical plan is root.sg.*.s. Then the path of the time series recorded in the physical plan generated by the eliminating * operation is root.sg.d1.s and root.sg.d2.s.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/0*hH0jHv5_QWl8lIIY" /></figure><p>In addition, IoTDB is designed with PlanExecutor to decouple the service layer and the underlying storage engine. The service layer is only responsible for receiving the requests and generating and executing the plans. It only needs to send the physical plan into PlanExecutor, without worrying about the implementation of the underlying storage engine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/0*nJW6WrVJmu71gGoH" /></figure><h4>3.2 Traversal of Metadata Tree in Query Task</h4><p>Apache IoTDB adopts a tree-like data model to implement the corresponding metadata module with the code under the /server/src/main/java/org/apache/iotdb/db/metadata package. IoTDB provides abundant data definition languages for metadata tree operations, including computing series and querying matching series, etc.</p><p>We can take time series query as an example. Time series query is a feature similar to the eliminating * operation mentioned above, whose physical path is generated from the logical path.</p><p>In the MTree code of Apache IoTDB on version 0.12 or older version, the time series query corresponds to the MTree.findPath method. This method is a recursive traversal operation for the metadata tree. In the recursive process, the number of parameters of the recursive function is quite large. In addition to the parameters inputted by the task at the beginning, it is also necessary to preserve some state information and result sets during the traversal process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/0*-UuqisQ6oeP8f-QH" /></figure><p>Implementation of this method also uses global variables to simplify the functional interface, but as analyzed above, the global variable method is difficult to execute multi-threading tasks. In order to solve this problem, the code on these versions uses the ThreadLocal feature in Java to define global variables as ThreadLocal, which eliminates the mutual interactions between threads.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/0*lYqfUfi1HaSLzPLU" /></figure><p>In version 0.13 of Apache IoTDB or newer, the MTree code has undergone major modifications, and the Traverser framework has been introduced. For each metadata tree query task, a specific Traverser class is defined in the code (some simple classes are embedded in the MTree code anonymously). The attributes of the Traverser classes include the input parameters of the task and the state information generated during the traversal process of the tree. Meanwhile, Traverser defines the traversal process of the tree, which is encapsulated in the traverse() method. Obviously, the Traverser classes define a metadata query task in two aspects, task description (input) and task execution (traversal method and state information).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/0*sCXrH6Ix7b06gG0m" /></figure><p>Meanwhile, Traverser has replaced the ThreadLocal feature used in Apache IoTDB before version 0.12 with attributes. A Traverser object corresponds to a query task, and its attributes are naturally isolated, so there is no mutual influence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/0*-gVrACErfUFpCKca" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d32a4164ac45" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>