tag:blogger.com,1999:blog-51648522585112186492024-03-05T06:40:24.966+01:00Carlo's CodeAnonymoushttp://www.blogger.com/profile/09980202786153862322noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-5164852258511218649.post-87205434216291019672012-11-22T21:39:00.003+01:002013-01-20T15:40:48.144+01:00Script per rimuovere un modulo in Magento<div>
Questo è uno script Python molto utile per rimuovere un modulo specifico da <b>Magento</b> se si dispone di una copia di tale modulo.<br />La copia deve essere nel formato canonico, cioé avere l'albero delle cartelle a partire da /app e /skin.</div>
<br />
<div>
es.<br />
myModuleRoot/app/code/local/myModule/...<br />
myModuleRoot/skin/frontend/default/default/myModule/...<br />
<br />
Qui è disponibile un archivio con lo script e il necessario un esempio di utilizzo: <a href="http://dl.dropbox.com/u/21498650/magento_module_script.zip">link</a><br />
Facendo riferimento al contenuto dell'archivio, per lanciare lo script è sufficiente il comando:<br />
<div style="text-align: center;">
<b style="color: #444444;">python ./magento_module_script.py path/myDir path/myTarget</b></div>
</div>
<div>
<br /></div>
<div>
Segue il codice dello script.<br />
<br />
<pre style="line-height: 16px;"><span style="color: green; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">os</span>
<span style="color: green; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">sys</span>
<span style="color: green; font-weight: bold;">def</span> <span style="color: #0060b0; font-weight: bold;">walk_in_deep</span>(<span style="color: #007020;">dir</span>):
elementList <span style="color: #303030;">=</span> [] <span style="color: grey;"># pool</span>
<span style="color: green; font-weight: bold;">if</span> <span style="font-weight: bold;">not</span> os<span style="color: #303030;">.</span>path<span style="color: #303030;">.</span>exists(<span style="color: #007020;">dir</span>):
<span style="color: green; font-weight: bold;">print</span> <span style="background-color: #fff0f0;">"ERR: dir not exists: "</span> <span style="color: #303030;">+</span> <span style="color: #007020;">dir</span>
<span style="color: green; font-weight: bold;">return</span> [];
<span style="color: green; font-weight: bold;">if</span> os<span style="color: #303030;">.</span>walk(<span style="color: #007020;">dir</span>) <span style="color: #303030;">!=</span> <span style="color: #007020;">None</span>:
<span style="color: grey;"># extract folder content</span>
root, subFolders, files <span style="color: #303030;">=</span> os<span style="color: #303030;">.</span>walk(<span style="color: #007020;">dir</span>)<span style="color: #303030;">.</span>next()
<span style="color: grey;"># walk in subfolders</span>
<span style="color: green; font-weight: bold;">for</span> subFolder <span style="font-weight: bold;">in</span> subFolders:
pathSubFolder <span style="color: #303030;">=</span> os<span style="color: #303030;">.</span>path<span style="color: #303030;">.</span>join(root,subFolder)
elementList<span style="color: #303030;">.</span>extend(walk_in_deep(pathSubFolder)) <span style="color: grey;"># recursive call</span>
elementList<span style="color: #303030;">.</span>append(pathSubFolder)
<span style="color: green; font-weight: bold;">for</span> <span style="color: #007020;">file</span> <span style="font-weight: bold;">in</span> files:
elementList<span style="color: #303030;">.</span>append(os<span style="color: #303030;">.</span>path<span style="color: #303030;">.</span>join(root,<span style="color: #007020;">file</span>))
<span style="color: green; font-weight: bold;">return</span> elementList
<span style="color: green; font-weight: bold;">def</span> <span style="color: #0060b0; font-weight: bold;">delete_module</span>(rootdir, targetdir):
elementList <span style="color: #303030;">=</span> walk_in_deep(rootdir) <span style="color: grey;"># paths array</span>
<span style="color: green; font-weight: bold;">for</span> elem <span style="font-weight: bold;">in</span> elementList:
elem <span style="color: #303030;">=</span> targetdir <span style="color: #303030;">+</span> elem[<span style="color: #007020;">len</span>(rootdir):]
<span style="color: green; font-weight: bold;">if</span> os<span style="color: #303030;">.</span>path<span style="color: #303030;">.</span>isdir(elem): <span style="color: grey;"># if is a directory path</span>
<span style="color: green; font-weight: bold;">try</span>:
os<span style="color: #303030;">.</span>rmdir(elem)
<span style="color: green; font-weight: bold;">except</span>:
<span style="color: green; font-weight: bold;">pass</span> <span style="color: grey;"># ignore</span>
<span style="color: green; font-weight: bold;">else</span>: <span style="color: grey;"># if is a file path</span>
<span style="color: green; font-weight: bold;">try</span>:
os<span style="color: #303030;">.</span>remove(elem)
<span style="color: green; font-weight: bold;">except</span>:
<span style="color: green; font-weight: bold;">pass</span> <span style="color: grey;"># ignore</span>
<span style="color: green; font-weight: bold;">if</span> __name__ <span style="color: #303030;">==</span> <span style="background-color: #fff0f0;">"__main__"</span>: <span style="color: grey;"># exec only if it's main script</span>
rootdir <span style="color: #303030;">=</span> sys<span style="color: #303030;">.</span>argv[<span style="color: #0000d0; font-weight: bold;">1</span>]
targetdir <span style="color: #303030;">=</span> sys<span style="color: #303030;">.</span>argv[<span style="color: #0000d0; font-weight: bold;">2</span>]
<span style="color: green; font-weight: bold;">if</span> (<span style="color: #007020;">len</span>(sys<span style="color: #303030;">.</span>argv)<span style="color: #303030;">!=</span><span style="color: #0000d0; font-weight: bold;">3</span>): <span style="color: grey;"># validation check</span>
<span style="color: green; font-weight: bold;">print</span> sys<span style="color: #303030;">.</span>argv
<span style="color: green; font-weight: bold;">print</span> <span style="background-color: #fff0f0;">'Insert module root and magento root.'</span>
<span style="color: green; font-weight: bold;">else</span>: <span style="color: grey;"># exec routine</span>
delete_module(rootdir, targetdir)</pre>
</div>
Anonymoushttp://www.blogger.com/profile/09980202786153862322noreply@blogger.com0tag:blogger.com,1999:blog-5164852258511218649.post-85438581336460677942012-10-25T15:07:00.000+02:002012-10-25T15:16:25.893+02:00Thesis<iframe src="https://docs.google.com/presentation/embed?id=1K_z2yS2LNuqr4BKfv4cRE8qiovsVfxKvt7Wq8QN5AGo&start=false&loop=false&delayms=15000" frameborder="0" width="800" height="590" allowfullscreen="true" mozallowfullscreen="true" webkitallowfullscreen="true"></iframe>Anonymoushttp://www.blogger.com/profile/09980202786153862322noreply@blogger.com0tag:blogger.com,1999:blog-5164852258511218649.post-59856538997967372662012-10-05T22:31:00.000+02:002013-01-21T18:17:48.657+01:00A funny Chrome extensionIf someone likes meme, I made the extension for him (•‿•)<br />
<br />
<a href="https://chrome.google.com/webstore/detail/memeticon/ailcjmkbchjpglniippdjaaamimdniko">link</a> to Chrome Store<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4g2Wl1lHWx9YtyAiy9E9Oek4r1eJi6k45Mddzyil_-gj6KluxztUxGurac3j_gJDsSSWy3nSozjILHA8qtOnKtrFvBwLBPp49StObmiTrLDFd0aPsdtd-AyJPNFCIEaawLVxUdZWc9b8/s1600/screen.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4g2Wl1lHWx9YtyAiy9E9Oek4r1eJi6k45Mddzyil_-gj6KluxztUxGurac3j_gJDsSSWy3nSozjILHA8qtOnKtrFvBwLBPp49StObmiTrLDFd0aPsdtd-AyJPNFCIEaawLVxUdZWc9b8/s640/screen.png" width="640" /></a></div>
<br />Anonymoushttp://www.blogger.com/profile/09980202786153862322noreply@blogger.com0tag:blogger.com,1999:blog-5164852258511218649.post-83909909889749945772012-09-05T10:43:00.000+02:002012-11-05T14:33:37.748+01:00A Simple viewer in pyOpenGL<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYAbc5Ddg6GKHiYBR2WpzgREMYRHZrUKbZy9BUqbrAM3B9c8Ytjx7-Wxbe0flxqBNiDeMG-E09LWCxl5CIbi45RIQjHPbfbbTh6tB2sEjdAcmi_uzW1b31sktM0KbS-NkwcvLJloLnfAE/s1600/viewer_example.bmp" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="341" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYAbc5Ddg6GKHiYBR2WpzgREMYRHZrUKbZy9BUqbrAM3B9c8Ytjx7-Wxbe0flxqBNiDeMG-E09LWCxl5CIbi45RIQjHPbfbbTh6tB2sEjdAcmi_uzW1b31sktM0KbS-NkwcvLJloLnfAE/s400/viewer_example.bmp" width="400" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhdLntOSbPu9PcrK3HZBCBMtruWJ_fBz3aBfzg4NiRr6PWTgq0QbdDyvgvwFUVvAoQ8YrGwaCA512UBn8sZljO1Fd2lagFX6dmahmvxroeuqczEve_gYeGdapvObpEIL9EncPyQ09wrLPo/s1600/viewer_command.bmp" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="212" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhdLntOSbPu9PcrK3HZBCBMtruWJ_fBz3aBfzg4NiRr6PWTgq0QbdDyvgvwFUVvAoQ8YrGwaCA512UBn8sZljO1Fd2lagFX6dmahmvxroeuqczEve_gYeGdapvObpEIL9EncPyQ09wrLPo/s640/viewer_command.bmp" width="640" /></a></div>
<div>
<br /></div>
<div>
<span style="background-color: white; color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 19px; text-align: justify;"><br /></span></div>
<div>
<span style="background-color: white; color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 19px; text-align: justify;">Download .py file : </span><a data-bitly-type="bitly_hover_card" href="http://dl.dropbox.com/u/21498650/viewer.py" style="-webkit-transition: color 0.3s; background-color: white; color: #009eb8; display: inline; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 19px; outline: none; text-align: justify; text-decoration: none;">link</a><span style="background-color: white; color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 19px; text-align: justify;"> </span></div>
<div>
<br /></div>
<div>
<span style="background-color: white; color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 19px; text-align: justify;">... or copy-paste the following code.</span><br />
<span style="background-color: white; color: #333333; font-family: 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 19px; text-align: justify;"><br /></span>
<br />
<pre style="background-color: white;"><span style="color: #7f0055; font-weight: bold;">from</span> OpenGL.GL <span style="color: #7f0055; font-weight: bold;">import</span> *
<span style="color: #7f0055; font-weight: bold;">from</span> OpenGL.GLU <span style="color: #7f0055; font-weight: bold;">import</span> *
<span style="color: #7f0055; font-weight: bold;">from</span> OpenGL.GLUT <span style="color: #7f0055; font-weight: bold;">import</span> *
<span style="color: #3f7f59;">#-----------</span>
<span style="color: #3f7f59;"># VARIABLES</span>
<span style="color: #3f7f59;">#-----------</span>
g_fViewDistance = 9.
g_Width = 600
g_Height = 600
g_nearPlane = 1.
g_farPlane = 1000.
action = <span style="color: #2a00ff;">""</span>
xStart = yStart = 0.
zoom = 65.
xRotate = 0.
yRotate = 0.
zRotate = 0.
xTrans = 0.
yTrans = 0.
<span style="color: #3f7f59;">#-------------------</span>
<span style="color: #3f7f59;"># SCENE CONSTRUCTOR</span>
<span style="color: #3f7f59;">#-------------------</span>
<span style="color: #7f0055; font-weight: bold;">def</span> scenemodel():
glRotate(90,0.,0.,1.)
glutSolidTeapot(1.)
<span style="color: #3f7f59;">#--------</span>
<span style="color: #3f7f59;"># VIEWER</span>
<span style="color: #3f7f59;">#--------</span>
<span style="color: #7f0055; font-weight: bold;">def</span> printHelp():
<span style="color: #7f0055; font-weight: bold;">print</span> <span style="color: #3f7f59;">"""\n\n </span>
<span style="color: #3f7f59;"> -------------------------------------------------------------------\n</span>
<span style="color: #3f7f59;"> Left Mousebutton - move eye position (+ Shift for third axis)\n</span>
<span style="color: #3f7f59;"> Middle Mousebutton - translate the scene\n</span>
<span style="color: #3f7f59;"> Right Mousebutton - move up / down to zoom in / out\n</span>
<span style="color: #3f7f59;"> Key - reset viewpoint\n</span>
<span style="color: #3f7f59;"> Key - exit the program\n</span>
<span style="color: #3f7f59;"> -------------------------------------------------------------------\n</span>
<span style="color: #3f7f59;"> \n"""</span>
<span style="color: #7f0055; font-weight: bold;">def</span> init():
glEnable(GL_NORMALIZE)
glLightfv(GL_LIGHT0,GL_POSITION,[ .0, 10.0, 10., 0. ] )
glLightfv(GL_LIGHT0,GL_AMBIENT,[ .0, .0, .0, 1.0 ]);
glLightfv(GL_LIGHT0,GL_DIFFUSE,[ 1.0, 1.0, 1.0, 1.0 ]);
glLightfv(GL_LIGHT0,GL_SPECULAR,[ 1.0, 1.0, 1.0, 1.0 ]);
glEnable(GL_LIGHT0)
glEnable(GL_LIGHTING)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LESS)
glShadeModel(GL_SMOOTH)
resetView()
<span style="color: #7f0055; font-weight: bold;">def</span> resetView():
<span style="color: #7f0055; font-weight: bold;">global</span> zoom, xRotate, yRotate, zRotate, xTrans, yTrans
zoom = 65.
xRotate = 0.
yRotate = 0.
zRotate = 0.
xTrans = 0.
yTrans = 0.
glutPostRedisplay()
<span style="color: #7f0055; font-weight: bold;">def</span> display():
<span style="color: #3f7f59;"># Clear frame buffer and depth buffer</span>
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
<span style="color: #3f7f59;"># Set up viewing transformation, looking down -Z axis</span>
glLoadIdentity()
gluLookAt(0, 0, -g_fViewDistance, 0, 0, 0, -.1, 0, 0) <span style="color: #3f7f59;">#-.1,0,0</span>
<span style="color: #3f7f59;"># Set perspective (also zoom)</span>
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(zoom, float(g_Width)/float(g_Height), g_nearPlane, g_farPlane)
glMatrixMode(GL_MODELVIEW)
<span style="color: #3f7f59;"># Render the scene</span>
polarView()
scenemodel()
<span style="color: #3f7f59;"># Make sure changes appear onscreen</span>
glutSwapBuffers()
<span style="color: #7f0055; font-weight: bold;">def</span> reshape(width, height):
<span style="color: #7f0055; font-weight: bold;">global</span> g_Width, g_Height
g_Width = width
g_Height = height
glViewport(0, 0, g_Width, g_Height)
<span style="color: #7f0055; font-weight: bold;">def</span> polarView():
glTranslatef( yTrans/100., 0.0, 0.0 )
glTranslatef( 0.0, -xTrans/100., 0.0)
glRotatef( -zRotate, 0.0, 0.0, 1.0)
glRotatef( -xRotate, 1.0, 0.0, 0.0)
glRotatef( -yRotate, .0, 1.0, 0.0)
<span style="color: #7f0055; font-weight: bold;">def</span> keyboard(key, x, y):
<span style="color: #7f0055; font-weight: bold;">global</span> zTr, yTr, xTr
<span style="color: #7f0055; font-weight: bold;">if</span>(key==<span style="color: #2a00ff;">'r'</span>): resetView()
<span style="color: #7f0055; font-weight: bold;">if</span>(key==<span style="color: #2a00ff;">'q'</span>): exit(0)
glutPostRedisplay()
<span style="color: #7f0055; font-weight: bold;">def</span> mouse(button, state, x, y):
<span style="color: #7f0055; font-weight: bold;">global</span> action, xStart, yStart
<span style="color: #7f0055; font-weight: bold;">if</span> (button==GLUT_LEFT_BUTTON):
<span style="color: #7f0055; font-weight: bold;">if</span> (glutGetModifiers() == GLUT_ACTIVE_SHIFT):
action = <span style="color: #2a00ff;">"MOVE_EYE_2"</span>
<span style="color: #7f0055; font-weight: bold;">else</span>:
action = <span style="color: #2a00ff;">"MOVE_EYE"</span>
<span style="color: #7f0055; font-weight: bold;">elif</span> (button==GLUT_MIDDLE_BUTTON):
action = <span style="color: #2a00ff;">"TRANS"</span>
<span style="color: #7f0055; font-weight: bold;">elif</span> (button==GLUT_RIGHT_BUTTON):
action = <span style="color: #2a00ff;">"ZOOM"</span>
xStart = x
yStart = y
<span style="color: #7f0055; font-weight: bold;">def</span> motion(x, y):
<span style="color: #7f0055; font-weight: bold;">global</span> zoom, xStart, yStart, xRotate, yRotate, zRotate, xTrans, yTrans
<span style="color: #7f0055; font-weight: bold;">if</span> (action==<span style="color: #2a00ff;">"MOVE_EYE"</span>):
xRotate += x - xStart
yRotate -= y - yStart
<span style="color: #7f0055; font-weight: bold;">elif</span> (action==<span style="color: #2a00ff;">"MOVE_EYE_2"</span>):
zRotate += y - yStart
<span style="color: #7f0055; font-weight: bold;">elif</span> (action==<span style="color: #2a00ff;">"TRANS"</span>):
xTrans += x - xStart
yTrans += y - yStart
<span style="color: #7f0055; font-weight: bold;">elif</span> (action==<span style="color: #2a00ff;">"ZOOM"</span>):
zoom -= y - yStart
<span style="color: #7f0055; font-weight: bold;">if</span> zoom > 150.:
zoom = 150.
<span style="color: #7f0055; font-weight: bold;">elif</span> zoom < 1.1:
zoom = 1.1
<span style="color: #7f0055; font-weight: bold;">else</span>:
<span style="color: #7f0055; font-weight: bold;">print</span>(<span style="color: #2a00ff;">"unknown action\n"</span>, action)
xStart = x
yStart = y
glutPostRedisplay()
<span style="color: #3f7f59;">#------</span>
<span style="color: #3f7f59;"># MAIN</span>
<span style="color: #3f7f59;">#------</span>
<span style="color: #7f0055; font-weight: bold;">if</span> __name__==<span style="color: #2a00ff;">"__main__"</span>:
<span style="color: #3f7f59;"># GLUT Window Initialization</span>
glutInit()
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB| GLUT_DEPTH) <span style="color: #3f7f59;"># zBuffer</span>
glutInitWindowSize (g_Width,g_Height)
glutInitWindowPosition (0 + 4, g_Height / 4)
glutCreateWindow (<span style="color: #2a00ff;">"Visualizzatore_2.0"</span>)
<span style="color: #3f7f59;"># Initialize OpenGL graphics state</span>
init ()
<span style="color: #3f7f59;"># Register callbacks</span>
glutReshapeFunc(reshape)
glutDisplayFunc(display)
glutMouseFunc(mouse)
glutMotionFunc(motion)
glutKeyboardFunc(keyboard)
printHelp()
<span style="color: #3f7f59;"># Turn the flow of control over to GLUT</span>
glutMainLoop()</pre>
<pre style="background-color: white;"></pre>
<pre style="background-color: white;"></pre>
</div>
Anonymoushttp://www.blogger.com/profile/09980202786153862322noreply@blogger.com0tag:blogger.com,1999:blog-5164852258511218649.post-77539431946271950462012-09-05T03:23:00.000+02:002012-09-07T14:16:27.132+02:00Useful Tips for Blogger Dynamic View <br />
You can add following CSS attributes in<br />
<br />
<br />
model >> personalize >> advanced >> add CSS<br />
<br />
<h3>
<span style="color: #666666;">Tips</span></h3>
<pre style="background-color: white;">#gadget-dock { <span style="color: #7f0055; font-weight: bold;">right</span>: 0; } <span style="color: #3f7f59;">/* undock widget left side bar*/</span>
.gadget-icons { <span style="color: #7f0055; font-weight: bold;">background-color</span>: red; } <span style="color: #3f7f59;">/* change background widget color */</span>
<span style="color: #3f7f59;">
</span></pre>
<pre style="background-color: white;"><pre><span style="color: #3f7f59;">/* change back color of flipcards in Flipcard view */
</span><span style="background-color: white;">.flipcard .back { </span><span style="color: #7f0055; font-weight: bold;">background</span><span style="background-color: white;">: white </span><span style="color: #7f0055; font-weight: bold;">!important</span><span style="background-color: white;">; }</span></pre>
.flipcard .back .overlay .title { <span style="color: #7f0055; font-weight: bold;">color</span>: black <span style="color: #7f0055; font-weight: bold;">!important</span>; }
.flipcard .back .overlay .date { <span style="color: #7f0055; font-weight: bold;">color</span>: red <span style="color: #7f0055; font-weight: bold;">!important</span>; }
<span style="color: #3f7f59;">/* to change the font colour for the profile text on the gadgets */</span>
profile-aboutme { <span style="color: #7f0055; font-weight: bold;">color</span>: white <span style="color: #7f0055; font-weight: bold;">!important</span>; }
<span style="color: #3f7f59;">/* set the location of the profile text */</span>
.profile-location { <span style="color: #7f0055; font-weight: bold;">color</span>: white <span style="color: #7f0055; font-weight: bold;">!important</span>; }
<span style="color: #3f7f59;">/* center blog title */</span>
.tabs {
<span style="color: #7f0055; font-weight: bold;">width</span>: 100% <span style="color: #7f0055; font-weight: bold;">!important</span>; <span style="color: #3f7f59;">/* if you have problems with links, set it to 85% */</span>
<span style="color: #7f0055; font-weight: bold;">text-align</span>: center <span style="color: #7f0055; font-weight: bold;">!important</span>;
}
<span style="color: #3f7f59;">/* set semi-transparent post background */</span>
.sidebar .item { <span style="color: #7f0055; font-weight: bold;">background</span>: #E4E4E4 <span style="color: #7f0055; font-weight: bold;">!important</span>; }
#items.items <span style="color: #7f0055; font-weight: bold;">li</span>.item, .overview-inner, .magazine #content, .ss .item,
.mosaic #content .item, .sidebar #content, #sidebar .item.selected,
.timeslide .item, .viewitem-content { <span style="color: #7f0055; font-weight: bold;">background</span>: <span style="color: #7f0055; font-weight: bold;">url</span><span style="color: #2a00ff;">(</span><span style="color: #2a00ff;">'image</span><span style="color: #3f3fbf;">.png</span><span style="color: #2a00ff;">'</span><span style="color: #2a00ff;">)</span> <span style="color: #7f0055; font-weight: bold;">!important</span>; }
.overview-panel .article, .magazine #feature, .viewitem-panel .article,
.sidebar #content .article { <span style="color: #7f0055; font-weight: bold;">background</span>: transparent <span style="color: #7f0055; font-weight: bold;">!important</span>; }
#overview .overview-backdrop{ opacity: 0 <span style="color: #7f0055; font-weight: bold;">!important</span>; }</pre>
<br />
<div>
<br />
<br /></div>
<div>
<div style="text-align: center;">
Thanks to <a href="http://www.southernspeakers.net/">http://www.southernspeakers.net/</a></div>
<br />
<br /></div>
Anonymoushttp://www.blogger.com/profile/09980202786153862322noreply@blogger.com0tag:blogger.com,1999:blog-5164852258511218649.post-3341924145783608272012-08-26T22:23:00.000+02:002013-01-21T18:23:48.651+01:00A simple Java prettyPrint() method for JSON<br />
Download JAR file : <a href="http://dl.dropbox.com/u/21498650/it.blogspot.carloluchessa.www.prettyprinterjSON.jar">link</a> ( It required to import <a href="http://code.google.com/p/json-simple/">JSONSimple</a> library )<br />
<br />
<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/09980202786153862322noreply@blogger.com0tag:blogger.com,1999:blog-5164852258511218649.post-5976977903772031232012-07-22T15:18:00.001+02:002012-09-05T11:53:21.865+02:00How To implement indexes in Redis using Jedis driver<div class="separator" style="clear: both; text-align: center;">
<a href="http://redis.io/images/redis-300dpi.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="106" src="http://redis.io/images/redis-300dpi.png" width="320" /></a></div>
<blockquote class="tr_bq">
<div style="text-align: justify;">
<span style="background-color: white; font-family: Arial, Helvetica, sans-serif;">"</span><span style="background-color: #fafafa; font-family: Arial, Helvetica, sans-serif; line-height: 19px;">Redis is an open source, advanced</span><span style="background-color: #fafafa; font-family: Arial, Helvetica, sans-serif; line-height: 19px;"> </span><strong style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-family: Arial, Helvetica, sans-serif; font: inherit; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"><span style="font-family: Arial, Helvetica, sans-serif;">key-value store</span></strong><span style="background-color: #fafafa; font-family: Arial, Helvetica, sans-serif; line-height: 19px;">. It is often referred to as a</span><span style="background-color: #fafafa; font-family: Arial, Helvetica, sans-serif; line-height: 19px;"> </span><strong style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-family: Arial, Helvetica, sans-serif; font: inherit; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"><span style="font-family: Arial, Helvetica, sans-serif;">data structure server</span></strong><span style="background-color: #fafafa; font-family: Arial, Helvetica, sans-serif; line-height: 19px;"> </span><span style="background-color: #fafafa; font-family: Arial, Helvetica, sans-serif; line-height: 19px;">since keys can contain</span><span style="background-color: #fafafa; font-family: Arial, Helvetica, sans-serif; line-height: 19px;"> </span><span style="background-color: white; font-family: Arial, Helvetica, sans-serif;">strings</span><span style="background-color: #fafafa; font-family: Arial, Helvetica, sans-serif; line-height: 19px;">,</span><span style="background-color: #fafafa; font-family: Arial, Helvetica, sans-serif; line-height: 19px;"> </span><span style="background-color: white; font-family: Arial, Helvetica, sans-serif;">hashes</span><span style="background-color: #fafafa; font-family: Arial, Helvetica, sans-serif; line-height: 19px;">, </span><span style="background-color: white; font-family: Arial, Helvetica, sans-serif;">lists</span><span style="background-color: #fafafa; font-family: Arial, Helvetica, sans-serif; line-height: 19px;">,</span><span style="background-color: #fafafa; font-family: Arial, Helvetica, sans-serif; line-height: 19px;"> </span><span style="background-color: white; font-family: Arial, Helvetica, sans-serif;">sets</span><span style="background-color: #fafafa; font-family: Arial, Helvetica, sans-serif; line-height: 19px;"> </span><span style="background-color: #fafafa; font-family: Arial, Helvetica, sans-serif; line-height: 19px;">and</span><span style="background-color: #fafafa; font-family: Arial, Helvetica, sans-serif; line-height: 19px;"> </span><span style="background-color: white; font-family: Arial, Helvetica, sans-serif;">sorted sets</span><span style="background-color: #fafafa; font-family: Arial, Helvetica, sans-serif; line-height: 19px;">.</span></div>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><br />
<div style="text-align: justify;">
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="background-color: white;">You can run </span><strong style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-family: Arial, Helvetica, sans-serif; font: inherit; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;">atomic operations</strong><span style="background-color: white;"> on these types, like appending to a string; incrementing the value in a hash; pushing to a list; computing set intersection, union and difference; or getting the member with highest ranking in a sorted set.</span></span></div>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span><span style="font-family: Arial, Helvetica, sans-serif;"></span>
<div style="text-align: justify;">
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="background-color: white;">In order to achieve its outstanding performance, Redis works with an </span><strong style="background-color: white; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-family: Arial, Helvetica, sans-serif; font: inherit; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;">in-memory dataset</strong><span style="background-color: white;">. Depending on your use case, you can persist it either by dumping the dataset to disk every once in a while, or by appending each command to a log." </span><span style="background-color: white; font-size: xx-small;">[<a href="http://redis.io/topics/introduction"><i>link</i></a>]</span></span></div>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span></blockquote>
<div style="text-align: justify;">
<span style="font-family: Arial, Helvetica, sans-serif;">Redis is simple to use and has powerful low level API, <span style="line-height: 15px;">through which</span> developers can implement complex tasks.</span></div>
<div style="text-align: justify;">
<span style="font-family: Arial, Helvetica, sans-serif;">It is a key-value store: to search for something we must know the corresponding entry key.</span></div>
<div style="text-align: justify;">
<span style="font-family: Arial, Helvetica, sans-serif;">However, sometimes is needed to retrive entries by value, for example when we want a subset of <a href="http://redis.io/topics/data-types">HASHES</a> (representing POJOs) having the same value in a specific field. In those cases there is not a single command to do that, so a workaround is required.</span></div>
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">My solution is to implement a sort of "index" of the field of interest, using <a href="http://redis.io/topics/data-types">SORTED SET</a> type offered by Redis.</span><br />
<blockquote class="tr_bq" style="text-align: justify;">
<span style="background-color: #fafafa; color: #333333; font-family: 'Helvetica Neue', sans-serif; font-size: 13px; line-height: 19px;"><i>"Redis Sorted Sets are, similarly to Redis Sets, non repeating collections of Strings. The difference is that every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, scores may be repeated."</i></span></blockquote>
<br />
<span style="font-family: Arial, Helvetica, sans-serif;">In the following Java code, methods take a <a href="https://github.com/xetorthio/jedis">Jedis instance</a> as parameter, used to communicate with Redis datastore.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">An example of pojo class:</span><br />
<br />
<pre style="line-height: 16px;"><span style="color: green; font-weight: bold;">public</span> <span style="color: green; font-weight: bold;">class</span> <span style="color: #b00060; font-weight: bold;">PojoClass</span> <span style="color: #303030;">{</span>
<span style="color: green; font-weight: bold;">private</span> Long id<span style="color: #303030;">;</span>
<span style="color: green; font-weight: bold;">private</span> <span style="color: #303090; font-weight: bold;">int</span> number<span style="color: #303030;">;</span>
<span style="color: green; font-weight: bold;">private</span> String text<span style="color: #303030;">;</span>
<span style="color: grey;">/* getters and setters omitted */</span>
<span style="color: #303030;">}</span></pre>
<br />
<span style="font-family: Arial, Helvetica, sans-serif;">Utility class contains methods to make </span><span style="font-family: 'Courier New', Courier, monospace;">id</span><span style="font-family: Arial, Helvetica, sans-serif;"> and </span><span style="font-family: 'Courier New', Courier, monospace;">pojoName</span><span style="font-family: Arial, Helvetica, sans-serif;"> from a pojo instance; a </span><span style="font-family: 'Courier New', Courier, monospace;">pojoId</span><span style="font-family: Arial, Helvetica, sans-serif;"> identify an unique istance of a pojo class in the datastore.</span><br />
<br />
<pre style="line-height: 16px;"><span style="color: green; font-weight: bold;">public</span> <span style="color: green; font-weight: bold;">class</span> <span style="color: #b00060; font-weight: bold;">Utility</span> <span style="color: #303030;">{</span>
<span style="color: green; font-weight: bold;">public</span> <span style="color: green; font-weight: bold;">static</span> String <span style="color: #0060b0; font-weight: bold;">getPojoName</span><span style="color: #303030;">(</span> Object pojo <span style="color: #303030;">)</span> <span style="color: #303030;">{</span>
<span style="color: green; font-weight: bold;">return</span> pojo<span style="color: #303030;">.</span><span style="color: #0000c0;">getClass</span><span style="color: #303030;">().</span><span style="color: #0000c0;">getName</span><span style="color: #303030;">();</span>
<span style="color: #303030;">}</span>
<span style="color: green; font-weight: bold;">public</span> <span style="color: green; font-weight: bold;">static</span> String <span style="color: #0060b0; font-weight: bold;">getId</span><span style="color: #303030;">(</span> Object pojo <span style="color: #303030;">)</span> <span style="color: #303030;">{</span>
<span style="color: green; font-weight: bold;">try</span> <span style="color: #303030;">{</span> <span style="color: green; font-weight: bold;">return</span> <span style="background-color: #fff0f0;">""</span><span style="color: #303030;">+</span>pojo<span style="color: #303030;">.</span><span style="color: #0000c0;">getClass</span><span style="color: #303030;">().</span><span style="color: #0000c0;">getField</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">"id"</span><span style="color: #303030;">).</span><span style="color: #0000c0;">get</span><span style="color: #303030;">(</span>pojo<span style="color: #303030;">); }</span>
<span style="color: green; font-weight: bold;">catch</span> <span style="color: #303030;">(</span>Exception ignored<span style="color: #303030;">)</span> <span style="color: #303030;">{</span> <span style="color: #303030;">}</span>
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">null</span><span style="color: #303030;">;</span>
<span style="color: #303030;">}</span>
<span style="color: green; font-weight: bold;">public</span> <span style="color: green; font-weight: bold;">static</span> String <span style="color: #0060b0; font-weight: bold;">getPojoId</span><span style="color: #303030;">(</span> Object pojo <span style="color: #303030;">)</span> <span style="color: #303030;">{</span>
<span style="color: green; font-weight: bold;">return</span> <span style="color: #0060b0; font-weight: bold;">getPojoName</span><span style="color: #303030;">(</span>pojo<span style="color: #303030;">)+</span><span style="background-color: #fff0f0;">"_"</span><span style="color: #303030;">+</span><span style="background-color: white; color: #0060b0; font-weight: bold;">getId</span><span style="background-color: white; color: #303030;">(</span><span style="background-color: white;">pojo</span><span style="background-color: white; color: #303030;">);</span>
<span style="color: #303030;">}</span>
<span style="color: #303030;">}</span></pre>
<br />
<span style="font-family: Arial, Helvetica, sans-serif;">IndexManager methods:</span><br />
<ul>
<li><span style="font-family: Arial, Helvetica, sans-serif;">calculateScore(Object value)</span></li>
<li><span style="font-family: Arial, Helvetica, sans-serif;">persistEntry(<span style="background-color: white; line-height: 16px;">Object pojoInstance</span><span style="background-color: white; color: #303030; line-height: 16px;">,</span><span style="background-color: white; line-height: 16px;"> String fieldName</span><span style="background-color: white; color: #303030; line-height: 16px;">,</span><span style="background-color: white; line-height: 16px;"> Object fieldValue</span><span style="background-color: white; color: #303030; line-height: 16px;">,</span><span style="background-color: white; line-height: 16px;"> Jedis jedis</span><span style="background-color: white;">)</span></span></li>
<li><span style="font-family: Arial, Helvetica, sans-serif;"><span style="background-color: white;">findEntries(<span style="background-color: white; line-height: 16px;">String pojoName</span><span style="background-color: white; color: #303030; line-height: 16px;">, </span><span style="background-color: white; line-height: 16px;">String fieldName, </span><span style="background-color: white; line-height: 16px;">Object fieldValue</span><span style="background-color: white; color: #303030; line-height: 16px;">,</span><span style="background-color: white; line-height: 16px;"> </span><span style="background-color: white; line-height: 16px;">Jedis jedis</span><span style="background-color: white;">)</span></span></span></li>
<li><span style="font-family: Arial, Helvetica, sans-serif;"><span style="background-color: white;">removeEntry(String remPojoId, String fieldName, Jedis jedis)</span></span></li>
</ul>
<br />
<br />
<span style="font-family: Arial, Helvetica, sans-serif;"><b>calculateScore()</b> calculates corrispective double value of a primitive Java type value; if input parameter is an instance of String, return its hashcode<i><span style="font-size: x-small;"> (NB: it may not be unique)</span></i>.</span><br />
<pre style="line-height: 16px;"><span style="color: green; font-weight: bold;">public</span> <span style="color: green; font-weight: bold;">static</span> <span style="color: #303090; font-weight: bold;">double</span> <span style="color: #0060b0; font-weight: bold;">calculateScore</span><span style="color: #303030;">(</span>Object value<span style="color: #303030;">)</span> <span style="color: #303030;">{</span>
Class<span style="color: #303030;"><?></span> valueClass <span style="color: #303030;">=</span> value<span style="color: #303030;">.</span><span style="color: #0000c0;">getClass</span><span style="color: #303030;">();</span>
Double score <span style="color: #303030;">=</span> <span style="color: #6000e0; font-weight: bold;">0.0</span><span style="color: #303030;">;</span>
<span style="color: green; font-weight: bold;">if</span> <span style="color: #303030;">(</span> valueClass<span style="color: #303030;">.</span><span style="color: #0000c0;">equals</span><span style="color: #303030;">(</span>Short<span style="color: #303030;">.</span><span style="color: #0000c0;">class</span><span style="color: #303030;">)</span> <span style="color: #303030;">||</span>
valueClass<span style="color: #303030;">.</span><span style="color: #0000c0;">equals</span><span style="color: #303030;">(</span>Integer<span style="color: #303030;">.</span><span style="color: #0000c0;">class</span><span style="color: #303030;">)</span> <span style="color: #303030;">||</span>
valueClass<span style="color: #303030;">.</span><span style="color: #0000c0;">equals</span><span style="color: #303030;">(</span>Long<span style="color: #303030;">.</span><span style="color: #0000c0;">class</span><span style="color: #303030;">)</span> <span style="color: #303030;">||</span>
valueClass<span style="color: #303030;">.</span><span style="color: #0000c0;">equals</span><span style="color: #303030;">(</span>Float<span style="color: #303030;">.</span><span style="color: #0000c0;">class</span><span style="color: #303030;">)</span> <span style="color: #303030;">||</span>
valueClass<span style="color: #303030;">.</span><span style="color: #0000c0;">equals</span><span style="color: #303030;">(</span>Double<span style="color: #303030;">.</span><span style="color: #0000c0;">class</span><span style="color: #303030;">)</span> <span style="color: #303030;">){</span>
score <span style="color: #303030;">=</span> Double<span style="color: #303030;">.</span><span style="color: #0000c0;">parseDouble</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">""</span><span style="color: #303030;">+</span>value<span style="color: #303030;">);</span>
<span style="color: #303030;">}</span> <span style="color: green; font-weight: bold;">else</span>
<span style="color: green; font-weight: bold;">if</span> <span style="color: #303030;">(</span> valueClass<span style="color: #303030;">.</span><span style="color: #0000c0;">equals</span><span style="color: #303030;">(</span>Character<span style="color: #303030;">.</span><span style="color: #0000c0;">class</span><span style="color: #303030;">)</span> <span style="color: #303030;">){</span>
score <span style="color: #303030;">=</span> <span style="color: #303030;">((</span>Integer<span style="color: #303030;">)</span>value<span style="color: #303030;">).</span><span style="color: #0000c0;">doubleValue</span><span style="color: #303030;">()</span> <span style="color: #303030;">;</span>
<span style="color: #303030;">}</span> <span style="color: green; font-weight: bold;">else</span>
<span style="color: green; font-weight: bold;">if</span> <span style="color: #303030;">(</span> valueClass<span style="color: #303030;">.</span><span style="color: #0000c0;">equals</span><span style="color: #303030;">(</span>Boolean<span style="color: #303030;">.</span><span style="color: #0000c0;">class</span><span style="color: #303030;">)</span> <span style="color: #303030;">){</span>
score <span style="color: #303030;">=</span> <span style="color: #303030;">((</span>Boolean<span style="color: #303030;">)</span>value<span style="color: #303030;">)?</span> <span style="color: #6000e0; font-weight: bold;">1.0</span> <span style="color: #303030;">:</span> <span style="color: #6000e0; font-weight: bold;">0.0</span><span style="color: #303030;">;</span>
<span style="color: #303030;">}</span> <span style="color: green; font-weight: bold;">else</span>
<span style="color: green; font-weight: bold;">if</span> <span style="color: #303030;">(</span> valueClass<span style="color: #303030;">.</span><span style="color: #0000c0;">equals</span><span style="color: #303030;">(</span>Byte<span style="color: #303030;">.</span><span style="color: #0000c0;">class</span><span style="color: #303030;">)</span> <span style="color: #303030;">){</span>
score <span style="color: #303030;">=</span> <span style="color: #303030;">((</span>Byte<span style="color: #303030;">)</span>value<span style="color: #303030;">).</span><span style="color: #0000c0;">doubleValue</span><span style="color: #303030;">();</span>
<span style="color: #303030;">}</span>
<span style="color: green; font-weight: bold;">if</span> <span style="color: #303030;">(</span> valueClass<span style="color: #303030;">.</span><span style="color: #0000c0;">equals</span><span style="color: #303030;">(</span>String<span style="color: #303030;">.</span><span style="color: #0000c0;">class</span><span style="color: #303030;">)</span> <span style="color: #303030;">){</span>
score <span style="color: #303030;">=</span> Double<span style="color: #303030;">.</span><span style="color: #0000c0;">parseDouble</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">""</span><span style="color: #303030;">+((</span>String<span style="color: #303030;">)</span>value<span style="color: #303030;">).</span><span style="color: #0000c0;">hashCode</span><span style="color: #303030;">());</span>
<span style="color: #303030;">}</span>
<span style="color: green; font-weight: bold;">return</span> score<span style="color: #303030;">;</span>
<span style="color: #303030;">}</span></pre>
<br />
<span style="font-family: Arial, Helvetica, sans-serif;">Command <a href="http://redis.io/commands/zadd">ZADD</a> must be used to insert "index" entries in the datastore:</span><br />
<pre style="line-height: 16px;"><pre><span style="color: green; font-weight: bold;">public</span> <span style="color: #303090; font-weight: bold;">void</span> <span style="color: #0060b0; font-weight: bold;">persistEntry</span><span style="color: #303030;">(String</span> pojoName<span style="color: #303030;">,</span> String fieldName<span style="color: #303030;">,</span> Object fieldValue<span style="color: #303030;">,</span> Jedis jedis<span style="color: #303030;">)</span> <span style="color: #303030;">{</span>
<span style="color: green; font-weight: bold;">if</span> <span style="color: #303030;">(!</span>value<span style="color: #303030;">.</span><span style="color: #0000c0;">getClass</span><span style="color: #303030;">().</span><span style="color: #0000c0;">equals</span><span style="color: #303030;">(</span>String<span style="color: #303030;">.</span><span style="color: #0000c0;">class</span><span style="color: #303030;">))</span> <span style="color: #303030;">{</span>
<span style="color: grey;">// if String.class : value must be persisted in index entry</span>
jedis<span style="color: #303030;">.</span><span style="color: #0000c0;">zadd</span><span style="color: #303030;">(</span><span style="background-color: white;">pojoName</span><span style="background-color: white; color: #303030;">+</span><span style="background-color: #fff0f0;">":"</span><span style="background-color: white; color: #303030;">+</span><span style="background-color: white;">fieldName</span><span style="background-color: white; color: #303030;">,</span><span style="background-color: white;"> </span><span style="color: green; font-weight: bold;">this</span><span style="color: #303030;">.</span><span style="color: #0000c0;">calculateScore</span><span style="color: #303030;">(</span>fieldValue<span style="color: #303030;">),</span> value<span style="color: #303030;">);</span>
<span style="color: #303030;">}</span> <span style="color: green; font-weight: bold;">else</span> <span style="color: #303030;">{</span>
jedis<span style="color: #303030;">.</span><span style="color: #0000c0;">zadd</span><span style="color: #303030;">(</span>pojoName<span style="color: #303030;">+</span><span style="background-color: #fff0f0;">":"</span><span style="color: #303030;">+</span>fieldName<span style="color: #303030;">,</span> <span style="color: green; font-weight: bold;">this</span><span style="color: #303030;">.</span><span style="color: #0000c0;">calculateScore</span><span style="color: #303030;">(</span>fieldValue<span style="color: #303030;">),</span> ""<span style="color: #303030;">);</span>
<span style="color: #303030;">}</span>
<span style="color: #303030;">}</span></pre>
</pre>
<span style="font-family: Arial, Helvetica, sans-serif;">findEntries() is useful to retrive entries with a specific field value:</span><br />
<pre style="line-height: 16px;"><span style="color: green; font-weight: bold;">public </span><span style="background-color: white; color: #303090; font-weight: bold;">Set<String></span><span style="background-color: white;"> </span><span style="background-color: white; color: #0060b0; font-weight: bold;">findEntries</span><span style="background-color: white; color: #303030;">(</span><span style="background-color: white;">String pojoName</span><span style="background-color: white; color: #303030;">, String fieldName, </span><span style="background-color: white;">Object fieldValue</span><span style="background-color: white; color: #303030;">,</span><span style="background-color: white;"> </span><span style="background-color: white;">Jedis jedis</span><span style="background-color: white; color: #303030;">)</span><span style="background-color: white;"> </span><span style="background-color: white; color: #303030;">{</span></pre>
<pre style="line-height: 16px;"> String key <span style="color: #303030;">=</span> pojoName<span style="color: #303030;">+</span><span style="background-color: #fff0f0;">":"</span><span style="color: #303030;">+</span>field<span style="color: #0000c0;">Name</span><span style="color: #303030;">;</span>
<span style="color: #303090; font-weight: bold;">double</span> minmaxScore <span style="color: #303030;">=</span> <span style="color: green; font-weight: bold;">this</span><span style="color: #303030;">.</span><span style="color: #0000c0;">calculateScore</span><span style="color: #303030;">(</span>fieldValue<span style="color: #303030;">);</span>
Set<span style="color: #303030;"><</span>String <span style="color: #303030;">></span>keys <span style="color: #303030;">=</span> <span style="color: green; font-weight: bold;">new</span> HashSet<span style="color: #303030;"><</span>String<span style="color: #303030;">>();</span>
<span style="color: green; font-weight: bold;">if</span> <span style="color: #303030;">(</span>fieldValue<span style="color: #303030;"> </span><span style="color: #990000;"><b>instanceof</b></span><span style="color: #303030;"> </span>String<span style="color: #303030;">)</span> <span style="color: #303030;">{</span>
Set<span style="color: #303030;"><</span>String<span style="color: #303030;">></span> keys_tmp <span style="color: #303030;">=</span> jedis<span style="color: #303030;">.</span><span style="color: #0000c0;">zrangeByScore</span><span style="color: #303030;">(</span>key<span style="color: #303030;">,</span> minmaxScore<span style="color: #303030;">,</span> minmaxScore<span style="color: #303030;">);</span>
<span style="color: green; font-weight: bold;">for</span> <span style="color: #303030;">(</span>String k <span style="color: #303030;">:</span> keys_tmp<span style="color: #303030;">)</span> <span style="color: #303030;">{</span>
String<span style="color: #303030;">[]</span> valueAndReferredEntity <span style="color: #303030;">=</span> k<span style="color: #303030;">.</span><span style="color: #0000c0;">split</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">"\\$\\$\\$"</span><span style="color: #303030;">);</span>
<span style="color: green; font-weight: bold;">if</span> <span style="color: #303030;">(</span>valueAndReferredEntity<span style="color: #303030;">[</span><span style="color: #0000d0; font-weight: bold;">0</span><span style="color: #303030;">].</span><span style="color: #0000c0;">equals</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">""</span><span style="color: #303030;">+</span>fieldValue<span style="color: #303030;">))</span> <span style="color: #303030;">{</span>
keys<span style="color: #303030;">.</span><span style="color: #0000c0;">add</span><span style="color: #303030;">(</span>valueAndReferredEntity<span style="color: #303030;">[</span><span style="color: #0000d0; font-weight: bold;">1</span><span style="color: #303030;">]);</span>
<span style="color: #303030;">}</span>
<span style="color: #303030;">}</span>
<span style="color: #303030;">}</span> <span style="color: green; font-weight: bold;">else</span> <span style="color: #303030;">{</span>
keys <span style="color: #303030;">=</span> jedis<span style="color: #303030;">.</span><span style="color: #0000c0;">zrangeByScore</span><span style="color: #303030;">(</span>key<span style="color: #303030;">,</span> minmaxScore<span style="color: #303030;">,</span> minmaxScore<span style="color: #303030;">);</span>
<span style="color: #303030;">}</span>
<span style="color: green; font-weight: bold;">return</span> keys<span style="color: #303030;">;</span>
<span style="color: #303030;">}</span></pre>
<br />
<span style="font-family: Arial, Helvetica, sans-serif;">When an istance of a PojoClass is removed from the DB, its indexes must be updated:</span><br />
<br />
<pre style="line-height: 16px;"><span style="color: green; font-weight: bold;">public</span> <span style="color: #303090; font-weight: bold;">void</span> <span style="color: #0060b0; font-weight: bold;">removeEntry</span><span style="color: #303030;">(</span>String remId<span style="color: #303030;">,</span> String remPojoName<span style="color: #303030;">,</span>
String fieldName<span style="color: #303030;">,</span> Object fieldValue<span style="color: #303030;">,</span> Jedis jedis<span style="color: #303030;">)</span> <span style="color: #303030;">{</span>
Set<span style="color: #303030;"><</span>String<span style="color: #303030;">></span> keys <span style="color: #303030;">=</span> <span style="color: green; font-weight: bold;">null</span><span style="color: #303030;">;</span>
String key <span style="color: #303030;">=</span> remPojoName<span style="color: #303030;">+</span><span style="background-color: #fff0f0;">":"</span><span style="color: #303030;">+</span>fieldName<span style="color: #303030;">;</span>
<span style="color: #303090; font-weight: bold;">double</span> minmaxScore <span style="color: #303030;">=</span> Utility<span style="color: #303030;">.</span><span style="color: #0000c0;">calculateScore</span><span style="color: #303030;">(</span>fieldValue<span style="color: #303030;">);</span>
keys <span style="color: #303030;">=</span> <span style="color: green; font-weight: bold;">new</span> HashSet<span style="color: #303030;"><</span>String<span style="color: #303030;">>();</span>
<span style="color: green; font-weight: bold;">if</span> <span style="color: #303030;">(</span>fieldValue <span style="color: green; font-weight: bold;">instanceof</span> String<span style="color: #303030;">)</span> <span style="color: #303030;">{</span>
keys <span style="color: #303030;">=</span> jedis<span style="color: #303030;">.</span><span style="color: #0000c0;">zrangeByScore</span><span style="color: #303030;">(</span>key<span style="color: #303030;">,</span> minmaxScore<span style="color: #303030;">,</span> minmaxScore<span style="color: #303030;">);</span>
<span style="color: green; font-weight: bold;">for</span> <span style="color: #303030;">(</span>String k <span style="color: #303030;">:</span> keys<span style="color: #303030;">)</span> <span style="color: #303030;">{</span>
String<span style="color: #303030;">[]</span> valueAndReferredEntity <span style="color: #303030;">=</span> k<span style="color: #303030;">.</span><span style="color: #0000c0;">split</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">"\\$\\$\\$"</span><span style="color: #303030;">);</span>
<span style="color: green; font-weight: bold;">if</span> <span style="color: #303030;">(</span>valueAndReferredEntity<span style="color: #303030;">[</span><span style="color: #0000d0; font-weight: bold;">0</span><span style="color: #303030;">].</span><span style="color: #0000c0;">equals</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">""</span><span style="color: #303030;">+</span>fieldValue<span style="color: #303030;">))</span> <span style="color: #303030;">{</span>
jedis<span style="color: #303030;">.</span><span style="color: #0000c0;">zrem</span><span style="color: #303030;">(</span>key<span style="color: #303030;">,</span> k<span style="color: #303030;">);</span>
<span style="color: #303030;">}</span>
<span style="color: #303030;">}</span>
<span style="color: #303030;">}</span> <span style="color: green; font-weight: bold;">else</span> <span style="color: #303030;">{</span>
keys <span style="color: #303030;">=</span> jedis<span style="color: #303030;">.</span><span style="color: #0000c0;">zrangeByScore</span><span style="color: #303030;">(</span>key<span style="color: #303030;">,</span> minmaxScore<span style="color: #303030;">,</span> minmaxScore<span style="color: #303030;">);</span>
<span style="color: green; font-weight: bold;">for</span> <span style="color: #303030;">(</span>String k <span style="color: #303030;">:</span> keys<span style="color: #303030;">)</span> <span style="color: #303030;">{</span>
<span style="color: green; font-weight: bold;">if</span> <span style="color: #303030;">(</span>k<span style="color: #303030;">.</span><span style="color: #0000c0;">equals</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">""</span><span style="color: #303030;">+</span>remId<span style="color: #303030;">))</span> <span style="color: #303030;">{</span> jedis<span style="color: #303030;">.</span><span style="color: #0000c0;">zrem</span><span style="color: #303030;">(</span>key<span style="color: #303030;">,</span> k<span style="color: #303030;">);</span> <span style="color: #303030;">}</span>
<span style="color: #303030;">}</span>
<span style="color: #303030;">}</span>
<span style="color: #303030;">}</span></pre>
<br />
<span style="background-color: white; font-family: Arial, Helvetica, sans-serif;">NB: Indexes should also be updated when indexed field values of a PojoInstance changes.</span><br />
<br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>Anonymoushttp://www.blogger.com/profile/09980202786153862322noreply@blogger.com0