def __init__(self, isMaster): self.TagModel = objectbox.Model() self.LedModel = objectbox.Model() # load entities with last property entry # self.anchor_entity = self.model.entity(Anchor, last_property_id=objectbox.model.IdUid(8, 1008)) self.tag_entity = self.TagModel.entity( Tag, last_property_id=objectbox.model.IdUid(8, 2008)) self.led_entity = self.LedModel.entity( Led, last_property_id=objectbox.model.IdUid(4, 3004)) # define number of entities self.TagModel.last_entity_id = objectbox.model.IdUid(3, 3) self.LedModel.last_entity_id = objectbox.model.IdUid(3, 3) sync_uri = "ws://192.168.5.1:9999" # initialize an empty DB before the first server start the server if isMaster is True: sync_uri = "ws://127.0.0.1:9999" sleep(1) # the Box, where you can put Tag / Led objects in self.tag = objectbox.Box( objectbox.Builder().model( self.TagModel).directory("tag-db").build(), Tag) self.led = objectbox.Box( objectbox.Builder().model( self.LedModel).sync_uri(sync_uri).directory("led-db").build(), Led)
def test_box_bulk(): ob = load_empty_test_objectbox() box = objectbox.Box(ob, TestEntity) box.put(TestEntity("first")) objects = [ TestEntity("second"), TestEntity("third"), TestEntity("fourth"), box.get(1) ] box.put(objects) assert box.count() == 4 assert objects[0].id == 2 assert objects[1].id == 3 assert objects[2].id == 4 assert objects[3].id == 1 assert_equal(box.get(objects[0].id), objects[0]) assert_equal(box.get(objects[1].id), objects[1]) assert_equal(box.get(objects[2].id), objects[2]) assert_equal(box.get(objects[3].id), objects[3]) objects_read = box.get_all() assert len(objects_read) == 4 assert_equal(objects_read[0], objects[3]) assert_equal(objects_read[1], objects[0]) assert_equal(objects_read[2], objects[1]) assert_equal(objects_read[3], objects[2]) # remove all removed = box.remove_all() assert removed == 4 assert box.count() == 0
class TasklistCmd(Cmd): prompt = "> " _ob = objectbox.Builder().model( get_objectbox_model()).directory("tasklist-db").build() _box = objectbox.Box(_ob, Task) def do_ls(self, _): """list tasks""" tasks = self._box.get_all() print("%3s %-29s %-29s %s" % ("ID", "Created", "Finished", "Text")) for task in tasks: print("%3d %-29s %-29s %s" % (task.id, format_date(task.date_created), format_date(task.date_finished), task.text)) def do_new(self, text: str): """create a new task with the given text (all arguments concatenated)""" task = Task() task.text = text task.date_created = now_ms() self._box.put(task) def do_done(self, id: str): """mark task with the given ID as done""" task = self._box.get(int(id)) task.date_finished = now_ms() self._box.put(task) def do_exit(self, _): """close the program""" raise SystemExit()
def test_box_basics(): ob = load_empty_test_objectbox() box = objectbox.Box(ob, TestEntity) assert box.is_empty() assert box.count() == 0 # create object = TestEntity() id = box.put(object) assert id == 1 assert id == object.id # create with a given ID and some data object = TestEntity() object.id = 5 object.bool = True object.int = 42 object.str = "foo" object.float = 4.2 object.bytes = bytes([1, 1, 2, 3, 5]) object.transient = "abcd" id = box.put(object) assert id == 5 assert id == object.id # check the count assert not box.is_empty() assert box.count() == 2 # read read = box.get(object.id) assert_equal(read, object) assert read.transient != object.transient # != # update object.str = "bar" id = box.put(object) assert id == 5 # read again read = box.get(object.id) assert_equal(read, object) # remove box.remove(object) box.remove(1) # check they're gone assert box.count() == 0 with pytest.raises(objectbox.NotFoundException): box.get(object.id) with pytest.raises(objectbox.NotFoundException): box.get(1)
def test_transactions(): ob = load_empty_test_objectbox() box = objectbox.Box(ob, TestEntity) assert box.is_empty() with ob.write_tx(): box.put(TestEntity("first")) box.put(TestEntity("second")) assert box.count() == 2 try: with ob.write_tx(): box.put(TestEntity("third")) box.put(TestEntity("fourth")) raise Exception("mission abort!") # exception must be propagated so this line must not execute assert 0 except Exception as err: assert str(err) == "mission abort!" # changes have been rolled back assert box.count() == 2 # can't write in a read TX try: with ob.read_tx(): box.put(TestEntity("third")) # exception must be propagated so this line must not execute assert 0 except Exception as err: assert "Cannot start a write transaction inside a read only transaction" in str( err)
endAt = get_property(json_campaign, 'endAt', str) campaign_last_prop_id, campaign_last_prop_uid = get_id_uid( json_campaign['lastPropertyId']) last_entity_id, last_entity_uid = get_id_uid(object_box['lastEntityId']) last_index_id, last_index_uid = get_id_uid(object_box['lastIndexId']) last_relation_id, last_relation_uid = get_id_uid(object_box['lastRelationId']) model = objectbox.Model() model.entity(CampaignDB, last_property_id=IdUid(campaign_last_prop_id, campaign_last_prop_uid)) model.last_entity_id = IdUid(last_entity_id, last_entity_uid) model.last_index_id = IdUid(last_index_id, last_index_uid) model.last_relation_id = IdUid(last_relation_id, last_relation_uid) print('Model={}'.format(model.last_entity_id)) print('Opening database...') ob = objectbox.Builder().model(model).directory("ouest").build() print('Build done') print('Opening table...') box = objectbox.Box(ob, CampaignDB) print('Dumping data') # id = box.put(person) for person in box.get_all(): print(person.first_name)
def __init__(self): self.ob = load_empty_test_objectbox() self.box = objectbox.Box(self.ob, TestEntity)
from objectbox.model import * import objectbox @Entity(id=1, uid=1) class Person(): id = Id(id=1, uid=1001) #first_name = Property(str, id=2, uid=1002) #last_name = Property(str, id=3, uid=1003) first_name=list() # from mypackage.model import Person # Configure ObjectBox: should be done only once in the whole program and the "ob" variable should be kept around model = objectbox.Model() model.entity(Person, last_property_id=objectbox.model.IdUid(3, 1003)) model.last_entity_id = objectbox.model.IdUid(1, 1) ob = objectbox.Builder().model(model).directory("db").build() # Open the box of "Person" entity. This can be called many times but you can also pass the variable around box = objectbox.Box(ob, Person) id = box.put(Person()) # Create person = box.get(id) # Read #print(person) #person.last_name = "Black" person.first_name.append("faisal") box.put(person) # Update #box.remove(person) # Delete print(person.first_name)