Browse Source

Change databases to use new collections

pull/8/head
Anton Tarasenko 2 years ago
parent
commit
5e511f79d5
  1. 8
      sources/Data/Database/Database.uc
  2. 133
      sources/Data/Database/Local/DBRecord.uc
  3. 6
      sources/Data/Database/Local/LocalDatabaseInstance.uc
  4. 8
      sources/Data/Database/Tasks/DBKeysTask.uc
  5. 162
      sources/Data/Database/Tests/TEST_LocalDatabase.uc

8
sources/Data/Database/Database.uc

@ -7,7 +7,7 @@
* All of the methods are asynchronous - they do not return requested * All of the methods are asynchronous - they do not return requested
* values immediately and instead require user to provide a handler function * values immediately and instead require user to provide a handler function
* that will be called once operation is completed. * that will be called once operation is completed.
* Copyright 2021 Anton Tarasenko * Copyright 2021-2022 Anton Tarasenko
*------------------------------------------------------------------------------ *------------------------------------------------------------------------------
* This file is part of Acedia. * This file is part of Acedia.
* *
@ -99,7 +99,7 @@ public function DBReadTask ReadData(
* Schedules writing `data` at the location inside the caller database, * Schedules writing `data` at the location inside the caller database,
* given by the `pointer`. * given by the `pointer`.
* *
* Only `AssociativeArray` (that represents JSON object) can be recorded as * Only `HashTable` (that represents JSON object) can be recorded as
* a database's root value (referred to by an empty JSON pointer ""). * a database's root value (referred to by an empty JSON pointer "").
* *
* @param pointer JSON pointer to the location in the database, where `data` * @param pointer JSON pointer to the location in the database, where `data`
@ -121,7 +121,7 @@ public function DBReadTask ReadData(
* * Data is actually written inside the database iff * * Data is actually written inside the database iff
* `result == DBR_Success`; * `result == DBR_Success`;
* * `result == DBR_InvalidData` iff either given `data`'s type is not * * `result == DBR_InvalidData` iff either given `data`'s type is not
* JSON-compatible or a non-`AssociativeArray` was attempted to be * JSON-compatible or a non-`HashTable` was attempted to be
* recorded as caller database's root value; * recorded as caller database's root value;
* * `DBR_InvalidPointer` can be produced if either `pointer == none` or * * `DBR_InvalidPointer` can be produced if either `pointer == none` or
* container of the value `pointer` points at does not exist. * container of the value `pointer` points at does not exist.
@ -230,7 +230,7 @@ public function DBSizeTask GetDataSize(JSONPointer pointer)
* * Use it to connect a handler for when reading task is complete: * * Use it to connect a handler for when reading task is complete:
* `GetDataKeys(...).connect = handler`, * `GetDataKeys(...).connect = handler`,
* where `handler` must have the following signature: * where `handler` must have the following signature:
* `connect(DBQueryResult result, DynamicArray keys)`. * `connect(DBQueryResult result, ArrayList keys)`.
* * Ownership of `keys` array returned in the `connect()` is considered * * Ownership of `keys` array returned in the `connect()` is considered
* to be transferred to whoever handled result of this query. * to be transferred to whoever handled result of this query.
* It must be deallocated once no longer needed. * It must be deallocated once no longer needed.

133
sources/Data/Database/Local/DBRecord.uc

@ -7,7 +7,7 @@
* Auxiliary data object that can store either a JSON array or an object in * Auxiliary data object that can store either a JSON array or an object in
* the local Acedia database. It is supposed to be saved and loaded * the local Acedia database. It is supposed to be saved and loaded
* to / from packages. * to / from packages.
* Copyright 2021 Anton Tarasenko * Copyright 2021-2022 Anton Tarasenko
*------------------------------------------------------------------------------ *------------------------------------------------------------------------------
* This file is part of Acedia. * This file is part of Acedia.
* *
@ -414,7 +414,7 @@ public final function bool SaveObject(
return false; return false;
} }
EmptySelf(); EmptySelf();
isJSONArray = (newItemAsCollection.class == class'DynamicArray'); isJSONArray = (newItemAsCollection.class == class'ArrayList');
FromCollection(newItemAsCollection); FromCollection(newItemAsCollection);
return true; return true;
} }
@ -558,11 +558,10 @@ public final function int GetObjectSize(JSONPointer jsonPointer)
* @return If `pointer` refers to the JSON object - all available keys. * @return If `pointer` refers to the JSON object - all available keys.
* `none` otherwise (including case of JSON arrays). * `none` otherwise (including case of JSON arrays).
*/ */
public final function DynamicArray GetObjectKeys(JSONPointer jsonPointer) public final function ArrayList GetObjectKeys(JSONPointer jsonPointer)
{ {
local int i; local int i;
local TextAPI api; local ArrayList resultKeys;
local DynamicArray resultKeys;
local array<StorageItem> items; local array<StorageItem> items;
local DBRecord referredObject; local DBRecord referredObject;
local DBRecordPointer pointer; local DBRecordPointer pointer;
@ -573,11 +572,10 @@ public final function DynamicArray GetObjectKeys(JSONPointer jsonPointer)
referredObject = pointer.record; referredObject = pointer.record;
if (referredObject.isJSONArray) return none; if (referredObject.isJSONArray) return none;
api = __().text; resultKeys = __().collections.EmptyArrayList();
resultKeys = __().collections.EmptyDynamicArray();
items = referredObject.storage; items = referredObject.storage;
for (i = 0; i < items.length; i += 1) { for (i = 0; i < items.length; i += 1) {
resultKeys.AddItem(api.FromString(items[i].k)); resultKeys.AddString(items[i].k);
} }
return resultKeys; return resultKeys;
} }
@ -604,7 +602,7 @@ public final function Database.DBQueryResult IncrementObject(
local int index; local int index;
local string itemKey; local string itemKey;
local DBRecord directContainer; local DBRecord directContainer;
local AssociativeArray objectAsAssociativeArray; local HashTable objectAsHashTable;
local DBRecordPointer pointer; local DBRecordPointer pointer;
if (jsonPointer == none) { if (jsonPointer == none) {
return DBR_InvalidPointer; return DBR_InvalidPointer;
@ -612,11 +610,11 @@ public final function Database.DBQueryResult IncrementObject(
if (jsonPointer.IsEmpty()) if (jsonPointer.IsEmpty())
{ {
// Special case - incrementing caller `DBRecord` itself // Special case - incrementing caller `DBRecord` itself
objectAsAssociativeArray = AssociativeArray(object); objectAsHashTable = HashTable(object);
if (objectAsAssociativeArray == none) { if (objectAsHashTable == none) {
return DBR_InvalidData; return DBR_InvalidData;
} }
FromCollection(objectAsAssociativeArray); FromCollection(objectAsHashTable);
return DBR_Success; return DBR_Success;
} }
// All the work will be done by the separate `IncrementItem()` method; // All the work will be done by the separate `IncrementItem()` method;
@ -768,7 +766,7 @@ private final function bool IncrementItem(
/** /**
* Extracts JSON object or array data from caller `DBRecord` as either * Extracts JSON object or array data from caller `DBRecord` as either
* `AssociativeArray` (for JSON objects) or `DynamicArray` (for JSON arrays). * `HashTable` (for JSON objects) or `ArrayList` (for JSON arrays).
* *
* Type conversion rules in immutable case: * Type conversion rules in immutable case:
* 1. 'null' -> `none`; * 1. 'null' -> `none`;
@ -776,8 +774,8 @@ private final function bool IncrementItem(
* 3. 'number' -> either `IntBox` or `FloatBox`, depending on * 3. 'number' -> either `IntBox` or `FloatBox`, depending on
* what seems to fit better; * what seems to fit better;
* 4. 'string' -> `Text`; * 4. 'string' -> `Text`;
* 5. 'array' -> `DynamicArray`; * 5. 'array' -> `ArrayList`;
* 6. 'object' -> `AssociativeArray`. * 6. 'object' -> `HashTable`.
* *
* Type conversion rules in mutable case: * Type conversion rules in mutable case:
* 1. 'null' -> `none`; * 1. 'null' -> `none`;
@ -785,13 +783,13 @@ private final function bool IncrementItem(
* 3. 'number' -> either `IntRef` or `FloatRef`, depending on * 3. 'number' -> either `IntRef` or `FloatRef`, depending on
* what seems to fit better; * what seems to fit better;
* 4. 'string' -> `MutableText`; * 4. 'string' -> `MutableText`;
* 5. 'array' -> `DynamicArray`; * 5. 'array' -> `ArrayList`;
* 6. 'object' -> `AssociativeArray`. * 6. 'object' -> `HashTable`.
* *
* @param makeMutable `false` if you want this method to produce * @param makeMutable `false` if you want this method to produce
* immutable types and `true` otherwise. * immutable types and `true` otherwise.
* @return `AssociativeArray` if caller `DBRecord` represents a JSON object * @return `HashTable` if caller `DBRecord` represents a JSON object
* and `DynamicArray` if it represents JSON array. * and `ArrayList` if it represents JSON array.
* Returned collection must have all of it's keys deallocated before being * Returned collection must have all of it's keys deallocated before being
* discarded. * discarded.
* `none` iff caller `DBRecord` was not initialized as either. * `none` iff caller `DBRecord` was not initialized as either.
@ -804,10 +802,10 @@ public final function Collection ToCollection(bool makeMutable)
} }
lockToCollection = true; lockToCollection = true;
if (isJSONArray) { if (isJSONArray) {
result = ToDynamicArray(makeMutable); result = ToArrayList(makeMutable);
} }
else { else {
result = ToAssociativeArray(makeMutable); result = ToHashTable(makeMutable);
} }
lockToCollection = false; lockToCollection = false;
return result; return result;
@ -815,28 +813,37 @@ public final function Collection ToCollection(bool makeMutable)
// Does not do any validation check, assumes caller `DBRecord` // Does not do any validation check, assumes caller `DBRecord`
// represents an array. // represents an array.
private final function Collection ToDynamicArray(bool makeMutable) private final function Collection ToArrayList(bool makeMutable)
{ {
local int i; local int i;
local DynamicArray result; local ArrayList result;
result = __().collections.EmptyDynamicArray(); local AcediaObject nextObject;
for (i = 0; i < storage.length; i += 1) { result = __().collections.EmptyArrayList();
result.AddItem(ConvertItemToObject(storage[i], makeMutable)); for (i = 0; i < storage.length; i += 1)
{
nextObject = ConvertItemToObject(storage[i], makeMutable);
result.AddItem(nextObject);
__().memory.Free(nextObject);
} }
return result; return result;
} }
// Does not do any validation check, assumes caller `DBRecord` // Does not do any validation check, assumes caller `DBRecord`
// represents an object. // represents an object.
private final function Collection ToAssociativeArray(bool makeMutable) private final function Collection ToHashTable(bool makeMutable)
{ {
local int i; local int i;
local AssociativeArray result; local HashTable result;
result = __().collections.EmptyAssociativeArray(); local Text nextKey;
local AcediaObject nextObject;
result = __().collections.EmptyHashTable();
for (i = 0; i < storage.length; i += 1) for (i = 0; i < storage.length; i += 1)
{ {
result.SetItem( __().text.FromString(storage[i].k), nextKey = __().text.FromString(storage[i].k);
ConvertItemToObject(storage[i], makeMutable)); nextObject = ConvertItemToObject(storage[i], makeMutable);
result.SetItem(nextKey, nextObject);
__().memory.Free(nextKey);
__().memory.Free(nextObject);
} }
return result; return result;
} }
@ -874,11 +881,11 @@ public final function EmptySelf()
* in case of the conflict. * in case of the conflict.
* *
* Can only convert items in passed collection that return `true` for * Can only convert items in passed collection that return `true` for
* `_.json.IsCompatible()` check. Any other values will be treated as `none`. * `__().json.IsCompatible()` check. Any other values will be treated as `none`.
* *
* Only works as long as caller `DBRecord` has the same container type as * Only works as long as caller `DBRecord` has the same container type as
* `source`. `isJSONArray` iff `source.class == class'DynamicArray` and * `source`. `isJSONArray` iff `source.class == class'ArrayList` and
* `!isJSONArray` iff `source.class == class'AssociativeArray`. * `!isJSONArray` iff `source.class == class'HashTable`.
* *
* Values that cannot be converted into JSON will be replaced with `none`. * Values that cannot be converted into JSON will be replaced with `none`.
* *
@ -886,42 +893,47 @@ public final function EmptySelf()
*/ */
public final function FromCollection(Collection source) public final function FromCollection(Collection source)
{ {
local DynamicArray asDynamicArray; local ArrayList asArrayList;
local AssociativeArray asAssociativeArray; local HashTable asHashTable;
asDynamicArray = DynamicArray(source); asArrayList = ArrayList(source);
asAssociativeArray = AssociativeArray(source); asHashTable = HashTable(source);
if (asDynamicArray != none && isJSONArray) { if (asArrayList != none && isJSONArray) {
FromDynamicArray(asDynamicArray); FromArrayList(asArrayList);
} }
if (asAssociativeArray != none && !isJSONArray) { if (asHashTable != none && !isJSONArray) {
FromAssociativeArray(asAssociativeArray); FromHashTable(asHashTable);
} }
} }
// Does not do any validation check. // Does not do any validation check.
private final function FromDynamicArray(DynamicArray source) private final function FromArrayList(ArrayList source)
{ {
local int i, length; local int i, length;
local AcediaObject nextObject;
length = source.GetLength(); length = source.GetLength();
for (i = 0; i < length; i += 1) { for (i = 0; i < length; i += 1)
storage[storage.length] = ConvertObjectToItem(source.GetItem(i)); {
nextObject = source.GetItem(i);
storage[storage.length] = ConvertObjectToItem(nextObject);
__().memory.Free(nextObject);
} }
} }
// Does not do any validation check. // Does not do any validation check.
private final function FromAssociativeArray(AssociativeArray source) private final function FromHashTable(HashTable source)
{ {
local int i, originalStorageLength; local int i, originalStorageLength;
local Iter iter; local Iter iter;
local string nextKey; local string nextKey;
local bool isNewKey; local bool isNewKey;
local AcediaObject nextObject;
originalStorageLength = storage.length; originalStorageLength = storage.length;
for (iter = source.Iterate(); !iter.HasFinished(); iter.Next()) for (iter = source.Iterate(); !iter.HasFinished(); iter.Next())
{ {
if (iter.GetKey() == none) { if (iter.GetKey() == none) {
continue; continue;
} }
nextKey = Text(iter.GetKey()).ToString(); nextKey = __().text.ToString(BaseText(iter.GetKey()));
isNewKey = true; isNewKey = true;
for (i = 0; i < originalStorageLength; i += 1) for (i = 0; i < originalStorageLength; i += 1)
{ {
@ -931,8 +943,11 @@ private final function FromAssociativeArray(AssociativeArray source)
break; break;
} }
} }
if (isNewKey) { if (isNewKey)
SetItem(storage.length, ConvertObjectToItem(iter.Get()), nextKey); {
nextObject = iter.Get();
SetItem(storage.length, ConvertObjectToItem(nextObject), nextKey);
__().memory.Free(nextObject);
} }
} }
iter.FreeSelf(); iter.FreeSelf();
@ -952,7 +967,7 @@ private final function StorageItem ConvertObjectToItem(AcediaObject data)
{ {
result.t = DBAT_Reference; result.t = DBAT_Reference;
newDBRecord = NewRecordFor(package); newDBRecord = NewRecordFor(package);
newDBRecord.isJSONArray = (data.class == class'DynamicArray'); newDBRecord.isJSONArray = (data.class == class'ArrayList');
newDBRecord.FromCollection(Collection(data)); newDBRecord.FromCollection(Collection(data));
result.s = string(newDBRecord.name); result.s = string(newDBRecord.name);
} }
@ -1063,11 +1078,11 @@ private final function bool IncrementItemByObject(
{ {
itemRecord = NewRecordFor(package); // DB was broken somehow itemRecord = NewRecordFor(package); // DB was broken somehow
item.s = string(itemRecord.name); item.s = string(itemRecord.name);
itemRecord.isJSONArray = (object.class == class'DynamicArray'); itemRecord.isJSONArray = (object.class == class'ArrayList');
} }
if ( (itemRecord.isJSONArray && object.class != class'DynamicArray') if ( (itemRecord.isJSONArray && object.class != class'ArrayList')
|| ( !itemRecord.isJSONArray || ( !itemRecord.isJSONArray
&& object.class != class'AssociativeArray')) && object.class != class'HashTable'))
{ {
return false; return false;
} }

6
sources/Data/Database/Local/LocalDatabaseInstance.uc

@ -4,7 +4,7 @@
* This class SHOULD NOT be deallocated manually. * This class SHOULD NOT be deallocated manually.
* This name was chosen so that more readable `LocalDatabase` could be * This name was chosen so that more readable `LocalDatabase` could be
* used in config for defining local databases through per-object-config. * used in config for defining local databases through per-object-config.
* Copyright 2021 Anton Tarasenko * Copyright 2021-2022 Anton Tarasenko
*------------------------------------------------------------------------------ *------------------------------------------------------------------------------
* This file is part of Acedia. * This file is part of Acedia.
* *
@ -223,7 +223,7 @@ public function DBWriteTask WriteData(JSONPointer pointer, AcediaObject data)
// We can only write JSON array as the root value // We can only write JSON array as the root value
if (data != none && pointer.GetLength() <= 0) { if (data != none && pointer.GetLength() <= 0) {
isDataStorable = (data.class == class'AssociativeArray'); isDataStorable = (data.class == class'HashTable');
} }
else { else {
isDataStorable = _.json.IsCompatible(data); isDataStorable = _.json.IsCompatible(data);
@ -294,7 +294,7 @@ public function DBSizeTask GetDataSize(JSONPointer pointer)
public function DBKeysTask GetDataKeys(JSONPointer pointer) public function DBKeysTask GetDataKeys(JSONPointer pointer)
{ {
local DynamicArray keys; local ArrayList keys;
local DBKeysTask keysTask; local DBKeysTask keysTask;
keysTask = DBKeysTask(MakeNewTask(class'DBKeysTask')); keysTask = DBKeysTask(MakeNewTask(class'DBKeysTask'));
if (!ValidatePointer(pointer, keysTask)) return keysTask; if (!ValidatePointer(pointer, keysTask)) return keysTask;

8
sources/Data/Database/Tasks/DBKeysTask.uc

@ -1,6 +1,6 @@
/** /**
* Variant of `DBTask` for `GetDataKeys()` query. * Variant of `DBTask` for `GetDataKeys()` query.
* Copyright 2021 Anton Tarasenko * Copyright 2021-2022 Anton Tarasenko
*------------------------------------------------------------------------------ *------------------------------------------------------------------------------
* This file is part of Acedia. * This file is part of Acedia.
* *
@ -19,11 +19,11 @@
*/ */
class DBKeysTask extends DBTask; class DBKeysTask extends DBTask;
var private DynamicArray queryKeysResponse; var private ArrayList queryKeysResponse;
delegate connect( delegate connect(
Database.DBQueryResult result, Database.DBQueryResult result,
DynamicArray keys, ArrayList keys,
Database source) {} Database source) {}
protected function Finalizer() protected function Finalizer()
@ -33,7 +33,7 @@ protected function Finalizer()
connect = none; connect = none;
} }
public function SetDataKeys(DynamicArray keys) public function SetDataKeys(/* take */ ArrayList keys)
{ {
queryKeysResponse = keys; queryKeysResponse = keys;
} }

162
sources/Data/Database/Tests/TEST_LocalDatabase.uc

@ -1,6 +1,6 @@
/** /**
* Set of tests for `DBRecord` class. * Set of tests for `DBRecord` class.
* Copyright 2020 Anton Tarasenko * Copyright 2021-2022 Anton Tarasenko
*------------------------------------------------------------------------------ *------------------------------------------------------------------------------
* This file is part of Acedia. * This file is part of Acedia.
* *
@ -22,10 +22,10 @@ class TEST_LocalDatabase extends TestCase
// Results of callback are written here // Results of callback are written here
var protected int resultSize; var protected int resultSize;
var protected DynamicArray resultKeys; var protected ArrayList resultKeys;
var protected Database.DBQueryResult resultType; var protected Database.DBQueryResult resultType;
var protected Database.DataType resultDataType; var protected Database.DataType resultDataType;
var protected AssociativeArray resultData; var protected HashTable resultData;
var protected AcediaObject resultObject; var protected AcediaObject resultObject;
protected function DBReadingHandler( protected function DBReadingHandler(
@ -35,12 +35,12 @@ protected function DBReadingHandler(
{ {
default.resultType = result; default.resultType = result;
default.resultObject = data; default.resultObject = data;
default.resultData = AssociativeArray(data); default.resultData = HashTable(data);
} }
protected function DBKeysHandler( protected function DBKeysHandler(
Database.DBQueryResult result, Database.DBQueryResult result,
DynamicArray keys, ArrayList keys,
Database source) Database source)
{ {
default.resultType = result; default.resultType = result;
@ -112,11 +112,11 @@ contains it:
local string source; local string source;
local Parser parser; local Parser parser;
local AssociativeArray root; local HashTable root;
local LocalDatabaseInstance db; local LocalDatabaseInstance db;
source = GetJSONTemplateString(); source = GetJSONTemplateString();
parser = __().text.ParseString(source); parser = __().text.ParseString(source);
root = AssociativeArray(__().json.ParseWith(parser)); root = HashTable(__().json.ParseWith(parser));
db = __().db.NewLocal(P("TEST_ReadOnly")); db = __().db.NewLocal(P("TEST_ReadOnly"));
db.WriteData(__().json.Pointer(), root); db.WriteData(__().json.Pointer(), root);
*/ */
@ -253,11 +253,11 @@ protected static function SubTest_LoadingPreparedSuccessRoot(
TEST_ExpectTrue(default.resultType == DBR_Success); TEST_ExpectTrue(default.resultType == DBR_Success);
TEST_ExpectTrue(default.resultData.GetLength() == 1); TEST_ExpectTrue(default.resultData.GetLength() == 1);
TEST_ExpectTrue(default.resultData TEST_ExpectTrue(default.resultData
.GetAssociativeArrayBy(P("/web-app")).GetLength() == 3); .GetHashTableBy(P("/web-app")).GetLength() == 3);
TEST_ExpectTrue(default.resultData TEST_ExpectTrue(default.resultData
.GetDynamicArrayBy(P("/web-app/servlet")).GetLength() == 5); .GetArrayListBy(P("/web-app/servlet")).GetLength() == 5);
TEST_ExpectTrue(default.resultData TEST_ExpectTrue(default.resultData
.GetAssociativeArrayBy(P("/web-app/servlet/0/init-param")) .GetHashTableBy(P("/web-app/servlet/0/init-param"))
.GetLength() == 42); .GetLength() == 42);
TEST_ExpectTrue(default.resultData TEST_ExpectTrue(default.resultData
.GetTextBy(P("/web-app/servlet/2/servlet-class")) .GetTextBy(P("/web-app/servlet/2/servlet-class"))
@ -536,18 +536,18 @@ protected static function Test_TaskChaining()
__().db.DeleteLocal(P("TEST_DB")); __().db.DeleteLocal(P("TEST_DB"));
} }
protected static function AssociativeArray GetJSONSubTemplateObject() protected static function HashTable GetJSONSubTemplateObject()
{ {
local Parser parser; local Parser parser;
parser = __().text.ParseString("{\"A\":\"simpleValue\",\"B\":11.12}"); parser = __().text.ParseString("{\"A\":\"simpleValue\",\"B\":11.12}");
return AssociativeArray(__().json.ParseWith(parser)); return HashTable(__().json.ParseWith(parser,, true));
} }
protected static function DynamicArray GetJSONSubTemplateArray() protected static function ArrayList GetJSONSubTemplateArray()
{ {
local Parser parser; local Parser parser;
parser = __().text.ParseString("[true, null, \"huh\"]"); parser = __().text.ParseString("[true, null, \"huh\"]");
return DynamicArray(__().json.ParseWith(parser)); return ArrayList(__().json.ParseWith(parser,, true));
} }
/* /*
@ -568,9 +568,9 @@ the database by using templates provided by `GetJSONSubTemplateObject()` and
*/ */
protected static function SubTest_WritingSuccess(LocalDatabaseInstance db) protected static function SubTest_WritingSuccess(LocalDatabaseInstance db)
{ {
local DBWriteTask task; local DBWriteTask task;
local DynamicArray templateArray; local ArrayList templateArray;
local AssociativeArray templateObject; local HashTable templateObject;
templateObject = GetJSONSubTemplateObject(); templateObject = GetJSONSubTemplateObject();
templateArray = GetJSONSubTemplateArray(); templateArray = GetJSONSubTemplateArray();
Issue("`WriteData()` call that is supposed to succeed reports failure."); Issue("`WriteData()` call that is supposed to succeed reports failure.");
@ -666,9 +666,9 @@ protected static function SubTest_WritingDataCheck_Mutable(
protected static function SubTest_WritingFailure(LocalDatabaseInstance db) protected static function SubTest_WritingFailure(LocalDatabaseInstance db)
{ {
local DBWriteTask task; local DBWriteTask task;
local DynamicArray templateArray; local ArrayList templateArray;
local AssociativeArray templateObject; local HashTable templateObject;
templateObject = GetJSONSubTemplateObject(); templateObject = GetJSONSubTemplateObject();
templateArray = GetJSONSubTemplateArray(); templateArray = GetJSONSubTemplateArray();
Issue("`WriteData()` does not report error when attempting writing data at" Issue("`WriteData()` does not report error when attempting writing data at"
@ -712,10 +712,10 @@ protected static function SubTest_WritingIntoSimpleValues(
protected static function SubTest_WritingArrayIndicies(LocalDatabaseInstance db) protected static function SubTest_WritingArrayIndicies(LocalDatabaseInstance db)
{ {
local DBWriteTask writeTask; local DBWriteTask writeTask;
local DynamicArray resultArray; local ArrayList resultArray;
local DynamicArray templateArray; local ArrayList templateArray;
local AssociativeArray templateObject; local HashTable templateObject;
templateObject = GetJSONSubTemplateObject(); templateObject = GetJSONSubTemplateObject();
templateArray = GetJSONSubTemplateArray(); templateArray = GetJSONSubTemplateArray();
db.WriteData(__().json.Pointer(P("")), templateObject); db.WriteData(__().json.Pointer(P("")), templateObject);
@ -731,7 +731,7 @@ protected static function SubTest_WritingArrayIndicies(LocalDatabaseInstance db)
Issue("Database cannot extend stored JSON array's length by assigning to" Issue("Database cannot extend stored JSON array's length by assigning to"
@ "the out-of-bounds index or \"-\"."); @ "the out-of-bounds index or \"-\".");
ReadFromDB(db, "/A"); ReadFromDB(db, "/A");
resultArray = DynamicArray(default.resultObject); resultArray = ArrayList(default.resultObject);
TEST_ExpectTrue(resultArray.GetLength() == 102); TEST_ExpectTrue(resultArray.GetLength() == 102);
TEST_ExpectNone(resultArray.GetItem(99)); TEST_ExpectNone(resultArray.GetItem(99));
TEST_ExpectTrue(resultArray.GetInt(100) == -342); TEST_ExpectTrue(resultArray.GetInt(100) == -342);
@ -741,9 +741,9 @@ protected static function SubTest_WritingArrayIndicies(LocalDatabaseInstance db)
protected static function SubTest_TaskChaining(LocalDatabaseInstance db) protected static function SubTest_TaskChaining(LocalDatabaseInstance db)
{ {
local DBWriteTask writeTask; local DBWriteTask writeTask;
local DynamicArray templateArray; local ArrayList templateArray;
local AssociativeArray templateObject; local HashTable templateObject;
templateObject = GetJSONSubTemplateObject(); templateObject = GetJSONSubTemplateObject();
templateArray = GetJSONSubTemplateArray(); templateArray = GetJSONSubTemplateArray();
db.WriteData(__().json.Pointer(P("")), templateObject); db.WriteData(__().json.Pointer(P("")), templateObject);
@ -759,16 +759,16 @@ protected static function SubTest_TaskChaining(LocalDatabaseInstance db)
TEST_ExpectTrue(Text(default.resultObject).ToString() == "huh"); TEST_ExpectTrue(Text(default.resultObject).ToString() == "huh");
ReadFromDB(db, "/B/2"); ReadFromDB(db, "/B/2");
TEST_ExpectTrue(default.resultType == DBR_Success); TEST_ExpectTrue(default.resultType == DBR_Success);
TEST_ExpectTrue(default.resultObject.class == class'DynamicArray'); TEST_ExpectTrue(default.resultObject.class == class'ArrayList');
TEST_ExpectTrue(DynamicArray(default.resultObject).GetLength() == 3); TEST_ExpectTrue(ArrayList(default.resultObject).GetLength() == 3);
TEST_ExpectTrue(DynamicArray(default.resultObject).GetBool(0) ); TEST_ExpectTrue(ArrayList(default.resultObject).GetBool(0) );
} }
protected static function Test_Removal() protected static function Test_Removal()
{ {
local LocalDatabaseInstance db; local LocalDatabaseInstance db;
local DynamicArray templateArray; local ArrayList templateArray;
local AssociativeArray templateObject; local HashTable templateObject;
templateObject = GetJSONSubTemplateObject(); templateObject = GetJSONSubTemplateObject();
templateArray = GetJSONSubTemplateArray(); templateArray = GetJSONSubTemplateArray();
db = __().db.NewLocal(P("TEST_DB")); db = __().db.NewLocal(P("TEST_DB"));
@ -828,9 +828,9 @@ protected static function SubTest_RemovalCheckValuesAfter(
TEST_ExpectTrue(default.resultData.GetLength() == 1); TEST_ExpectTrue(default.resultData.GetLength() == 1);
TEST_ExpectTrue(default.resultData.HasKey(P("A"))); TEST_ExpectTrue(default.resultData.HasKey(P("A")));
TEST_ExpectTrue( TEST_ExpectTrue(
default.resultData.GetDynamicArray(P("A")).GetLength() == 2); default.resultData.GetArrayList(P("A")).GetLength() == 2);
TEST_ExpectTrue(default.resultData.GetDynamicArray(P("A")).GetBool(0)); TEST_ExpectTrue(default.resultData.GetArrayList(P("A")).GetBool(0));
TEST_ExpectTrue(default.resultData.GetDynamicArray(P("A")) TEST_ExpectTrue(default.resultData.GetArrayList(P("A"))
.GetText(1).ToString() .GetText(1).ToString()
== "huh"); == "huh");
} }
@ -852,8 +852,8 @@ protected static function SubTest_RemovalRoot(LocalDatabaseInstance db)
protected static function Test_Increment() protected static function Test_Increment()
{ {
local LocalDatabaseInstance db; local LocalDatabaseInstance db;
local DynamicArray templateArray; local ArrayList templateArray;
local AssociativeArray templateObject; local HashTable templateObject;
templateObject = GetJSONSubTemplateObject(); templateObject = GetJSONSubTemplateObject();
templateArray = GetJSONSubTemplateArray(); templateArray = GetJSONSubTemplateArray();
db = __().db.NewLocal(P("TEST_DB")); db = __().db.NewLocal(P("TEST_DB"));
@ -911,8 +911,8 @@ protected static function SubTest_IncrementNull(LocalDatabaseInstance db)
task.TryCompleting(); task.TryCompleting();
TEST_ExpectTrue(default.resultType == DBR_Success); TEST_ExpectTrue(default.resultType == DBR_Success);
ReadFromDB(db, "/B/A/1/"); ReadFromDB(db, "/B/A/1/");
TEST_ExpectTrue(DynamicArray(default.resultObject).GetLength() == 3); TEST_ExpectTrue(ArrayList(default.resultObject).GetLength() == 3);
TEST_ExpectNone(DynamicArray(default.resultObject).GetItem(1)); TEST_ExpectNone(ArrayList(default.resultObject).GetItem(1));
task = db.IncrementData( task = db.IncrementData(
__().json.Pointer(P("/B/A/1//1")), GetJSONSubTemplateArray()); __().json.Pointer(P("/B/A/1//1")), GetJSONSubTemplateArray());
task.connect = DBIncrementHandler; task.connect = DBIncrementHandler;
@ -924,10 +924,10 @@ protected static function SubTest_IncrementNull(LocalDatabaseInstance db)
task.TryCompleting(); task.TryCompleting();
TEST_ExpectTrue(default.resultType == DBR_Success); TEST_ExpectTrue(default.resultType == DBR_Success);
ReadFromDB(db, "/B/A/1/"); ReadFromDB(db, "/B/A/1/");
TEST_ExpectTrue(default.resultObject.class == class'DynamicArray'); TEST_ExpectTrue(default.resultObject.class == class'ArrayList');
TEST_ExpectNotNone(DynamicArray(default.resultObject).GetDynamicArray(1)); TEST_ExpectNotNone(ArrayList(default.resultObject).GetArrayList(1));
TEST_ExpectTrue( TEST_ExpectTrue(
DynamicArray(default.resultObject).GetDynamicArray(1).GetInt(1) == 2); ArrayList(default.resultObject).GetArrayList(1).GetInt(1) == 2);
} }
protected static function SubTest_IncrementBool(LocalDatabaseInstance db) protected static function SubTest_IncrementBool(LocalDatabaseInstance db)
@ -1009,10 +1009,10 @@ protected static function SubTest_IncrementString(LocalDatabaseInstance db)
Text(default.resultObject).ToString() == "simpleValue!?"); Text(default.resultObject).ToString() == "simpleValue!?");
} }
protected static function AssociativeArray GetHelperObject() protected static function HashTable GetHelperObject()
{ {
local AssociativeArray result; local HashTable result;
result = __().collections.EmptyAssociativeArray(); result = __().collections.EmptyHashTable();
result.SetItem(P("A"), __().text.FromString("complexString")); result.SetItem(P("A"), __().text.FromString("complexString"));
result.SetItem(P("E"), __().text.FromString("str")); result.SetItem(P("E"), __().text.FromString("str"));
result.SetItem(P("F"), __().ref.float(45)); result.SetItem(P("F"), __().ref.float(45));
@ -1024,7 +1024,7 @@ protected static function SubTest_IncrementObject(LocalDatabaseInstance db)
local DBIncrementTask task; local DBIncrementTask task;
Issue("JSON objects are not incremented properly."); Issue("JSON objects are not incremented properly.");
task = db.IncrementData(__().json.Pointer(P("")), task = db.IncrementData(__().json.Pointer(P("")),
__().collections.EmptyAssociativeArray()); __().collections.EmptyHashTable());
task.connect = DBIncrementHandler; task.connect = DBIncrementHandler;
task.TryCompleting(); task.TryCompleting();
TEST_ExpectTrue(default.resultType == DBR_Success); TEST_ExpectTrue(default.resultType == DBR_Success);
@ -1046,16 +1046,16 @@ protected static function SubTest_IncrementObject(LocalDatabaseInstance db)
default.resultData.GetText(P("E")).ToString() == "str"); default.resultData.GetText(P("E")).ToString() == "str");
TEST_ExpectTrue(default.resultData.GetFloat(P("F")) == 45); TEST_ExpectTrue(default.resultData.GetFloat(P("F")) == 45);
TEST_ExpectTrue( TEST_ExpectTrue(
default.resultData.GetItem(P("B")).class == class'AssociativeArray'); default.resultData.GetItem(P("B")).class == class'HashTable');
Issue("Incrementing JSON objects can overwrite existing data."); Issue("Incrementing JSON objects can overwrite existing data.");
TEST_ExpectTrue( TEST_ExpectTrue(
default.resultData.GetText(P("A")).ToString() == "simpleValue!?"); default.resultData.GetText(P("A")).ToString() == "simpleValue!?");
} }
protected static function DynamicArray GetHelperArray() protected static function ArrayList GetHelperArray()
{ {
local DynamicArray result; local ArrayList result;
result = __().collections.EmptyDynamicArray(); result = __().collections.EmptyArrayList();
result.AddItem(__().text.FromString("complexString")); result.AddItem(__().text.FromString("complexString"));
result.AddItem(__().ref.float(45)); result.AddItem(__().ref.float(45));
result.AddItem(none); result.AddItem(none);
@ -1068,45 +1068,45 @@ protected static function SubTest_IncrementArray(LocalDatabaseInstance db)
local DBIncrementTask task; local DBIncrementTask task;
Issue("JSON arrays are not incremented properly."); Issue("JSON arrays are not incremented properly.");
task = db.IncrementData(__().json.Pointer(P("/B/A")), task = db.IncrementData(__().json.Pointer(P("/B/A")),
__().collections.EmptyDynamicArray()); __().collections.EmptyArrayList());
task.connect = DBIncrementHandler; task.connect = DBIncrementHandler;
task.TryCompleting(); task.TryCompleting();
TEST_ExpectTrue(default.resultType == DBR_Success); TEST_ExpectTrue(default.resultType == DBR_Success);
ReadFromDB(db, "/B/A"); ReadFromDB(db, "/B/A");
TEST_ExpectTrue(DynamicArray(default.resultObject).GetLength() == 3); TEST_ExpectTrue(ArrayList(default.resultObject).GetLength() == 3);
TEST_ExpectTrue( TEST_ExpectTrue(
DynamicArray(default.resultObject).GetText(2).ToString() == "huh"); ArrayList(default.resultObject).GetText(2).ToString() == "huh");
task = db.IncrementData(__().json.Pointer(P("/B/A")), task = db.IncrementData(__().json.Pointer(P("/B/A")),
GetHelperArray()); GetHelperArray());
task.connect = DBIncrementHandler; task.connect = DBIncrementHandler;
task.TryCompleting(); task.TryCompleting();
TEST_ExpectTrue(default.resultType == DBR_Success); TEST_ExpectTrue(default.resultType == DBR_Success);
ReadFromDB(db, "/B/A"); ReadFromDB(db, "/B/A");
TEST_ExpectTrue(DynamicArray(default.resultObject).GetLength() == 7); TEST_ExpectTrue(ArrayList(default.resultObject).GetLength() == 7);
TEST_ExpectTrue(DynamicArray(default.resultObject).GetBool(0)); TEST_ExpectTrue(ArrayList(default.resultObject).GetBool(0));
TEST_ExpectNotNone( TEST_ExpectNotNone(
DynamicArray(default.resultObject).GetAssociativeArray(1)); ArrayList(default.resultObject).GetHashTable(1));
TEST_ExpectTrue( TEST_ExpectTrue(
DynamicArray(default.resultObject).GetText(2).ToString() == "huh"); ArrayList(default.resultObject).GetText(2).ToString() == "huh");
TEST_ExpectTrue( TEST_ExpectTrue(
DynamicArray(default.resultObject).GetText(3).ToString() ArrayList(default.resultObject).GetText(3).ToString()
== "complexString"); == "complexString");
TEST_ExpectTrue(DynamicArray(default.resultObject).GetFloat(4) == 45); TEST_ExpectTrue(ArrayList(default.resultObject).GetFloat(4) == 45);
TEST_ExpectNone(DynamicArray(default.resultObject).GetItem(5)); TEST_ExpectNone(ArrayList(default.resultObject).GetItem(5));
TEST_ExpectTrue(DynamicArray(default.resultObject).GetBool(6)); TEST_ExpectTrue(ArrayList(default.resultObject).GetBool(6));
} }
protected static function CheckValuesAfterIncrement(AssociativeArray root) protected static function CheckValuesAfterIncrement(HashTable root)
{ {
local DynamicArray jsonArray; local ArrayList jsonArray;
TEST_ExpectTrue(root.GetBoolBy(P("/D"))); TEST_ExpectTrue(root.GetBoolBy(P("/D")));
TEST_ExpectTrue(root.GetFloatBy(P("/B/B")) == 10.12); TEST_ExpectTrue(root.GetFloatBy(P("/B/B")) == 10.12);
TEST_ExpectTrue( TEST_ExpectTrue(
root.GetTextBy(P("/A")).ToString() root.GetTextBy(P("/A")).ToString()
== "simpleValue!?"); == "simpleValue!?");
jsonArray = root.GetDynamicArrayBy(P("/B/A")); jsonArray = root.GetArrayListBy(P("/B/A"));
TEST_ExpectTrue(jsonArray.GetBool(0)); TEST_ExpectTrue(jsonArray.GetBool(0));
TEST_ExpectNotNone(jsonArray.GetAssociativeArray(1)); TEST_ExpectNotNone(jsonArray.GetHashTable(1));
TEST_ExpectTrue(jsonArray.GetText(2).ToString() == "huh"); TEST_ExpectTrue(jsonArray.GetText(2).ToString() == "huh");
TEST_ExpectTrue(jsonArray.GetText(3).ToString() == "complexString"); TEST_ExpectTrue(jsonArray.GetText(3).ToString() == "complexString");
TEST_ExpectTrue(jsonArray.GetFloat(4) == 45); TEST_ExpectTrue(jsonArray.GetFloat(4) == 45);
@ -1115,7 +1115,7 @@ protected static function CheckValuesAfterIncrement(AssociativeArray root)
// Test root itself // Test root itself
TEST_ExpectTrue(root.GetLength() == 6); TEST_ExpectTrue(root.GetLength() == 6);
TEST_ExpectTrue(root.GetText(P("A")).ToString() == "simpleValue!?"); TEST_ExpectTrue(root.GetText(P("A")).ToString() == "simpleValue!?");
TEST_ExpectTrue(root.GetItem(P("B")).class == class'AssociativeArray'); TEST_ExpectTrue(root.GetItem(P("B")).class == class'HashTable');
TEST_ExpectTrue(root.GetFloat(P("C")) == 5.5); TEST_ExpectTrue(root.GetFloat(P("C")) == 5.5);
TEST_ExpectTrue(root.GetBool(P("D"))); TEST_ExpectTrue(root.GetBool(P("D")));
TEST_ExpectTrue(root.GetText(P("E")).ToString() == "str"); TEST_ExpectTrue(root.GetText(P("E")).ToString() == "str");
@ -1142,8 +1142,8 @@ protected static function IncrementExpectingFail(
protected static function SubTest_IncrementRewriteBool( protected static function SubTest_IncrementRewriteBool(
LocalDatabaseInstance db, LocalDatabaseInstance db,
DynamicArray templateArray, ArrayList templateArray,
AssociativeArray templateObject) HashTable templateObject)
{ {
Issue("JSON boolean values are rewritten by non-boolean values."); Issue("JSON boolean values are rewritten by non-boolean values.");
IncrementExpectingFail(db, "/D", none); IncrementExpectingFail(db, "/D", none);
@ -1159,8 +1159,8 @@ protected static function SubTest_IncrementRewriteBool(
protected static function SubTest_IncrementRewriteNumeric( protected static function SubTest_IncrementRewriteNumeric(
LocalDatabaseInstance db, LocalDatabaseInstance db,
DynamicArray templateArray, ArrayList templateArray,
AssociativeArray templateObject) HashTable templateObject)
{ {
Issue("JSON numeric values are rewritten by non-numeric values."); Issue("JSON numeric values are rewritten by non-numeric values.");
IncrementExpectingFail(db, "/B/B", none); IncrementExpectingFail(db, "/B/B", none);
@ -1175,8 +1175,8 @@ protected static function SubTest_IncrementRewriteNumeric(
protected static function SubTest_IncrementRewriteString( protected static function SubTest_IncrementRewriteString(
LocalDatabaseInstance db, LocalDatabaseInstance db,
DynamicArray templateArray, ArrayList templateArray,
AssociativeArray templateObject) HashTable templateObject)
{ {
Issue("JSON string values are rewritten by non-`Text`/`MutableText`" Issue("JSON string values are rewritten by non-`Text`/`MutableText`"
@ "values."); @ "values.");
@ -1193,10 +1193,10 @@ protected static function SubTest_IncrementRewriteString(
protected static function SubTest_IncrementRewriteObject( protected static function SubTest_IncrementRewriteObject(
LocalDatabaseInstance db, LocalDatabaseInstance db,
DynamicArray templateArray, ArrayList templateArray,
AssociativeArray templateObject) HashTable templateObject)
{ {
Issue("JSON objects are rewritten by non-`AssociativeArray` values."); Issue("JSON objects are rewritten by non-`HashTable` values.");
IncrementExpectingFail(db, "", none); IncrementExpectingFail(db, "", none);
IncrementExpectingFail(db, "", db); IncrementExpectingFail(db, "", db);
IncrementExpectingFail(db, "", __().box.bool(true)); IncrementExpectingFail(db, "", __().box.bool(true));
@ -1210,10 +1210,10 @@ protected static function SubTest_IncrementRewriteObject(
protected static function SubTest_IncrementRewriteArray( protected static function SubTest_IncrementRewriteArray(
LocalDatabaseInstance db, LocalDatabaseInstance db,
DynamicArray templateArray, ArrayList templateArray,
AssociativeArray templateObject) HashTable templateObject)
{ {
Issue("JSON arrays are rewritten by non-`DynamicArray` values."); Issue("JSON arrays are rewritten by non-`ArrayList` values.");
IncrementExpectingFail(db, "/B/A", none); IncrementExpectingFail(db, "/B/A", none);
IncrementExpectingFail(db, "/B/A", db); IncrementExpectingFail(db, "/B/A", db);
IncrementExpectingFail(db, "/B/A", __().box.bool(true)); IncrementExpectingFail(db, "/B/A", __().box.bool(true));
@ -1243,8 +1243,8 @@ protected static function SubTest_IncrementMissing(LocalDatabaseInstance db)
db.CheckDataType(__().json.Pointer(P("/L"))).connect = DBCheckHandler; db.CheckDataType(__().json.Pointer(P("/L"))).connect = DBCheckHandler;
ReadFromDB(db, "/B/A/1/"); ReadFromDB(db, "/B/A/1/");
TEST_ExpectTrue(default.resultDataType == JSON_Number); TEST_ExpectTrue(default.resultDataType == JSON_Number);
TEST_ExpectTrue(DynamicArray(default.resultObject).GetLength() == 12); TEST_ExpectTrue(ArrayList(default.resultObject).GetLength() == 12);
TEST_ExpectTrue(DynamicArray(default.resultObject).GetInt(11) == 85); TEST_ExpectTrue(ArrayList(default.resultObject).GetInt(11) == 85);
} }
defaultproperties defaultproperties

Loading…
Cancel
Save