<commit_before># Copyright 2014 Aerospike, Inc

# 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 aerospike
import time

MODULE_NAME = "expire_bin"
GET_OP = "get"
PUT_OP = "put"
BATCH_PUT_OP = "puts"
TTL_OP = "ttl"
TOUCH_OP = "touch"
CLEAN_OP = "clean"

class ExpireBin:
	def __init__(self, client, policy):
		"""Initialize the ExpireBin module

		Args:
			client -- aerospike client to perform ops
			policy -- policy to use for ops
		"""

		self.client = client
		self.policy = policy

	def get(self, key, *bins):
		"""Attempt to retrieve values from list of bins. The bins
		can be expire bins or normal bins.

		Args:
			key -- tuple (namespace, set, record name)
			*bins -- one or more bin names to retrieve values from

		Returns:
			list: A list with each value respective to the list of bin
			names passed in. If a bin is expired or empty, the index
			in the list will contain None.

		Raises:
			Exception: Exception with details of server error.
		"""
		return self.client.apply(key, MODULE_NAME, GET_OP, list(bins), self.policy)

	def put(self, key, bin, val, bin_ttl, create):
		"""Create or update expire bins. If the create flag is set to true,
		all newly created bins will be expire bins otherwise, only normal bins will be created
		and existing expire bins will be updated. Note: existing expire bins will not be converted
		into normal bins if create flag is off.

		Args:
			key -- tuple (namespace, set, record name)
			bin -- bin name
			val -- value to store into bin
			bin_ttl -- expiration time in seconds or -1 for no expiration
			create -- bool, set to true to create expire bins, false to create normal bins

		Returns:
			int: 0 if success, 1 otherwise

		Raises:
			Exception: Exception with details of server error.
		"""
		if create:
			create = 0
		else:
			create = 1
		return self.client.apply(key, MODULE_NAME, PUT_OP, [bin, val, bin_ttl, create], self.policy)

	def puts(self, key, *binMaps):
		"""Batch create or update expire bins for a given key. Use the dict
			{'bin' : bin_name, 'val' : bin_value, 'bin_ttl' : ttl}
		to store each put operation. Omit the bin_ttl to turn bin creation off.

		Args:
			key -- tuple (namespace, set, record name)
			*binMaps -- dict {'bin' : bin_name, 'val' : bin_val, 'bin_ttl' : ttl}

		Returns:
			int: 0 if all ops succeeded, 1 if error occurred

		Raises:
			Exception: Exception with details of server error.

		"""
		return self.client.apply(key, MODULE_NAME, BATCH_PUT_OP, list(binMaps), self.policy)

	def touch(self, key, *mapBins):
		"""Batch update the bin TTLs. Us this method to change or reset the bin TTL of
		multiple bins in a record. Use a dict to store each touch operation.

		Args:
			key -- tuple (namespace, set, record name)
			*mapBins -- dict {'bin' : bin_name, 'bin_ttl' : ttl}

		Returns:
			int: 0 on success of ALL touch operations, 1 if a failure occurs

		Raises:
			Exception: Exception with details of server error.
		"""
		return self.client.apply(key, MODULE_NAME, TOUCH_OP, list(mapBins), self.policy)

	def clean(self, scan, *bins):
		"""Clear out the expired bins on a scan of the database

		Args:
			scan -- Scan object to run bin clean on
			*bins -- bin names to clean out

		Raises:
			Exception: Exception with details of server error.
		"""
		def callback((key, meta, record)):
			self.client.apply(key, MODULE_NAME, "does_not_exist", list(bins), self.policy)
		scan.foreach(callback)

	def ttl(self, key, bin):
		"""Get the time bin will expire in seconds.

		Args:
			key -- tuple (namespace, set, record name)
			bin -- bin name

		Returns:
			int: Time to expire in seconds

		Raises:
			Exception: Exception with details of server error.
		"""
		return self.client.apply(key, MODULE_NAME, TTL_OP, [bin], self.policy)

