Skip to content

Commit 4de7d80

Browse files
committed
Merge branch '12-index-support' into dev
2 parents 021cf3a + 921db8c commit 4de7d80

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

objectbox/model/properties.py

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

6666

67+
class IndexType(IntEnum):
68+
value = OBXPropertyFlags_INDEXED
69+
hash = OBXPropertyFlags_INDEX_HASH
70+
hash64 = OBXPropertyFlags_INDEX_HASH64
71+
72+
6773
class Property:
68-
def __init__(self, py_type: type, id: int, uid: int, type: PropertyType = None):
74+
def __init__(self, py_type: type, id: int, uid: int, type: PropertyType = None, index: bool = None, index_type: IndexType = None):
6975
self._id = id
7076
self._uid = uid
7177
self._name = "" # set in Entity.fill_properties()
@@ -82,6 +88,18 @@ def __init__(self, py_type: type, id: int, uid: int, type: PropertyType = None):
8288
self._fb_slot = self._id - 1
8389
self._fb_v_offset = 4 + 2*self._fb_slot
8490

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

tests/model.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from objectbox.model import *
2+
from objectbox.model.properties import IndexType
23
import numpy as np
34
from datetime import datetime
45
from typing import Generic, Dict, Any
@@ -7,15 +8,17 @@
78
@Entity(id=1, uid=1)
89
class TestEntity:
910
id = Id(id=1, uid=1001)
10-
str = Property(str, id=2, uid=1002)
11+
# TODO Enable indexing dynamically, e.g. have a constructor to enable index(es).
12+
# E.g. indexString=False (defaults to false). Same for bytes.
13+
str = Property(str, id=2, uid=1002, index=True)
1114
bool = Property(bool, id=3, uid=1003)
12-
int64 = Property(int, type=PropertyType.long, id=4, uid=1004)
13-
int32 = Property(int, type=PropertyType.int, id=5, uid=1005)
14-
int16 = Property(int, type=PropertyType.short, id=6, uid=1006)
15+
int64 = Property(int, type=PropertyType.long, id=4, uid=1004, index=True)
16+
int32 = Property(int, type=PropertyType.int, id=5, uid=1005, index=True, index_type=IndexType.hash)
17+
int16 = Property(int, type=PropertyType.short, id=6, uid=1006, index_type=IndexType.hash)
1518
int8 = Property(int, type=PropertyType.byte, id=7, uid=1007)
1619
float64 = Property(float, type=PropertyType.double, id=8, uid=1008)
1720
float32 = Property(float, type=PropertyType.float, id=9, uid=1009)
18-
bytes = Property(bytes, id=10, uid=1010)
21+
bytes = Property(bytes, id=10, uid=1010, index_type=IndexType.hash64)
1922
ints = Property(np.ndarray, type=PropertyType.intVector, id=11, uid=1011)
2023
longs = Property(np.ndarray, type=PropertyType.longVector, id=12, uid=1012)
2124
floats = Property(np.ndarray, type=PropertyType.floatVector, id=13, uid=1013)
@@ -32,6 +35,7 @@ class TestEntity:
3235
def __init__(self, string: str = ""):
3336
self.str = string
3437

38+
3539
@Entity(id=2, uid=2)
3640
class TestEntityDatetime:
3741
id = Id(id=1, uid=2001)

tests/test_index.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
# bytes - index type overwritten to hash64
32+
assert box._entity.properties[9]._index_type == IndexType.hash64
33+
34+
35+
def test_index_error():
36+
@Entity(id=3, uid=3)
37+
class TestEntityInvalidIndex:
38+
id = Id(id=1, uid=3001)
39+
40+
# Cannot set index type when index is False
41+
try:
42+
str = Property(str, id=2, uid=3002, index=False, index_type=IndexType.hash)
43+
except Exception:
44+
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)