Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
.vscode
.eggs
.pytest_cache
.tox
Expand Down
2 changes: 1 addition & 1 deletion pyignite/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def _build(self, client: 'Client' = None) -> int:
+ len(field_buffer)
)
header.length = header.schema_offset + ctypes.sizeof(schema_class)
header.hash_code = hashcode(field_buffer + bytes(schema))
header.hash_code = hashcode(field_buffer)

# reuse the results
self._buffer = bytes(header) + field_buffer + bytes(schema)
Expand Down
2 changes: 1 addition & 1 deletion pyignite/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def hashcode(string: Union[str, bytes]) -> int:
:param string: UTF-8-encoded string identifier of binary buffer,
:return: hash code.
"""
result = 0
result = 1 if isinstance(string, (bytes, bytearray)) else 0
for char in string:
try:
char = ord(char)
Expand Down
123 changes: 123 additions & 0 deletions tests/test_cache_composite_key_class_sql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.

from collections import OrderedDict

from pyignite import GenericObjectMeta
from pyignite.datatypes import (
IntObject, String
)


class StudentKey(
metaclass=GenericObjectMeta,
type_name='test.model.StudentKey',
schema=OrderedDict([
('ID', IntObject),
('DEPT', String)
])
):
pass


class Student(
metaclass=GenericObjectMeta,
type_name='test.model.Student',
schema=OrderedDict([
('NAME', String),
])
):
pass


create_query = '''CREATE TABLE StudentTable (
id INT(11),
dept VARCHAR,
name CHAR(24),
PRIMARY KEY (id, dept))
WITH "CACHE_NAME=StudentCache, KEY_TYPE=test.model.StudentKey, VALUE_TYPE=test.model.Student"'''

insert_query = '''INSERT INTO StudentTable (id, dept, name) VALUES (?, ?, ?)'''

select_query = 'SELECT _KEY, id, dept, name FROM StudentTable'

drop_query = 'DROP TABLE StudentTable IF EXISTS'


def test_cache_get_with_composite_key_finds_sql_value(client):
"""
Should query a record with composite key and calculate
internal hashcode correctly.
"""

client.sql(drop_query)

# Create table.
result = client.sql(create_query)
assert next(result)[0] == 0

student_key = StudentKey(1, 'Acct')
student_val = Student('John')

# Put new Strudent with StudentKey.
result = client.sql(insert_query, query_args=[student_key.ID, student_key.DEPT, student_val.NAME])
assert next(result)[0] == 1

# Cache get finds the same value.
studentCache = client.get_cache('StudentCache')
val = studentCache.get(student_key)
assert val is not None
assert val.NAME == student_val.NAME

query_result = list(client.sql(select_query, include_field_names=True))

validate_query_result(student_key, student_val, query_result)


def test_python_sql_finds_inserted_value_with_composite_key(client):
"""
Insert a record with a composite key and query it with SELECT SQL.
"""

client.sql(drop_query)

# Create table.
result = client.sql(create_query)
assert next(result)[0] == 0

student_key = StudentKey(2, 'Business')
student_val = Student('Abe')

# Put new value using cache.
studentCache = client.get_cache('StudentCache')
studentCache.put(student_key, student_val)

# Find the value using SQL.
query_result = list(client.sql(select_query, include_field_names=True))

validate_query_result(student_key, student_val, query_result)


def validate_query_result(student_key, student_val, query_result):
'''
Compare query result with expected key and value.
'''
assert len(query_result) == 2
sql_row = dict(zip(query_result[0], query_result[1]))

assert sql_row["_KEY"][0] == student_key._buffer
assert sql_row['ID'] == student_key.ID
assert sql_row['DEPT'] == student_key.DEPT
assert sql_row['NAME'] == student_val.NAME