dbTalk Databases Forums  

Managing Dynamic Data Structures with Lightweight Database

comp.databases comp.databases


Discuss Managing Dynamic Data Structures with Lightweight Database in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Neo
 
Posts: n/a

Default Managing Dynamic Data Structures with Lightweight Database - 08-08-2007 , 01:55 PM






Below example script, C and VB code show how to manage dynamic data
structures with a lightweight, memory-resident database. During the
1st run, it creates john and mary, each with gender attributes. During
the 2nd run, it creates bob with gender and age attributes. It also
adds age attribute to existing john. During 3rd run, it adds the
attribute body-build-type to mary and bob. Note that attribute build
can have multiple values. During the 4th run, it creates sue with all
attributes using low-level functions. It also modifies john's age and
deletes one of mary's build attribute values. At the end of each C/VB
run, prints persons, attributes and their values stored in db. The
script is submitted via a small GUI front end (250 Kb). The C and VB
code interface to the db via a Windows DLL (128 Kb). The data is
stored in a single file. For more details, see www.dbfordummies.com

(; * 1st run *)
(new 'gender)

(; Create a person named john and set his gender to male)
(new 'john 'person)
(set+ john gender 'male)

(; Create a person named mary and set her gender to female)
(new 'mary 'person)
(set+ mary gender 'female)


(; * 2nd run *)
(new 'age)

(; Create a person named bob)
(; Set his gender to male and age to 35)
(new 'bob 'person)
(set+ bob gender 'male)
(set+ bob age '35)

(; Set john's age to 30)
(set+ john age '30)

(; Get all person that are male)
(; Gets john and bob)
(and (get person instance *)
(get * gender male))


(; * 3nd run *)
(new 'build)

(; Set bob's build to tall)
(set+ bob build 'tall)

(; Set mary's build to thin and petite)
(set+ mary build 'thin)
(set+ mary build 'petite)


(; * 4th run *)
(; Create a person named sue, gender female, age 21)
(new 'sue 'person)
(set+ (it) gender 'female)
(set+ (it) age '21)
(set+ (it) age '21)

(; Set sue's build to fat and short)
(set+ sue build 'fat)
(set+ sue build 'short)

(; Change john's age from 30 to 40)
(new '40 'age)
(change (get john age 30) 40)

(; Delete mary's build is thin)
(delete (get mary build thin))


Reply With Quote
  #2  
Old   
Neo
 
Posts: n/a

Default Re: Managing Dynamic Data Structures with Lightweight Database - 08-08-2007 , 01:58 PM






Below implements the example via C. For more details, see
www.dbfordummies.com/Dev/C/InstallTestDLL.asp

int main(int argc, char* argv[])
{
Db_init();
int errCode = Db_FileSpec_set_r(_T("Db1.dbd"));

if (!Db_File_exists_b()){
// ****************************************
// * This code is executed during 1st run *

// Create db file
errCode = Db_File_create_r(kDbSzDef_g);

// Open db file
errCode = Db_File_open_r();

// Create gender
Xp_process_r(_T("(new 'gender)"));

// Create a person named john and set his gender to male
Xp_process_r(_T("(new 'john 'person)"));
Xp_process_r(_T("(set+ john gender 'male)"));

// Create a person named mary and set her gender to female
Xp_process_r(_T("(new 'mary 'person)"));
Xp_process_r(_T("(set+ mary gender 'female)"));
}
else{
// Open db file
errCode = Db_File_open_r();

// If bob is missing in db
int* pBob = AStr_getDefN(_T("bob"));
if (!pBob){
// *******************************************
// * This is code is executed during 2nd run *

// Create age
Xp_process_r(_T("(new 'age)"));

// Create a person named bob
// Set his gender to male and age to 35
Xp_process_r(_T("(new 'bob 'person)"));
Xp_process_r(_T("(set+ bob gender 'male)"));
Xp_process_r(_T("(set+ bob age '35)"));

// Set john's age to 30
Xp_process_r(_T("(set+ john age '30)"));

// Get all person that are male
// Following prints john and bob
int* pQry = Xp_compile(
_T("(and (get person instance *) (get * gender male))"));
while (int* pPersonX=Xp_execute(pQry)){
TCHAR sName[kStrSz_g+1] = _T("");
N_Name_get(pPersonX, sName, kStrSz_g);
wprintf(_T("%s\n"), sName);
}
}
else{
int* pBuild = AStr_getDefN(_T("build"));
if (!pBuild){
// *******************************************
// * This is code is executed during 3nd run *

// Create body build
Xp_process_r(_T("(new 'build)"));

// Set bob's build to tall
Xp_process_r(_T("(set+ bob build 'tall)"));

// Set mary's build to thin and petite
Xp_process_r(_T("(set+ mary build 'thin)"));
Xp_process_r(_T("(set+ mary build 'petite)"));
}
else{
int* pSue = AStr_getDefN(_T("sue"));
if (!pSue){
// *******************************************
// * This is code is executed during 4th run *
// It uses low-level methods

// Create a person named sue
pSue = N_create();
int* pPerson = AStr_getDefN(_T("person"));
N_SVO_set(pPerson, pInst_g, pSue);
int* pStrSue = AStr_getStr(_T("sue"), TRUE);
N_SVO_set(pSue, pName_g, pStrSue);

// Set sue's gender to female
int* pGender = AStr_getDefN(_T("gender"));
int* pFemale = AStr_getDefN(_T("female"));
N_SVO_set(pSue, pGender, pFemale);

// Set sue's age to 21
int* pAge = AStr_getDefN(_T("age"));
int* pStr21 = AStr_getStr(_T("21"), TRUE);
int* pAge21 = N_ClsInst_get(pAge, pStr21, TRUE);
N_SVO_set(pSue, pAge, pAge21);

// Set sue's build to fat and short
N_EA_setV_wAStr(pSue, pBuild, _T("fat"));
N_EA_setV_wAStr(pSue, pBuild, _T("short"));

// Change john's age from 30 to 40
int* pJohn = AStr_getDefN(_T("john"));
int* pStr30 = AStr_getStr(_T("30"), TRUE);
int* pAge30 = N_ClsInst_get(pAge, pStr30, TRUE);
int* pStr40 = AStr_getStr(_T("40"), TRUE);
int* pAge40 = N_ClsInst_get(pAge, pStr40, TRUE);
N_SV_changeO(pJohn, pAge, pAge30, pAge40);

// Delete mary's build is thin
int* pMary = AStr_getDefN(_T("mary"));
int* pThin = AStr_getDefN(_T("thin"));
int* pSVO = N_SVO_get(pMary, pBuild, pThin);
N_delete(pSVO);
}
}
}
}


// * This is code is executed during all runs *
// Print each person's attributes and values

// During 1st run, prints:
// john gender male
// mary gender female

// During 2nd run, prints:
// john gender male
// john age 30
// mary gender female
// bob gender male
// bob age 35

// During 3rd run, prints:
// john gender male
// john age 30
// mary gender female
// mary build thin
// mary build petite
// bob gender male
// bob age 35
// bob build tall

// During 4th run, prints:
// john gender male
// john age 40
// mary gender female
// mary build petite
// bob gender male
// bob age 35
// bob build tall
// sue gender female
// sue age 21
// sue build fat
// sue build short

int* pQry = Xp_compile(_T("(get person instance *)"));
while (int* pPtX=Xp_execute(pQry)){
int* pA[256]; int* p = (int*)pA;
int* eS = Xp_Node(pPtX, p);
int* eSVO = Xp_S_getSVO(eS, p);
while (int* pSVO=Xp_execute(eSVO)){
int* pSub = N_SVO_getElem(pSVO, kSeqSub_g);
int* pVb = N_SVO_getElem(pSVO, kSeqVb_g);
int* pObj = N_SVO_getElem(pSVO, kSeqObj_g);
if (pVb != pName_g){
// Print "person attribute value"
TCHAR sEntity[kStrSz_g+1] = _T("");
N_Name_get(pSub, sEntity, kStrSz_g);

TCHAR sProp[kStrSz_g+1] = _T("");
N_Name_get(pVb, sProp, kStrSz_g);

TCHAR sVal[kStrSz_g+1] = _T("");
N_Name_get(pObj, sVal, kStrSz_g);

wprintf(_T("%s %s %s\n"), sEntity, sProp, sVal);
}
}
}


Db_File_save();
Db_File_close_r();
return 0;
}



Reply With Quote
  #3  
Old   
Neo
 
Posts: n/a

Default Re: Managing Dynamic Data Structures with Lightweight Database - 08-08-2007 , 02:01 PM



Below implements the example via Visual Basic. For more details, see
www.dbfordummies.com/Dev/VB/InstallTestDLL.asp

'This code manages dynamic data structures via dbd
Private Sub Form_Load()
Const kDbSzDef_g = 256000
Dim errCode&

Db_init
errCode& = Db_FileSpec_set_r("Db1.dbd")
If (False = Db_File_exists_b) Then
'****************************************
'* This code is executed during 1st run *

'Create db file
errCode = Db_File_create_r(kDbSzDef_g)

'Open db file
errCode = Db_File_open_r()

'Create gender
Xp_process_r ("(new 'gender)")

'Create a person named john and set his gender to male
Xp_process_r ("(new 'john 'person)")
Xp_process_r ("(set+ john gender 'male)")

'Create a person named mary and set her gender to female
Xp_process_r ("(new 'mary 'person)")
Xp_process_r ("(set+ mary gender 'female)")
Else
'Open db file
errCode = Db_File_open_r()

'If bob is missing in db
Dim pBob&: pBob& = AStr_getDefN("bob")
If (0 = pBob) Then
'*******************************************
'* This is code is executed during 2nd run *

'Create age,
Xp_process_r ("(new 'age)")

'Create a person named bob
'Set his gender to male and age to 35
Xp_process_r ("(new 'bob 'person)")
Xp_process_r ("(set+ bob gender 'male)")
Xp_process_r ("(set+ bob age '35)")

'Set john's age to 30
Xp_process_r ("(set+ john age '30)")

'Get all person that are male
'Following prints john and bob
Dim pQry&
pQry& = Xp_compile("(and (get person instance *) (get *
gender male))")
Dim pPersonX&: pPersonX& = Xp_execute(pQry&)
Do While (pPersonX&)
Const strSz = 255
Dim sName$: sName$ = Space(strSz)
Call N_Name_get(pPersonX&, sName$, strSz)
sName$ = TrimStr$(sName$)
Debug.Print sName

pPersonX& = Xp_execute(pQry&)
Loop
Else
Dim pBuild&: pBuild& = AStr_getDefN&("build")
If (0 = pBuild) Then
'*******************************************
'* This is code is executed during 3nd run *

'Create body build
Xp_process_r ("(new 'build)")

'Set bob's build to tall
Xp_process_r ("(set+ bob build 'tall)")

'Set mary's build to thin and petite
Xp_process_r ("(set+ mary build 'thin)")
Xp_process_r ("(set+ mary build 'petite)")
Else
Dim pSue&: pSue& = AStr_getDefN("sue")
If (0 = pSue) Then
'*******************************************
'* This is code is executed during 4th run *
'It uses low-level methods

'Create a person named sue
pSue& = N_create(0, 0)
Dim pPerson&: pPerson& = AStr_getDefN("person")
Dim pInst_g&: pInst_g& = AStr_getDefN("instance")
Call N_SVO_set(pPerson&, pInst_g&, pSue&)
Dim pStrSue&: pStrSue& = AStr_getStr("sue", True)
Dim pName_g&: pName_g& = AStr_getDefN("name")
Call N_SVO_set(pSue&, pName_g&, pStrSue&)

'Set sue's gender to female
Dim pGender&: pGender& = AStr_getDefN("gender")
Dim pFemale&: pFemale& = AStr_getDefN("female")
Call N_SVO_set(pSue&, pGender&, pFemale&)

'Set sue's age to 21
Dim pAge&: pAge& = AStr_getDefN("age")
Dim pStr21&: pStr21& = AStr_getStr("21", True)
Dim pAge21&: pAge21& = N_ClsInst_get(pAge&, pStr21&,
True)
Call N_SVO_set(pSue&, pAge&, pAge21&)

'Set sue's build to fat and short
Call N_EA_setV_wAStr(pSue&, pBuild&, "fat", False)
Call N_EA_setV_wAStr(pSue&, pBuild&, "short", False)

'Change john's age from 30 to 40
Dim pJohn&: pJohn& = AStr_getDefN("john")
Dim pStr30&: pStr30& = AStr_getStr("30", True)
Dim pAge30&: pAge30& = N_ClsInst_get(pAge&, pStr30&,
True)
Dim pStr40&: pStr40& = AStr_getStr("40", True)
Dim pAge40&: pAge40& = N_ClsInst_get(pAge&, pStr40&,
True)
Call N_SV_changeO(pJohn&, pAge&, pAge30&, pAge40&)

'Delete mary's build is thin
Dim pMary&: pMary& = AStr_getDefN("mary")
Dim pThin&: pThin& = AStr_getDefN("thin")
Dim pSVO&: pSVO& = N_SVO_get(pMary&, pBuild&, pThin&,
1)
N_delete (pSVO&)
End If
End If
End If
End If


'********************************************
'* This is code is executed during all runs *
'Print each person's attributes and values

'During 1st run, prints:
' john gender male
' mary gender female

'During 2nd run, prints:
' john gender male
' john age 30
' mary gender female
' bob gender male
' bob age 35

'During 3rd run, prints:
' john gender male
' john age 30
' mary gender female
' mary build thin
' mary build petite
' bob gender male
' bob age 35
' bob build tall

'During 4th run, prints:
' john gender male
' john age 40
' mary gender female
' mary build petite
' bob gender male
' bob age 35
' bob build tall
' sue gender female
' sue age 21
' sue build fat
' sue build short

pName_g& = AStr_getDefN("name")
pQry& = Xp_compile("(get person instance *)")
Dim pPtX&: pPtX& = Xp_execute(pQry&)
Do While (pPtX&)
Dim pA&(256)
Dim p&: p& = VarPtr(pA(0))
Dim eS&: eS& = Xp_Node(pPtX, p&)
Dim eSVO&: eSVO& = Xp_S_getSVO(eS, p&)
pSVO& = Xp_execute(eSVO&)
Do While (pSVO&)
Dim pSub&: pSub& = N_SVO_getElem(pSVO&, 0)
Dim pVb&: pVb& = N_SVO_getElem(pSVO&, 1)
Dim pObj&: pObj& = N_SVO_getElem(pSVO&, 2)
If (pVb& <> pName_g&) Then
'Print "person attribute value"
Dim sEntity$: sEntity$ = Space$(strSz)
Call N_Name_get(pSub, sEntity$, strSz)
sEntity$ = TrimStr$(sEntity$)

Dim sProp$: sProp$ = Space$(strSz)
Call N_Name_get(pVb&, sProp$, strSz)
sProp$ = TrimStr$(sProp$)

Dim sVal$: sVal$ = Space$(strSz)
Call N_Name_get(pObj&, sVal$, strSz)
sVal$ = TrimStr$(sVal$)

Debug.Print sEntity$, sProp, sVal
End If

pSVO = Xp_execute(eSVO)

Loop

pPtX& = Xp_execute(pQry&)
Loop


Db_File_save
Db_File_close_r (0)

End Sub


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.