def main():
	config = { 'hosts' : [ ('127.0.0.1', 3000) ]}
	policy = { "timeout" : 2000 }
	testClient = aerospike.client(config).connect()
	eb = ExpireBin(testClient, policy)
	key1 = ("test", "expireBin", "eb1")
	key2 = ("test", "expireBin", "eb2")
	key3 = ("test", "expireBin", "eb3")

	print "Creating expire bins..."

	print "TestBin 1: {0}".format("Inserted!" if eb.put(key1, "TestBin", "Hello World", -1, True) is 0 else "Insertion Failed")
	print "TestBin 2: {0}".format("Inserted!" if eb.put(key2, "TestBin", "I don't expire", -1, True) is 0 else "Insertion Failed")
	print "TestBin 3: {0}".format("Inserted!" if eb.put(key3, "TestBin", "I will expire soon", 5, True) is 0 else "Insertion Failed")

	print "Getting expire bins..."

	print "TestBin 1: {0}".format(eb.get(key1, "TestBin"))
	print "TestBin 2: {0}".format(eb.get(key2, "TestBin"))
	print "TestBin 3: {0}".format(eb.get(key3, "TestBin"))

	print "Current time is: {0}\nGetting bin TTLs...".format(time.time())
	print "TestBin 1 TTL: {0}", eb.ttl(key1, "TestBin")
	print "TestBin 2 TTL: {0}", eb.ttl(key2, "TestBin")
	print "TestBin 3 TTL: {0}", eb.ttl(key3, "TestBin")

	print "Waiting for TestBin 3 to expire..."

	time.sleep(10)

	print "Getting expire bins again..."

	print "TestBin 1: {0}".format(eb.get(key1, "TestBin"))
	print "TestBin 2: {0}".format(eb.get(key2, "TestBin"))
	print "TestBin 3: {0}".format(eb.get(key3, "TestBin"))

	print "Cleaning bins..."

	#eb.clean_rec(key1, "TestBin")
	#eb.clean_rec(key2, "TestBin")
	#eb.clean_rec(key3, "TestBin")
	testScan = testClient.scan("test", "expireBin")
	eb.clean(testScan, "TestBin")

if __name__ == "__main__":
	main()

<commit_msg>cleanup python wrapper<commit_after># Copyright 2014 Aerospike, Inc

# 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 aerospike
import time

MODULE_NAME = "expire_bin"
GET_OP = "get"
PUT_OP = "put"
BATCH_PUT_OP = "puts"
TTL_OP = "ttl"
TOUCH_OP = "touch"
CLEAN_OP = "clean"

class ExpireBin:
	def __init__(self, client, policy):
		"""Initialize the ExpireBin module

		Args:
			client -- aerospike client to perform ops
			policy -- policy to use for ops
		"""

		self.client = client
		self.policy = policy

	def get(self, key, *bins):
		"""Attempt to retrieve values from list of bins. The bins
		can be expire bins or normal bins.

		Args:
			key -- tuple (namespace, set, record name)
			*bins -- one or more bin names to retrieve values from

		Returns:
			list: A list with each value respective to the list of bin
			names passed in. If a bin is expired or empty, the index
			in the list will contain None.

		Raises:
			Exception: Exception with details of server error.
		"""
		return self.client.apply(key, MODULE_NAME, GET_OP, list(bins), self.policy)

	def put(self, key, bin, val, bin_ttl, create):
		"""Create or update expire bins. If the create flag is set to true,
		all newly created bins will be expire bins otherwise, only normal bins will be created
		and existing expire bins will be updated. Note: existing expire bins will not be converted
		into normal bins if create flag is off.

		Args:
			key -- tuple (namespace, set, record name)
			bin -- bin name
			val -- value to store into bin
			bin_ttl -- expiration time in seconds or -1 for no expiration
			create -- bool, set to true to create expire bins, false to create normal bins

		Returns:
			int: 0 if success, 1 otherwise

		Raises:
			Exception: Exception with details of server error.
		"""
		if create:
			create = 0
		else:
			create = 1
		return self.client.apply(key, MODULE_NAME, PUT_OP, [bin, val, bin_ttl, create], self.policy)

	def puts(self, key, *binMaps):
		"""Batch create or update expire bins for a given key. Use the dict
			{'bin' : bin_name, 'val' : bin_value, 'bin_ttl' : ttl}
		to store each put operation. Omit the bin_ttl to turn bin creation off.

		Args:
			key -- tuple (namespace, set, record name)
			*binMaps -- dict {'bin' : bin_name, 'val' : bin_val, 'bin_ttl' : ttl}

		Returns:
			int: 0 if all ops succeeded, 1 if error occurred

		Raises:
			Exception: Exception with details of server error.

		"""
		return self.client.apply(key, MODULE_NAME, BATCH_PUT_OP, list(binMaps), self.policy)

	def touch(self, key, *mapBins):
		"""Batch update the bin TTLs. Us this method to change or reset the bin TTL of
		multiple bins in a record. Use a dict to store each touch operation.

		Args:
			key -- tuple (namespace, set, record name)
			*mapBins -- dict {'bin' : bin_name, 'bin_ttl' : ttl}

		Returns:
			int: 0 on success of ALL touch operations, 1 if a failure occurs

		Raises:
			Exception: Exception with details of server error.
		"""
		return self.client.apply(key, MODULE_NAME, TOUCH_OP, list(mapBins), self.policy)

	def clean(self, scan, *bins):
		"""Clear out the expired bins on a scan of the database

		Args:
			scan -- Scan object to run bin clean on
			*bins -- bin names to clean out

		Raises:
			Exception: Exception with details of server error.
		"""
		raise Exception("Scan UDF not yet implemented in python client")
		#def callback((key, meta, record)):
		#	self.client.apply(key, MODULE_NAME, "does_not_exist", list(bins), self.policy)
		#scan.foreach(callback)

	def ttl(self, key, bin):
		"""Get the time bin will expire in seconds.

		Args:
			key -- tuple (namespace, set, record name)
			bin -- bin name

		Returns:
			int: Time to expire in seconds

		Raises:
			Exception: Exception with details of server error.
		"""
		return self.client.apply(key, MODULE_NAME, TTL_OP, [bin], self.policy)

