Skip to content

Commit de8b6e1

Browse files
committed
Add index support and test #12
1 parent aabf170 commit de8b6e1

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

objectbox/model/properties.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,14 @@ class PropertyType(IntEnum):
6262
}
6363

6464

65+
class IndexType(IntEnum):
66+
value = OBXPropertyFlags_INDEXED
67+
hash = OBXPropertyFlags_INDEX_HASH
68+
hash64 = OBXPropertyFlags_INDEX_HASH64
69+
70+
6571
class Property:
66-
def __init__(self, py_type: type, id: int, uid: int, type: PropertyType = None):
72+
def __init__(self, py_type: type, id: int, uid: int, type: PropertyType = None, index: bool = None, index_type: IndexType = None):
6773
self._id = id
6874
self._uid = uid
6975
self._name = "" # set in Entity.fill_properties()
@@ -80,6 +86,18 @@ def __init__(self, py_type: type, id: int, uid: int, type: PropertyType = None):
8086
self._fb_slot = self._id - 1
8187
self._fb_v_offset = 4 + 2*self._fb_slot
8288

89+
if index_type:
90+
if index == True or index == None:
91+
self._index = True
92+
self._index_type = index_type
93+
elif index == False:
94+
raise Exception(f"trying to set index type on property with id {self._id} while index is set to False")
95+
else:
96+
self._index = index if index != None else False
97+
if index:
98+
self._index_type = IndexType.value if self._py_type != str else IndexType.hash
99+
100+
83101
def __determine_ob_type(self) -> OBXPropertyType:
84102
ts = self._py_type
85103
if ts == str:

tests/model.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
from objectbox.model import *
2+
from objectbox.model.properties import IndexType
23
import numpy as np
34
from datetime import datetime
45

56

67
@Entity(id=1, uid=1)
78
class TestEntity:
89
id = Id(id=1, uid=1001)
9-
str = Property(str, id=2, uid=1002)
10+
str = Property(str, id=2, uid=1002, index=True)
1011
bool = Property(bool, id=3, uid=1003)
11-
int64 = Property(int, type=PropertyType.long, id=4, uid=1004)
12-
int32 = Property(int, type=PropertyType.int, id=5, uid=1005)
13-
int16 = Property(int, type=PropertyType.short, id=6, uid=1006)
12+
int64 = Property(int, type=PropertyType.long, id=4, uid=1004, index=True)
13+
int32 = Property(int, type=PropertyType.int, id=5, uid=1005, index=True, index_type=IndexType.hash)
14+
int16 = Property(int, type=PropertyType.short, id=6, uid=1006, index_type=IndexType.hash)
1415
int8 = Property(int, type=PropertyType.byte, id=7, uid=1007)
1516
float64 = Property(float, type=PropertyType.double, id=8, uid=1008)
1617
float32 = Property(float, type=PropertyType.float, id=9, uid=1009)
@@ -30,6 +31,7 @@ class TestEntity:
3031
def __init__(self, string: str = ""):
3132
self.str = string
3233

34+
3335
@Entity(id=2, uid=2)
3436
class TestEntityDatetime:
3537
id = Id(id=1, uid=2001)

tests/test_index.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import objectbox
2+
from objectbox.model import *
3+
from objectbox.model.properties import IndexType
4+
import pytest
5+
from tests.model import TestEntity
6+
from tests.common import (
7+
autocleanup,
8+
load_empty_test_objectbox,
9+
)
10+
11+
def test_index_basics():
12+
ob = load_empty_test_objectbox()
13+
box = objectbox.Box(ob, TestEntity)
14+
15+
# create
16+
object = TestEntity()
17+
box.put(object)
18+
19+
# string - default index type is hash
20+
assert box._entity.properties[1]._index_type == IndexType.hash
21+
22+
# int64 - default index type is value
23+
assert box._entity.properties[3]._index_type == IndexType.value
24+
25+
# int32 - index type overwritten to hash
26+
assert box._entity.properties[4]._index_type == IndexType.hash
27+
28+
# int16 - specify index type w/o explicitly enabling index
29+
assert box._entity.properties[5]._index_type == IndexType.hash
30+
31+
32+
def test_index_error():
33+
@Entity(id=3, uid=3)
34+
class TestEntityInvalidIndex:
35+
id = Id(id=1, uid=3001)
36+
37+
# Cannot set index type when index is False
38+
try:
39+
str = Property(str, id=2, uid=3002, index=False, index_type=IndexType.hash)
40+
except Exception:
41+
assert pytest.raises(Exception, match='trying to set index type on property of id 2 while index is set to False')

0 commit comments

Comments
 (0)