From 35eaaee627f2eb98065fe938542bc65434a9beca Mon Sep 17 00:00:00 2001 From: aum Date: Tue, 17 Aug 2004 16:57:21 +0000 Subject: [PATCH] added -l flag for local-only put/get --- apps/stasher/python/src/code.leo | 75 +++++++++++++++++++++++------- apps/stasher/python/src/stasher.py | 69 +++++++++++++++++++++------ 2 files changed, 115 insertions(+), 29 deletions(-) diff --git a/apps/stasher/python/src/code.leo b/apps/stasher/python/src/code.leo index 19ba423c8..4ae647bd2 100644 --- a/apps/stasher/python/src/code.leo +++ b/apps/stasher/python/src/code.leo @@ -2,7 +2,7 @@ - + @@ -291,9 +291,9 @@ spawnproc killproc i2psocket -usage +usage err -main +main MAINLINE mainline @@ -767,6 +767,10 @@ except: - a raw string which will be hashed into a KHash object - val - a string, the value associated with the key + Keywords: + - local - default False - if True, limits the insert to the + local node + If the value is larger than L{maxValueSize}, a L{KValueTooLarge} exception will occur. """ @@ -4976,10 +4980,10 @@ stasher.logToSocket = 19199 try: opts, args = getopt.getopt(sys.argv[1:], - "h?vV:S:C:sd:f", + "h?vV:S:C:sd:fl", ['help', 'version', 'samaddr=', 'clientaddr=', 'verbosity=', 'status', 'datadir=', 'foreground', - 'shortversion', + 'shortversion', 'localonly', ]) except: traceback.print_exc(file=sys.stdout) @@ -4989,6 +4993,7 @@ stasher.logToSocket = 19199 verbosity = 2 debug = False foreground = False + localOnly = False for opt, val in opts: @@ -5022,6 +5027,9 @@ stasher.logToSocket = 19199 sys.stdout.flush() sys.exit(0) + elif opt in ['-l', '--localonly']: + localOnly = True + #print "Debug - bailing" #print repr(opts) #print repr(args) @@ -5147,7 +5155,10 @@ stasher.logToSocket = 19199 else: outfile = sys.stdout - res = client.get(key) + if logVerbosity >= 3: + sys.stderr.write("Searching for key - may take up to %s seconds or more\n" % ( + timeout['findData'])) + res = client.get(key, local=localOnly) if res == None: err("Failed to retrieve '%s'" % key) sys.exit(1) @@ -5179,7 +5190,10 @@ stasher.logToSocket = 19199 if len(val) > maxValueSize: err("File is too big - please trim to %s" % maxValueSize) - res = client.put(key, val) + if logVerbosity >= 3: + sys.stderr.write("Inserting key - may take up to %s seconds\n" % ( + timeout['findNode'] + timeout['store'])) + res = client.put(key, val, local=localOnly) if res == None: err("Failed to insert '%s'" % key) sys.exit(1) @@ -5274,6 +5288,8 @@ stasher.logToSocket = 19199 print " default is ~/.stasher" print " -f, --foreground - only valid for 'start' cmd - runs the node" print " in foreground without spawning - for debugging" + print " -l, --localonly - only valid for get/put - restricts the get/put" + print " operation to the local node only" print print "Commands:" print " start [<nodename>]" @@ -5306,7 +5322,6 @@ stasher.logToSocket = 19199 sys.exit(0) - class KNodeServer(KBase, SocketServer.ThreadingMixIn, SocketServer.TCPServer): @@ -5351,8 +5366,9 @@ stasher.logToSocket = 19199 self.log(3, "cmd=%s args=%s" % (repr(cmd), repr(args))) - if cmd == "get": - value = node.get(args) + if cmd in ["get", "getlocal"]: + isLocal = cmd == "getlocal" + value = node.get(args, local=isLocal) if value == None: write("notfound\n") else: @@ -5362,11 +5378,12 @@ stasher.logToSocket = 19199 finish() return - elif cmd == "put": + elif cmd in ["put", "putlocal"]: + isLocal = cmd == "putlocal" try: size = int(readline()) value = read(size) - res = node.put(args, value) + res = node.put(args, value, local=isLocal) if res: write("ok\n") else: @@ -5490,14 +5507,27 @@ stasher.logToSocket = 19199 raise Exception("Not connected to valid stasher interface") -def get(self, key): +def get(self, key, **kw): """ sends a get command to stasher socket, and retrieves and interprets result + + Arguments: + - key - key to retrieve + + Keywords: + - local - default False - if True, only looks in local storage + + Returns key's value if found, or None if key not found """ + if kw.get('local', False): + cmd = 'getlocal' + else: + cmd = 'get' + self.connect() - self.write("get %s\n" % key) + self.write("%s %s\n" % (cmd, key)) self.flush() #print "waiting for resp line" @@ -5525,12 +5555,25 @@ stasher.logToSocket = 19199 self.connect() self.close() -def put(self, key, val): +def put(self, key, val, **kw): """ Tells remote stasher port to insert a file into the network + + Arguments: + - key - key to insert under + - val - value to insert under this key + + Keywords: + - local - default False - if True, only looks in local storage + """ + if kw.get('local', False): + cmd = 'putlocal' + else: + cmd = 'put' + self.connect() - self.write("put %s\n" % key) + self.write("%s %s\n" % (cmd, key)) self.write("%s\n" % len(val)) self.write(val) self.flush() diff --git a/apps/stasher/python/src/stasher.py b/apps/stasher/python/src/stasher.py index bfed65246..d007282a2 100644 --- a/apps/stasher/python/src/stasher.py +++ b/apps/stasher/python/src/stasher.py @@ -2753,8 +2753,9 @@ class KNodeReqHandler(KBase, SocketServer.StreamRequestHandler): self.log(3, "cmd=%s args=%s" % (repr(cmd), repr(args))) - if cmd == "get": - value = node.get(args) + if cmd in ["get", "getlocal"]: + isLocal = cmd == "getlocal" + value = node.get(args, local=isLocal) if value == None: write("notfound\n") else: @@ -2764,11 +2765,12 @@ class KNodeReqHandler(KBase, SocketServer.StreamRequestHandler): finish() return - elif cmd == "put": + elif cmd in ["put", "putlocal"]: + isLocal = cmd == "putlocal" try: size = int(readline()) value = read(size) - res = node.put(args, value) + res = node.put(args, value, local=isLocal) if res: write("ok\n") else: @@ -2884,14 +2886,27 @@ class KNodeClient(KBase): #@-node:close #@+node:get - def get(self, key): + def get(self, key, **kw): """ sends a get command to stasher socket, and retrieves and interprets result + + Arguments: + - key - key to retrieve + + Keywords: + - local - default False - if True, only looks in local storage + + Returns key's value if found, or None if key not found """ + if kw.get('local', False): + cmd = 'getlocal' + else: + cmd = 'get' + self.connect() - self.write("get %s\n" % key) + self.write("%s %s\n" % (cmd, key)) self.flush() #print "waiting for resp line" @@ -2908,12 +2923,25 @@ class KNodeClient(KBase): #@-node:get #@+node:put - def put(self, key, val): + def put(self, key, val, **kw): """ Tells remote stasher port to insert a file into the network + + Arguments: + - key - key to insert under + - val - value to insert under this key + + Keywords: + - local - default False - if True, only looks in local storage + """ + if kw.get('local', False): + cmd = 'putlocal' + else: + cmd = 'put' + self.connect() - self.write("put %s\n" % key) + self.write("%s %s\n" % (cmd, key)) self.write("%s\n" % len(val)) self.write(val) self.flush() @@ -3220,6 +3248,10 @@ class KNode(KBase): - a raw string which will be hashed into a KHash object - val - a string, the value associated with the key + Keywords: + - local - default False - if True, limits the insert to the + local node + If the value is larger than L{maxValueSize}, a L{KValueTooLarge} exception will occur. """ @@ -4035,6 +4067,8 @@ def usage(detailed=False, ret=0): print " default is ~/.stasher" print " -f, --foreground - only valid for 'start' cmd - runs the node" print " in foreground without spawning - for debugging" + print " -l, --localonly - only valid for get/put - restricts the get/put" + print " operation to the local node only" print print "Commands:" print " start []" @@ -4067,7 +4101,6 @@ def usage(detailed=False, ret=0): sys.exit(0) - #@-node:usage #@+node:err def err(msg): @@ -4085,10 +4118,10 @@ def main(): try: opts, args = getopt.getopt(sys.argv[1:], - "h?vV:S:C:sd:f", + "h?vV:S:C:sd:fl", ['help', 'version', 'samaddr=', 'clientaddr=', 'verbosity=', 'status', 'datadir=', 'foreground', - 'shortversion', + 'shortversion', 'localonly', ]) except: traceback.print_exc(file=sys.stdout) @@ -4098,6 +4131,7 @@ def main(): verbosity = 2 debug = False foreground = False + localOnly = False for opt, val in opts: @@ -4131,6 +4165,9 @@ def main(): sys.stdout.flush() sys.exit(0) + elif opt in ['-l', '--localonly']: + localOnly = True + #print "Debug - bailing" #print repr(opts) #print repr(args) @@ -4256,7 +4293,10 @@ def main(): else: outfile = sys.stdout - res = client.get(key) + if logVerbosity >= 3: + sys.stderr.write("Searching for key - may take up to %s seconds or more\n" % ( + timeout['findData'])) + res = client.get(key, local=localOnly) if res == None: err("Failed to retrieve '%s'" % key) sys.exit(1) @@ -4288,7 +4328,10 @@ def main(): if len(val) > maxValueSize: err("File is too big - please trim to %s" % maxValueSize) - res = client.put(key, val) + if logVerbosity >= 3: + sys.stderr.write("Inserting key - may take up to %s seconds\n" % ( + timeout['findNode'] + timeout['store'])) + res = client.put(key, val, local=localOnly) if res == None: err("Failed to insert '%s'" % key) sys.exit(1)