def main():
	config = { 'hosts' : [ ('127.0.0.1', 3000) ]}
	policy = { "timeout" : 2000 }
	testClient = aerospike.client(config).connect()
	eb = ExpireBin(testClient, policy)
	key1 = ("test", "expireBin", "eb1")
	key2 = ("test", "expireBin", "eb2")
	key3 = ("test", "expireBin", "eb3")

	print "Creating expire bins..."

	print "TestBin 1: {0}".format("Inserted!" if eb.put(key1, "TestBin", "Hello World", -1, True) is 0 else "Insertion Failed")
	print "TestBin 2: {0}".format("Inserted!" if eb.put(key2, "TestBin", "I don't expire", -1, True) is 0 else "Insertion Failed")
	print "TestBin 3: {0}".format("Inserted!" if eb.put(key3, "TestBin", "I will expire soon", 5, True) is 0 else "Insertion Failed")

	print "Getting expire bins..."

	print "TestBin 1: {0}".format(eb.get(key1, "TestBin"))
	print "TestBin 2: {0}".format(eb.get(key2, "TestBin"))
	print "TestBin 3: {0}".format(eb.get(key3, "TestBin"))

	print "Current time is: {0}\nGetting bin TTLs...".format(time.time())
	print "TestBin 1 TTL: {0}", eb.ttl(key1, "TestBin")
	print "TestBin 2 TTL: {0}", eb.ttl(key2, "TestBin")
	print "TestBin 3 TTL: {0}", eb.ttl(key3, "TestBin")

	print "Waiting for TestBin 3 to expire..."

	time.sleep(10)

	print "Getting expire bins again..."

	print "TestBin 1: {0}".format(eb.get(key1, "TestBin"))
	print "TestBin 2: {0}".format(eb.get(key2, "TestBin"))
	print "TestBin 3: {0}".format(eb.get(key3, "TestBin"))

	# scan udf not yet implemented in python client
	#print "Cleaning bins..."

	#testScan = testClient.scan("test", "expireBin")
	#eb.clean(testScan, "TestBin")

if __name__ == "__main__":
	main()


{'fraction_non_alphanumeric': 0.12317556736691133, 'fraction_numerical': 0.014257993756854805, 'mean_word_length': 3.703968253968254, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4005350', 'n_tokens_mistral': 4379, 'n_tokens_neox': 3877, 'n_words': 1523}