Skip to content

Commit 6cca6ea

Browse files
committed
iobeam.createDataStore will return previously made DataStore if it has the same columns
1 parent d7b0d80 commit 6cca6ea

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

iobeam/iobeam.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ def createDataStore(self, columns):
310310
DataStore object with those columns and being tracked
311311
by this client for sending.
312312
"""
313+
for store in self._batches:
314+
if store.hasSameColumns(columns):
315+
return store
316+
313317
ds = data.DataStore(columns)
314318
self._batches.append(ds)
315319

iobeam/resources/data.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,16 @@ def rows(self):
210210
ret.append(r.copy())
211211
return ret
212212

213+
def hasSameColumns(self, cols):
214+
"""Check if this datastore has exactly a list of columns."""
215+
if cols is None or not isinstance(cols, list):
216+
return False
217+
elif len(cols) != len(self._columns):
218+
return False
219+
else:
220+
return set(cols) == set(self._columns)
221+
222+
213223
def split(self, chunkSize):
214224
"""Split a store into multiple batches with `chunkSize` rows.
215225

tests/resources/test_data.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,21 @@ def verify(time, data):
203203
for (t, d) in cases:
204204
verify(t, d)
205205

206+
def test_hasSameColumns(self):
207+
columns = ["a", "b", "c"]
208+
ds = data.DataStore(columns)
209+
cases = [
210+
(["c", "b", "a"], True),
211+
(["b", "c", "a"], True),
212+
(["a"], False),
213+
(["a", "b", "d"], False),
214+
(["a", "b", "b"], False),
215+
("abc", False)
216+
]
217+
218+
for c, res in cases:
219+
self.assertEqual(ds.hasSameColumns(c), res)
220+
206221
def test_split(self):
207222
columns = ["a", "b", "c"]
208223
ds = data.DataStore(columns)

tests/test_iobeam.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,18 @@ def test_addDataStore(self):
369369
client.addDataStore(ds)
370370
self.assertEqual(1, len(client._batches))
371371

372+
def test_createDataStoreWithSameCols(self):
373+
dummy = DummyBackend()
374+
backend = request.DummyRequester(dummy)
375+
client = self._makeTempClient(backend=backend, deviceId="fake")
376+
377+
ds = client.createDataStore(["col1", "col2", "col3"])
378+
self.assertEqual(1, len(client._batches))
379+
ds2 = client.createDataStore(["col3", "col1", "col2"])
380+
self.assertEqual(1, len(client._batches))
381+
self.assertEqual(ds, ds2)
382+
383+
372384
def test_sendWithBatch(self):
373385
dummy = DummyBackend()
374386
backend = request.DummyRequester(dummy)

0 commit comments

Comments
 (0)