-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
collection.py
249 lines (205 loc) · 8.56 KB
/
collection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# PyStore: Flat-file datastore for timeseries data
# https://github.com/ranaroussi/pystore
#
# Copyright 2018-2020 Ran Aroussi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import time
import shutil
import dask.dataframe as dd
import multitasking
from . import utils
from .item import Item
from . import config
class Collection(object):
def __repr__(self):
return "PyStore.collection <%s>" % self.collection
def __init__(self, collection, datastore, engine="pyarrow"):
self.engine = engine
self.datastore = datastore
self.collection = collection
self.items = self.list_items()
self.snapshots = self.list_snapshots()
def _item_path(self, item, as_string=False):
p = utils.make_path(self.datastore, self.collection, item)
if as_string:
return str(p)
return p
@multitasking.task
def _list_items_threaded(self, **kwargs):
self.items = self.list_items(**kwargs)
def list_items(self, **kwargs):
dirs = utils.subdirs(utils.make_path(self.datastore, self.collection))
if not kwargs:
return set(dirs)
matched = []
for d in dirs:
meta = utils.read_metadata(utils.make_path(
self.datastore, self.collection, d))
del meta["_updated"]
m = 0
keys = list(meta.keys())
for k, v in kwargs.items():
if k in keys and meta[k] == v:
m += 1
if m == len(kwargs):
matched.append(d)
return set(matched)
def item(self, item, snapshot=None, filters=None, columns=None):
return Item(item, self.datastore, self.collection,
snapshot, filters, columns, engine=self.engine)
def index(self, item, last=False):
data = dd.read_parquet(self._item_path(item, as_string=True),
columns="index", engine=self.engine)
if not last:
return data.index.compute()
return float(str(data.index).split(
"\nName")[0].split("\n")[-1].split(" ")[0])
def delete_item(self, item, reload_items=False):
shutil.rmtree(self._item_path(item))
self.items.remove(item)
if reload_items:
self.items = self._list_items_threaded()
return True
@multitasking.task
def write_threaded(self, item, data, metadata={},
npartitions=None,
overwrite=False, epochdate=False,
reload_items=False, **kwargs):
return self.write(item, data, metadata,
npartitions, overwrite,
epochdate, reload_items,
**kwargs)
def write(self, item, data, metadata={},
npartitions=None, overwrite=False,
epochdate=False, reload_items=False,
**kwargs):
if utils.path_exists(self._item_path(item)) and not overwrite:
raise ValueError("""
Item already exists. To overwrite, use `overwrite=True`.
Otherwise, use `<collection>.append()`""")
if isinstance(data, Item):
data = data.to_pandas()
else:
# work on copy
data = data.copy()
if epochdate or "datetime" in str(data.index.dtype):
data = utils.datetime_to_int64(data)
if (1 == data.index.nanosecond).any() and "times" not in kwargs:
kwargs["times"] = "int96"
if data.index.name == "":
data.index.name = "index"
if npartitions is None:
memusage = data.memory_usage(deep=True).sum()
if isinstance(data, dd.DataFrame):
npartitions = int(
1 + memusage.compute() // config.PARTITION_SIZE)
data = data.repartition(npartitions=npartitions)
else:
npartitions = int(
1 + memusage // config.PARTITION_SIZE)
data = dd.from_pandas(data, npartitions=npartitions)
else:
if not isinstance(data, dd.DataFrame):
data = dd.from_pandas(data, npartitions=npartitions)
dd.to_parquet(data, self._item_path(item, as_string=True), overwrite=overwrite,
compression="snappy", engine=self.engine, **kwargs)
utils.write_metadata(utils.make_path(
self.datastore, self.collection, item), metadata)
# update items
self.items.add(item)
if reload_items:
self._list_items_threaded()
def append(self, item, data, npartitions=None, epochdate=False,
threaded=False, reload_items=False, **kwargs):
if not utils.path_exists(self._item_path(item)):
raise ValueError(
"""Item do not exists. Use `<collection>.write(...)`""")
# work on copy
data = data.copy()
try:
if epochdate or ("datetime" in str(data.index.dtype) and
any(data.index.nanosecond) > 0):
data = utils.datetime_to_int64(data)
old_index = dd.read_parquet(self._item_path(item, as_string=True),
columns=[], engine=self.engine
).index.compute()
data = data[~data.index.isin(old_index)]
except Exception:
return
if data.empty:
return
if data.index.name == "":
data.index.name = "index"
# combine old dataframe with new
current = self.item(item)
new = dd.from_pandas(data, npartitions=1)
combined = dd.concat([current.data, new])
if npartitions is None:
memusage = combined.memory_usage(deep=True).sum()
if isinstance(combined, dd.DataFrame):
memusage = memusage.compute()
npartitions = int(1 + memusage // config.PARTITION_SIZE)
combined = combined.repartition(npartitions=npartitions).drop_duplicates(
keep="last"
)
tmp_item = "__" + item
# write data
write = self.write_threaded if threaded else self.write
write(tmp_item, combined, npartitions=npartitions,
metadata=current.metadata, overwrite=False,
epochdate=epochdate, reload_items=reload_items, **kwargs)
try:
multitasking.wait_for_tasks()
self.delete_item(item=item,reload_items=False)
shutil.move(self._item_path(tmp_item),self._item_path(item))
self._list_items_threaded()
except Exception as errn:
raise ValueError(
"Error: %s" % repr(errn)
) from errn
def create_snapshot(self, snapshot=None):
if snapshot:
snapshot = "".join(
e for e in snapshot if e.isalnum() or e in [".", "_"])
else:
snapshot = str(int(time.time() * 1000000))
src = utils.make_path(self.datastore, self.collection)
dst = utils.make_path(src, "_snapshots", snapshot)
shutil.copytree(src, dst,
ignore=shutil.ignore_patterns("_snapshots"))
self.snapshots = self.list_snapshots()
return True
def list_snapshots(self):
snapshots = utils.subdirs(utils.make_path(
self.datastore, self.collection, "_snapshots"))
return set(snapshots)
def delete_snapshot(self, snapshot):
if snapshot not in self.snapshots:
# raise ValueError("Snapshot `%s` doesn't exist" % snapshot)
return True
shutil.rmtree(utils.make_path(self.datastore, self.collection,
"_snapshots", snapshot))
self.snapshots = self.list_snapshots()
return True
def delete_snapshots(self):
snapshots_path = utils.make_path(
self.datastore, self.collection, "_snapshots")
shutil.rmtree(snapshots_path)
os.makedirs(snapshots_path)
self.snapshots = self.list_snapshots()
return True