Hi Folks, We are trying to fetch the index of data from the list without using the Full Scan on the record we will be having the AAID and we have to fetch the index of only that userId currently it is giving the index in list format i.e. [0,1,2,...]
need to get the index of that specific AAID which in our case is 1
Note: Bucket Id is calculated on the basis of first 9 character of the AAID
129709 (BucketId): {
LIST(
'70806216-b083-3e19-81d6-4179d891e8f5' (AAID1):{
MAP(
1234 (FlightAdId): 1800 (TimeStamp)
4374 (FlightAdId): 1600 (TimeStamp)
6876 (FlightAdId): 1600 (TimeStamp)
inner_ttl: 1800 (Maximum of above three Timestamp)
)
},
'70806216-s793-3d69-90d6-4179d891e8g5' (AAID2):{
MAP(
7657 (FlightAdId): 900 (TimeStamp)
5435 (FlightAdId): 500 (TimeStamp)
inner_ttl: 900 (Maximum of above two Timestamp)
)
},
'70806216-s793-3d69-70d6-6459d891b4j6' (AAID3):{
MAP(
9878 (FlightAdId): 12000 (TimeStamp)
4454 (FlightAdId): 1000 (TimeStamp)
inner_ttl: 12000 (Maximum of above two Timestamp)
)
}
...other AAID's
)
}
Below is the code for achieving the above structure
Note: UserId and AAID are interchangeable term
def write(cls, user_Id, flight_ad_Id, expiration_timestamp, ttl):
if cls.client is not None:
try:
ns = cls.namespace
bucketId = cls.calculateBucketId(user_Id)
key = (ns, None, bucketId)
bin_name = 'bin1'
index = 0
ctx = [cdt_ctx.cdt_ctx_map_key(user_Id)]
ops = [
list_operations.list_get_by_value(bin_name, user_Id, aerospike.LIST_RETURN_VALUE, ctx),
]
ops3 = [
list_operations.list_get_by_value(bin_name, user_Id, aerospike.LIST_RETURN_INDEX, ctx)
]
a = cls.client.exists(key=key)
if a[1] is not None:
record_exists = cls.client.operate(key, ops)
# Check if the key user_Id exists in the list of keys
key_exists = any(user_Id in record.keys() for record in record_exists[2][bin_name])
record_index = cls.client.operate(key, ops3)
index = record_index[2][bin_name][-1] --- **Getting Wrong INDEX VALUE**
else:
record_exists = None
key_exists = False
existing_ttl = 0
existing_inner_ttl = 0
inner_ttl_value = datetime.now() + timedelta(seconds=ttl)
inner_ttl_value = inner_ttl_value.timestamp()
inner_ttl_to_fetch = 'inner_ttl'
if key_exists and record_exists is not None and record_exists[2][bin_name] is not None and len(record_exists[2][bin_name]) != 0:
for i in record_exists[2][bin_name]:
if user_Id in i:
existing_data = i # Access the first element of the list
existing_ttl = record_exists[1]['ttl']
existing_inner_ttl = existing_data[user_Id][inner_ttl_to_fetch]
else:
existing_data = {} # Initialize as empty dictionary
if existing_inner_ttl <= inner_ttl_value:
inner_ttl_value = inner_ttl_value
else:
inner_ttl_value = existing_inner_ttl
# Update or insert flight_ad_Id and expiration_timestamp
existing_data[user_Id] = {**existing_data.get(user_Id, {}), flight_ad_Id: expiration_timestamp, inner_ttl_to_fetch: int(inner_ttl_value)}
# Update or insert the list into Aerospike
if existing_ttl <= ttl:
meta = {'ttl': int(ttl)}
else:
meta = {'ttl': int(existing_ttl)}
if key_exists:
ops2 = [
list_operations.list_set(bin_name=bin_name, index=index, value=existing_data, policy=aerospike.LIST_WRITE_DEFAULT)
]
else:
ops2 = [
list_operations.list_append(bin_name=bin_name, value=existing_data, policy=aerospike.LIST_WRITE_DEFAULT)
]
cls.client.operate(policy=cls.write_policy, key=key, list=ops2, meta=meta)
except Exception as e:
print(e)
pass
write(
user_Id='70806216-s793-3d69-90d6-4179d891e8g5', flight_ad_Id=64099, expiration_timestamp= 1714411696000, ttl=1800
)