Filipp Voronov
Filipp Voronov

Reputation: 4197

Entity with an array field and objectify

I use Objectify 5.0 for interacting with GAE Datastore. I have an entity class like this:

@Entity
public class A {
    @Id long id;
    int[][] data = new int[365][];
}

But when I try to save an entity I have the following error: java.util.ArrayList is not a supported property type.

What is the best way of dealing with entities having array fields? Is there any alternative to @Mapify / @Stringify for custom serializing or a better solution for the problem exists?

Upvotes: 0

Views: 343

Answers (1)

stickfigure
stickfigure

Reputation: 13556

The datastore does not natively store two-dimensional arrays. What's happening here is that Objectify is turning that int[][] into a ArrayList<ArrayList<Long>> and the native API is rejecting it.

If you want to store an int[][], you'll need to figure out how to translate that structure into something the datastore can handle. One option is an array of objects that have one field, itself an array of ints. Basically Foo[] where Foo has one field, an int[]. This will not necessarily be space efficient and it may be expensive to serialize, especially with large matrices.

Another option is to use @Serialize. It should be reasonably efficient spacewise.

If you want optimal storage, figure out how to compress it yourself - probably into a byte[]. You can pick an algorithm that optimizes for the expected level of sparsity. A very sparse multidimensional array is best stored as a series of coordinates.

Upvotes: 1

Related Questions