Skip to content

Instantly share code, notes, and snippets.

@igneus
Created March 4, 2019 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igneus/61f7fbfdf87883a41a2647da2bc55472 to your computer and use it in GitHub Desktop.
Save igneus/61f7fbfdf87883a41a2647da2bc55472 to your computer and use it in GitHub Desktop.
BaseX - examples of concurrent connections (fails in Python, OK in other tested languages)
<?php
include("BaseXClient.php");
define('RECORDS', 1700);
define('RECORD_SIZE', 3000);
function connect()
{
return new Session('localhost', 1984, 'admin', 'admin');
}
// populate database with test data
$session = connect();
$session->execute('drop database test_database');
$session->execute('check test_database');
foreach (range(1, RECORDS) as $i) {
$text = str_repeat('a', RECORD_SIZE);
$session->add("$i.xml", "<a key=\"$i\">$text</a>");
}
$session->close();
## try concurrent connections
$session = connect();
$session2 = connect();
$session2->execute('check test_database');
$query = $session->query("collection('test_database')");
while ($query->more()) {
$item = $query->next();
echo "$item\n";
$key = preg_match('/(\d+)/', $item, $matches);
$session2->replace("{$matches[0]}.xml", '<b></b>');
}
import re
from BaseXClient import BaseXClient
RECORDS = 1700
RECORD_SIZE = 3000
def connect():
return BaseXClient.Session('localhost', 1984, 'admin', 'admin')
## populate database with test data
session = connect()
session.execute('drop database test_database')
session.execute('check test_database')
for i in range(1, RECORDS):
session.add('{}.xml'.format(i), '<a key="{}">{}</a>'.format(i, 'a' * RECORD_SIZE))
session.close()
## try concurrent connections
session = connect()
session2 = connect()
session2.execute("check test_database")
query = session.query("collection('test_database')")
for code, item in query.iter():
print(item)
key = re.search(r'(\d+)', item).group(0)
session2.replace("{}.xml".format(key), '<b></b>')
require_relative './BaseXClient.rb'
RECORDS = 1700
RECORD_SIZE = 3000
def connect
BaseXClient::Session.new("localhost", 1984, "admin", "admin")
end
## populate database with test data
session = connect
session.execute 'drop database test_database'
session.execute 'check test_database'
1.upto(RECORDS) do |i|
text = 'a' * RECORD_SIZE
session.add "#{i}.xml", "<a key=\"#{i}\">#{text}</a>"
end
session.close
## try concurrent connections
session = connect
session2 = connect
session2.execute 'check test_database'
query = session.query "collection('test_database')"
while query.more
item = query.next
puts item
key = item.match(/(\d+)/)[0]
session2.replace "#{key}.xml", '<b></b>'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment