// File: /workspace/github_c_makefile_star2_O2/coyove[P]chann/./lib/unqlite/unqlite.c
// Function: CollectionCacheInstallRecord
static int CollectionCacheInstallRecord(
	unqlite_col *pCol, /* Target collection */
	jx9_int64 nId,     /* Unique record ID */
	jx9_value *pValue  /* JSON value */
	)
{
	unqlite_col_record *pRecord;
	sxu32 iBucket;
	/* Fetch the record first */
	pRecord = CollectionCacheFetchRecord(pCol,nId);
	if( pRecord ){
		/* Record already installed, overwrite its old value  */
		jx9MemObjStore(pValue,&pRecord->sValue);
		return UNQLITE_OK;
	}
	/* Allocate a new instance */
	pRecord = (unqlite_col_record *)SyMemBackendPoolAlloc(&pCol->pVm->sAlloc,sizeof(unqlite_col_record));
	if( pRecord == 0 ){
		return UNQLITE_NOMEM;
	}
	/* Zero the structure */
	SyZero(pRecord,sizeof(unqlite_col_record));
	/* Fill in the structure */
	jx9MemObjInit(pCol->pVm->pJx9Vm,&pRecord->sValue);
	jx9MemObjStore(pValue,&pRecord->sValue);
	pRecord->nId = nId;
	pRecord->pCol = pCol;
	/* Install in the corresponding bucket */
	iBucket = COL_RECORD_HASH(nId) & (pCol->nRecSize - 1);
	pRecord->pNextCol = pCol->apRecord[iBucket];
	if( pCol->apRecord[iBucket] ){
		pCol->apRecord[iBucket]->pPrevCol = pRecord;
	}
	pCol->apRecord[iBucket] = pRecord;
	/* Link */
	MACRO_LD_PUSH(pCol->pList,pRecord);
	pCol->nRec++;
	if( (pCol->nRec >= pCol->nRecSize * 3) && pCol->nRec < 100000 ){
		/* Allocate a new larger table */
		sxu32 nNewSize = pCol->nRecSize << 1;
		unqlite_col_record *pEntry;
		unqlite_col_record **apNew;
		sxu32 n;
		
		apNew = (unqlite_col_record **)SyMemBackendAlloc(&pCol->pVm->sAlloc, nNewSize * sizeof(unqlite_col_record *));
		if( apNew ){
			/* Zero the new table */
			SyZero((void *)apNew, nNewSize * sizeof(unqlite_col_record *));
			/* Rehash all entries */
			n = 0;
			pEntry = pCol->pList;
			for(;;){
				/* Loop one */
				if( n >= pCol->nRec ){
					break;
				}
				pEntry->pNextCol = pEntry->pPrevCol = 0;
				/* Install in the new bucket */
				iBucket = COL_RECORD_HASH(pEntry->nId) & (nNewSize - 1);
				pEntry->pNextCol = apNew[iBucket];
				if( apNew[iBucket]  ){
					apNew[iBucket]->pPrevCol = pEntry;
				}
				apNew[iBucket] = pEntry;
				/* Point to the next entry */
				pEntry = pEntry->pNext;
				n++;
			}
			/* Release the old table and reflect the change */
			SyMemBackendFree(&pCol->pVm->sAlloc,(void *)pCol->apRecord);
			pCol->apRecord = apNew;
			pCol->nRecSize = nNewSize;
		}
	}
	/* All done */
	return UNQLITE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
callq 0x37fa6
testq %rax, %rax
je 0x38023
addq $0x10, %rax
movq %r12, %rdi
movq %rax, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x35ceb
movq (%rbx), %rdi
addq $0x8, %rdi
pushq $0x70
popq %rsi
callq 0x279f7
testq %rax, %rax
je 0x38172
movq %rax, %r15
pushq $0x70
popq %rsi
movq %rax, %rdi
callq 0x27aea
movq (%rbx), %rax
movq 0xe0(%rax), %rdi
movq %r15, %r13
addq $0x10, %r13
movq %r13, %rsi
callq 0x35772
movq %r12, %rdi
movq %r13, %rsi
callq 0x35ceb
movq %r14, 0x8(%r15)
movq %rbx, (%r15)
movl 0xdc(%rbx), %r12d
leal -0x1(%r12), %eax
andl %eax, %r14d
movq 0xc8(%rbx), %rax
movq (%rax,%r14,8), %rcx
movq %rcx, 0x50(%r15)
testq %rcx, %rcx
je 0x3809b
movq %r15, 0x58(%rcx)
movq %r15, (%rax,%r14,8)
movq 0xd0(%rbx), %rax
testq %rax, %rax
je 0x380b3
movq %rax, 0x60(%r15)
movq %r15, 0x68(%rax)
movq %r15, 0xd0(%rbx)
movl 0xd8(%rbx), %eax
incl %eax
movl %eax, 0xd8(%rbx)
leal (%r12,%r12,2), %ecx
cmpl %ecx, %eax
setb %cl
cmpl $0x186a0, %eax          # imm = 0x186A0
setae %al
orb %cl, %al
jne 0x38172
movq (%rbx), %rdi
addq $0x8, %rdi
movl %r12d, %r15d
shll $0x4, %r15d
movl %r15d, %esi
callq 0x2ca19
testq %rax, %rax
je 0x38172
movq %rax, %r14
leaq 0xd0(%rbx), %r13
addl %r12d, %r12d
movq %rax, %rdi
movl %r15d, %esi
callq 0x27aea
movl 0xd8(%rbx), %eax
leal -0x1(%r12), %ecx
xorps %xmm0, %xmm0
subl $0x1, %eax
jb 0x38151
movq (%r13), %r13
movups %xmm0, 0x50(%r13)
movl 0x8(%r13), %edx
andl %ecx, %edx
movq (%r14,%rdx,8), %rsi
movq %rsi, 0x50(%r13)
testq %rsi, %rsi
je 0x38147
movq %r13, 0x58(%rsi)
movq %r13, (%r14,%rdx,8)
addq $0x60, %r13
jmp 0x38122
movq (%rbx), %rdi
movq 0xc8(%rbx), %rsi
addq $0x8, %rdi
callq 0x2d420
movq %r14, 0xc8(%rbx)
movl %r12d, 0xdc(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq