2020import uuid as uuidlib
2121from copy import deepcopy
2222from collections .abc import Sequence
23+ from datetime import timedelta
2324
2425from btrdb .utils .buffer import PointBuffer
2526from btrdb .point import RawPoint , StatPoint
2627from btrdb .transformers import StreamSetTransformer
2728from btrdb .exceptions import BTrDBError , InvalidOperation
2829from btrdb .utils .timez import currently_as_ns , to_nanoseconds
2930from btrdb .utils .conversion import AnnotationEncoder , AnnotationDecoder
31+ from btrdb .utils .general import pointwidth as pw
3032
3133
3234##########################################################################
@@ -122,7 +124,7 @@ def exists(self):
122124 return False
123125 raise bte
124126
125- def count (self , start = MINIMUM_TIME , end = MAXIMUM_TIME , pointwidth = 62 , version = 0 ):
127+ def count (self , start = MINIMUM_TIME , end = MAXIMUM_TIME , pointwidth = 62 , precise = False , version = 0 ):
126128 """
127129 Compute the total number of points in the stream
128130
@@ -134,6 +136,9 @@ def count(self, start=MINIMUM_TIME, end=MAXIMUM_TIME, pointwidth=62, version=0):
134136 windows of time, you may also need to adjust the pointwidth to ensure
135137 that the count granularity is captured appropriately.
136138
139+ Alternatively you can set the precise argument to True which will
140+ give an exact count to the nanosecond but may be slower to execute.
141+
137142 Parameters
138143 ----------
139144 start : int or datetime like object, default: MINIMUM_TIME
@@ -145,7 +150,14 @@ def count(self, start=MINIMUM_TIME, end=MAXIMUM_TIME, pointwidth=62, version=0):
145150 :func:`btrdb.utils.timez.to_nanoseconds` for valid input types)
146151
147152 pointwidth : int, default: 62
148- Specify the number of ns between data points (2**pointwidth)
153+ Specify the number of ns between data points (2**pointwidth). If the value
154+ is too large for the time window than the next smallest, appropriate
155+ pointwidth will be used.
156+
157+ precise : bool, default: False
158+ Forces the use of a windows query instead of aligned_windows
159+ to determine exact count down to the nanosecond. This will
160+ be some amount slower than the aligned_windows version.
149161
150162 version : int, default: 0
151163 Version of the stream to query
@@ -155,8 +167,17 @@ def count(self, start=MINIMUM_TIME, end=MAXIMUM_TIME, pointwidth=62, version=0):
155167 int
156168 The total number of points in the stream for the specified window.
157169 """
158- points = self .aligned_windows (start , end , pointwidth , version )
159- return sum ([point .count for point , _ in points ])
170+
171+ if not precise :
172+ pointwidth = min (pointwidth , pw .from_nanoseconds (to_nanoseconds (end ) - to_nanoseconds (start ))- 1 )
173+ points = self .aligned_windows (start , end , pointwidth , version )
174+ return sum ([point .count for point , _ in points ])
175+
176+ depth = 0
177+ width = to_nanoseconds (end ) - to_nanoseconds (start )
178+ points = self .windows (start , end , width , depth , version )
179+ return sum ([point .count for point , _ in points ])
180+
160181
161182 @property
162183 def btrdb (self ):
0 commit comments