OpenHFT/Chronicle-Values中的@Array(length= ?) 注解是如何使用的

How is @Array(length= ?) Annotation in OpenHFT/Chronicle-Values Used

这个问题是关于Chronicle-Values

站点中找到的一个示例是:

interface SomeStats {
    @Array(length=100)
    long getPercentFreqAt(int index);
    void setPercentFreqAt(int index, long percentFreq);
    long addPercentFreqAt(int index, long addition);
}

此处注解只应用于其中一种方法。这是否意味着之后的所有方法都被视为处理数组数据?

我发现 one of the test 个案例

package net.openhft.chronicle.values;

public interface HasArraysInterface {
    @Array(length = 4)
    void setFlagAt(int idx, boolean flag);

    boolean getFlagAt(int idx);

    @Array(length = 4)
    void setByteAt(int idx, byte b);

    byte getByteAt(int idx);

    @Array(length = 4)
    void setShortAt(int idx, short s);

    short getShortAt(int idx);

    @Array(length = 4)
    void setCharAt(int idx, char ch);

    char getCharAt(int idx);

    @Array(length = 4)
    void setIntAt(int idx, int i);

    int getIntAt(int idx);

    @Array(length = 4)
    void setFloatAt(int idx, float f);

    float getFloatAt(int idx);

    @Array(length = 4)
    void setLongAt(int idx, long l);

    long getLongAt(int idx);

    @Array(length = 4)
    void setDoubleAt(int idx, double d);

    double getDoubleAt(int idx);

    @Array(length = 4)
    void setStringAt(int idx, @MaxUtf8Length(8) String s);

    String getStringAt(int idx);
}

据我了解,您可以在此接口中拥有多个数组,其中 @Array(length = 4) 适用于以 At 结尾的方法,直到下一个注释。这样对吗?

此外,您可以使用类似以下内容来模拟 4 个双精度数组和 8 个字符串数组:

    @Array(length = 4)
    void setDoubleAt(int idx, double d);

    double getDoubleAt(int idx);

    @Array(length = 8)
    void setStringAt(int idx, @MaxUtf8Length(8) String s);

    String getStringAt(int idx);

一个接口内分配多个@Array(length= ?)的多个数组的内存布局是怎样的?您可以在面向列或面向行的布局之间进行选择吗?如果 length 不同,将如何处理布局?

也代替:

interface SomeStats {
    @Array(length=100)
    long getPercentFreqAt(int index);
    void setPercentFreqAt(int index, long percentFreq);
    long addPercentFreqAt(int index, long addition);
}

你能不能写成:

@Array(length=100)
interface SomeStats {
    long getPercentFreqAt(int index);
    void setPercentFreqAt(int index, long percentFreq);
    long addPercentFreqAt(int index, long addition);
}

暗示@Array(length=100)适用于整个界面。

您还可以将指定长度推迟到创建点吗?

数组长度是必需的,因为 space 已就地分配。当您指定 N 的长度时,这意味着您只能有索引 0 <= index < N.

  1. Here the annotation is only applied to the one of the methods. Does this imply all methods afterwards are treated as working on the array data?

来自 @Array javadoc:

This annotation must be put on a single method accessing the array elements: getter, or setter, or adder, etc.

我。 e. @Array 应该放在 one (any) 方法上,访问一个字段。 "Field" 由标准访问器方法定义,名称 i. e.在您的示例中,在 SomeStats 值接口中,有一个名为 PercentFreq 的(数组)字段。如果您将 getAnotherField()setAnotherField() 之类的方法添加到此接口,它将是一个单独的字段,@Array 注释不适用,即。 e.它将是一个 "scalar" 字段,除非您将 @Array 放在 getAnotherField()setAnotherField() 上并向这些方法添加 int index 参数,并且 -At 后缀.

2.

What I understood from this is that you can have multiple arrays within this interface where @Array(length = 4) applies to methods ending with At until the next annotation. Is this right?

不,不是 "until the next annotation"。注释应用于 字段 。字段访问器方法(由字段名称标识)可以在接口定义中按任何顺序进行。

3.

What is the memory layout of multiple arrays allocated with multiple @Array(length= ?) within one interface? Can you choose between column oriented or row oriented layout? How will the layouts be handled if the length is different?

在您的示例中,布局为:

Double element 0 (8 bytes)
Double element 1 (8 bytes)
Double element 2 (8 bytes)
Double element 3 (8 bytes)
String element 0 (9 bytes: 1 bytes to encode size + 8 bytes of reserved space)
...
String element 7 (9 bytes)

要进行基于行的对齐,您应该定义另一个接口:

interface Row {
    void setDouble(double d);
    double getDouble();

    void setString(@MaxUtf8Length(8) String s);
    String getString();
}

然后定义数组或行:

interface MyTopInterface {
    @Array(length=8)
    void setRowAt(int index, Row row);
    Row getRowAt(int index);
}

这将与 "column orientied" 布局一样高效并占用相同数量的内存。 Read about nested structures in the tutorial.

4.

implying @Array(length=100) applies to the whole interface.

你不能这样做,使用上面建议的模式(嵌套界面)

5.

Also can you defer specifying the length until the point of creation?

不,Chronicle Values 的整个想法是它们的总大小是静态已知的。