def getgin(self): __func = gdp.gdp_event_getgin __func.argtypes = [POINTER(self.gdp_event_t)] __func.restype = POINTER(_gdp.GDP_GIN.gdp_gin_t) gin_ptr = __func(self.ptr) gin = _gdp.GDP_GIN(None, None, ptr=gin_ptr) gin.event = self # garbage collection workaround return gin
def main(name_str): # create a python object _name = gdp.GDP_NAME(name_str) gin_handle = gdp.GDP_GIN(_name, gdp.GDP_MODE_RO) # this is the actual subscribe call gin_handle.subscribe_by_recno(0, 0, None) while True: # This blocks, until there is a new event event = gin_handle.get_next_event(None) datum = event["datum"] print datum["buf"].peek()
def main(name_str, keyfile=None): # the data that will be written data = generate_random_data(1000, 1000) # XXX handling secret key at Python level doesn't work at the moment open_info = {} if keyfile is not None: skey = gdp.EP_CRYPTO_KEY(filename=keyfile, keyform=gdp.EP_CRYPTO_KEYFORM_PEM, flags=gdp.EP_CRYPTO_F_SECRET) open_info = {'skey': skey} gin_name = gdp.GDP_NAME(name_str) _name = "".join(["%0.2x" % ord(x) for x in gin_name.internal_name()]) print "opening", _name gin_handle = gdp.GDP_GIN(gin_name, gdp.GDP_MODE_RA, open_info) # writing the actual data for (idx, s) in enumerate(data): print "writing message", idx datum = gdp.GDP_DATUM() datum["buf"].write(s) # C implementation does not handle more than # a single datum appends at a time... yet. gin_handle.append_async([datum]) ## collect as many events as we had append operations for idx in xrange(len(data)): e = gin_handle.get_next_event(None) assert e["type"] == gdp.GDP_EVENT_CREATED or \ e["type"] == gdp.GDP_EVENT_SUCCESS ################################ #### reading the data back #### ################################ read_data = [] gin_handle.read_by_recno_async(-1 * len(data), len(data)) while True: e = gin_handle.get_next_event(None) if e["type"] == gdp.GDP_EVENT_DONE: break assert e["type"] == gdp.GDP_EVENT_DATA read_data.append(e["datum"]["buf"].peek()) print len(data), len(read_data) for idx in xrange(len(data)): # verify correctness if data[idx] == read_data[idx]: print "message %d matches" % idx
def main(name_str, tv_sec, tv_nsec, tv_accuracy=0.5): # create a python object _name = gdp.GDP_NAME(name_str) print _name.printable_name() # Assume that the GCL already exists gin_handle = gdp.GDP_GIN(_name, gdp.GDP_MODE_RO) # Create a dictionary for the timestamp. This is the preferred format # for timstamps in gdp. ts = {'tv_sec': tv_sec, 'tv_nsec': tv_nsec, 'tv_accuracy': tv_accuracy} # query by time stamp datum = gin_handle.read_by_ts(ts) print datum
def main(name_str, keyfile): skey = gdp.EP_CRYPTO_KEY(filename=keyfile, keyform=gdp.EP_CRYPTO_KEYFORM_PEM, flags=gdp.EP_CRYPTO_F_SECRET) # Create a GDP_NAME object from a python string provided as argument _name = gdp.GDP_NAME(name_str) # There's a GCL with the given name, so let's open it gin_handle = gdp.GDP_GIN(_name, gdp.GDP_MODE_AO, open_info={'skey': skey}) datum = gdp.GDP_DATUM() while True: line = sys.stdin.readline().strip() # read from stdin datum["buf"].reset() datum["buf"].write(line) gin_handle.append(datum) # Write this datum to the GCL
def main(name_str, start, stop): # create a python object _name = gdp.GDP_NAME(name_str) print _name.printable_name() # Assume that the GCL already exists gin_handle = gdp.GDP_GIN(_name, gdp.GDP_MODE_RO) # initialize this to the first record number recno = start while recno <= stop: try: datum = gin_handle.read_by_recno(recno) print datum["buf"].peek() recno += 1 except Exception as e: # Typically, end of log. raise e
def main(name_str, keyfile=None): # the data that will be written data = generate_random_data(100, 10) # XXX handling secret key at Python level doesn't work at the moment open_info = {} if keyfile is not None: skey = gdp.EP_CRYPTO_KEY(filename=keyfile, keyform=gdp.EP_CRYPTO_KEYFORM_PEM, flags=gdp.EP_CRYPTO_F_SECRET) open_info = {'skey': skey} gin_name = gdp.GDP_NAME(name_str) _name = "".join(["%0.2x" % ord(x) for x in gin_name.internal_name()]) print "opening", _name gin_handle = gdp.GDP_GIN(gin_name, gdp.GDP_MODE_RA, open_info) # writing the actual data datum = gdp.GDP_DATUM() # reusable datum for (idx, s) in enumerate(data): print "writing message", idx datum["buf"].reset() datum["buf"].write(s) gin_handle.append(datum) ################################ #### reading the data back #### ################################ # read by recno (synchronous) read_by_recno = [] for idx in xrange(-1*len(data),0): print "reading message", -1*idx, "from the end" datum = gin_handle.read_by_recno(idx) # -n => n-th record from end read_by_recno.append(datum["buf"].peek()) for idx in xrange(len(data)): # verify correctness if data[idx] == read_by_recno[idx]: print "message %d matches" % idx
def __init__(self, logname, limit=10000): """ Initialize with just the log name, and optionally cache size limit is the number of records to keep in the cache. This is a soft limit, which means that we will go over the limit on various occasions, but we will try to be within a certain factor of the specified limit (by default, 2). This enables us to minimize the cleanup overhead. """ gdp.gdp_init() # No side-effects of calling this multiple times # gdp.dbg_set("*=20") self.logname = logname self.lh = gdp.GDP_GIN(gdp.GDP_NAME(logname), gdp.GDP_MODE_RO) self.limit = limit self.cache = {} # recno => record cache (limited size) self.atime = {} # recno => time of access (same size as cache) ## populate the limits self.leastRecent() self.mostRecent()
def main(name_str, start, stop): # create a python object _name = gdp.GDP_NAME(name_str) # Assume that the GCL already exists gin_handle = gdp.GDP_GIN(_name, gdp.GDP_MODE_RO) # this is the actual subscribe call gin_handle.read_by_recno_async(start, stop - start + 1) # timeout t = {'tv_sec': 0, 'tv_nsec': 500 * (10**6), 'tv_accuracy': 0.0} while True: # This could return a None, after the specified timeout event = gin_handle.get_next_event(t) if event is None or event["type"] == gdp.GDP_EVENT_DONE: break datum = event["datum"] assert event["gin"] == gin_handle ## sanity check print datum["buf"].peek()